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

Author Topic: Unexpected semantic error  (Read 1701 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Unexpected semantic error
« on: September 14, 2013, 07:38:48 am »
Hello

I was trying out the code given in The SFML Game Dev book in the first chapter:

Here are main.cpp, Game.h and Game.cpp

Game.h
#ifndef GAME_H
#define GAME_H


#include <SFML/Graphics.hpp>

class Game : private sf::NonCopyable
{
    public:
        Game();
        void run();
        ~Game();

    private:
        void processEvents();
        void update();
        void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
        void render();

    private:
        sf::RenderWindow mWindow;
        sf::CircleShape mPlayer;

        bool mIsMovingUp;
        bool mIsMovingDown;
        bool mIsMovingLeft;
        bool mIsMovingRight;
};

#endif // GAME_H
 

Game.cpp
#include "Game.h"


Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application")
{
    mPlayer.setRadius(40.0f);
    mPlayer.setPosition(100.f, 100.f);
    mPlayer.setFillColor(sf::Color::Cyan);
}

void Game::run()
{
    while(mWindow.isOpen())
    {
        processEvents();
        update();
        render();
    }
}

void Game::processEvents()
{
    sf::Event event;
    while(mWindow.pollEvent((event)))
    {
        switch(event.type)
        {
            case sf::Event::KeyPressed:
                handlePlayerInput(event.key.code, true);
                break;
            case sf::Event::KeyReleased:
                handlePlayerInput(event.key.code, false);
                break;
            case sf::Event::Closed:
                mWindow.close();
                break;
        }
    }
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
    if(key == sf::Keyboard::W)
        mIsMovingUp = isPressed;
    else if(key == sf::Keyboard::S)
        mIsMovingDown = isPressed;
    else if(key == sf::Keyboard::A)
        mIsMovingLeft = isPressed;
    else if(key == sf::Keyboard::D)
        mIsMovingRight = isPressed;

}

void Game::update()
{
    sf::Vector2f movement(0.f, 0.f);

    if(mIsMovingUp)
       movement.y -= 1.f;
    if(mIsMovingDown)
        movement.y += 1.f;
    if(mIsMovingLeft)
        movement.x -= 1.f;
    if(mIsMovingRight)
        movement.x += 1.f;

    mPlayer.move(movement);

}

void Game::render()
{
    mWindow.clear();
    mWindow.draw(mPlayer);
    mWindow.display();
}

Game::~Game()
{
    //dtor

 

main.cpp
#include "Game.h"

int main()
{
    Game game;
    game.run();
}
 

But whenever I am running my code, the circle starts moving upwards automatically and if I press the W key it stops moving and then everything works normal and as expected.

If I change Game.cpp as follows, everything is normal:
.
.
.
Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mIsMovingUp(false)
{
.
.
.
 

So what's exactly the problem in the code in the first case and why is the variable mIsMovingUp only initialized during runtime?

Thanks

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: Unexpected semantic error
« Reply #1 on: September 14, 2013, 08:57:38 am »
Yeah looks like the variables didn't get initialized. Where's the code e exactly from? If it's copied from the book you must know that the book doesn't always show the full code, thus such initialization might have been left out, but might still be implemented in the online source.
If it is from the Git repo then you should let Nexus/Groogy/Grimshaw know. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Unexpected semantic error
« Reply #2 on: September 14, 2013, 02:35:09 pm »
This is clearly not the original code, so you should have a look at GitHub ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: AW: Unexpected semantic error
« Reply #3 on: September 14, 2013, 05:18:26 pm »
Yeah looks like the variables didn't get initialized. Where's the code e exactly from? If it's copied from the book you must know that the book doesn't always show the full code, thus such initialization might have been left out, but might still be implemented in the online source.
If it is from the Git repo then you should let Nexus/Groogy/Grimshaw know. ;)

Yes, I had some idea regarding this since in the beginning the authors have clearly stated they'll be only teaching game programming, not C++ and advised to have prior C++ knowledge or read a good C++ book in parallel. You made it more clear. An I

This is clearly not the original code, so you should have a look at GitHub ;)

I was just skimming through the first chapter in the book and there you(and your co-authors) have defined a basic Game class for moving a cyan circle. I got it from there. But in the git repo the variables are initialized using the default ctor.

Thanks ;)

 

anything