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
|
#include "room.h"
#include <stdio.h>
#include "texture.h"
static SDL_Rect *rect(int x, int y, int w, int h)
{
SDL_Rect *res = malloc(sizeof(*res));
res->x = x;
res->y = y;
res->w = w;
res->h = h;
return res;
}
tt_room *tt_room_load(char c)
{
tt_room *res = malloc(sizeof(*res));
char path[] = "data/rooms/0";
path[11] = c;
FILE *f = fopen(path, "r");
int i, j;
for (i = 0; i != TT_ROOM_H; ++i) {
for (j = 0; j != TT_ROOM_W; ++j) {
int type = fgetc(f);
res->tiletypes[i][j] = type;
res->bodies[i][j] = 0;
int id = fgetc(f);
if (id == ' ') id = '0';
if (id < '0' || '9' < id) goto error;
id = (id - '0') * 16;
if (type == '.') res->tiles[i][j] = rect(id, 16, 16, 16);
else if (type == '#') res->tiles[i][j] = rect(id, 0, 16, 16);
else goto error;
}
fgetc(f);
}
fclose(f);
return res;
error:
fclose(f);
free(res);
return 0;
}
void tt_room_draw(SDL_Renderer *rdr, SDL_Texture *txr, tt_room *room)
{
int i, j;
for (i = 0; i != TT_ROOM_H; ++i) {
for (j = 0; j != TT_ROOM_W; ++j) {
const SDL_Rect *src = room->tiles[i][j];
if (src) {
SDL_Rect dst = { 30 + 32 * j, 30 + 32 * i, 32, 32 };
SDL_RenderCopy(rdr, txr, src, &dst);
}
}
}
}
|