0
\$\begingroup\$

I created a window successfully in SFML using a Game class which hold all of the key components of the game. I then tried to start rendering shapes on the screen. When I first rendered the shapes, I was creating local copies inside of the render function. Of course, that doesn't allow me to do much and it isn't good organization of code, so I moved the main shape of the game I have in mind to the Game class. Now the shape doesn't show up at all. The application doesn't crash but it just doesn't show the shape. I feel like this might have something to do with the scope of the shape, but I just can't make sense of it. I looked at "The white square problem" on SFMLs site but I do not feel like this relates since the shape is a member of the class (which I thought meant it shouldn't go out of scope until the instance of the class does). I'm using g++ and am on a iMac running El Capitan if that helps any. Any help at all is greatly appreciated! Please help me understand this if you can!

p.s. Feel free to comment on any part of my code that isn't even directly related to the question but rather should just be improved.

Main.cpp

#include "Game.hpp"

int main(int argc, char** argv)
{
    Game game;

    game.setup();

    while (game.isRunning())
    {
        game.events();
        game.update();
        game.render();
    }

    game.cleanup();

    return 0;
}

Game.hpp

#ifndef GAME_H
#define GAME_H

#include <SFML/Graphics.hpp>

class Game
{

private:
    enum GameState
    {
        GAME_STATE_RUNNING,
        GAME_STATE_CLOSING
    };

    sf::RenderWindow window;
    GameState state;
    sf::CircleShape leader;

public:
    Game();
    ~Game();

    void setup();
    bool isRunning();
    void events();
    void update();
    void render();
    void cleanup();

};

#endif

Game.cpp

#include <SFML/Graphics.hpp>

#include "Game.hpp"

Game::Game() 
{
}

Game::~Game() 
{
}

void Game::setup()
{
    this->window.create(sf::VideoMode(1000, 750), "Follo");

    this->leader.setFillColor(sf::Color::Black);
    this->leader.setPosition(100, 100);

    this->state = GAME_STATE_RUNNING;
}

void Game::events()
{
    sf::Event event;

    while (this->window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
        {
            this->state = GAME_STATE_CLOSING;
        }
    }
}

bool Game::isRunning()
{
    return this->state != GAME_STATE_CLOSING;
}

void Game::update()
{
}

void Game::render()
{
    this->window.clear(sf::Color(203, 209, 219, 255));
    this->window.draw(this->leader);
    this->window.display();
}

void Game::cleanup()
{
}

Makefile

COMPILE         = g++ -c
LINK            = g++ -o
FLAGS           = -g -Wall -O2 
O_FILES         = Main.o Game.o
LIB_PATH        = /usr/local/lib
INCLUDE_PATH    = /usr/local/include
LIBS            = -lsfml-graphics -lsfml-window -lsfml-system
EXE_NAME        = Follo

program: build
    ./$(EXE_NAME)

build: $(O_FILES)
    $(LINK) $(EXE_NAME) $(O_FILES) -L$(LIB_PATH) $(LIBS)

Main.o: Main.cpp
    $(COMPILE) $(FLAGS) Main.cpp 

Game.o: Game.cpp Game.hpp
    $(COMPILE) $(FLAGS) Game.cpp 
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You're not initializing leader explicitly in your game class constructor. That means it is default-constructed, and the default constructor for CircleShape creates a circle with a radius of 0.

You should do something like

Game::Game()
: leader(10.0f) {
}

for your Game constructor to give the circle a reasonable radius (in this case, 10).

\$\endgroup\$
1
  • \$\begingroup\$ Wow. That was a silly mistake to make! Thanks a lot for pointing that out! \$\endgroup\$
    – Callischon
    Jan 10, 2017 at 4:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .