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

Author Topic: Need help in creating a game state manager class  (Read 4768 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
Need help in creating a game state manager class
« on: August 21, 2013, 12:41:37 pm »
Hello

I am creating a pong clone. I have already made it using procedural programming but now I am working on it to make it object oriented. So, in my game code I have a GameStateManager class in which I have member functions which represent each state, like coresponding to the showing menu state, I have a member function called showMenu() which does all the needful upon being called in the main() function through the objects of my GameStateManager class. Things have been pretty well  but when I define a similar function for the state playing vs computer, an unexpected situation arises. Everytime I call the function that is responsible for displaying the players' paddles, the ball and the background, the function is called but the game gets stuck-the reason being everytime it is called it re-initializes everything to the default and even if I press the keys, nothing happens becuase again and again the function is called in the main(). I hope I am clear in explaining my problem

Thanks

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Need help in creating a game state manager class
« Reply #1 on: August 21, 2013, 01:20:38 pm »
So split it into one function that initializes everything and another function that draws/updates everything?

You'll probably have to show us complete and minimal code to get a real answer.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Need help in creating a game state manager class
« Reply #2 on: August 21, 2013, 04:04:55 pm »
So split it into one function that initializes everything and another function that draws/updates everything?

You'll probably have to show us complete and minimal code to get a real answer.

Thanks. I am trying out creating a Paddle and ball class and then pass objects of those class to my game manager class member function by reference. It might just work.

An after that if I face any problem, I'll show my completer code.

Thanx again :)

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Need help in creating a game state manager class
« Reply #3 on: August 21, 2013, 04:55:11 pm »
Ok. I'm done with coding. Here's my Paddle class, Ball class and Game state Manager class. Plese note that I haven't documented my code fully( or even partially). And one more thing I'd like to make clear - I am not a professional or brilliant programmer - I'm still learning. So if I have committed any laughable mistake or  made the code too long than necessary, please pardon me.

Paddle.h:
/** Paddle.h **/

#ifndef PADDLE_H
#define PADDLE_H

#include <SFML/Graphics.hpp>

class Paddle
{
    private:
        //sf::RectangleShape PaddleObject;

    public:
        sf::RectangleShape PaddleObject;

        Paddle();
        Paddle(sf::Vector2f PADDLE_OBJECT_SIZE, sf::Color PADDLE_OBJECT_COLOR,
               sf::Vector2f PADDLE_OBJECT_POSITION);
        ~Paddle();
};

#endif // PADDLE_H
 

Paddle.cpp
/** Paddle.cpp **/

#include "Paddle.h"

Paddle::Paddle()
{

}

Paddle::Paddle(sf::Vector2f PADDLE_OBJECT_SIZE, sf::Color PADDLE_OBJECT_COLOR,
               sf::Vector2f PADDLE_OBJECT_POSITION)
{
    PaddleObject;
    PaddleObject.setSize(PADDLE_OBJECT_SIZE);
    PaddleObject.setFillColor(PADDLE_OBJECT_COLOR);
    PaddleObject.setPosition(PADDLE_OBJECT_POSITION);
}

Paddle::~Paddle()
{

}
 

Ball.h:
/** Ball.h **/

#ifndef BALL_H
#define BALL_H

#include <SFML/Graphics.hpp>
#include "Paddle.h"

class Ball
{
    private:
       //sf::CircleShape BallObject;

    public:
        sf::CircleShape BallObject;
        Ball();
        Ball(float BALL_OBJECT_RADIUS, sf::Color BALL_OBJECT_COLOR,
             sf::Vector2f BALL_OBJECT_POSITION);
        ~Ball();
};

#endif // BALL_H
 

Ball.cpp:
/** Ball.cpp **/

#include "Ball.h"

Ball::Ball()
{

}

Ball::Ball(float BALL_OBJECT_RADIUS, sf::Color BALL_OBJECT_COLOR,
             sf::Vector2f BALL_OBJECT_POSITION)
{
    BallObject.setRadius(BALL_OBJECT_RADIUS);
    BallObject.setFillColor(BALL_OBJECT_COLOR);
    BallObject.setPosition(BALL_OBJECT_POSITION);
}

Ball::~Ball()
{

}
 

GameState.h:
/** GameState.h **/

#ifndef GAMESTATE_H_INCLUDED
#define GAMESTATE_H_INCLUDED

enum GameState{
    showingMainMenu,
    showingPlayMenu,
    showingOptionsMenu,
    showingHowToPlay,
    showingAbout,
    playingVsComputer,
    playingVsHuman,
    quitting
};

#endif // GAMESTATE_H_INCLUDED

 

GameStateManager.h:
/** GameStateManager.h **/

#ifndef GAMESTATEMANAGER_H
#define GAMESTATEMANAGER_H


#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <sstream>
#include "GameState.h"
//#include "Constants.h"
#include "Paddle.h"
#include "Ball.h"

class GameStateManager
{
    private:
       GameState CurrentState, PreviousState;
       Paddle Player_1, Player_2 ;
       Ball GameBall;

    public:

        //static Ball ball;
        //static Paddle player1, player2;

        GameStateManager();

        ~GameStateManager();

        void setGameState(GameState STATE);
        GameState getGameState();

        void showMainMenu(sf::RenderWindow &GAME_WINDOW);

        void showPlayMenu(sf::RenderWindow &GAME_WINDOW);

        void showOptionsMenu(sf::RenderWindow &GAME_WINDOW);

        void showHowToPlay(sf::RenderWindow &GAME_WINDOW);

        void showAbout(sf::RenderWindow &GAME_WINDOW);

        void playVsComputer(sf::RenderWindow &GAME_WINDOW, int GAME_WINDOW_WIDTH, int GAME_WINDOW_HEIGHT,
                            Paddle &PLAYER_1_PADDLE, Paddle &PLAYER_2_PADDLE, Ball &GAME_BALL);

        void playVsHuman(sf::RenderWindow &GAME_WINDOW,
                    int GAME_WINDOW_WIDTH, int GAME_WINDOW_HEIGHT);

        void quitGame(sf::RenderWindow &GAME_WINDOW);
};

#endif // GAMESTATEMANAGER_H
 

GameStateManager.cpp:
#include "GameStateManager.h"
#include <iostream>


GameStateManager::GameStateManager()
{
    CurrentState = showingMainMenu;
}

GameStateManager::~GameStateManager()
{
    //dtor
}

void GameStateManager::setGameState(GameState STATE)
{
    CurrentState = STATE;
}

GameState GameStateManager::getGameState()
{
    return CurrentState;
}


void GameStateManager::showMainMenu(sf::RenderWindow &GAME_WINDOW)
{
    GAME_WINDOW.clear(sf::Color(0, 0, 0));

    /** BACKGROUND IMAGE SPRITE **/
    sf::Texture BackgroundTexture;
    if(!BackgroundTexture.loadFromFile("background.png"))
        GAME_WINDOW.close();

    sf::Sprite BackgroundSprite;
    BackgroundSprite.setTexture(BackgroundTexture);

    /** MENU TITLE SPRITE */
    sf::Texture MenuTitle;
    if(!MenuTitle.loadFromFile("menutitle.png"))
        GAME_WINDOW.close();

    sf::Sprite MenuTitleSprite;
    MenuTitleSprite.setTexture(MenuTitle);
    MenuTitleSprite.setPosition(sf::Vector2f(265, 250));

    /** MENU BUTTON TEXTURE **/
    sf::Texture MenuTexture;
    if(!MenuTexture.loadFromFile("menu.png"))
        GAME_WINDOW.close();

    /** MENU BUTTONS **/

    // PLAY BUTTON
    sf::Sprite PlayButtonSprite;
    PlayButtonSprite.setTexture(MenuTexture);
    PlayButtonSprite.setTextureRect(sf::IntRect(0, 7, 100, 35));
    PlayButtonSprite.setPosition(sf::Vector2f(305, 350));

    // OPTIONS BUTTON
    sf::Sprite OptionsButtonSprite;
    OptionsButtonSprite.setTexture(MenuTexture);
    OptionsButtonSprite.setTextureRect(sf::IntRect(0, 40, 170, 25));
    OptionsButtonSprite.setPosition(sf::Vector2f(305, 385));

    // HOW TO PLAY BUTTON
    sf::Sprite HowToPlayButtonSprite;
    HowToPlayButtonSprite.setTexture(MenuTexture);
    HowToPlayButtonSprite.setTextureRect(sf::IntRect(0, 70, 256, 25));
    HowToPlayButtonSprite.setPosition(sf::Vector2f(305, 415));

    // ABOUT BUTTON
    sf::Sprite AboutButtonSprite;
    AboutButtonSprite.setTexture(MenuTexture);
    AboutButtonSprite.setTextureRect(sf::IntRect(0, 100, 130, 25));
    AboutButtonSprite.setPosition(sf::Vector2f(305, 447));

    // QUIT BUTTON
    sf::Sprite QuitButtonSprite;
    QuitButtonSprite.setTexture(MenuTexture);
    QuitButtonSprite.setTextureRect(sf::IntRect(0, 130, 95, 30));
    QuitButtonSprite.setPosition(sf::Vector2f(305, 475));


    sf::Vector2i MousePosition = sf::Mouse::getPosition(GAME_WINDOW);
    std::cout << MousePosition.x << ", " << MousePosition.y << std::endl;   // omit this later

    if(MousePosition.x > 308 && MousePosition.x < 435 && MousePosition.y > 351 && MousePosition.y < 370)
    {
        PlayButtonSprite.setTextureRect(sf::IntRect(105, 7, 135, 35));

        OptionsButtonSprite.setTextureRect(sf::IntRect(0, 40, 170, 25));
        HowToPlayButtonSprite.setTextureRect(sf::IntRect(0, 70, 256, 25));
        AboutButtonSprite.setTextureRect(sf::IntRect(0, 100, 130, 25));
        QuitButtonSprite.setTextureRect(sf::IntRect(0, 130, 95, 30));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            //CurrentState = showingPlayMenu;
            setGameState(showingPlayMenu);
        }
    }

    if(MousePosition.x > 307 && MousePosition.x < 510 && MousePosition.y > 385 && MousePosition.y < 405)
    {
        OptionsButtonSprite.setTextureRect(sf::IntRect(170, 40, 210, 25));

        PlayButtonSprite.setTextureRect(sf::IntRect(0, 7, 100, 35));
        HowToPlayButtonSprite.setTextureRect(sf::IntRect(0, 70, 256, 25));
        AboutButtonSprite.setTextureRect(sf::IntRect(0, 100, 130, 25));
        QuitButtonSprite.setTextureRect(sf::IntRect(0, 130, 95, 30));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
//            CurrentState = showingOptionsMenu;
        }
    }

    if(MousePosition.x > 307 && MousePosition.x < 598 && MousePosition.y > 415 && MousePosition.y < 434)
    {
        HowToPlayButtonSprite.setTextureRect(sf::IntRect(260, 70, 560, 25));

        PlayButtonSprite.setTextureRect(sf::IntRect(0, 7, 100, 35));
        OptionsButtonSprite.setTextureRect(sf::IntRect(0, 40, 170, 25));
        AboutButtonSprite.setTextureRect(sf::IntRect(0, 100, 130, 25));
        QuitButtonSprite.setTextureRect(sf::IntRect(0, 130, 95, 30));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
//            CurrentState = showingHowToPlay;
        }
    }

    if(MousePosition.x > 306 && MousePosition.x < 474 && MousePosition.y > 450 && MousePosition.y < 466)
    {
        AboutButtonSprite.setTextureRect(sf::IntRect(135, 100, 175, 25));

        PlayButtonSprite.setTextureRect(sf::IntRect(0, 7, 100, 35));
        OptionsButtonSprite.setTextureRect(sf::IntRect(0, 40, 170, 25));
        HowToPlayButtonSprite.setTextureRect(sf::IntRect(0, 70, 256, 25));
        QuitButtonSprite.setTextureRect(sf::IntRect(0, 130, 95, 30));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
//            CurrentState = showingAbout;
        }
    }

    if(MousePosition.x > 310 && MousePosition.x < 435 && MousePosition.y > 478 && MousePosition.y < 502)
    {
        QuitButtonSprite.setTextureRect(sf::IntRect(100, 130, 135, 30));

        PlayButtonSprite.setTextureRect(sf::IntRect(0, 7, 100, 35));
        OptionsButtonSprite.setTextureRect(sf::IntRect(0, 40, 170, 25));
        HowToPlayButtonSprite.setTextureRect(sf::IntRect(0, 70, 256, 25));
        AboutButtonSprite.setTextureRect(sf::IntRect(0, 100, 130, 25));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            PreviousState = CurrentState;
            CurrentState = quitting;
        }
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
    {
        PreviousState = CurrentState;
        CurrentState = quitting;
    }

    GAME_WINDOW.draw(BackgroundSprite);
    GAME_WINDOW.draw(MenuTitleSprite);
    GAME_WINDOW.draw(PlayButtonSprite);
    GAME_WINDOW.draw(OptionsButtonSprite);
    GAME_WINDOW.draw(HowToPlayButtonSprite);
    GAME_WINDOW.draw(AboutButtonSprite);
    GAME_WINDOW.draw(QuitButtonSprite);
}

void GameStateManager::showPlayMenu(sf::RenderWindow &GAME_WINDOW)
{
    GAME_WINDOW.clear(sf::Color(0, 0, 0));

    /** BACKGROUND IMAGE SPRITE **/
    sf::Texture BackgroundTexture;
    if(!BackgroundTexture.loadFromFile("background.png"))
        GAME_WINDOW.close();

    sf::Sprite BackgroundSprite;
    BackgroundSprite.setTexture(BackgroundTexture);

    /** PLAY MENU TEXTURE **/
    sf::Texture MenuTexture;
    if(!MenuTexture.loadFromFile("menu.png"))
        GAME_WINDOW.close();

    /** PLAY MENU TITLE **/

    sf::Sprite PlayMenuTitle;
    PlayMenuTitle.setTexture(MenuTexture);
    PlayMenuTitle.setTextureRect(sf::IntRect(0, 7, 100, 35));
    PlayMenuTitle.setPosition(sf::Vector2f(330, 300));

    /** PLAY MENU BUTTON SPRITES **/
    // PLAYING VS COMPUTER BUTTON
    sf::Sprite PlayVSComputerButton;
    PlayVSComputerButton.setTexture(MenuTexture);
    PlayVSComputerButton.setTextureRect(sf::IntRect(0, 165, 370, 25));    // Fill these later
    PlayVSComputerButton.setPosition(sf::Vector2f(210, 380));            // Fill these later

    // PLAYING VS HUMAN BUTTON
    sf::Sprite PlayVShumanButton;
    PlayVShumanButton.setTexture(MenuTexture);
    PlayVShumanButton.setTextureRect(sf::IntRect(0, 195, 300, 30));    // Fill these later
    PlayVShumanButton.setPosition(sf::Vector2f(240, 430));            // Fill these later

    // BACK BUTTON SPRITE
    sf::Sprite BackButtonSprite;
    BackButtonSprite.setTexture(MenuTexture);
    BackButtonSprite.setTextureRect(sf::IntRect(6, 326, 90, 21));
    BackButtonSprite.setPosition(sf::Vector2f(335, 500));


    sf::Vector2i MousePosition = sf::Mouse::getPosition(GAME_WINDOW);
    std::cout << MousePosition.x << ", " << MousePosition.y << std::endl;   // omit this later

    if(MousePosition.x > 215 && MousePosition.x < 570 && MousePosition.y > 380 && MousePosition.y < 402) // 215, 570, 380, 402
    {
        PlayVSComputerButton.setTextureRect(sf::IntRect(370, 165, 410, 25));
        PlayVShumanButton.setTextureRect(sf::IntRect(0, 195, 300, 30));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            CurrentState = playingVsComputer;
        }
    }
    if(MousePosition.x > 245 && MousePosition.x < 534 && MousePosition.y > 432 && MousePosition.y < 454) // 215, 570, 380, 402
    {
        PlayVShumanButton.setTextureRect(sf::IntRect(305, 195, 340, 30));
        PlayVSComputerButton.setTextureRect(sf::IntRect(0, 165, 370, 25));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            CurrentState = playingVsHuman;
        }
    }

    if(MousePosition.x > 336 && MousePosition.x < 420 && MousePosition.y > 501 && MousePosition.y < 517)
    {
        //ShowMenu(sf::RenderWindow &GAME_WINDOW);
        BackButtonSprite.setTextureRect(sf::IntRect(105, 326, 125, 25));

        PlayVShumanButton.setTextureRect(sf::IntRect(0, 195, 300, 30));
        PlayVSComputerButton.setTextureRect(sf::IntRect(0, 165, 370, 25));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            CurrentState = showingMainMenu;
        }
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
    {
        PreviousState = CurrentState;
        CurrentState = quitting;
    }

    GAME_WINDOW.draw(BackgroundSprite);
    GAME_WINDOW.draw(PlayMenuTitle);
    GAME_WINDOW.draw(PlayVSComputerButton);
    GAME_WINDOW.draw(PlayVShumanButton);
    GAME_WINDOW.draw(BackButtonSprite);
}

void GameStateManager::showOptionsMenu(sf::RenderWindow &GAME_WINDOW)
{

    GAME_WINDOW.clear(sf::Color(0, 0, 0));



    //GAME_WINDOW.draw();

}

// I'll write these functions later
/*void GameStateManager::showHowToPlay(sf::RenderWindow &GAME_WINDOW);
void GameStateManager::showAbout(sf::RenderWindow &GAME_WINDOW);*/



void playVsComputer(sf::RenderWindow &GAME_WINDOW, int GAME_WINDOW_WIDTH, int GAME_WINDOW_HEIGHT,
                            Paddle &PLAYER_1_PADDLE, Paddle &PLAYER_2_PADDLE, Ball &GAME_BALL)
{
    GAME_WINDOW.clear(sf::Color(0, 0,0));

    sf::RectangleShape top(sf::Vector2f(800, 30));
    sf::RectangleShape bottom(sf::Vector2f(800, 30));
    sf::RectangleShape left(sf::Vector2f(30, 600));
    sf::RectangleShape right(sf::Vector2f(30, 600));
    sf::RectangleShape separationLine(sf::Vector2f(5, 540));

    top.setFillColor(sf::Color::Green);
    bottom.setFillColor(sf::Color::Green);
    left.setFillColor(sf::Color::Green);
    right.setFillColor(sf::Color::Green);
    separationLine.setFillColor(sf::Color::Green);

    top.setPosition(0, 0);
    bottom.setPosition(0, 570);
    left.setPosition(0,0);
    right.setPosition(770, 0);
    separationLine.setPosition(GAME_WINDOW_WIDTH / 2 - separationLine.getSize().x, 30);

    sf::Vector2<float> ballSpeed(0.2, 0.2);

    float paddlespeedY = 450.0f;

    float currentTime = 0.0;
    float previousTime = 0.0;
    float deltaTime = 0.0;
    sf::Clock Clock;

    sf::Font gameFont;
    if(!gameFont.loadFromFile("BankGothic.ttf"))
        GAME_WINDOW.close();

    sf::Text score("0   0", gameFont, 50);
    score.setColor(sf::Color(44, 127, 255));
    score.setPosition(sf::Vector2f(GAME_WINDOW_WIDTH/2 - 68, 20)) ;

    unsigned int player1_Score = 0, player2_Score = 0;

    sf::SoundBuffer paddleHit;
    if(!paddleHit.loadFromFile("paddlehit.wav"))
        GAME_WINDOW.close();

    sf::Sound sound_PaddleHit;
    sound_PaddleHit.setBuffer(paddleHit);

    sf::SoundBuffer scoreUpdate;
    if(!scoreUpdate.loadFromFile("scoreupdate.wav"))
        GAME_WINDOW.close();

    sf::Sound sound_ScoreUpdate;
    sound_ScoreUpdate.setBuffer(scoreUpdate);

    bool pressSpace= false;
    int spaceCounter = 0;

    sf::FloatRect player1_Rect = PLAYER_1_PADDLE.PaddleObject.getGlobalBounds();
    sf::FloatRect player2_Rect = PLAYER_1_PADDLE.PaddleObject.getGlobalBounds();
    sf::FloatRect topRect = top.getGlobalBounds();
    sf::FloatRect bottomRect = bottom.getGlobalBounds();
    sf::FloatRect leftRect = left.getGlobalBounds();
    sf::FloatRect rightRect = right.getGlobalBounds();

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        PLAYER_1_PADDLE.PaddleObject.move(0, - (paddlespeedY * deltaTime));

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        PLAYER_1_PADDLE.PaddleObject.move(0, paddlespeedY * deltaTime);

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && spaceCounter == 0)
    {
        pressSpace = true;
        spaceCounter = 1;
    }

    sf::Vector2i MousePosition = sf::Mouse::getPosition(GAME_WINDOW);
    std::cout << MousePosition.x << ", " << MousePosition.y << std::endl;

    if (GAME_BALL.BallObject.getPosition().y < PLAYER_2_PADDLE.PaddleObject.getPosition().y)
        PLAYER_2_PADDLE.PaddleObject.move(0, - paddlespeedY / 2000);
    else if(GAME_BALL.BallObject.getPosition().y +  2 * GAME_BALL.BallObject.getRadius() >
            PLAYER_2_PADDLE.PaddleObject.getPosition().y + PLAYER_2_PADDLE.PaddleObject.getSize().y)
        PLAYER_2_PADDLE.PaddleObject.move(0, paddlespeedY / 2000);

    if(PLAYER_1_PADDLE.PaddleObject.getGlobalBounds().intersects(topRect))
    {
        PLAYER_1_PADDLE.PaddleObject.setPosition(PLAYER_1_PADDLE.PaddleObject.getPosition().x,
                                                 top.getPosition().y + 30);
    }

    if(PLAYER_1_PADDLE.PaddleObject.getGlobalBounds().intersects(bottomRect))
    {
        PLAYER_1_PADDLE.PaddleObject.setPosition(PLAYER_1_PADDLE.PaddleObject.getPosition().x,
                            bottom.getPosition().y - PLAYER_1_PADDLE.PaddleObject.getSize().y - 0);
    }

    if(PLAYER_2_PADDLE.PaddleObject.getGlobalBounds().intersects(topRect))
    {
        PLAYER_2_PADDLE.PaddleObject.setPosition(PLAYER_2_PADDLE.PaddleObject.getPosition().x,
                                                 top.getPosition().y + 30);
    }
    if(PLAYER_2_PADDLE.PaddleObject.getGlobalBounds().intersects(bottomRect))
    {
        PLAYER_2_PADDLE.PaddleObject.setPosition(PLAYER_2_PADDLE.PaddleObject.getPosition().x,
                                bottom.getPosition().y - PLAYER_2_PADDLE.PaddleObject.getSize().y - 0);
    }

    if(GAME_BALL.BallObject.getGlobalBounds().intersects(player1_Rect))
    {
        sound_PaddleHit.play();
        sf::FloatRect p = PLAYER_2_PADDLE.PaddleObject.getGlobalBounds();
        sf::FloatRect b = GAME_BALL.BallObject.getGlobalBounds();
        ballSpeed.x = - ballSpeed.x;
        ballSpeed.y = (b.top + b.height / 2 - p.top - p.height / 2) / 100;
    }

    if(GAME_BALL.BallObject.getGlobalBounds().intersects(player2_Rect))
    {
        sound_PaddleHit.play();
        sf::FloatRect p = PLAYER_2_PADDLE.PaddleObject.getGlobalBounds();
        sf::FloatRect b = GAME_BALL.BallObject.getGlobalBounds();
        ballSpeed.x = - ballSpeed.x;
        ballSpeed.y = (b.top + b.height / 2 - p.top - p.height / 2) / 1000;
    }

    if(GAME_BALL.BallObject.getGlobalBounds().intersects(bottomRect))
    {
        ballSpeed.y = -ballSpeed.y;
    }

    if(GAME_BALL.BallObject.getGlobalBounds().intersects(topRect))
    {
        ballSpeed.y = -ballSpeed.y;
    }

    if(GAME_BALL.BallObject.getGlobalBounds().intersects(leftRect))
    {
        sound_ScoreUpdate.play();
        player2_Score++;
        std::stringstream Player_score;
        Player_score << player1_Score << "   " << player2_Score;
        score.setString(Player_score.str());
        GAME_BALL.BallObject.setPosition(sf::Vector2f(GAME_WINDOW_WIDTH / 2 - 5, GAME_WINDOW_HEIGHT / 2 - 5));
        PLAYER_2_PADDLE.PaddleObject.setPosition(sf::Vector2f(
                PLAYER_2_PADDLE.PaddleObject.getPosition().x, GAME_WINDOW_HEIGHT / 2 - 5));
        pressSpace = false;
        spaceCounter = 0;
        ballSpeed.x = 0.2;
        ballSpeed.y = 0.2;
    }

    if(GAME_BALL.BallObject.getGlobalBounds().intersects(rightRect))
    {
        sound_ScoreUpdate.play();
        player1_Score++;
        std::stringstream Player_score;
        Player_score << player1_Score << "   " << player2_Score;
        score.setString(Player_score.str());
        GAME_BALL.BallObject.setPosition(sf::Vector2f(GAME_WINDOW_WIDTH / 2 - 5, GAME_WINDOW_HEIGHT / 2 - 5));
        PLAYER_1_PADDLE.PaddleObject.setPosition(sf::Vector2f(
                PLAYER_2_PADDLE.PaddleObject.getPosition().x, GAME_WINDOW_HEIGHT / 2 - 5));
        pressSpace = false;
        spaceCounter = 0;
        ballSpeed.x = 0.2;
        ballSpeed.y = 0.2;
    }

    if(pressSpace && spaceCounter == 0)
        GAME_BALL.BallObject.move(ballSpeed.x, ballSpeed.y);

    if(player1_Score >= 10)
        score.setPosition(sf::Vector2f(GAME_WINDOW_WIDTH / 2 - 100, 20));

    else if(player1_Score < 10)
        score.setPosition(sf::Vector2f(GAME_WINDOW_WIDTH / 2 - 68, 20)) ;

    GAME_WINDOW.draw(top);
    GAME_WINDOW.draw(bottom);
    GAME_WINDOW.draw(left);
    GAME_WINDOW.draw(right);
    GAME_WINDOW.draw(separationLine);
    GAME_WINDOW.draw(PLAYER_1_PADDLE.PaddleObject);
    GAME_WINDOW.draw(PLAYER_2_PADDLE.PaddleObject);
    GAME_WINDOW.draw(GAME_BALL.BallObject);
    GAME_WINDOW.draw(score);

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
        GAME_WINDOW.close();


}

// I'll write this function later
/*void GameStateManager::playVsHuman(sf::RenderWindow &GAME_WINDOW, int GAME_WINDOW_WIDTH, int GAME_WINDOW_HEIGHT)
{

}*/


void GameStateManager::quitGame(sf::RenderWindow &GAME_WINDOW)
{
    sf::Texture quitmsg;
    if(!quitmsg.loadFromFile("quitmessage.png"))
        GAME_WINDOW.close();

    sf::Sprite QuitMsgSprite;
    QuitMsgSprite.setTexture(quitmsg);
    QuitMsgSprite.setTextureRect(sf::IntRect(0, 2, 622, 184));
    QuitMsgSprite.setPosition(sf::Vector2f(85, 200));

    sf::Vector2i MousePosition = sf::Mouse::getPosition(GAME_WINDOW);

    if(MousePosition.x > 240 && MousePosition.x < 388 && MousePosition.y > 309 && MousePosition.y < 363)
    {
        QuitMsgSprite.setTextureRect(sf::IntRect(2, 198, 622, 184));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            GAME_WINDOW.close();
    }

    if(MousePosition.x > 395 && MousePosition.x < 542 && MousePosition.y > 309 && MousePosition.y < 363)
    {
        QuitMsgSprite.setTextureRect(sf::IntRect(2, 394, 622, 184));

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            CurrentState = PreviousState;
    }

    if(MousePosition.x > 677 && MousePosition.x < 696 && MousePosition.y > 208 && MousePosition.y < 228)
    {
        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            CurrentState = PreviousState;
    }

    GAME_WINDOW.draw(QuitMsgSprite);
}
 


And here's main.cpp:
#include <SFML/Graphics.hpp>
#include "GameState.h"
#include "GameStateManager.h"
#include "Constants.h"
#include "Paddle.h"
#include "Ball.h"
#include <iostream>

int main()
{
    sf::RenderWindow GameWindow(sf::VideoMode(GAME_WINDOW_WIDTH, GAME_WINDOW_HEIGHT,
            GAME_WINDOW_BPP), GAME_WINDOW_CAPTION, sf::Style::Close);

    GameStateManager g_SManager;

    sf::Vector2f Player1_Size = sf::Vector2f(10, 60);
    sf::Color Player1_Color = sf::Color::Red;
    sf::Vector2f Player1_Position = sf::Vector2f(40, 260);

    sf::Vector2f Player2_Size = sf::Vector2f(10, 60);
    sf::Color Player2_Color = sf::Color::Red;
    sf::Vector2f Player2_Position = sf::Vector2f(750, 200);

    Paddle Player1(Player1_Size, Player1_Color, Player1_Position);
    Paddle Player2(Player2_Size, Player2_Color, Player2_Position);

    const float Ball_Radius = 5.0f;
    sf::Color Ball_Color = sf::Color::White;
    sf::Vector2f Ball_Position = sf::Vector2f(GAME_WINDOW_WIDTH / 2 - 5,
                            GAME_WINDOW_HEIGHT / 2 - 5);

    Ball GameBall(Ball_Radius, Ball_Color, Ball_Position);

    while(GameWindow.isOpen())
    {
        sf::Event GameEvent;
        while(GameWindow.pollEvent(GameEvent))
        {
            switch(GameEvent.type)
            {
                case sf::Event::Closed:
                    GameWindow.close();
                    break;
            }
        }

        switch(g_SManager.getGameState())
        {
        case showingMainMenu:
            g_SManager.showMainMenu(GameWindow);
            break;
        case showingPlayMenu:
            g_SManager.showPlayMenu(GameWindow);
            break;
        case quitting:
            g_SManager.quitGame(GameWindow);
            break;
        case playingVsComputer:
            g_SManager.playVsComputer(GameWindow, GAME_WINDOW_WIDTH, GAME_WINDOW_HEIGHT,
                                          Player1, Player2, GameBall);
            break;
        }
        GameWindow.display();
    }

    return EXIT_SUCCESS;
}

 


But this produces an error:
Undefined reference to void playVsComputer(sf::RenderWindow & , int, int , Paddle &, Paddle &, Ball &)

I really don't know what is going wrong  :-[ :(

EDIT:
To all forum members who are reading this post :
After rectification of errors in my code, I'll remove this post. Once my code is complete, I'll post it in the Projects sub forum. If this condition of mine is violating any forum rules, please personal message me stating so.
« Last Edit: August 21, 2013, 05:02:47 pm by The illusionist mirage »

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Need help in creating a game state manager class
« Reply #4 on: August 21, 2013, 06:50:06 pm »
Woops, my bad I had the wrong answer here...yea like Gobbles said below me.  You're missing the class scope on that function.
« Last Edit: August 21, 2013, 09:38:55 pm by hipsterdufus »

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Need help in creating a game state manager class
« Reply #5 on: August 21, 2013, 07:32:04 pm »
You have a lot of update and draw code mixed together... It honestly looks like it went through a blender.

Maybe you should try setting up some function calls to separate everything into Input->Update->Draw. Might make things easier to read.

Also I should note that your doing .LoadFromFile every frame for stuff. You only need to do this once, you just need to make sure the font/texture/whatever else stays alive as long as it's being drawn.

Actually going through it some more, you should consider picking up a good C++ book and a good game dev book. Both will have a wealth of knowledge in learning this stuff.

BUT if all your looking for is a fix to your issue, this is the problem:
void playVsComputer(sf::RenderWindow &GAME_WINDOW, int GAME_WINDOW_WIDTH, int GAME_WINDOW_HEIGHT,
                            Paddle &PLAYER_1_PADDLE, Paddle &PLAYER_2_PADDLE, Ball &GAME_BALL)
{
...
 

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Need help in creating a game state manager class
« Reply #6 on: August 22, 2013, 11:59:50 am »
Quote from: Gobbles

Actually going through it some more, you should consider picking up a good C++ book and a good game dev book. Both will have a wealth of knowledge in learning this stuff.


Can you please suggest a game dev book for beginners. The ones I use for learning C++ are The complete C++ reference by Herbert Schildt and Learn C++ in 21 Days by Jesse Liberty & Bradley Jones.

But I didn't consult any game dev book until now.


Thanks

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Need help in creating a game state manager class
« Reply #7 on: August 22, 2013, 12:25:39 pm »
The SFML Game Development book is pretty neat, especially since it was written for SFML from community members.

It includes a chapter about state stack handling. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Need help in creating a game state manager class
« Reply #8 on: August 22, 2013, 12:39:52 pm »
The SFML Game Development book is pretty neat, especially since it was written for SFML from community members.

It includes a chapter about state stack handling. ;)


Yeah, I know. I saw the sample chapters at the PacktPub website and also the preview at safari books But no book store near me or in my state sells hard copies. And to get an e-book you need a credit card or PayPal etc, which I don't have. :(

But programming is my passion, I will learn, one way or the other. Maybe it'll take some time but one day I'll surely grab hold of C++ and then I can moonwalk with SFML. :)

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Need help in creating a game state manager class
« Reply #9 on: August 23, 2013, 08:53:16 pm »
Can you please suggest a game dev book for beginners. The ones I use for learning C++ are The complete C++ reference by Herbert Schildt and Learn C++ in 21 Days by Jesse Liberty & Bradley Jones.

But I didn't consult any game dev book until now.


Thanks

My apologies on the delay, I've used this book: http://www.amazon.com/Introduction-Game-Development-Second-Edition/dp/1584506792 and it is an amazing read and reference tool. Best Game Development book I've seen yet. Although kind of pricey I wouldn't trade my copy for anything

 

anything