Skip to content

Commit e2842a5

Browse files
committed
Chapter5: Play and Exit buttons
1 parent e6874e7 commit e2842a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1284
-0
lines changed

Chapter5.1/SDL_game.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.645
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SDL_game", "SDL_game\SDL_game.vcxproj", "{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}.Debug|x64.ActiveCfg = Debug|x64
17+
{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}.Debug|x64.Build.0 = Debug|x64
18+
{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}.Debug|x86.ActiveCfg = Debug|Win32
19+
{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}.Debug|x86.Build.0 = Debug|Win32
20+
{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}.Release|x64.ActiveCfg = Release|x64
21+
{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}.Release|x64.Build.0 = Release|x64
22+
{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}.Release|x86.ActiveCfg = Release|Win32
23+
{9CF7E47B-7AD3-4297-9BC4-76D4652359C5}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {62EC434C-F3A8-4ECE-8EF9-A9120020982B}
30+
EndGlobalSection
31+
EndGlobal

Chapter5.1/SDL_game/Enemy.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "Enemy.h"
2+
3+
Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams)
4+
{
5+
}
6+
7+
void Enemy::draw()
8+
{
9+
SDLGameObject::draw(); // we now use SDLGameObject
10+
}
11+
void Enemy::update()
12+
{
13+
m_position.setX(m_position.getX() + 1);
14+
m_position.setY(m_position.getY() + 1);
15+
m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
16+
}
17+
void Enemy::clean()
18+
{
19+
}
20+
21+
22+

Chapter5.1/SDL_game/Enemy.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include "SDLGameObject.h"
3+
class Enemy : public SDLGameObject
4+
{
5+
public:
6+
Enemy(const LoaderParams* pParams);
7+
8+
virtual void draw();
9+
virtual void update();
10+
virtual void clean();
11+
};
12+

Chapter5.1/SDL_game/Game.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <SDL.h>
2+
#include <SDL_image.h>
3+
#include "Game.h"
4+
#include "InputHandler.h"
5+
#include "MenuState.h"
6+
#include "PlayState.h"
7+
Game* Game::s_pInstance = 0;
8+
9+
bool Game::init(const char *title, int xpos, int ypos, int height, int width, int flags)
10+
{
11+
if (SDL_Init(SDL_INIT_EVERYTHING) >= 0) {
12+
m_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags);
13+
if (m_pWindow != 0)
14+
{
15+
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
16+
17+
m_pGameStateMachine = new GameStateMachine();
18+
m_pGameStateMachine->changeState(new MenuState());
19+
20+
21+
TheTextureManager::Instance()->load("assets/animate-alpha.png", "animate", m_pRenderer);
22+
23+
//m_gameObjects.push_back(new Player(new LoaderParams(100, 100, 128, 82, "animate")));
24+
//m_gameObjects.push_back(new Enemy(new LoaderParams(300, 300, 128, 82, "animate")));
25+
26+
m_bRunning = true;
27+
return true;
28+
}
29+
}
30+
return false;
31+
}
32+
33+
void Game::render()
34+
{
35+
SDL_SetRenderDrawColor(m_pRenderer, 0, 100, 0, 255);
36+
SDL_RenderClear(m_pRenderer);
37+
38+
m_pGameStateMachine->render();
39+
40+
for (vector<GameObject*>::size_type i = 0; i != m_gameObjects.size(); i++)
41+
{
42+
m_gameObjects[i]->draw();
43+
}
44+
SDL_RenderPresent(m_pRenderer);
45+
}
46+
void Game::update()
47+
{
48+
for (vector<GameObject*>::size_type i = 0; i != m_gameObjects.size(); i++)
49+
{
50+
m_gameObjects[i]->update();
51+
}
52+
m_pGameStateMachine->update();
53+
}
54+
55+
void Game::handleEvents()
56+
{
57+
TheInputHandler::Instance()->update();
58+
59+
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_RETURN))
60+
{
61+
m_pGameStateMachine->changeState(new PlayState());
62+
}
63+
64+
}
65+
void Game::clean()
66+
{
67+
SDL_DestroyRenderer(m_pRenderer);
68+
SDL_DestroyWindow(m_pWindow);
69+
}
70+
71+
bool Game::running() { return m_bRunning; }
72+
73+
void Game::quit() {
74+
m_bRunning = false;
75+
}

Chapter5.1/SDL_game/Game.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
#ifndef __GAME__
3+
#define __GAME__
4+
5+
#include <SDL.h>
6+
#include <vector>
7+
#include "TextureManager.h"
8+
#include "GameObject.h"
9+
#include "Player.h"
10+
#include "Enemy.h"
11+
#include "GameStateMachine.h"
12+
13+
enum game_states
14+
{
15+
MENU = 0,
16+
PLAY = 1,
17+
GAMEOVER = 2
18+
};
19+
20+
class Game
21+
{
22+
public:
23+
bool init(const char *title, int xpos, int ypos, int height, int width, int flags);
24+
void render();
25+
void update();
26+
void handleEvents();
27+
void clean();
28+
bool running();
29+
void quit();
30+
31+
int m_currentFrame;
32+
33+
GameObject *m_go;
34+
GameObject *m_player;
35+
GameObject *m_enemy;
36+
37+
vector<GameObject*> m_gameObjects;
38+
39+
static Game *Instance()
40+
{
41+
if (s_pInstance == 0)
42+
{
43+
s_pInstance = new Game();
44+
return s_pInstance;
45+
}
46+
return s_pInstance;
47+
}
48+
49+
SDL_Renderer* getRenderer() const { return m_pRenderer; }
50+
51+
private:
52+
SDL_Window* m_pWindow;
53+
SDL_Renderer* m_pRenderer;
54+
55+
bool m_bRunning;
56+
static Game *s_pInstance;
57+
GameStateMachine *m_pGameStateMachine;
58+
};
59+
60+
typedef Game TheGame;
61+
62+
#endif // !__GAME__
63+

Chapter5.1/SDL_game/GameObject.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "GameObject.h"
2+
#include "TextureManager.h"
3+
4+
5+
void GameObject::draw() { cout << "draw game object"; }
6+
void GameObject::update() { cout << "update game object"; }
7+
void GameObject::clean() { cout << "clean game object"; }

Chapter5.1/SDL_game/GameObject.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#ifndef __GAMEOBJECT__
3+
#define __GAMEOBJECT__
4+
5+
#include <iostream>
6+
#include <SDL.h>
7+
#include "LoaderParams.h"
8+
9+
using namespace std;
10+
11+
class GameObject
12+
{
13+
public:
14+
virtual void draw();
15+
virtual void update();
16+
virtual void clean();
17+
18+
protected:
19+
GameObject(const LoaderParams *pParams) {}
20+
virtual ~GameObject() {}
21+
};
22+
23+
#endif // !__GAMEOBJECT__
24+

Chapter5.1/SDL_game/GameState.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
#ifndef __GAMESTATE__
3+
#define __GAMESTATE__
4+
#include <iostream>
5+
#include <string>
6+
using namespace std;
7+
class GameState
8+
{
9+
public:
10+
virtual void update() = 0;
11+
virtual void render() = 0;
12+
virtual bool onEnter() = 0;
13+
virtual bool onExit() = 0;
14+
15+
virtual string getStateID() const = 0;
16+
};
17+
18+
#endif // !__GAMESTATE__
19+
20+
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "GameStateMachine.h"
2+
3+
void GameStateMachine::pushState(GameState * pState)
4+
{
5+
m_gameStates.push_back(pState);
6+
m_gameStates.back()->onEnter();
7+
}
8+
9+
void GameStateMachine::changeState(GameState * pState)
10+
{
11+
if (!m_gameStates.empty())
12+
{
13+
if (m_gameStates.back()->getStateID() == pState->getStateID())
14+
{
15+
return; // do nothing
16+
}
17+
if (m_gameStates.back()->onExit())
18+
{
19+
delete m_gameStates.back();
20+
m_gameStates.pop_back();
21+
}
22+
}
23+
m_gameStates.push_back(pState);
24+
m_gameStates.back()->onEnter();
25+
}
26+
27+
void GameStateMachine::popState()
28+
{
29+
if (!m_gameStates.empty())
30+
{
31+
if (m_gameStates.back()->onExit())
32+
{
33+
delete m_gameStates.back();
34+
m_gameStates.pop_back();
35+
}
36+
}
37+
}
38+
39+
void GameStateMachine::update()
40+
{
41+
if (!m_gameStates.empty())
42+
{
43+
m_gameStates.back()->update();
44+
}
45+
}
46+
void GameStateMachine::render()
47+
{
48+
if (!m_gameStates.empty())
49+
{
50+
m_gameStates.back()->render();
51+
}
52+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#ifndef __GAMESTATEMACHINE__
3+
#define __GAMESTATEMACHINE__
4+
#include <vector>
5+
#include"GameState.h"
6+
7+
class GameStateMachine
8+
{
9+
public:
10+
void pushState(GameState *pState);
11+
void changeState(GameState *pState);
12+
void popState();
13+
void update();
14+
void render();
15+
16+
private:
17+
vector<GameState *> m_gameStates;
18+
};
19+
20+
21+
#endif // !__GAMESTATEMACHINE__
22+
23+

0 commit comments

Comments
 (0)