-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.c
345 lines (316 loc) · 11 KB
/
structs.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
#include <stdio.h>
#include <string.h>
#include <SDL2/SDL.h>
#define NUM_TILES 26
typedef enum {false, true} bool;
typedef enum sprindex{/*{{{*/
clear,
dude,
pallete,
panel,
graves,
clearbump,
select,
dblue,
lblue,
dgreen,
lgreen,
dred,
lred,
dyellow,
lyellow,
dteal,
lteal,
dpurpleflat,
rock,
tealflat,
yellowflat,
anotherflat,
up,
right,
left,
down
}sprindex;/*}}}*/
typedef enum special{/*{{{*/
redkey_door,
redkey,
shotgun,
devil_trap
}special;/*}}}*/
typedef struct tile{/*{{{*/
int id;
sprindex sp;
SDL_Rect loc;
bool walkable;
bool draw;
special keyword;
}tile;/*}}}*/
typedef struct cursor{/*{{{*/
sprindex sp;
int x;
int y;
}cursor;/*}}}*/
/* SHOULD BE LESS GLOBAL */
int d_sel = 1;
SDL_Window *win = NULL;
SDL_Renderer *rend = NULL;
SDL_Texture *atlas = NULL;
bool game_quit = false;
cursor ch = {.sp = dude, .x = 20, .y = 11};
cursor m = {.sp = select};
/* FIX: LOAD MAP FROM FILE */
int layer_base[22][40]=/*{{{*/
{
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,03,03,03,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,03,03,03,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00}
};/*}}}*/
int layer_atop[22][40]=/*{{{*/
{
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,18,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00}
};/*}}}*/
int init()/*{{{*/
{
SDL_Rect d_bounds;
SDL_DisplayMode d_mode;
if(SDL_Init(SDL_INIT_VIDEO) != 0) // SDL_INIT_AUDIO if needed
{
printf("SDL_Init Error: %s\n", SDL_GetError());
return 1;
}
int should_be_zero = SDL_GetCurrentDisplayMode(d_sel, &d_mode);
if(should_be_zero != 0)
SDL_Log("Could not get display mode for video display #%d: %s", d_sel, SDL_GetError());
else if(SDL_GetDisplayBounds(d_sel, &d_bounds) != 0)
{
SDL_Log("SDL_GetDisplayBounds failed: %s", SDL_GetError());
return 1;
}
win = SDL_CreateWindow
(
"An SDL2 win",
d_bounds.x,
d_bounds.y,
d_bounds.w,
d_bounds.h,
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE
);
if(win == NULL)
{
SDL_Log("Could not create window: %s\n", SDL_GetError());
return 1;
}
rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
if(rend == NULL)
{
SDL_Log("Could not create renerer: %s\n", SDL_GetError());
return 1;
}
SDL_ShowCursor(0);
return 0;
}/*}}}*/
int tini()/*{{{*/
{
SDL_DestroyTexture(atlas);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}/*}}}*/
int main (int argc, char *argv[])
{
int i = 0;
int j = 0;
SDL_Surface *sur = NULL;
SDL_Rect img_tile[NUM_TILES];
SDL_Rect target;
int scale = 32;
/* HANDLE ARGUMENTS *//*{{{*/
for(i = 1; i < argc; i++)
{
if(strncmp(argv[i], "-d", 3) == 0)
{
i++;
d_sel = atoi(argv[i]);
}else if(strncmp(argv[i], "-s", 3) == 0)
{
i++;
scale = atoi(argv[i]);
}
}/*}}}*/
init();
/* DEFINE TILE-ATLAS SRC_RECTS *//*{{{*/
for(i = 0; i < NUM_TILES; i++)
{
img_tile[i].x = i * 16;
img_tile[i].y = 0;
img_tile[i].h = 16;
img_tile[i].w = 16;
}/*}}}*/
/* LOAD THE ATLAS */ /*{{{*/
sur = SDL_LoadBMP("tiles.bmp");
SDL_SetColorKey(sur, SDL_TRUE, SDL_MapRGB(sur->format, 255, 0, 255));
atlas = SDL_CreateTextureFromSurface(rend, sur);
SDL_FreeSurface(sur);/*}}}*/
SDL_Event e;
Uint32 ms_update = 1000/120;
Uint32 ms_render = 1000/60;
//Uint32 ms_loop = ms_render * 2;
Uint32 last_update = 0;
Uint32 last_render = 0;
//Uint32 last_loop = 0;
int bg_pulse = 0;
while(!game_quit)
{
while(SDL_PollEvent(&e))/*{{{*/
{
switch(e.type)
{
case SDL_QUIT:
SDL_Log("Program quit after %i ticks", e.quit.timestamp);
game_quit = true;
break;
case SDL_MOUSEMOTION:
m.x = e.motion.x / scale;
m.y = e.motion.y / scale;
break;
case SDL_MOUSEWHEEL:
scale += ((int)e.wheel.y)*8;
if(scale < 8)
scale = 8;
break;
case SDL_KEYDOWN:
switch(e.key.keysym.sym)
{
case SDLK_q:
SDL_Log("Program quit after %i ticks", e.quit.timestamp);
game_quit = true;
break;
case SDLK_UP:
case SDLK_w:
ch.sp = up;
ch.y -= 1;
break;
case SDLK_LEFT:
case SDLK_a:
ch.sp = left;
ch.x -= 1;
break;
case SDLK_RIGHT:
case SDLK_d:
ch.sp = right;
ch.x += 1;
break;
case SDLK_DOWN:
case SDLK_s:
ch.sp = down;
ch.y += 1;
break;
}
break;
}
}/*}}}*/
/* LOGIC *//*{{{*/
if(SDL_TICKS_PASSED(SDL_GetTicks(), last_update + ms_update))
{
last_update = SDL_GetTicks();
if(bg_pulse < 255)
bg_pulse++;
else
bg_pulse = 0;
}/*}}}*/
/* RENDER ( FIX: RENDER ONLY THE SCREEN AND ONLY WHAT NEEDS TO CHANGE! ) *//*{{{*/
if(SDL_TICKS_PASSED(SDL_GetTicks(), last_render + ms_render))
{
/* CLEAR RENDERER */
last_render = SDL_GetTicks();
SDL_SetRenderDrawColor(rend,bg_pulse,bg_pulse,bg_pulse,255);
SDL_RenderClear(rend);
/* PAINT TILES TO THE RENDERER */
for(i = 0; i < 22; i++)
{
for(j = 0; j < 40; j++)
{
target.x = j * scale;
target.y = i * scale;
target.h = scale;
target.w = scale;
SDL_RenderCopy(rend, atlas, &img_tile[layer_base[i][j]], &target);
}
}
for(i = 0; i < 22; i++)
{
for(j = 0; j < 40; j++)
{
target.x = j * scale;
target.y = i * scale;
target.h = scale;
target.w = scale;
SDL_RenderCopy(rend, atlas, &img_tile[layer_atop[i][j]], &target);
}
}
target.x = ch.x * scale;
target.y = ch.y * scale;
target.h = scale;
target.w = scale;
SDL_RenderCopy(rend, atlas, &img_tile[ch.sp], &target);
target.x = m.x * scale;
target.y = m.y * scale;
target.h = scale;
target.w = scale;
SDL_RenderCopy(rend, atlas, &img_tile[m.sp], &target);
}/*}}}*/
SDL_RenderPresent(rend);
/* THROTTLE FIX: TIE TO LOGIC UPDATE *//*{{{*/
//if(!SDL_TICKS_PASSED(SDL_GetTicks(), last_loop + ms_loop))
//{
// SDL_Delay((SDL_GetTicks() - last_loop) + ms_loop);
//}
//last_loop = SDL_GetTicks();
/* UNSATISFACTORY TROTTLE METHODOLOGY */
SDL_Delay(1);/*}}}*/
}
tini();
printf("I ran till the end\n");
return 0;
}