I followed the book
SFML Game Development, and find problem in doing the chapter 2, about resources management.
Here I used:
#include "Game.h"
#include <SFML/Graphics.hpp>
void main()
{
Game game;
game.run();
}
While in the loading function, it turns out rendering a white rectangle.
Game.h
#pragma oncE
#include <SFML/Graphics.hpp>
#include <ResourceHolder.hpp>
#include <iostream>
#include <assert.h>
class Game
{
public:
Game();
~Game();
void run();
private:
void processEvents();
void update(sf::Time deltaTime);
void render();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
private:
sf::Sprite player;
sf::RenderWindow window;
sf::Texture playerTexture;
bool pIsMovingUp= false;
bool pIsMovingDown = false;
bool pIsMovingLeft = false;
bool pIsMovingRight = false;
};
Game.cpp
Game::Game()
: window(sf::VideoMode(800, 600), "Simulator", sf::Style::Default)
, player()
{
// window.setFramerateLimit(25);
//Try to load resources
ResourceHolder<sf::Texture, Textures::ID> textures;
try
{
textures.load(Textures::MainRole, "Media/textures/a.png");
}
catch (std::runtime_error& e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
player.setOrigin(100.0f, 100.0f);
player.setPosition(400.0f, 300.0f);
player.setTexture(textures.get(Textures::MainRole));
}
If I use Game class to run the program, a white rectangle will be the result.
However, when I copy the source code from the book's github, it is OK. And the source code is using main.cpp to run the whole program. Here's the github link.
https://github.com/SFML/SFML-Game-Development-Book/tree/master/02_Resources