Skip to content

Commit 1f202e1

Browse files
committed
MenuButton and PlayerState behaviour fixed/repaired
1 parent fe9244d commit 1f202e1

12 files changed

+138
-25
lines changed

Game.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,15 @@ void Game::render()
7979
SDL_RenderClear(m_pRenderer); //clean renderer
8080

8181
//actual render images
82-
for(int i = 0; i < m_gameObjects.size(); i++) //we draw all gameObject images
83-
{
84-
m_gameObjects[i]->draw(); //virtual GameObject methods allow us to use the propper inner level class method.
85-
}
82+
m_pGameStateMachine->render();
8683

8784
///////////////////
8885
SDL_RenderPresent(m_pRenderer); //draw in window
8986
}
9087

9188
void Game::update()
9289
{
93-
for (int i = 0; i < m_gameObjects.size(); i++)//we update all gameObjects
94-
{
95-
m_gameObjects[i]->update(); //virtual GameObject methods allow us to use the propper inner level class methods
96-
}
90+
m_pGameStateMachine->update();
9791
}
9892

9993
void Game::handleEvents()
@@ -113,7 +107,6 @@ void Game::quit()
113107

114108
void Game::clean()
115109
{
116-
cout << "natejem el joc" << endl;
117110
SDL_DestroyWindow(m_pWindow);
118111
SDL_DestroyRenderer(m_pRenderer);
119112
TheInputHandler::Instance()->clean();
@@ -124,3 +117,8 @@ bool Game::running() const
124117
{
125118
return m_running;
126119
}
120+
121+
GameStateMachine* Game::getStateMachine()
122+
{
123+
return m_pGameStateMachine;
124+
}

Game.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Game //SINGLETON
4747

4848
//GETs
4949
bool running() const;
50+
GameStateMachine* getStateMachine();
5051

5152

5253
private:
@@ -59,8 +60,8 @@ class Game //SINGLETON
5960
bool m_running; //Atribut per mirar si continua el loop del joc.
6061
SDL_Window* m_pWindow; //Window del joc.
6162
SDL_Renderer* m_pRenderer; //Renderer del joc.
62-
int m_currentFrame;
63-
GameStateMachine* m_pGameStateMachine;
63+
int m_currentFrame; //NOTE: is it needed?
64+
GameStateMachine* m_pGameStateMachine; //ACTUAL GAMESTATEMACHINE
6465

6566
//POLYMORFISM OBJECTS (have it declared allow us to create them everywhere
6667

GameStateMachine.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#include "GameStateMachine.h"
1+
#include "GameStateMachine.h"
2+
#include <iostream>
3+
4+
using namespace std;
25

36
GameStateMachine::GameStateMachine()
47
{
@@ -33,7 +36,6 @@ void GameStateMachine::changeState(GameState *pState)
3336
}
3437
if (m_gameStates.back()->onExit())
3538
{
36-
delete m_gameStates.back();
3739
m_gameStates.pop_back();
3840
}
3941
}
@@ -44,3 +46,19 @@ void GameStateMachine::changeState(GameState *pState)
4446
m_gameStates.back()->onEnter();
4547

4648
}
49+
50+
void GameStateMachine::update()
51+
{
52+
if(!m_gameStates.empty())
53+
{
54+
m_gameStates.back()->update();
55+
}
56+
}
57+
58+
void GameStateMachine::render()
59+
{
60+
if(!m_gameStates.empty())
61+
{
62+
m_gameStates.back()->render();
63+
}
64+
}

GameStateMachine.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ class GameStateMachine //manages all gamestates
1313

1414
void pushState(GameState* pState); //add state without removing the previous state
1515
void changeState(GameState* pState); //remove current state and change adding another
16-
void popState(); //remove current state without adding another
16+
void popState(); //remove current state without adding another
17+
18+
void update();
19+
void render();
20+
1721
protected:
1822
private:
1923

MenuState.cpp

+45-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#include "MenuState.h"
1+
#include "MenuState.h"
2+
#include "TextureManager.h"
3+
#include "Game.h"
24
#include <iostream>
35
using namespace std;
46

@@ -16,22 +18,62 @@ string MenuState::getStateID() const
1618

1719
void MenuState::update()
1820
{
19-
//nothing for now
21+
for(int i = 0; i < m_gameObjects.size(); i++)
22+
{
23+
m_gameObjects[i]->update();
24+
}
2025
}
2126

2227
void MenuState::render()
2328
{
24-
//nothig for now
29+
for (int i = 0; i < m_gameObjects.size(); i++)
30+
{
31+
m_gameObjects[i]->draw();
32+
}
2533
}
2634

2735
bool MenuState::onEnter()
2836
{
37+
if(!TheTextureManager::Instance()->load("img/button.png","playbutton", TheGame::Instance()->getRenderer()))
38+
{
39+
return false;
40+
}
41+
if(!TheTextureManager::Instance()->load("img/exit.png","exitbutton", TheGame::Instance()->getRenderer()))
42+
{
43+
return false;
44+
}
45+
46+
GameObject* button1 = new MenuButton(new LoaderParams(100, 100, 400, 100, "playbutton"), s_menuToPlay);
47+
GameObject* button2 = new MenuButton(new LoaderParams(100,300, 400, 100, "exitbutton"), s_exitFromMenu);
48+
49+
m_gameObjects.push_back(button1);
50+
m_gameObjects.push_back(button2);
51+
2952
cout << "entering MenuState" << endl;
3053
return true;
3154
}
3255

3356
bool MenuState::onExit()
3457
{
58+
for(int i = 0; i < m_gameObjects.size(); i++)
59+
{
60+
m_gameObjects[i]->clean();
61+
}
62+
m_gameObjects.clear();
63+
TheTextureManager::Instance()->clearFromTextureMap("playbutton"); //clean from TM
64+
TheTextureManager::Instance()->clearFromTextureMap("exitbutton"); //clean from TM
3565
cout << "exiting MenuState" << endl;
3666
return true;
3767
}
68+
69+
void MenuState::s_menuToPlay()
70+
{
71+
cout << "Play button clicked" << endl; //Go to play GameState
72+
TheGame::Instance()->getStateMachine()->changeState(new PlayState());
73+
}
74+
75+
void MenuState::s_exitFromMenu()
76+
{
77+
TheGame::Instance()->quit(); //Exit game.
78+
cout << "Exit button clicked" << endl;
79+
}

MenuState.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ using namespace std;
55

66
#include <string>
77
#include "GameState.h"
8+
#include <vector>
9+
#include "GameObject.h"
10+
#include "MenuButton.h"
811

912
class MenuState : public GameState
1013
{
@@ -21,7 +24,12 @@ class MenuState : public GameState
2124
protected:
2225
private:
2326

24-
static const string s_menuID;
27+
static const string s_menuID; //name of the class
28+
vector<GameObject*> m_gameObjects; //container for GameObjects
29+
30+
//call back functions for menu item, works for use as pointers of the MenuButton
31+
static void s_menuToPlay();
32+
static void s_exitFromMenu();
2533
};
2634

2735
#endif // MENUSTATE_H

PlayState.cpp

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "PlayState.h"
2+
#include "TextureManager.h"
3+
#include "Game.h"
24
#include <iostream>
35

46
const string PlayState::s_playID = "PLAY";
@@ -12,22 +14,44 @@ PlayState::PlayState()
1214

1315
void PlayState::update()
1416
{
15-
//nothing for now
17+
for(int i = 0; i < m_gameObjects.size(); i++)
18+
{
19+
m_gameObjects[i]->update();
20+
}
1621
}
1722

1823
void PlayState::render()
1924
{
20-
//nothing for now
25+
for (int i = 0; i < m_gameObjects.size(); i++)
26+
{
27+
m_gameObjects[i]->draw();
28+
}
2129
}
2230

2331
bool PlayState::onEnter()
2432
{
25-
cout << "entering PlayState" << endl;
33+
if(!TheTextureManager::Instance()->load("img/helicopter.png","helicopter", TheGame::Instance()->getRenderer())) //can't load images
34+
{
35+
return false;
36+
}
37+
GameObject* player = new Player (new LoaderParams(100, 100, 128, 55, "helicopter"));
38+
m_gameObjects.push_back(player);
39+
40+
cout << "Entering PlayState" << endl;
2641
return true;
2742
}
2843

2944
bool PlayState::onExit()
3045
{
46+
for (int i = 0; i < m_gameObjects.size(); i++) //clear all existing gameObjects
47+
{
48+
m_gameObjects[i]->clean();
49+
}
50+
m_gameObjects.clear(); //clear vector
51+
52+
//clean textures
53+
TheTextureManager::Instance()->clearFromTextureMap("helicopter");
54+
3155
cout << "exiting PlayState " << endl;
3256
return true;
3357
}

PlayState.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using namespace std;
55

66
#include "GameState.h"
7-
#include <string>
7+
#include <string>
8+
#include <vector>
9+
#include "GameObject.h"
810

911

1012
class PlayState : public GameState
@@ -21,7 +23,8 @@ class PlayState : public GameState
2123
protected:
2224
private:
2325

24-
static const string s_playID;
26+
static const string s_playID; //string ID identificator
27+
vector<GameObject*> m_gameObjects; //vector with all the gameObjects
2528
};
2629

2730
#endif // PLAYSTATE_H

Player.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void Player::update()
1818

1919
handleInput();
2020

21-
m_currentFrame = int((SDL_GetTicks() / 100) % 6);
21+
m_currentFrame = int((SDL_GetTicks() / 100) % 5);
2222

2323
SDLGameObject::update(); //call the up-class
2424
}

SDLGameObject.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject (pParams)
1616

1717
void SDLGameObject::draw()
1818
{
19-
TextureManager::Instance()->drawFrame(m_textureID, int(m_position.getX()), int(m_position.getY()), m_width,
19+
if(m_velocity.getX() > 0)
20+
{
21+
TextureManager::Instance()->drawFrame(m_textureID, int(m_position.getX()), int(m_position.getY()), m_width,
22+
m_height, m_currentRow, m_currentFrame,
23+
TheGame::Instance()->getRenderer(), SDL_FLIP_HORIZONTAL);
24+
}
25+
else
26+
{
27+
TextureManager::Instance()->drawFrame(m_textureID, int(m_position.getX()), int(m_position.getY()), m_width,
2028
m_height, m_currentRow, m_currentFrame,
2129
TheGame::Instance()->getRenderer());
30+
}
2231
}
2332

2433
void SDLGameObject::update()

TextureManager.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ void TextureManager::drawFrame(std::string id, int x, int y, int width, int heig
5353
destRect.y = y;
5454

5555
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); //Load current frame on the buffer game.
56+
}
57+
58+
void TextureManager::clearFromTextureMap(string id)
59+
{
60+
m_textureMap.erase(id);
5661
}

TextureManager.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TextureManager //its a SINGLETON (unitary class) it's called TextureManage
3333
void drawFrame(string id, int x, int y, int width, int height,
3434
int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); //draw image sprite
3535

36+
void clearFromTextureMap(string id); //delete SDL_Texture from the map
3637
private:
3738
map<string, SDL_Texture*> m_textureMap; //map with all the SDL_Textures
3839
TextureManager(){}; //now is a SINGLETON

0 commit comments

Comments
 (0)