This repository was archived by the owner on Mar 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
169 lines (140 loc) · 3.73 KB
/
Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#include "tinyxml.h"
#include "Game.h"
#include "InputHandler.h"
#include "MainMenuState.h"
#include "GameObjectFactory.h"
#include "Button.h"
#include "Player.h"
#include "Boss1.h"
#include "Fairy.h"
Game *Game::s_pInstance = nullptr; // Skeleton pointer to asure there is only one instance
Game::Game() :
m_pWindow(nullptr),
m_pRenderer(nullptr),
m_bRunning(false),
m_pGameStateMachine(0),
m_playerLives(3),
m_scrollSpeed(0.8f),
m_bLevelComplete(false),
m_bChangingState(false)
{
// hardcode here and initializer list
m_levelFiles.push_back("data/map1.tmx");
m_currentLevel = 1;
}
Game::~Game()
{
m_pRenderer = nullptr;
m_pWindow = nullptr;
}
bool Game::init(const char *title, int xpos, int ypos,
int width, int height, int flags)
{
m_gameWidth = width;
m_gameHeight = height;
// Initialize SDL create window, renderer and set render color
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cerr << "Failed when initializing SDL. Error: "
<< SDL_GetError() << std::endl;
return false;
}
else
{
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width,
height, flags);
if (m_pWindow == nullptr)
{
std::cerr << "Failed when creating window. Error: "
<< SDL_GetError() << std::endl;
return false;
}
else
{
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if (m_pRenderer == nullptr)
{
std::cout << "Failed when creating renderer. Error: "
<< SDL_GetError() << std::endl;
return false;
}
else
{
SDL_SetRenderDrawColor(m_pRenderer, 255, 255,
255, 255);
}
}
}
// Init extended parts: img and ttf TODO: we will also initialize audio
// subsystem here too
if (IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG) == 0)
{
std::cerr << "Failed when initializing SDL_image.\n";
return false;
}
if (TTF_Init() != 0)
{
std::cerr << "Failed when initializing SDL_ttf.\n";
return false;
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
std::cerr << "Failed when initializing SDL_mixer.\n SDL_mixer Error: " << Mix_GetError() << std::endl;
return false;
}
// Add sound effects here and move them other better place in the future
// Register types for the game
TheGameObjectFactory::Instance()->registerType("Button", new ButtonCreator());
TheGameObjectFactory::Instance()->registerType("Player", new PlayerCreator());
TheGameObjectFactory::Instance()->registerType("Boss1", new Boss1Creator());
TheGameObjectFactory::Instance()->registerType("Fairy", new FairyCreator());
// Create game state machine and push a menu state.
m_pGameStateMachine = new GameStateMachine();
m_pGameStateMachine->changeState(new MainMenuState());
// Set running bool true
m_bRunning = true;
return true;
}
void Game::render(void)
{
SDL_RenderClear(m_pRenderer);
// We call game statae machine render function actually do everything
m_pGameStateMachine->render();
SDL_RenderPresent(m_pRenderer);
}
void Game::update(void)
{
// Acturally we call game state machine update function to do every thing
m_pGameStateMachine->update();
}
void Game::handleEvents(void)
{
TheInputHandler::Instance()->update();
}
// Destroy window and renderer
void Game::clean(void)
{
std::cerr << "Cleanning game\n";
// Destroy window
SDL_DestroyWindow(m_pWindow);
m_pWindow = nullptr;
// Destroy renderer
SDL_DestroyRenderer(m_pRenderer);
m_pRenderer = nullptr;
IMG_Quit();
Mix_Quit();
TTF_Quit();
SDL_Quit();
m_bRunning = false;
}
void Game::setCurrentLevel(int currentLevel)
{
m_currentLevel = currentLevel;
// code that load new level
TheGame::Instance()->getStateMachine()->changeState(new MainMenuState());
m_bLevelComplete = false;
}