簡體   English   中英

圖像未在SFML中使用精靈顯示

[英]Images not displayed using sprites in SFML

我已使用C ++ 17在Linux Mint 18.1上學習了SFML 2.3.2,以學習如何使用GUI。 作為Hello World之后的第二個項目,我正在嘗試復制Snake,即舊手機已經預裝的游戲。 到目前為止,大多數內容都可以使用,除了一些較小的例外,我稍后將要處理,其中一些是由於游戲尚未完全完成而引起的。

只是為了確保我理解正確,因為我以前從未使用過低級語言來處理GUI和圖像。 首先,將圖像加載到紋理中,然后將紋理添加到精靈中,然后將該精靈繪制到窗口上?

該程序可完美編譯,正確初始化所有內容,並且運行時沒有重大無法解釋的問題。 除非圖像不顯示。 精靈在那里並以其默認的單色背景色顯示,但不顯示任何圖像。

我做錯了什么,如何解決? 謝謝!

// HEADERS
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/System/String.hpp>
#include <SFML/Window/Keyboard.hpp>

// CREATE NEW FONT
sf::Font create_font()
{
    sf::Font f;
    bool id = f.loadFromFile("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf");

    if (id == false)
    {
        std::cerr << "Font:\tCould not be loaded." << std::endl;
    }
    else
    {
        std::cerr << "Font:\tLoaded successfully." << std::endl;
    }

    return f;

}

// CREATE NEW SPRITE USING TEXTURE, GIVEN ARE PATH WITH COORDINATES AND DIMENSIONS
sf::Sprite load_img(std::string path, long x, long y, long w, long h)
{
    sf::Texture t;
    bool id = t.loadFromFile(path);

    if (id == false)
    {
        std::cerr << "Texture:\t" << path << "\tFailed to load." << std::endl;
    }
    else
    {
        std::cerr << "Texture:\t" << path << "\tLoaded successfully." << std::endl;
    }

    sf::Sprite s(t);
    s.setTextureRect(sf::IntRect((int)x, (int)y, (int)w, (int)h));
    s.setPosition(x, y);
    return s;
}


// MAIN FUNCTION
int main()
{
    // DECLARING/DEFINING VARIABLES
    unsigned long window_width = 512;
    unsigned long window_height = 512;
    unsigned long score = 0;
    unsigned long head_old_position_x, head_old_position_y;
    std::string title = "Snek";
    std::string wdir = "/home/kate/Documents/coding/snek/";

    // WINDOW
    sf::RenderWindow window(sf::VideoMode(window_width, window_height), title);
    window.setFramerateLimit(60);

    // SPRITES
    sf::Sprite background = load_img(wdir + "img/background.png", 0, 0, 512, 512);
    sf::Sprite head = load_img(wdir + "img/head.png", 47, 39, 8, 8);
    sf::Sprite body = load_img(wdir + "img/body.png", 39, 39, 8, 8);
    sf::Sprite poison = load_img(wdir + "img/poison.png", 119, 119, 8, 8);
    sf::Sprite trap = load_img(wdir + "img/trap.png", 159, 159, 8, 8);
    sf::Sprite candy = load_img(wdir + "img/candy.png", 199, 199, 8, 8);

    // FONT
    sf::Font font = create_font();

    // TEXT
    sf::Text score_display(title, font, 20);
    sc// HEADERS
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/System/String.hpp>
#include <SFML/Window/Keyboard.hpp>

// CREATE NEW FONT
sf::Font create_font()
{
    sf::Font f;
    bool id = f.loadFromFile("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf");

    if (id == false)
    {
        std::cerr << "Font:\tCould not be loaded." << std::endl;
    }
    else
    {
        std::cerr << "Font:\tLoaded successfully." << std::endl;
    }

    return f;

}

// CREATE NEW SPRITE USING TEXTURE, GIVEN ARE PATH WITH COORDINATES AND DIMENSIONS
sf::Sprite load_img(std::string path, long x, long y, long w, long h)
{
    sf::Texture t;
    bool id = t.loadFromFile(path);

    if (id == false)
    {
        std::cerr << "Texture:\t" << path << "\tFailed to load." << std::endl;
    }
    else
    {
        std::cerr << "Texture:\t" << path << "\tLoaded successfully." << std::endl;
    }

    sf::Sprite s(t);
    s.setTextureRect(sf::IntRect((int)x, (int)y, (int)w, (int)h));
    s.setPosition(x, y);
    return s;
}


// MAIN FUNCTION
int main()
{
    // DECLARING/DEFINING VARIABLES
    unsigned long window_width = 512;
    unsigned long window_height = 512;
    unsigned long score = 0;
    unsigned long head_old_position_x, head_old_position_y;
    std::string title = "Snek";
    std::string wdir = "/home/kate/Documents/coding/snek/";

    // WINDOW
    sf::RenderWindow window(sf::VideoMode(window_width, window_height), title);
    window.setFramerateLimit(60);

    // SPRITES
    sf::Sprite background = load_img(wdir + "/img/background.png", 0, 0, 512, 512);
    sf::Sprite head = load_img(wdir + "/img/head.png", 47, 39, 8, 8);
    sf::Sprite body = load_img(wdir + "/img/body.png", 39, 39, 8, 8);
    sf::Sprite poison = load_img(wdir + "/img/poison.png", 119, 119, 8, 8);
    sf::Sprite trap = load_img(wdir + "/img/trap.png", 159, 159, 8, 8);
    sf::Sprite candy = load_img(wdir + "/img/candy.png", 199, 199, 8, 8);

    // FONT
    sf::Font font = create_font();

    // TEXT
    sf::Text score_display(title, font, 20);
    score_display.setString(std::to_string(score));
    score_display.setPosition(5, 5);

    // LOOP
    while (window.isOpen())
    {
        sf::Event event;
        while ( window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            { window.close(); }

            head_old_position_x = head.getPosition().x;
            head_old_position_y = head.getPosition().y;

            // MOVEMENT BASED ON KEYBOARD INPUT
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
                head.setPosition(head.getPosition().x - 8, head.getPosition().y);
                body.setPosition(head_old_position_x, head_old_position_y);
            }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
                head.setPosition(head.getPosition().x, head.getPosition().y - 8);
                body.setPosition(head_old_position_x, head_old_position_y);
            }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
                head.setPosition(head.getPosition().x + 8, head.getPosition().y);
                body.setPosition(head_old_position_x, head_old_position_y);
            }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
                head.setPosition(head.getPosition().x, head.getPosition().y + 8);
                body.setPosition(head_old_position_x, head_old_position_y);
            }


            if (body.getPosition().x == candy.getPosition().x && body.getPosition().y == candy.getPosition().y)
            {
                score ++;
                score_display.setString(std::to_string(score));
            }


            // REFRESH WINDOW
            window.clear();
            window.draw(background);
            window.draw(head);
            window.draw(body);
            window.draw(poison);
            window.draw(trap);
            window.draw(candy);
            window.draw(score_display);
            window.display();
        }
    }

    return 0;
}
ore_display.setString(std::to_string(score));
    score_display.setPosition(5, 5);

    // LOOP
    while (window.isOpen())
    {
        sf::Event event;
        while ( window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            { window.close(); }

            head_old_position_x = head.getPosition().x;
            head_old_position_y = head.getPosition().y;

            // MOVEMENT BASED ON KEYBOARD INPUT
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
                head.setPosition(head.getPosition().x - 8, head.getPosition().y);
                body.setPosition(head_old_position_x, head_old_position_y);
            }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
                head.setPosition(head.getPosition().x, head.getPosition().y - 8);
                body.setPosition(head_old_position_x, head_old_position_y);
            }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
                head.setPosition(head.getPosition().x + 8, head.getPosition().y);
                body.setPosition(head_old_position_x, head_old_position_y);
            }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
                head.setPosition(head.getPosition().x, head.getPosition().y + 8);
                body.setPosition(head_old_position_x, head_old_position_y);
            }


            if (body.getPosition().x == candy.getPosition().x && body.getPosition().y == candy.getPosition().y)
            {
                score ++;
                score_display.setString(std::to_string(score));
            }


            // REFRESH WINDOW
            window.clear();
            window.draw(background);
            window.draw(head);
            window.draw(body);
            window.draw(poison);
            window.draw(trap);
            window.draw(candy);
            window.draw(score_display);
            window.display();
        }
    }

    return 0;
}

您的紋理超出范圍,請參閱什么時候對象“超出范圍”? 更多細節。 您可以在load_img函數中聲明紋理,但是sf::Texture t; 退出load_img並返回sprite后,將刪除。

因此,這是一個更新的函數,您可以在其中將紋理存儲在load_img函數之外,並通過引用將其傳遞。 僅顯示相關部分。 首先,將紋理傳遞給load_img函數。 刪除sf::Texture t;

sf::Sprite load_img(std::string path, long x, long y, long w, long h, sf::Texture& t)
{
    bool id = t.loadFromFile(path);
    [...]
}

聲明紋理並將其傳遞給load_img函數。

sf::Texture headtexture;
sf::Sprite head = load_img(wdir + "img/head.png", 47, 39, 8, 8, headtexture);

如果對所有精靈進行此操作,它將起作用並正確顯示圖像/精靈。

編輯 2017年9月25日

根據評論,這是一個沒有功能的最小工作示例:

// HEADERS
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/System/String.hpp>
#include <SFML/Window/Keyboard.hpp>

// MAIN FUNCTION
int main()
{
    // DECLARING/DEFINING VARIABLES
    unsigned long window_width = 512;
    unsigned long window_height = 512;
    std::string title = "Snek";
    std::string wdir = "/home/kate/Documents/coding/snek/";

    // WINDOW
    sf::RenderWindow window(sf::VideoMode(window_width, window_height), title);
    window.setFramerateLimit(60);

    // SPRITES
    sf::Texture headtexture;
    sf::Sprite head;

    headtexture.loadFromFile(wdir + "img/head.png");
    head.setTexture(headtexture);
    head.setTextureRect(sf::IntRect(47, 39, 8, 8));
    head.setPosition(30, 30);

    // LOOP
    while (window.isOpen())
    {
        sf::Event event;
        while ( window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            { window.close(); }

            // REFRESH WINDOW
            window.clear();
            window.draw(head);
            window.display();
        }
    }

    return 0;
}

您已經失去了對紋理的參考 在您的情況下,紋理是load_img()函數中的局部變量。

在任何精靈使用它時,都應保持紋理加載並可用。

我建議您閱讀SFML教程的這一部分,其中解釋了此問題。

好吧,我想我已經知道了。

可能什么也不顯示,因為事件循環中包含所有繪圖內容

嘗試將這些內容放入while(window.isOpen())循環作用域中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM