-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
318 lines (256 loc) · 6.65 KB
/
Game.cpp
File metadata and controls
318 lines (256 loc) · 6.65 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "Game.h"
Game::Game()
{
// Start SDL
int result = startSDL();
if (result < 0)
{
std::cout << "Failed to start SDL!\n";
std::cout << "Error message: " << SDL_GetError() << "\n";
}
else
{
// If SDL started fine, then load resources.
resource = new ResourceManager(renderer);
loadResources();
// Load many systems
ProcessManager* process = new ProcessManager();
LuaInterface* luaInterface = new LuaInterface();
LogManager* logManager = new LogManager();
ComponentManager* componentManager = new ComponentManager();
Services::provide(resource);
Services::provide(process);
Services::provide(luaInterface);
Services::provide(logManager);
Services::provide(componentManager);
}
}
Game::~Game()
{
std::cout << "Calling destructor.\n";
// Delete all services, which calls their respective destructors
delete Services::resourceManager();
delete Services::processManager();
delete Services::logManager();
delete Services::luaInterface();
delete Services::componentManager();
// Destroy window
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
// Quit SDL subsystems
SDL_Quit();
}
int Game::startSDL()
{
// Null out variables to begin with.
window = NULL;
screenSurface = NULL;
renderer = NULL;
// Start SDL video systems
int result = SDL_Init(SDL_INIT_VIDEO);
if (result < 0) return -1;
// Create window
window = SDL_CreateWindow(
"Tower Defense",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if (window == NULL) return -1;
// Extract window's surface
screenSurface = SDL_GetWindowSurface(window);
// Setup hardware accelerated renderer.
renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL) return -1;
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
return 0;
}
void Game::loadResources()
{
// Use Resource.cpp to load the specific graphics for the rudimentary game.
// Loads all textures into memory
ifstream myfile;
myfile.open("res\\loadtextures.txt");
if (myfile.is_open())
{
string line;
string nextID;
string nextFilepath;
bool needsNextID = true;
while ( getline (myfile,line) )
{
if (line == "") continue;
if (needsNextID)
{
nextID = line;
needsNextID = false;
} else {
nextFilepath = line;
resource->loadTexture(nextID, nextFilepath);
needsNextID = true;
}
#ifdef DEBUG
cout << line << '\n';
#endif
}
myfile.close();
myfile.clear();
}
#ifdef DEBUG
cout << "\nFinished loading textures\n\n";
#endif
// Assemble textures into animations.
ifstream animation;
animation.open("res\\loadanimations.txt");
if (animation.is_open())
{
string line;
int frameCount;
string animationID;
string* textureIDs;
float* delayTimes;
// 0 - needs animationID, 1 - needs length,
// 2 - needs textureID, 3 - needs corresponding delay
int state = 0;
int currentFrame = 0;
while (getline(animation,line))
{
if (line == "") continue;
#ifdef DEBUG
cout << line << ", and state: " << state << '\n';
#endif
switch (state)
{
// Read animationID, move on to read length
case 0:
animationID = line;
state = 1;
break;
// Read length, move on to read textureID
case 1:
frameCount = atoi(line.c_str());
textureIDs = new string[frameCount];
delayTimes = new float[frameCount];
state = 2;
break;
// Read textureID, move on to frame delay
case 2:
textureIDs[currentFrame] = line;
state = 3;
break;
// Read delay, either finish or next textureID
case 3:
float delay = (float)atof(line.c_str());
delayTimes[currentFrame] = delay;
currentFrame++;
if (currentFrame == frameCount)
{
// Done with this one.
resource->addMultiTexAnimation(
textureIDs,
delayTimes,
frameCount,
animationID);
// Reset state
currentFrame = 0;
state = 0;
}
else
{
// Need more frames.
state = 2;
}
break;
} // end switch
} // end input loop
animation.close();
}
#ifdef DEBUG
cout << "\nFinished loading animations\n\n";
#endif
}
void Game::MainLoop()
{
int standardDelayMs = 50;
bool isRunning = true;
SDL_Event e;
/*ComponentManager* componentManager = Services::componentManager();
AnimationComponent* component = new AnimationComponent(resource, "creep2green");
PhysicsComponent* physics = new PhysicsComponent(0.0f, 0.0f, 0.0f, 0.0f);
componentManager->addComponent(physics, ComponentType::PHYSICS);
componentManager->addComponent(component, ComponentType::ANIMATION);
vector<Component*> v;
v.push_back(component);
v.push_back(physics);
int newobj = componentManager->newGameObject(v);
*/
// Note- there is still unimplemented funcitonality!
// Something about GameObject and its knowing whether to die or
// not depending on whether it has any references yet. Please
// somehow handle that?
lua_State* L = Services::luaInterface()->L;
luaL_dofile(L, "test1.lua");
// Make a coroutine of moveEntity
int coroutine = Services::luaInterface()->newCoroutine("testCoroutine");
lua_getglobal(L, "resumeCoroutine");
// Once to know what index is the coroutine,
// a second time to give the coroutine its own number
lua_pushinteger(L, coroutine);
lua_pushinteger(L, coroutine);
lua_call(L, 2, 0);
// Event handler needs to be moved elsewhere.
// Like, soon.
while (isRunning)
{
// Poll events...
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
isRunning = false;
}
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
case SDLK_DOWN:
case SDLK_LEFT:
case SDLK_RIGHT:
break;
default:
break;
}
}
}
// Handle the frame in a basic way
SDL_RenderClear(renderer);
//resource->getTexture("ship")->draw();
//physics->newx((*myEntity)->getX());
//physics->newy((*myEntity)->getY());
ComponentList* list = Services::componentManager()->getComponents(ComponentType::ANIMATION);
if (list != NULL)
{
for (int i = 0; i < list->getListSize(); i++)
{
Component* comp = list->getComponent(i);
if (comp != NULL && comp->getState()
== ComponentState::ALIVE)
{
((AnimationComponent*)comp)->draw();
}
}
}
// ALL UPDATING HAPPENS IN SECONDS
float dt = (float)(standardDelayMs / 1000.0f);
// Update all running processes. This is the section where
// scripts are largely resumed too
Services::processManager()->update(dt);
// Update all the components of animation
if (list != NULL) list->updateComponents(dt);
SDL_RenderPresent(renderer);
SDL_Delay(standardDelayMs);
}
}