From a236f83d8cd680f956677ac6d65f0a953036e144 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 19 Feb 2021 10:02:23 +0300 Subject: Room can be loaded. --- data/background.bmp | Bin 1994550 -> 0 bytes data/map | 5 +++ data/rooms/01 | 16 +++++++++ data/rooms/02 | 16 +++++++++ data/rooms/03 | 16 +++++++++ data/rooms/04 | 16 +++++++++ data/rooms/05 | 16 +++++++++ data/rooms/06 | 16 +++++++++ data/rooms/07 | 16 +++++++++ data/rooms/08 | 16 +++++++++ data/rooms/09 | 16 +++++++++ data/rooms/10 | 16 +++++++++ data/shape.bmp | Bin 22666 -> 0 bytes data/textures.bmp | Bin 0 -> 22666 bytes src/main.c | 66 ++++++++++------------------------ src/player.h | 20 +++++++++++ src/room.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/room.h | 27 ++++++++++++++ src/texture.c | 22 ++++++++++++ src/texture.h | 23 ++++++++++++ src/utils.c | 10 ------ src/utils.h | 11 ------ 22 files changed, 378 insertions(+), 68 deletions(-) delete mode 100644 data/background.bmp create mode 100644 data/map create mode 100644 data/rooms/01 create mode 100644 data/rooms/02 create mode 100644 data/rooms/03 create mode 100644 data/rooms/04 create mode 100644 data/rooms/05 create mode 100644 data/rooms/06 create mode 100644 data/rooms/07 create mode 100644 data/rooms/08 create mode 100644 data/rooms/09 create mode 100644 data/rooms/10 delete mode 100644 data/shape.bmp create mode 100644 data/textures.bmp create mode 100644 src/player.h create mode 100644 src/room.c create mode 100644 src/room.h create mode 100644 src/texture.c create mode 100644 src/texture.h delete mode 100644 src/utils.c delete mode 100644 src/utils.h diff --git a/data/background.bmp b/data/background.bmp deleted file mode 100644 index f96cabe..0000000 Binary files a/data/background.bmp and /dev/null differ diff --git a/data/map b/data/map new file mode 100644 index 0000000..8d4473a --- /dev/null +++ b/data/map @@ -0,0 +1,5 @@ +01 = 02 = 03 . 07 = 08 +. . | . . . | . . +. . 04 = 05 = 06 = 09 +. . . . . . . . | +. . . . . . . . 10 diff --git a/data/rooms/01 b/data/rooms/01 new file mode 100644 index 0000000..1ea08e7 --- /dev/null +++ b/data/rooms/01 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # # # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # # # # # # # # # # # diff --git a/data/rooms/02 b/data/rooms/02 new file mode 100644 index 0000000..18739f6 --- /dev/null +++ b/data/rooms/02 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # # # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +X . . . . . . . . . . . . . . . . . X +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # X # # # # # # # # # diff --git a/data/rooms/03 b/data/rooms/03 new file mode 100644 index 0000000..d6185ff --- /dev/null +++ b/data/rooms/03 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # # # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +X . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # # # # # # # # # # # diff --git a/data/rooms/04 b/data/rooms/04 new file mode 100644 index 0000000..e0d902a --- /dev/null +++ b/data/rooms/04 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # X # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . X +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # # # # # # # # # # # diff --git a/data/rooms/05 b/data/rooms/05 new file mode 100644 index 0000000..a3f2532 --- /dev/null +++ b/data/rooms/05 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # # # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +X . . . . . . . . . . . . . . . . . X +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # # # # # # # # # # # diff --git a/data/rooms/06 b/data/rooms/06 new file mode 100644 index 0000000..d72c22e --- /dev/null +++ b/data/rooms/06 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # X # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +X . . . . . . . . . . . . . . . . . X +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # # # # # # # # # # # diff --git a/data/rooms/07 b/data/rooms/07 new file mode 100644 index 0000000..0407287 --- /dev/null +++ b/data/rooms/07 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # # # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . X +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # X # # # # # # # # # diff --git a/data/rooms/08 b/data/rooms/08 new file mode 100644 index 0000000..d6185ff --- /dev/null +++ b/data/rooms/08 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # # # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +X . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # # # # # # # # # # # diff --git a/data/rooms/09 b/data/rooms/09 new file mode 100644 index 0000000..e689452 --- /dev/null +++ b/data/rooms/09 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # # # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +X . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # X # # # # # # # # # diff --git a/data/rooms/10 b/data/rooms/10 new file mode 100644 index 0000000..1d0cfe4 --- /dev/null +++ b/data/rooms/10 @@ -0,0 +1,16 @@ +zelda +# # # # # # # # # X # # # # # # # # # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . Q +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# . . . . . . . . . . . . . . . . . # +# # # # # # # # # # # # # # # # # # # diff --git a/data/shape.bmp b/data/shape.bmp deleted file mode 100644 index a59ced7..0000000 Binary files a/data/shape.bmp and /dev/null differ diff --git a/data/textures.bmp b/data/textures.bmp new file mode 100644 index 0000000..a59ced7 Binary files /dev/null and b/data/textures.bmp differ diff --git a/src/main.c b/src/main.c index b0c87e2..2bdf6e3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,60 +1,32 @@ #include -#include "utils.h" - - -enum { shape_size = 16 }; +#include "room.h" +#include "texture.h" int main(int argc, char **argv) { - SDL_Window* window; - SDL_Renderer* renderer; - SDL_Texture* background; - SDL_Texture* shape; - - SDL_Rect src; - SDL_Rect dst; - - src.x = 0; - src.y = 0; - src.w = shape_size; - src.h = shape_size; - - dst.x = 640 / 2 - shape_size; - dst.y = 480 / 2 - shape_size; - dst.w = shape_size * 2; - dst.h = shape_size * 2; - - window = SDL_CreateWindow("T A K E T H I S", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - 640, 480, 0); - renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + SDL_Window *wdw = SDL_CreateWindow("T A K E T H I S", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + 950, 540, 0); + SDL_Renderer *rdr = SDL_CreateRenderer(wdw, -1, + SDL_RENDERER_ACCELERATED); + SDL_Texture *txr = tt_texture_load(rdr); + tt_room *room = tt_room_load(1); - background = load_texture(renderer, "data/background.bmp"); - shape = load_texture(renderer, "data/shape.bmp"); + SDL_SetRenderDrawColor(rdr, 215, 174, 0, 255); - int i; int n; - for (i = 0; i < 2; ++i) { - for(n = 0; n < 4; ++n) { - src.x = shape_size * (n % 2); - if (n > 1) { - src.y = shape_size; - } else { - src.y = 0; - } - SDL_RenderCopy(renderer, background, NULL, NULL); - SDL_RenderCopy(renderer, shape, &src, &dst); - SDL_RenderPresent(renderer); - SDL_Delay(500); - } + for (n = 0; n != 200; ++n) { + SDL_RenderClear(rdr); + tt_room_draw(rdr, txr, room); + SDL_RenderPresent(rdr); + SDL_Delay(25); } - SDL_DestroyTexture(shape); - SDL_DestroyTexture(background); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); + SDL_DestroyTexture(txr); + SDL_DestroyRenderer(rdr); + SDL_DestroyWindow(wdw); SDL_Quit(); return 0; } diff --git a/src/player.h b/src/player.h new file mode 100644 index 0000000..427c9d3 --- /dev/null +++ b/src/player.h @@ -0,0 +1,20 @@ +#ifndef TAKETHIS_INCLUDED_ROOM +#define TAKETHIS_INCLUDED_ROOM + + +typedef struct tt_room { + SDL_Rect *tiles[15][19]; + tt_body *bodies[15][19]; +} tt_room; + + +tt_room *tt_room_load(int num); + +void tt_room_draw(tt_room *room); + +void tt_room_rtol(tt_room *right, tt_room *left, int permille); + +void tt_room_utod(tt_room *up, tt_room *down, int permille); + + +#endif diff --git a/src/room.c b/src/room.c new file mode 100644 index 0000000..389793c --- /dev/null +++ b/src/room.c @@ -0,0 +1,102 @@ +#include "room.h" + +#include +#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); + } + } + } +} diff --git a/src/room.h b/src/room.h new file mode 100644 index 0000000..40a6974 --- /dev/null +++ b/src/room.h @@ -0,0 +1,27 @@ +#ifndef TAKETHIS_INCLUDED_ROOM +#define TAKETHIS_INCLUDED_ROOM + + +#include + + +typedef struct tt_room { + const SDL_Rect *tiles[15][19]; + void *bodies[15][19]; +} tt_room; + + +tt_room *tt_room_load(int num); + +void tt_room_draw(SDL_Renderer *rdr, SDL_Texture *txr, tt_room *room); + +/* +void tt_room_rtol(SDL_Renderer *rdr, SDL_Texture *txr, + tt_room *right, tt_room *left, int permille); + +void tt_room_utod(SDL_Renderer *rdr, SDL_Texture *txr, + tt_room *up, tt_room *down, int permille); +*/ + + +#endif diff --git a/src/texture.c b/src/texture.c new file mode 100644 index 0000000..3606274 --- /dev/null +++ b/src/texture.c @@ -0,0 +1,22 @@ +#include "texture.h" + + +const SDL_Rect tt_texture_floor = { 16 * 7, 0, 16, 16 }; +const SDL_Rect tt_texture_corner_lu = { 16 * 5, 0, 16, 16 }; +const SDL_Rect tt_texture_corner_ru = { 16 * 6, 0, 16, 16 }; +const SDL_Rect tt_texture_corner_ld = { 16 * 5, 16, 16, 16 }; +const SDL_Rect tt_texture_corner_rd = { 16 * 6, 16, 16, 16 }; +const SDL_Rect tt_texture_wall_l = { 16 * 8, 0, 16, 16 }; +const SDL_Rect tt_texture_wall_r = { 16 * 8, 16, 16, 16 }; +const SDL_Rect tt_texture_wall_u = { 16 * 9, 0, 16, 16 }; +const SDL_Rect tt_texture_wall_d = { 16 * 9, 16, 16, 16 }; +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_Texture *res = SDL_CreateTextureFromSurface(rdr, surf); + SDL_FreeSurface(surf); + return res; +} diff --git a/src/texture.h b/src/texture.h new file mode 100644 index 0000000..48ecdeb --- /dev/null +++ b/src/texture.h @@ -0,0 +1,23 @@ +#ifndef TAKETHIS_INCLUDED_TEXTURE +#define TAKETHIS_INCLUDED_TEXTURE + + +#include + + +extern const SDL_Rect tt_texture_floor; +extern const SDL_Rect tt_texture_corner_lu; +extern const SDL_Rect tt_texture_corner_ru; +extern const SDL_Rect tt_texture_corner_ld; +extern const SDL_Rect tt_texture_corner_rd; +extern const SDL_Rect tt_texture_wall_l; +extern const SDL_Rect tt_texture_wall_r; +extern const SDL_Rect tt_texture_wall_u; +extern const SDL_Rect tt_texture_wall_d; +extern const SDL_Rect tt_texture_player; + + +SDL_Texture *tt_texture_load(SDL_Renderer *rdr); + + +#endif diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index 35bd0a9..0000000 --- a/src/utils.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "utils.h" - - -SDL_Texture *load_texture(SDL_Renderer *renderer, const char *path) -{ - SDL_Surface *s = SDL_LoadBMP(path); - SDL_Texture *res = SDL_CreateTextureFromSurface(renderer, s); - SDL_FreeSurface(s); - return res; -} diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index 6b726ab..0000000 --- a/src/utils.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef TAKETHIS_INCLUDED_UTILS -#define TAKETHIS_INCLUDED_UTILS - - -#include - - -SDL_Texture *load_texture(SDL_Renderer *renderer, const char *path); - - -#endif -- cgit v1.2.3