summaryrefslogtreecommitdiff
path: root/src/room.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/room.c')
-rw-r--r--src/room.c100
1 files changed, 29 insertions, 71 deletions
diff --git a/src/room.c b/src/room.c
index 389793c..3d8d6c0 100644
--- a/src/room.c
+++ b/src/room.c
@@ -4,94 +4,52 @@
#include "texture.h"
-#define ROOM_W 19
-#define ROOM_H 15
-
-
-static char *readline(FILE *f)
+static SDL_Rect *rect(int x, int y, int w, int h)
{
- 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;
+ SDL_Rect *res = malloc(sizeof(*res));
+ res->x = x;
+ res->y = y;
+ res->w = w;
+ res->h = h;
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 *tt_room_load(char c)
{
tt_room *res = malloc(sizeof(*res));
- char path[] = "data/rooms/00";
- path[11] = '0' + num / 10;
- path[12] = '0' + num % 10;
+ char path[] = "data/rooms/0";
+ path[11] = c;
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;
- }
+ 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 != ROOM_H; ++i) {
- for (j = 0; j != ROOM_W; ++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 };