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

Pages: [1]
1
Pong.cpp :
#include "Game.h"

int main()
{
        Game Pong;
        Pong.ChangeState(Game::State::MAINMENU);

        while (Pong.IsRunning())
        {
                Pong.Run();
        }

        return 0;
}



 
GameState.h :
#ifndef GAMESTATE_HPP
#define GAMESTATE_HPP

class Game;

class GameState{
public:
        virtual void HandleEvents(Game& game) = 0;
        virtual void Update(Game& game) = 0;
        virtual void Draw(Game& game) = 0;
        virtual      ~GameState() {};
};

#endif
 

Game.h :
#ifndef GAME_HPP
#define GAME_HPP

#include <SFML\Graphics.hpp>
#include "GameState.h"
#include "MainMenuState.h"
#include "PlayState.h"
#include <memory>

class Game{
public:

        enum class State{ MAINMENU, PLAY };
        sf::RenderWindow window;

        Game();
        void Run();
        void ChangeState(State);
        bool IsRunning();
        void setRunning(bool);
        ~Game();
       

       

private:
       
        std::unique_ptr<GameState> CurrentState;
       
        bool isRunning;
};

#endif
Game.cpp :
#include"Game.h"


Game::Game()
{
        window.create(sf::VideoMode(1280, 720), "Pong");
        isRunning = true;
        //std::move(std::unique_ptr<MainMenuState>(new MainMenuState));
}


void Game::Run()
{
        while (window.isOpen())
        {
                CurrentState->HandleEvents(*this);
                window.clear(sf::Color(0, 134, 194));
                CurrentState->Update(*this);
                CurrentState->Draw(*this);
                window.display();
        }
}

void Game::ChangeState(State state)
{
        switch (state)
        {
        case State::MAINMENU: CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState)); break;
        case State::PLAY: CurrentState = std::move(std::unique_ptr<PlayState>(new PlayState)); break;
        }
}

bool Game::IsRunning()
{
        return isRunning;
}

void Game::setRunning(bool running)
{
        isRunning = running;
}



Game::~Game()
{

}

MainMenuState.h :
#ifndef MAINMENUSTATE_HPP
#define MAINMENUSTATE_HPP

#include "GameState.h"
#include "SFML\Graphics.hpp"

class MainMenuState : public GameState{
public:

        sf::Text StartGame;
        sf::Text QuitGame;

        sf::Font font;

        MainMenuState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Draw(Game &game);
        ~MainMenuState();

        bool bStartGameSelected = false;
        bool bQuitGameSelected = false;

};

#endif

 
MainMenuState.cpp :
#include "MainMenuState.h"
#include "Game.h"



MainMenuState::MainMenuState()
{
       
        if(!font.loadFromFile("assets\\fonts\\HAMMERHEAD.ttf"))
        {
                printf("Unable to load font");
        }
       
        StartGame.setFont(font);
        QuitGame.setFont(font);

        StartGame.setString("Start Game");
        QuitGame.setString("Quit Game");

        StartGame.setCharacterSize(50);
        QuitGame.setCharacterSize(50);

       
        StartGame.setPosition(440.f,300.f);
        QuitGame.setPosition(440.f,350.f);

}

void MainMenuState::HandleEvents(Game &game)
{
        sf::Event event;
        while (game.window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                {
                        game.window.close();
                        game.setRunning(false);
                }
                if (event.type == sf::Event::MouseButtonReleased)
                {
                        if (event.mouseButton.button == 0)
                        {
                                if (bStartGameSelected)
                                {
                                        game.ChangeState(Game::State::PLAY);
                                }

                                else if (bQuitGameSelected)
                                {
                                        game.window.close();
                                        game.setRunning(false);
                                }
                        }
                }

        }

}

void MainMenuState::Update(Game &game)
{

        sf::Vector2i pixelPos = sf::Mouse::getPosition(game.window);
        sf::Vector2f worldPos = game.window.mapPixelToCoords(pixelPos);

        if (StartGame.getGlobalBounds().contains(worldPos) && StartGame.getColor() != sf::Color::Green)
        {
                StartGame.setColor(sf::Color::Green);
                bStartGameSelected = true;
        }
        else if (!StartGame.getGlobalBounds().contains(worldPos) && StartGame.getColor() == sf::Color::Green)
        {
                StartGame.setColor(sf::Color::White);
                bStartGameSelected = false;
        }



        if (QuitGame.getGlobalBounds().contains(worldPos) && QuitGame.getColor() != sf::Color::Green)
        {
                QuitGame.setColor(sf::Color::Green);
                bQuitGameSelected = true;
        }
        else if (!QuitGame.getGlobalBounds().contains(worldPos) && QuitGame.getColor() == sf::Color::Green)
        {
                QuitGame.setColor(sf::Color::White);
                bQuitGameSelected = false;
        }

}


void MainMenuState::Draw(Game &game)
{
        game.window.draw(StartGame);
        game.window.draw(QuitGame);
}

MainMenuState::~MainMenuState()
{
        printf("MainMenu Destructor called\n");
}

PlayState.h :
#ifndef PLAYSTATE_HPP
#define PLAYSTATE_HPP

#include "GameState.h"

class PlayState : public GameState{
public:

        PlayState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Draw(Game &game);
        ~PlayState();
};

#endif

PlayState.cpp :
#include "PlayState.h"
#include "Game.h"

PlayState::PlayState()
{
        printf("PlayState-Constructor called\n");
}

void PlayState::HandleEvents(Game &game)
{
        sf::Event event;
        while (game.window.pollEvent(event))
        {
                if (event.type == sf::Event::KeyPressed)
                {
                        if (event.type == sf::Keyboard::Escape)
                        {
                                game.ChangeState(Game::State::MAINMENU);
                        }
                }

        }
       
}

void PlayState::Update(Game &game)
{

}

void PlayState::Draw(Game &game)
{

}

PlayState::~PlayState()
{

}
 

Thats my code.....

2
Yes , thisis my Loop :

while (window.isOpen())
   {
      CurrentState->HandleEvents(*this);
      window.clear(sf::Color(0, 134, 194));
      CurrentState->Update(*this);
      CurrentState->Draw(*this);
      window.display();
   }

so this shouldn't be the problem.

3
Hello,
Im making a Pong Game with SFML. But when i start the Game it just draws the Background of whatever is behind the window. If I start the Game with the Dektop behind the Game Window it just draws the Desktop-Background. But it worked previously. Is it possible that the OS is causing the problems ?

4
General / Re: TextPosition relative to window size
« on: April 28, 2016, 05:04:43 pm »
Thanks man, helped me a lot ;D

5
General / [SOLVED]TextPosition relative to window size
« on: April 28, 2016, 04:44:31 pm »
Hello, I'm making a Pong Game, and for this i made a MainMenu gamestate. There's a text for Start Game and one for End Game. Now i made this do change the color of the text if I'm moving my mous over it:

if (txtStartGame.getGlobalBounds().contains(
      sf::Mouse::getPosition(game.window).x,
      sf::Mouse::getPosition(game.window).y) &&
      txtStartGame.getColor() != sf::Color::Green)
        {
            //changecolor
        }

now this works great, but only if I dont change the size of my window. If I change the size of the window, i have to move the mouse on another spot to change the color of the text i want to change.

im making the window like this:

window.create(sf::VideoMode(1280, 720), "Pong");

and the text im making like this:

        txtStartGame.setFont(font);
   txtStartGame.setString("Start Game");
   txtStartGame.setCharacterSize(30);
   txtStartGame.setPosition(400.f, 500.f);


So what am I doing wrong ?

(Sorry for my bad English by the way xD)

6
Yeah, thank you. I hate it when this happens xD

7
Hello, i have a little Problem. I'm programming a little Pong Game. For this I'm using two Gamestates, the MainMenu and the Playstate. Now if I am at the Playstate, i want the ESCPAE Button to go back to the MainMenu.
Now if I'm in the Playstate and move the mouse to the left side of the window, it also goes back to the MainMenu.

With this code :

 if (event.type = sf::Event::KeyPressed)
      {
         if (event.key.code == sf::Keyboard::Escape)
         {
            game.ChangeState(Game::gameStates::MAINMENU);
         }
      }

I'm checking if ESCAPE was pressed. Now if I dont use this code, it doesnt move back to the MainMenu if i move the mouse to the left of the window.

Do you have any ideas what I'm doing wrong?

Pages: [1]
anything