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();
...