Skip to content

Commit 3ad49c5

Browse files
committed
Simplify data loading by using helper function
1 parent c385253 commit 3ad49c5

8 files changed

+82
-36
lines changed

src/drawing.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "drawing.h"
44
#include "rooms.h"
5+
#include "loading.h"
56

67
void drawscreen (SDL_Renderer *renderer,uint stagedata[][22][32],SDL_Texture *tiles,uint room,uint counter[],uint changeflag,Mix_Chunk *fx[],uint changetiles) {
78

@@ -233,22 +234,22 @@ void showparchment (SDL_Renderer *renderer,uint *parchment) {
233234

234235
switch (*parchment) {
235236
case 3:
236-
yparchment = IMG_LoadTexture(renderer, DATADIR "/graphics/parchment1.png");
237+
yparchment = loadtexture(renderer, "parchment1");
237238
break;
238239
case 8:
239-
yparchment = IMG_LoadTexture(renderer, DATADIR "/graphics/parchment2.png");
240+
yparchment = loadtexture(renderer, "parchment2");
240241
break;
241242
case 12:
242-
yparchment = IMG_LoadTexture(renderer, DATADIR "/graphics/parchment3.png");
243+
yparchment = loadtexture(renderer, "parchment3");
243244
break;
244245
case 14:
245-
yparchment = IMG_LoadTexture(renderer, DATADIR "/graphics/parchment4.png");
246+
yparchment = loadtexture(renderer, "parchment4");
246247
break;
247248
case 16:
248-
yparchment = IMG_LoadTexture(renderer, DATADIR "/graphics/parchment5.png");
249+
yparchment = loadtexture(renderer, "parchment5");
249250
break;
250251
case 21:
251-
yparchment = IMG_LoadTexture(renderer, DATADIR "/graphics/parchment6.png");
252+
yparchment = loadtexture(renderer, "parchment6");
252253
break;
253254
}
254255

@@ -259,7 +260,7 @@ void showparchment (SDL_Renderer *renderer,uint *parchment) {
259260

260261
void redparchment (SDL_Renderer *renderer,struct hero *jean) {
261262

262-
SDL_Texture *rparchment = IMG_LoadTexture(renderer, DATADIR "/graphics/redparch.png");
263+
SDL_Texture *rparchment = loadtexture(renderer, "redparch");
263264
SDL_RenderCopy(renderer,rparchment,NULL,NULL);
264265
SDL_DestroyTexture(rparchment);
265266

@@ -269,7 +270,7 @@ void redparchment (SDL_Renderer *renderer,struct hero *jean) {
269270

270271
void blueparchment (SDL_Renderer *renderer,struct hero *jean) {
271272

272-
SDL_Texture *bparchment = IMG_LoadTexture(renderer, DATADIR "/graphics/blueparch.png");
273+
SDL_Texture *bparchment = loadtexture(renderer, "blueparch");
273274
SDL_RenderCopy(renderer,bparchment,NULL,NULL);
274275
SDL_DestroyTexture(bparchment);
275276

src/ending.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/* ending.c */
22

33
#include "ending.h"
4+
#include "loading.h"
45

56
void ending (SDL_Window *screen,uint8_t *state) {
67

7-
SDL_Texture *tiles = IMG_LoadTexture(renderer, DATADIR "/graphics/tiles.png");
8-
SDL_Texture *text = IMG_LoadTexture(renderer, DATADIR "/graphics/ending.png");
8+
SDL_Texture *tiles = loadtexture(renderer, "tiles");
9+
SDL_Texture *text = loadtexture(renderer, "ending");
910

1011
SDL_Rect srcdoor = {600,72,64,48};
1112
SDL_Rect desdoor = {96,72,64,48};
1213

13-
Mix_Music *bso = Mix_LoadMUS(DATADIR "/sounds/PrayerofHopeN.ogg");
14+
Mix_Music *bso = loadmus("PrayerofHopeN");
1415

1516
int16_t x = 0;
1617

src/game.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void game(SDL_Window *screen,uint8_t *state,uint8_t *grapset,uint8_t *fullscreen
3535
SDL_SetColorKey(tilesb, SDL_TRUE, SDL_MapRGB(tilesb->format, 0, 0, 0) );
3636
SDL_Texture *tiles = SDL_CreateTextureFromSurface(renderer, tilesb);
3737
SDL_FreeSurface(tilesb);
38-
SDL_Texture *fonts = IMG_LoadTexture(renderer, DATADIR "/graphics/fonts.png");
38+
SDL_Texture *fonts = loadtexture(renderer, "fonts");
3939

4040
/* Loading musics */
4141
loadingmusic(bso,fx);

src/gameover.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* gameover.c */
22

33
#include "gameover.h"
4+
#include "loading.h"
45

56
void gameover (SDL_Window *screen,uint8_t *state) {
67

7-
SDL_Texture *gameover = IMG_LoadTexture(renderer, DATADIR "/graphics/gameover.png");
8-
9-
Mix_Music *bso = Mix_LoadMUS(DATADIR "/sounds/GameOverV2N.ogg");
8+
SDL_Texture *gameover = loadtexture(renderer, "gameover");
9+
Mix_Music *bso = loadmus("GameOverV2N");
1010

1111
SDL_RenderClear(renderer);
1212
SDL_RenderCopy(renderer,gameover,NULL,NULL);

src/history.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/* history.c */
22

33
#include "history.h"
4+
#include "loading.h"
45

56
void history(SDL_Window *screen,uint8_t *state,uint8_t *grapset,uint8_t *fullscreen) {
67

78
SDL_Event keyp;
89

910
/* Load audio */
10-
Mix_Music *music = Mix_LoadMUS(DATADIR "/sounds/ManhuntN.ogg");
11+
Mix_Music *music = loadmus("ManhuntN");
1112

1213
/* Loading PNG */
13-
SDL_Texture *tiles = IMG_LoadTexture(renderer, DATADIR "/graphics/tiles.png");
14-
SDL_Texture *text = IMG_LoadTexture(renderer, DATADIR "/graphics/history.png");
14+
SDL_Texture *tiles = loadtexture(renderer, "tiles");
15+
SDL_Texture *text = loadtexture(renderer, "history");
1516

1617
SDL_Rect srcjean = {320,88,16,24};
1718
SDL_Rect desjean = {0,100,16,24};

src/loading.c

+54-15
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,61 @@ void loaddata(uint stagedata[][22][32],int enemydata[][7][15]) {
5757
void loadingmusic(Mix_Music *bso[],Mix_Chunk *fx[]) {
5858

5959
/* Musics */
60-
bso[0] = Mix_LoadMUS(DATADIR "/sounds/PrayerofHopeN.ogg");
61-
bso[1] = Mix_LoadMUS(DATADIR "/sounds/AreaIChurchN.ogg");
62-
bso[2] = Mix_LoadMUS(DATADIR "/sounds/GameOverV2N.ogg");
63-
bso[3] = Mix_LoadMUS(DATADIR "/sounds/HangmansTree.ogg");
64-
bso[4] = Mix_LoadMUS(DATADIR "/sounds/AreaIICavesV2N.ogg");
65-
bso[5] = Mix_LoadMUS(DATADIR "/sounds/EvilFightN.ogg");
66-
bso[6] = Mix_LoadMUS(DATADIR "/sounds/AreaIIIHellN.ogg");
67-
bso[7] = Mix_LoadMUS(DATADIR "/sounds/ManhuntwoodN.ogg");
60+
bso[0] = loadmus("PrayerofHopeN");
61+
bso[1] = loadmus("AreaIChurchN");
62+
bso[2] = loadmus("GameOverV2N");
63+
bso[3] = loadmus("HangmansTree");
64+
bso[4] = loadmus("AreaIICavesV2N");
65+
bso[5] = loadmus("EvilFightN");
66+
bso[6] = loadmus("AreaIIIHellN");
67+
bso[7] = loadmus("ManhuntwoodN");
6868

6969
/* Fxs */
70-
fx[0] = Mix_LoadWAV(DATADIR "/sounds/shoot.ogg");
71-
fx[1] = Mix_LoadWAV(DATADIR "/sounds/doorfx.ogg");
72-
fx[2] = Mix_LoadWAV(DATADIR "/sounds/Item.ogg");
73-
fx[3] = Mix_LoadWAV(DATADIR "/sounds/jump.ogg");
74-
fx[4] = Mix_LoadWAV(DATADIR "/sounds/slash.ogg");
75-
fx[5] = Mix_LoadWAV(DATADIR "/sounds/mechanismn.ogg");
76-
fx[6] = Mix_LoadWAV(DATADIR "/sounds/onedeathn.ogg");
70+
fx[0] = loadwav("shoot");
71+
fx[1] = loadwav("doorfx");
72+
fx[2] = loadwav("Item");
73+
fx[3] = loadwav("jump");
74+
fx[4] = loadwav("slash");
75+
fx[5] = loadwav("mechanismn");
76+
fx[6] = loadwav("onedeathn");
7777

7878
}
79+
80+
static char* makepath(const char *folder, const char *file, const char *ext) {
81+
static int len1 = strlen(DATADIR) + 1; // sep
82+
int len2 = strlen(folder) + 1; // sep
83+
int len3 = strlen(file) + 1; // sep
84+
int len4 = strlen(ext) + 1; // null
85+
86+
char *result = malloc(len1+len2+len3+len4);
87+
sprintf(result, "%s/%s/%s.%s", DATADIR, folder, file, ext);
88+
89+
return result;
90+
}
91+
92+
SDL_Texture *loadtexture(SDL_Renderer *renderer, const char *file) {
93+
char *path = makepath("graphics", file, "png");
94+
95+
SDL_Texture *result = IMG_LoadTexture(renderer, path);
96+
97+
free(path);
98+
return result;
99+
}
100+
101+
Mix_Music *loadmus(const char *file) {
102+
char *path = makepath("sounds", file, "ogg");
103+
104+
Mix_Music *result = Mix_LoadMUS(path);
105+
106+
free(path);
107+
return result;
108+
}
109+
110+
Mix_Chunk *loadwav(const char *file) {
111+
char *path = makepath("sounds", file, "ogg");
112+
113+
Mix_Chunk *result = Mix_LoadWAV(path);
114+
115+
free(path);
116+
return result;
117+
}

src/loading.h

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66

77
void loadingmusic(Mix_Music *bso[],Mix_Chunk *fx[]);
88
void loaddata(uint stagedata[][22][32],int enemydata[][7][15]);
9+
SDL_Texture *loadtexture(SDL_Renderer *renderer, const char *file);
10+
Mix_Music *loadmus(const char *file);
11+
Mix_Chunk *loadwav(const char *file);

src/startscreen.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* startscreen.c */
22

33
#include "startscreen.h"
4+
#include "loading.h"
45

56
void startscreen(SDL_Window *screen,uint8_t *state,uint8_t *grapset,uint8_t *fullscreen) {
67

@@ -14,11 +15,11 @@ void startscreen(SDL_Window *screen,uint8_t *state,uint8_t *grapset,uint8_t *ful
1415
SDL_Event keyp;
1516

1617
/* Loading PNG */
17-
SDL_Texture *intro = IMG_LoadTexture(renderer, DATADIR "/graphics/intro.png");
18-
SDL_Texture *intromd = IMG_LoadTexture(renderer, DATADIR "/graphics/intromd.png");
18+
SDL_Texture *intro = loadtexture(renderer, "intro");
19+
SDL_Texture *intromd = loadtexture(renderer, "intromd");
1920

2021
/* Load audio */
21-
Mix_Music *music = Mix_LoadMUS(DATADIR "/sounds/MainTitleN.ogg");
22+
Mix_Music *music = loadmus("MainTitleN");
2223

2324
while (exit != 1) {
2425

0 commit comments

Comments
 (0)