diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2021-02-19 10:02:23 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2021-02-19 10:02:23 +0300 |
commit | a236f83d8cd680f956677ac6d65f0a953036e144 (patch) | |
tree | 21bc959204fb8082c475890d0d8722b296d0c140 /src | |
parent | 2d74b68ac770601ef9e4d621752f9229c56cb2f3 (diff) | |
download | takethis-a236f83d8cd680f956677ac6d65f0a953036e144.tar takethis-a236f83d8cd680f956677ac6d65f0a953036e144.tar.xz takethis-a236f83d8cd680f956677ac6d65f0a953036e144.zip |
Room can be loaded.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 66 | ||||
-rw-r--r-- | src/player.h | 20 | ||||
-rw-r--r-- | src/room.c | 102 | ||||
-rw-r--r-- | src/room.h | 27 | ||||
-rw-r--r-- | src/texture.c | 22 | ||||
-rw-r--r-- | src/texture.h | 23 | ||||
-rw-r--r-- | src/utils.c | 10 | ||||
-rw-r--r-- | src/utils.h | 11 |
8 files changed, 213 insertions, 68 deletions
@@ -1,60 +1,32 @@ #include <SDL2/SDL.h> -#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 <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); + } + } + } +} 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 <SDL2/SDL.h> + + +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 <SDL2/SDL.h> + + +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 <SDL2/SDL.h> - - -SDL_Texture *load_texture(SDL_Renderer *renderer, const char *path); - - -#endif |