-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
317 lines (249 loc) · 10.3 KB
/
main.c
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include "./base.h"
#include "./wrappers/pio-texture.h"
#include "./wrappers/pio-text-font.h"
#include "./wrappers/pio-window.h"
#include "./objects/tile.h"
#include "./objects/miner.h"
#include "./objects/camera.h"
#include "./objects/level.h"
#include "./render.h"
#include "./objects/controls.h"
#define INIT_WIDTH 1366
#define INIT_HEIGHT 720
#define INIT_MINER_ROW 2
#define INIT_MINER_COL 3
miner_t miner;
camera_t camera;
pioWindow_t gameWindow;
level_t currLevel;
const char *TITLE = "pioGame";
SDL_Renderer *gRenderer = NULL;
bool init();
void closeAll();
bool init() {
bool success = true;
miner = createMiner(INIT_MINER_ROW, INIT_MINER_COL);
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
printf("Warning: Linear texture filtering not enabled!\n");
}
SDL_Window *gWindow = SDL_CreateWindow(TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, INIT_WIDTH, INIT_HEIGHT, SDL_WINDOW_RESIZABLE);
if(gWindow == NULL) {
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
gameWindow.window = gWindow;
updateWindowDims(&gameWindow);
camera = createCamera(gameWindow);
gRenderer = SDL_CreateRenderer(gameWindow.window, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer == NULL) {
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
//SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0);
//Initialize image 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 font loading
if(TTF_Init() == -1) {
printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
success = false;
}
//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;
}
}
}
}
return success;
}
void closeAll() {
closeMedia();
SDL_DestroyRenderer(gRenderer);
gRenderer = NULL;
destroyPioWindow(&gameWindow);
TTF_Quit();
IMG_Quit();
Mix_Quit();
SDL_Quit();
}
void resetLevel(int levelNo) {
startMusic();
switch (levelNo) {
case 1:
fillLevel(&currLevel, "./assets/maps/cave_1.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 2:
fillLevel(&currLevel, "./assets/maps/cave_2.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 3:
fillLevel(&currLevel, "./assets/maps/cave_3.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 4:
fillLevel(&currLevel, "./assets/maps/cave_4.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 5:
fillLevel(&currLevel, "./assets/maps/cave_5.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 6:
fillLevel(&currLevel, "./assets/maps/cave_6.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 7:
fillLevel(&currLevel, "./assets/maps/cave_7.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 8:
fillLevel(&currLevel, "./assets/maps/cave_8.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 9:
fillLevel(&currLevel, "./assets/maps/cave_9.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
case 10:
fillLevel(&currLevel, "./assets/maps/cave_10.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
break;
default:
break;
}
}
void gameOver() {
updateGameBar(currLevel, gRenderer, 0);
renderGameBar(currLevel, gameWindow, gRenderer, true);
SDL_RenderPresent(gRenderer);
SDL_Delay(10000);
fillLevel(&currLevel, "./assets/maps/cave_1.txt");
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
miner.level = 1;
miner.lives = 10;
}
int main(int argc, char *args[]) {
Uint32 lastTime = 0, currentTime = 0;
Uint32 levelTime = 0, levelLastTime = 0;
Uint32 waterTime = 0, waterLastTime = 0;
if(!init()) {
printf("Failed to initialize!\n");
} else {
if(!loadMedia(gRenderer)) {
printf("Failed to load media!\n");
} else {
bool quit = false;
bool pause = false;
SDL_Event e;
int playing10 = 0;
int playing30 = 0;
int playing60 = 0;
fillLevel(&currLevel, "./assets/maps/cave_1.txt");
//fillLevel(&currLevel, "./assets/maps/createdLevel.txt");
miner.level = 1;
updateMiner(&miner, currLevel.startMinerRow, currLevel.startMinerCol);
startMusic();
while(!quit) {
if(playing60 == 0 && currLevel.timeLimit <= (currLevel.timeCpy * 10) / 100) {
printf("gone 60\n");
startMusic60();
playing60 = 1;
} else if(playing30 == 0 && currLevel.timeLimit <= (currLevel.timeCpy * 20) / 100) {
printf("gone 30\n");
startMusic30();
playing30 = 1;
} else if(playing10 == 0 && currLevel.timeLimit <= (currLevel.timeCpy * 30) / 100 && currLevel.timeLimit != 0) {
printf("gone 10\n");
startMusic10();
playing10 = 1;
}
if(miner.lives <= 0) {
gameOver();
}
while(SDL_PollEvent(&e) != 0) {
if(e.type == SDL_QUIT) {
quit = true;
} else if(e.type == SDL_KEYDOWN) {
if(!pause) {
moveMiner(&miner, &currLevel, e);
}
switch (e.key.keysym.sym) {
case SDLK_r:
resetLevel(miner.level);
break;
case SDLK_p:
if(pause == true) pause = false;
else if(pause == false) pause = true;
break;
default:
break;
}
} else if(e.type == SDL_WINDOWEVENT) {
switch (e.window.event) {
case SDL_WINDOWEVENT_RESIZED:
updateWindowDims(&gameWindow);
updateCamera(&camera, gameWindow);
break;
default:
break;
}
}
}
// updates
if(!pause) {
currentTime = SDL_GetTicks();
if(currentTime > lastTime + 200) {
updateMap(&currLevel, &miner, camera, gameWindow, gRenderer);
lastTime = currentTime;
}
levelTime = SDL_GetTicks();
if(levelTime > levelLastTime + 1000) {
currLevel.timeLimit--;
levelLastTime = levelTime;
if(currLevel.timeLimit <= 0) {
currLevel.timeLimit = 0;
updateGameBar(currLevel, gRenderer, miner.lives);
renderGameBar(currLevel, gameWindow, gRenderer, pause);
printf("gone\n");
playing10 = 0;
playing30 = 0;
playing60 = 0;
stopMusic();
SDL_RenderPresent(gRenderer);
SDL_Delay(3000);
miner.lives--;
resetLevel(miner.level);
}
}
waterTime = SDL_GetTicks();
if(waterTime > waterLastTime + currLevel.waterMs) {
updateWater(&currLevel);
waterLastTime = waterTime;
}
}
updateGameBar(currLevel, gRenderer, miner.lives);
updateCameraPosition(&camera, miner.row, miner.col, currLevel.row, currLevel.col);
renderMap(currLevel, camera, gameWindow, gRenderer);
renderGameBar(currLevel, gameWindow, gRenderer, pause);
SDL_RenderPresent(gRenderer);
}
}
}
closeAll();
return 0;
}