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
|
#include "room.h"
#include <stdio.h>
#include "texture.h"
#define ROOM_W 19
#define ROOM_H 15
static char *readline(FILE *f)
{
int len = 0;
char *res = malloc(1);
int c = fgetc(f);
while (c != EOF && c != '\n') {
res = realloc(res, len + 2);
res[len++] = c;
c = fgetc(f);
}
res[len] = 0;
return res;
}
static char skipspaces(FILE *f)
{
int c = fgetc(f);
while (c == ' ' || c == '\n' || c == '\t') c = fgetc(f);
return c == EOF ? 0 : c;
}
tt_room *tt_room_load(int num)
{
tt_room *res = malloc(sizeof(*res));
char path[] = "data/rooms/00";
path[11] = '0' + num / 10;
path[12] = '0' + num % 10;
FILE *f = fopen(path, "r");
char *type = readline(f);
free(type);
int i, j;
for (i = 0; i != ROOM_H; ++i) {
for (j = 0; j != ROOM_W; ++j) {
char c = skipspaces(f);
if (c == '.') {
res->tiles[i][j] = &tt_texture_floor;
res->bodies[i][j] = 0;
} else if (c == '#') {
if (i == 0 && j == 0) {
res->tiles[i][j] = &tt_texture_corner_lu;
} else if (i == 0 && j == ROOM_W - 1) {
res->tiles[i][j] = &tt_texture_corner_ru;
} else if (i == ROOM_H - 1 && j == 0) {
res->tiles[i][j] = &tt_texture_corner_ld;
} else if (i == ROOM_H - 1 && j == ROOM_W - 1) {
res->tiles[i][j] = &tt_texture_corner_rd;
} else if (i == 0) {
res->tiles[i][j] = &tt_texture_wall_u;
} else if (i == ROOM_H - 1) {
res->tiles[i][j] = &tt_texture_wall_d;
} else if (j == 0) {
res->tiles[i][j] = &tt_texture_wall_l;
} else if (j == ROOM_W - 1) {
res->tiles[i][j] = &tt_texture_wall_r;
} else {
fclose(f);
free(res);
return 0;
}
res->bodies[i][j] = 0;
/* } else if (c == '@') {
res->tiles[i][j] = tt_tile_floor;
res->bodies[i][j] = tt_player_spawn();
if (!res->bodies[i][j]) {
flcose(f);
free(res);
return 0;
} */
} else {
fclose(f);
free(res);
return 0;
}
}
}
return res;
}
void tt_room_draw(SDL_Renderer *rdr, SDL_Texture *txr, tt_room *room)
{
int i, j;
for (i = 0; i != ROOM_H; ++i) {
for (j = 0; j != 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);
}
}
}
}
|