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 - kanbaru

Pages: [1]
1
Graphics / Re: Load a white rectangle using Game headerfile
« on: March 17, 2019, 03:50:11 am »
Thanks a lot for all the replies.
I am sorry that I post a trivial question, I am new to SFML. Next time, I will pay attention to the format and official document.
I solved this problem by adding static to the ResourceHolder:
static ResourceHolder<sf::Texture, Textures::ID> textures;
Anyway, thanks for your help.

2
Graphics / Load a white rectangle using Game headerfile
« on: March 16, 2019, 01:31:17 pm »
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

Pages: [1]