Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - congwei

Pages: [1]
1
General / Re: image cannot display
« on: November 25, 2023, 06:42:53 pm »
I know the reason, if put sf::Texture t to class member, it work.

2
General / image cannot display
« on: November 25, 2023, 05:59:42 pm »
I create a sfml project with cmake on windows, but image cannot display

bye the way, I am a beginner of c++ and sfml, so ignore the style of code, I just want it work.

src/main.cpp
#include "game.hpp"
#include <SFML/Graphics.hpp>

int main() {

    Game game;

    sf::Clock clock;
    sf::Time elapsed;

    float frametime = 1.0f / 60.0f;

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

        // 1. 事件处理
        game.handleInput();

        // 2. 更新物理逻辑
        if (elapsed.asSeconds() >= frametime) {
            elapsed -= sf::seconds(frametime);
            game.update(); // 60次/秒
        }
        elapsed += clock.restart();

        // 3. 渲染
        game.window.clear();
        game.render();
        game.window.display();
    }

    return 0;
}
 

src/game.hpp
class Game {
  public:
    sf::RenderWindow window;

    sf::Sprite s;

    Game() {
        window.create({320, 480}, "hello");

        sf::Texture t;
        t.loadFromFile("images/tiles.png");

        s.setTexture(t);
    }

    void handleInput() {}

    void update() {}

    void render() { window.draw(s); }
};
 

but if I put draw code into main.cpp, it's work, I dont know why. like this:
src/main.cpp
       ...
        // 3. 渲染
        game.window.clear();
        // game.render();
        sf::Texture t;
        t.loadFromFile("images/tiles.png");

        sf::Sprite s(t);
        game.window.draw(s);
        game.window.display();
       ...
 


Pages: [1]
anything