diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/room.c | 100 | ||||
-rw-r--r-- | src/room.h | 10 | ||||
-rw-r--r-- | src/texture.c | 2 |
4 files changed, 38 insertions, 76 deletions
@@ -12,7 +12,7 @@ int main(int argc, char **argv) SDL_Renderer *rdr = SDL_CreateRenderer(wdw, -1, SDL_RENDERER_ACCELERATED); SDL_Texture *txr = tt_texture_load(rdr); - tt_room *room = tt_room_load(1); + tt_room *room = tt_room_load('0'); SDL_SetRenderDrawColor(rdr, 215, 174, 0, 255); @@ -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 }; @@ -5,13 +5,17 @@ #include <SDL2/SDL.h> +#define TT_ROOM_W 19 +#define TT_ROOM_H 15 + typedef struct tt_room { - const SDL_Rect *tiles[15][19]; - void *bodies[15][19]; + char tiletypes[TT_ROOM_H][TT_ROOM_W]; + const SDL_Rect *tiles[TT_ROOM_H][TT_ROOM_W]; + void *bodies[TT_ROOM_H][TT_ROOM_W]; } tt_room; -tt_room *tt_room_load(int num); +tt_room *tt_room_load(char c); void tt_room_draw(SDL_Renderer *rdr, SDL_Texture *txr, tt_room *room); diff --git a/src/texture.c b/src/texture.c index 3606274..e046f6f 100644 --- a/src/texture.c +++ b/src/texture.c @@ -15,7 +15,7 @@ const SDL_Rect tt_texture_player = { 0, 0, 0, 0 }; SDL_Texture *tt_texture_load(SDL_Renderer *rdr) { - SDL_Surface *surf = SDL_LoadBMP("data/textures.bmp"); + SDL_Surface *surf = SDL_LoadBMP("data/tiles.bmp"); SDL_Texture *res = SDL_CreateTextureFromSurface(rdr, surf); SDL_FreeSurface(surf); return res; |