-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
442 lines (363 loc) · 14.7 KB
/
render.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "./render.h"
pioTexture_t playerTexture;
pioTexture_t emptyTexture;
pioTexture_t dirtTexture;
pioTexture_t borderTexture;
pioTexture_t rockTexture;
pioTexture_t diamondTexture;
pioTexture_t doorTexture;
pioTexture_t closedDoorTexture;
pioTexture_t spiderTexture;
pioTexture_t monsterTexture;
pioTexture_t waterTexture;
pioTexture_t pauseTexture;
pioTexture_t playTexture;
pioTexture_t livesTexture;
pioTexture_t timeTexture;
pioTexture_t diamondSymbolTexture;
TTF_Font *gFont = NULL;
pioTextFont_t mainText;
pioTextFont_t diamondCount;
pioTextFont_t mapTimer;
pioTextFont_t minerLives;
pioTextFont_t levelName;
Mix_Music *gMusic = NULL;
Mix_Music *gm10 = NULL;
Mix_Music *gm30 = NULL;
Mix_Music *gm60 = NULL;
Mix_Chunk *boulderFall = NULL;
Mix_Chunk *diamondCollect = NULL;
Mix_Chunk *dirtRemove = NULL;
bool loadMedia(SDL_Renderer *renderer) {
bool success = true;
//Load music
gMusic = Mix_LoadMUS("./assets/sounds/music.wav");
if(gMusic == NULL) {
printf( "Failed to load sound gMusic! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
gm10 = Mix_LoadMUS("./assets/sounds/gm10.wav");
if(gm10 == NULL) {
printf( "Failed to load sound gm10! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
gm30 = Mix_LoadMUS("./assets/sounds/gm30.wav");
if(gm30 == NULL) {
printf( "Failed to load sound gm30! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
gm60 = Mix_LoadMUS("./assets/sounds/gm60.wav");
if(gm60 == NULL) {
printf( "Failed to load sound gm60! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
//Load sounds
boulderFall = Mix_LoadWAV("./assets/sounds/boulderFall.wav");
if(boulderFall == NULL) {
printf( "Failed to load sound boulderFall! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
//Load sounds
diamondCollect = Mix_LoadWAV("./assets/sounds/diamondCollect.wav");
if(diamondCollect == NULL) {
printf( "Failed to load sound diamondCollect! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
//Load sounds
dirtRemove = Mix_LoadWAV("./assets/sounds/dirtRemove.wav");
if(dirtRemove == NULL) {
printf( "Failed to load sound dirtRemove! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
gFont = TTF_OpenFont("./assets/font/zig.ttf", 28);
if(gFont == NULL) {
printf("Failed to load zig font! SDL_ttf Error: %s\n",TTF_GetError());
success = false;
}
SDL_Color textColor = {255,255,255};
levelName = loadPioTextFont("init", textColor, gFont, renderer);
resizePioTexture(&(levelName.texture), 32, 32);
if(levelName.texture.texture == NULL) {
printf("Failed to load levelName text image!\n");
success = false;
}
diamondCount = loadPioTextFont("init", textColor, gFont, renderer);
resizePioTexture(&(diamondCount.texture), 32, 32);
if(diamondCount.texture.texture == NULL) {
printf("Failed to load diamondCount text image!\n");
success = false;
}
mapTimer = loadPioTextFont("init", textColor, gFont, renderer);
resizePioTexture(&(mapTimer.texture), 32, 32);
if(mapTimer.texture.texture == NULL) {
printf("Failed to load mapTimer text image!\n");
success = false;
}
minerLives = loadPioTextFont("init", textColor, gFont, renderer);
resizePioTexture(&(minerLives.texture), 32, 32);
if(minerLives.texture.texture == NULL) {
printf("Failed to load minerLives text image!\n");
success = false;
}
playerTexture = loadPioTexture("./assets/image/playerTexture.png", renderer);
resizePioTexture(&playerTexture, TILE_WIDTH, TILE_HEIGHT);
if(playerTexture.texture == NULL) {
printf("Failed to load playerTexture image!\n");
success = false;
}
emptyTexture = loadPioTexture("./assets/image/emptyTexture.png", renderer);
resizePioTexture(&emptyTexture, TILE_WIDTH, TILE_HEIGHT);
if(emptyTexture.texture == NULL) {
printf("Failed to load emptyTexture image!\n");
success = false;
}
dirtTexture = loadPioTexture("./assets/image/dirtTexture.png", renderer);
resizePioTexture(&dirtTexture, TILE_WIDTH, TILE_HEIGHT);
if(dirtTexture.texture == NULL) {
printf("Failed to load dirtTexture image!\n");
success = false;
}
borderTexture = loadPioTexture("./assets/image/borderTexture.png", renderer);
resizePioTexture(&borderTexture, TILE_WIDTH, TILE_HEIGHT);
if(borderTexture.texture == NULL) {
printf("Failed to load borderTexture image!\n");
success = false;
}
rockTexture = loadPioTexture("./assets/image/rockTexture.png", renderer);
resizePioTexture(&rockTexture, TILE_WIDTH, TILE_HEIGHT);
if(rockTexture.texture == NULL) {
printf("Failed to load rockTexture image!\n");
success = false;
}
diamondTexture = loadPioTexture("./assets/image/diamondTexture.png", renderer);
resizePioTexture(&diamondTexture, TILE_WIDTH, TILE_HEIGHT);
if(diamondTexture.texture == NULL) {
printf("Failed to load diamondTexture image!\n");
success = false;
}
doorTexture = loadPioTexture("./assets/image/doorTexture.png", renderer);
resizePioTexture(&doorTexture, TILE_WIDTH, TILE_HEIGHT);
if(doorTexture.texture == NULL) {
printf("Failed to load doorTexture image!\n");
success = false;
}
closedDoorTexture = loadPioTexture("./assets/image/closedDoorTexture.png", renderer);
resizePioTexture(&closedDoorTexture, TILE_WIDTH, TILE_HEIGHT);
if(closedDoorTexture.texture == NULL) {
printf("Failed to load closedDoorTexture image!\n");
success = false;
}
spiderTexture = loadPioTexture("./assets/image/spiderTexture.png", renderer);
resizePioTexture(&spiderTexture, TILE_WIDTH, TILE_HEIGHT);
if(spiderTexture.texture == NULL) {
printf("Failed to load spiderTexture image!\n");
success = false;
}
monsterTexture = loadPioTexture("./assets/image/monsterTexture.png", renderer);
resizePioTexture(&monsterTexture, TILE_WIDTH, TILE_HEIGHT);
if(monsterTexture.texture == NULL) {
printf("Failed to load monsterTexture image!\n");
success = false;
}
waterTexture = loadPioTexture("./assets/image/waterTexture.png", renderer);
resizePioTexture(&waterTexture, TILE_WIDTH, TILE_HEIGHT);
if(waterTexture.texture == NULL) {
printf("Failed to load waterTexture image!\n");
success = false;
}
pauseTexture = loadPioTexture("./assets/image/pauseTexture.png", renderer);
resizePioTexture(&pauseTexture, 32, 32);
if(pauseTexture.texture == NULL) {
printf("Failed to load pauseTexture image!\n");
success = false;
}
playTexture = loadPioTexture("./assets/image/playTexture.png", renderer);
resizePioTexture(&playTexture, 32, 32);
if(playTexture.texture == NULL) {
printf("Failed to load playTexture image!\n");
success = false;
}
livesTexture = loadPioTexture("./assets/image/livesTexture.png", renderer);
resizePioTexture(&livesTexture, 32, 32);
if(livesTexture.texture == NULL) {
printf("Failed to load livesTexture image!\n");
success = false;
}
timeTexture = loadPioTexture("./assets/image/timeTexture.png", renderer);
resizePioTexture(&timeTexture, 32, 32);
if(timeTexture.texture == NULL) {
printf("Failed to load timeTexture image!\n");
success = false;
}
diamondSymbolTexture = loadPioTexture("./assets/image/diamondTexture.png", renderer);
resizePioTexture(&diamondSymbolTexture, 32, 32);
if(diamondSymbolTexture.texture == NULL) {
printf("Failed to load diamondSymbolTexture image!\n");
success = false;
}
return success;
}
void stopMusic() {
Mix_HaltMusic();
}
void startMusic() {
Mix_HaltMusic();
Mix_PlayMusic(gMusic, -1);
}
void startMusic10() {
Mix_HaltMusic();
Mix_PlayMusic(gm10, -1);
}
void startMusic30() {
Mix_HaltMusic();
Mix_PlayMusic(gm30, -1);
}
void startMusic60() {
Mix_HaltMusic();
Mix_PlayMusic(gm60, 3);
}
void playBoulderFall() {
Mix_PlayChannel(-1, boulderFall, 0);
}
void playDiamondCollect() {
Mix_PlayChannel(-1, diamondCollect, 0);
}
void playDirtRemove() {
Mix_PlayChannel(-1, dirtRemove, 0);
}
void closeMedia() {
destroyPioTexture(&dirtTexture);
destroyPioTexture(&borderTexture);
destroyPioTexture(&playerTexture);
destroyPioTexture(&emptyTexture);
destroyPioTexture(&rockTexture);
destroyPioTexture(&diamondTexture);
destroyPioTexture(&doorTexture);
destroyPioTexture(&spiderTexture);
destroyPioTexture(&monsterTexture);
destroyPioTexture(&waterTexture);
destroyPioTexture(&pauseTexture);
destroyPioTexture(&livesTexture);
destroyPioTexture(&timeTexture);
}
void renderMap(level_t level, camera_t camera, pioWindow_t window, SDL_Renderer *renderer) {
SDL_RenderClear(renderer);
//i specifies the Y coordinate of render pixel
for(int i = 0; i < level.row; i++) {
//j specifies the X coordinate of render pixel
for(int j = 0; j < level.col; j++) {
tile_t currentTile = createTile(i, j);
tile_t cam = createTile(camera.row, camera.col);
int topBarMenu = 32;
int diffX = (window.width/2) - cam.center_x;
int diffY = (window.height/2) - cam.center_y;
if(isInsideCamera(window, camera, i, j)) {
switch (level.map[i][j]) {
case borderTile:
renderPioTexture(borderTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case playerTile:
renderPioTexture(playerTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case dirtTile:
renderPioTexture(dirtTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case emptyTile:
renderPioTexture(emptyTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case rockTile:
renderPioTexture(rockTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case fallingRockTile:
renderPioTexture(rockTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case diamondTile:
renderPioTexture(diamondTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case fallingDiamondTile:
renderPioTexture(diamondTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case closedDoorTile:
renderPioTexture(closedDoorTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case openDoorTile:
renderPioTexture(doorTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case spiderR:
case spiderB:
case spiderL:
case spiderT:
case spiderTile:
renderPioTexture(spiderTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case movingSpiderTile:
renderPioTexture(spiderTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case monsterTile:
renderPioTexture(monsterTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case movingMonsterTile:
renderPioTexture(monsterTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
case waterTile:
renderPioTexture(waterTexture, currentTile.center_x + diffX, currentTile.center_y + diffY, renderer);
break;
default:
break;
}
}
}
}
}
void renderGameBar(level_t level, pioWindow_t window, SDL_Renderer *renderer, bool isPaused) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 30);
SDL_Rect gameBar;
gameBar.x = 0,
gameBar.y = 0,
gameBar.w = window.width,
gameBar.h = 32;
SDL_RenderFillRect(renderer, &gameBar);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
renderPioTextureCornered(levelName.texture, 0, 0, renderer);
if(isPaused) {
renderPioTextureCornered(pauseTexture, window.width - 32, 0, renderer);
} else {
renderPioTextureCornered(playTexture, window.width - 32, 0, renderer);
}
renderPioTextureCornered(livesTexture, window.width - 64, 0, renderer);
renderPioTextureCornered(minerLives.texture, window.width - 128, 0, renderer);
renderPioTextureCornered(timeTexture, (window.width / 2) + 32, 0, renderer);
renderPioTextureCornered(mapTimer.texture, (window.width / 2) + 64, 0, renderer);
renderPioTextureCornered(diamondCount.texture, (window.width / 2) - 32, 0, renderer);
renderPioTextureCornered(diamondSymbolTexture, (window.width / 2) - 64, 0, renderer);
}
void updateGameBar(level_t level, SDL_Renderer *renderer, int lives) {
char diaText[5];
char mapTimeText[12];
char minerLivesText[12];
sprintf(minerLivesText, "%d", lives);
if(level.timeLimit == 0) {
strcpy(mapTimeText, "Times up");
} else {
sprintf(mapTimeText, "%d", level.timeLimit);
}
if(level.diamondCount <= 0) {
sprintf(diaText, "%d", 0);
updatePioTextFont(&diamondCount, diaText, renderer);
} else {
sprintf(diaText, "%d", level.diamondCount);
updatePioTextFont(&diamondCount, diaText, renderer);
}
updatePioTextFont(&mapTimer, mapTimeText, renderer);
if(lives <= 0) {
updatePioTextFont(&levelName, "GAME OVER", renderer);
updatePioTextFont(&minerLives, minerLivesText, renderer);
} else {
updatePioTextFont(&minerLives, minerLivesText, renderer);
updatePioTextFont(&levelName, level.name, renderer);
}
}
void renderOnDeath(level_t level, camera_t camera, pioWindow_t window, SDL_Renderer *renderer) {
renderMap(level, camera, window, renderer);
renderGameBar(level, window, renderer, true);
}