-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·353 lines (297 loc) · 6.7 KB
/
main.cpp
File metadata and controls
executable file
·353 lines (297 loc) · 6.7 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
//#include <sstream>
#include <string>
//#include <iostream>
//#include <iomanip>
//#include <stdio.h>
#include "CUfoAttack.h"
using namespace std;
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
SDL_Joystick* gGameController = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
TTF_Font *gFont = NULL;
bool gSelect = false;
bool gStart = false;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Load joystick
gGameController = SDL_JoystickOpen(0);
if (gGameController == NULL)
{
printf("Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
}
//Set texture filtering to linear
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
}
//Initialize SDL_mixer
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
//Create window
gWindow = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (gRenderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
SDL_RendererInfo info;
SDL_GetRendererInfo(gRenderer, &info);
printf("%s\n", info.name);
//Initialize renderer color
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderSetLogicalSize(gRenderer, 800, 600);
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
success = false;
}
//Initialize SDL_ttf
if (TTF_Init() == -1)
{
printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
success = false;
}
}
}
gFont = TTF_OpenFont("fonts/8bitoperator/8bitoperator.ttf", 32);
if (gFont == NULL)
{
printf("Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError());
//success = false;
}
SDL_ShowCursor(0);
}
return success;
}
#undef main
int main()
{
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
CUfoAttack *level = new CUfoAttack();
CEngine *gEngine = new CEngine(gRenderer);
level->InitGame(gEngine, gRenderer, gFont);
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
while (!quit)
{
Uint8 jSelect, jStart;
if (gGameController != NULL)
{
jSelect = SDL_JoystickGetButton(gGameController, 6);
jStart = SDL_JoystickGetButton(gGameController, 7);
//printf("%d %d\n", jStart, jSelect);
if (jSelect == 1 && jStart == 1)
{
quit = true;
break;
}
}
// Check for special keys like shift and control.
const Uint8 *keys = SDL_GetKeyboardState(NULL);
//Handle events on queuxe
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
else if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
level->KeyDown(e.key.keysym.sym);
//Select surfaces based on key press
switch (e.key.keysym.sym)
{
case SDLK_h:
if (keys[SDL_SCANCODE_RCTRL] || keys[SDL_SCANCODE_LCTRL])
{
gEngine->ShowHitBox(true);
}
break;
case SDLK_j:
if (keys[SDL_SCANCODE_RCTRL] || keys[SDL_SCANCODE_LCTRL])
{
gEngine->ShowHitBox(false);
}
break;
case SDLK_1:
gSelect = true;
if (gStart == true)
{
quit = true;
}
break;
case SDLK_5:
gStart = true;
if (gSelect == true)
{
quit = true;
}
break;
case SDLK_ESCAPE:
quit = true;
break;
default:
break;
}
}
else if (e.type == SDL_KEYUP && e.key.repeat == 0)
{
level->KeyUp(e.key.keysym.sym);
//Select surfaces based on key press
switch (e.key.keysym.sym)
{
case SDLK_SPACE:
level->StartGame();
break;
case SDLK_1:
gSelect = false;
level->StartGame();
break;
case SDLK_5:
gStart = false;
break;
default:
break;
}
}
else if (e.type == SDL_JOYBUTTONDOWN)
{
switch (e.jbutton.button)
{
case 0:
case 5:
level->KeyDown(SDLK_RIGHT);
break;
case 2:
level->KeyDown(SDLK_UP);
break;
case 3:
case 4:
level->KeyDown(SDLK_LEFT);
break;
case 7:
if (level->IsGameOver() == true)
{
level->StartGame();
}
break;
}
}
else if (e.type == SDL_JOYBUTTONUP)
{
switch (e.jbutton.button)
{
case 0:
case 5:
level->KeyUp(SDLK_RIGHT);
break;
case 2:
level->KeyUp(SDLK_UP);
break;
case 3:
case 4:
level->KeyUp(SDLK_LEFT);
break;
}
}
else if (e.type == SDL_JOYAXISMOTION)
{
//Motion on controller 0
if (e.jaxis.which == 0)
{
//X axis motion
if (e.jaxis.axis == 0)
{
//Left of dead zone
if (e.jaxis.value < -8000)
{
// Map joystick left to a keyboard stroke.
level->KeyDown(SDLK_a);
}
//Right of dead zone
else if (e.jaxis.value > 8000)
{
// Map jouystick right to keyboard stroke.
level->KeyDown(SDLK_d);
}
else
{
// Zero out the joystick when centered.
level->KeyUp(SDLK_a);
level->KeyUp(SDLK_d);
}
}
//Y axis motion
else if (e.jaxis.axis == 1)
{
//Below of dead zone
if (e.jaxis.value < -8000)
{
}
//Above of dead zone
else if (e.jaxis.value > 8000)
{
}
else
{
}
}
}
}
else if (e.type == SDL_MOUSEMOTION)
{
//Get mouse position
int x, y;
SDL_GetMouseState(&x, &y);
}
}
level->Update();
rand(); // Keep the random number generator unpredictable.
SDL_RenderClear(gRenderer);
gEngine->Render(); // Dump eveything to the screen.
SDL_RenderPresent(gRenderer);
}
SDL_Quit();
}
return 0;
}