From a365de1b0ab91f9c057196a601d96d2aed6e48fb Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Sun, 28 Feb 2021 23:29:25 +0300 Subject: o_O --- src/body.c | 69 ++++++++++++++++ src/body.h | 26 +++++++ src/game.c | 111 ++++++++++++++++++++++++++ src/game.h | 12 +++ src/globals.c | 14 ++++ src/globals.h | 25 ++++++ src/main.c | 52 +++++++++---- src/map.c | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/map.h | 18 +++++ src/player.c | 61 +++++++++++++++ src/player.h | 29 ++++--- src/room.c | 59 ++++---------- src/room.h | 27 ++++--- src/texture.c | 22 ------ src/texture.h | 23 ------ 15 files changed, 663 insertions(+), 131 deletions(-) create mode 100644 src/body.c create mode 100644 src/body.h create mode 100644 src/game.c create mode 100644 src/game.h create mode 100644 src/globals.c create mode 100644 src/globals.h create mode 100644 src/map.c create mode 100644 src/map.h create mode 100644 src/player.c delete mode 100644 src/texture.c delete mode 100644 src/texture.h (limited to 'src') diff --git a/src/body.c b/src/body.c new file mode 100644 index 0000000..d0e7938 --- /dev/null +++ b/src/body.c @@ -0,0 +1,69 @@ +#include "body.h" + +#include +#include "room.h" +#include "globals.h" + + +static void move(tt_body *b, int d) +{ + b->xrem += b->xvel * d; + b->yrem += b->yvel * d; + b->x += b->xrem / 1000; + b->y += b->yrem / 1000; + b->xrem = b->xrem % 1000; + b->yrem = b->yrem % 1000; +} + + +void tt_body_move(int d) +{ + tt_room *r = ttplayer.room; + int i, j; + for (i = 0; i != TT_ROOM_H; ++i) { + for (j = 0; j != TT_ROOM_W; ++j) { + tt_body *w = r->walls[i][j]; + if (w) w->rem += d; + } + } + for (i = 0; i != r->bodies_count; ++i) { + tt_body *b = r->bodies + i; + b->rem += d; + int ex = b->x; + int exr = b->xrem; + int ey = b->y; + int eyr = b->yrem; + move(b, d); + { + SDL_Rect box = { b->x + 5, b->y + 2, 22, 28 }; + if (!tt_room_collide(r, &box) && !tt_room_out(r, &box)) continue; + } + b->x = ex; + b->y = ey; + b->xrem = exr; + b->yrem = eyr; + b->xvel *= -1; + move(b, d); + { + SDL_Rect box = { b->x + 5, b->y + 2, 22, 28 }; + if (!tt_room_collide(r, &box) && !tt_room_out(r, &box)) continue; + } + b->x = ex; + b->y = ey; + b->xrem = exr; + b->yrem = eyr; + b->xvel *= -1; + b->yvel *= -1; + move(b, d); + { + SDL_Rect box = { b->x + 5, b->y + 2, 22, 28 }; + if (!tt_room_collide(r, &box) && !tt_room_out(r, &box)) continue; + } + b->x = ex; + b->y = ey; + b->xrem = exr; + b->yrem = eyr; + b->xvel *= -1; + move(b, d); + } +} diff --git a/src/body.h b/src/body.h new file mode 100644 index 0000000..fcb15bc --- /dev/null +++ b/src/body.h @@ -0,0 +1,26 @@ +#ifndef TT_INCLUDED_BODY +#define TT_INCLUDED_BODY + + +typedef struct tt_body { + int x; + int y; + int xrem; + int yrem; + int xvel; + int yvel; + int txrrow; + int txrcol; + int rem; + int anim; + int rate; + void (*collision_act)(struct tt_body *b); + char *msg; + int msglen; +} tt_body; + + +void tt_body_move(int d); + + +#endif diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..444f9ff --- /dev/null +++ b/src/game.c @@ -0,0 +1,111 @@ +#include "game.h" + +#include +#include "globals.h" + + +int magic = 0; + +static int ticks; + +static void step(int d) +{ + int xw = ttplayer.xwalk * 137; + int yw = ttplayer.ywalk * 137; + if (xw || yw) { + ttplayer.rem += d; + } + if (xw && yw) { + ttplayer.xrem += (double)(xw * d) / sqrt(2); + ttplayer.yrem += (double)(yw * d) / sqrt(2); + } else if (xw || yw) { + ttplayer.xrem += xw * d; + ttplayer.yrem += yw * d; + } + int x = ttplayer.x; + int y = ttplayer.y; + ttplayer.x += ttplayer.xrem / 1000; + ttplayer.y += ttplayer.yrem / 1000; + ttplayer.xrem = ttplayer.xrem % 1000; + ttplayer.yrem = ttplayer.yrem % 1000; + { + SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 }; + int out = tt_room_out(ttplayer.room, &box); + if (out) ttplayer.room = ttplayer.room->neighbours[out - 1]; + if (out == 1) ttplayer.y = TT_ROOM_H * 32 - 32; + else if (out == 2) ttplayer.x = 0; + else if (out == 3) ttplayer.y = 0; + else if (out == 4) ttplayer.x = TT_ROOM_W * 32 - 32; + } + { + SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 }; + if (tt_room_collide(ttplayer.room, &box)) { + ttplayer.x = x; + ttplayer.y = y; + } + } + tt_body_move(d); + { + int i; + SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 }; + for (i = 0; i != ttplayer.room->bodies_count; ++i) { + tt_body *b = ttplayer.room->bodies + i; + SDL_Rect body = { 4 + b->x, 4 + b->y, 24, 24 }; + if (SDL_HasIntersection(&body, &box)) b->collision_act(b); + } + } +} + + +void gotofirstroom() +{ + ttplayer.room = ttmap + '0'; + magic = 0; + ticks = SDL_GetTicks(); +} + +void tt_mainloop() +{ + int keyw, keys, keya, keyd, arru, arrr, arrd, arrl; + keyw = keya = keys = keyd = arru = arrr = arrd = arrl = 0; + ticks = SDL_GetTicks(); + int q = 0; + while (!q) { + SDL_Event e; + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) q = 1; + else if (e.type == SDL_KEYDOWN) { + int code = e.key.keysym.scancode; + if (code == SDL_SCANCODE_W) keyw = 1; + else if (code == SDL_SCANCODE_S) keys = 1; + else if (code == SDL_SCANCODE_A) keya = 1; + else if (code == SDL_SCANCODE_D) keyd = 1; + else if (code == SDL_SCANCODE_UP) arru = 1; + else if (code == SDL_SCANCODE_RIGHT) arrr = 1; + else if (code == SDL_SCANCODE_DOWN) arrd = 1; + else if (code == SDL_SCANCODE_LEFT) arrl = 1; + } else if (e.type == SDL_KEYUP) { + int code = e.key.keysym.scancode; + if (code == SDL_SCANCODE_W) keyw = 0; + else if (code == SDL_SCANCODE_S) keys = 0; + else if (code == SDL_SCANCODE_A) keya = 0; + else if (code == SDL_SCANCODE_D) keyd = 0; + else if (code == SDL_SCANCODE_UP) arru = 0; + else if (code == SDL_SCANCODE_RIGHT) arrr = 0; + else if (code == SDL_SCANCODE_DOWN) arrd = 0; + else if (code == SDL_SCANCODE_LEFT) arrl = 0; + } + } + ttplayer.ywalk = (keys | arrd) - (keyw | arru); + ttplayer.xwalk = (keyd | arrr) - (keya | arrl); + int newticks = SDL_GetTicks(); + step(newticks - ticks); + ticks = newticks; + SDL_RenderClear(ttrdr); + tt_player_draw(); + SDL_RenderPresent(ttrdr); + if (magic == tt_gotofirstroom) { + gotofirstroom(); + } + } +} diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..6f84f24 --- /dev/null +++ b/src/game.h @@ -0,0 +1,12 @@ +#ifndef TT_INCLUDED_GAME +#define TT_INCLUDED_GAME + + +enum { tt_gotofirstroom = 1 }; + +extern int magic; + +void tt_mainloop(); + + +#endif diff --git a/src/globals.c b/src/globals.c new file mode 100644 index 0000000..0a95a00 --- /dev/null +++ b/src/globals.c @@ -0,0 +1,14 @@ +#include "globals.h" + + +TTF_Font *ttfont; + +SDL_Texture *tttxr; + +SDL_Renderer *ttrdr; + +Mix_Music *ponpon; + +tt_room ttmap[256]; + +tt_player ttplayer; diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 0000000..e4d03a1 --- /dev/null +++ b/src/globals.h @@ -0,0 +1,25 @@ +#ifndef TT_INCLUDED_GLOBALS +#define TT_INCLUDED_GLOBALS + + +#include +#include +#include +#include "room.h" +#include "player.h" + + +extern TTF_Font *ttfont; + +extern SDL_Texture *tttxr; + +extern SDL_Renderer *ttrdr; + +extern Mix_Music *ponpon; + +extern tt_room ttmap[256]; + +extern tt_player ttplayer; + + +#endif diff --git a/src/main.c b/src/main.c index 44f4a43..01756ae 100644 --- a/src/main.c +++ b/src/main.c @@ -1,32 +1,52 @@ #include -#include "room.h" -#include "texture.h" +#include +#include +#include +#include +#include "globals.h" +#include "map.h" +#include "player.h" +#include "game.h" +static SDL_Texture *loadtxr(const char *path) +{ + SDL_Surface *surf = SDL_LoadBMP(path); + SDL_Texture *res = SDL_CreateTextureFromSurface(ttrdr, surf); + SDL_FreeSurface(surf); + return res; +} + int main(int argc, char **argv) { + SDL_Init(SDL_INIT_EVERYTHING); + TTF_Init(); + Mix_Init(MIX_INIT_OGG); + Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096); 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('0'); + ttrdr = SDL_CreateRenderer(wdw, -1, SDL_RENDERER_ACCELERATED); + tttxr = loadtxr("data/txr.bmp"); + ttfont = TTF_OpenFont("data/font.otf", 24); + ponpon = Mix_LoadMUS("data/ponpon.ogg"); + tt_map_load(); - SDL_SetRenderDrawColor(rdr, 215, 174, 0, 255); + srand(time(0)); + SDL_SetRenderDrawColor(ttrdr, 0, 0, 0, 255); - int n; - for (n = 0; n != 200; ++n) { - SDL_RenderClear(rdr); - tt_room_draw(rdr, txr, room); - SDL_RenderPresent(rdr); - SDL_Delay(25); - } + tt_mainloop(); - SDL_DestroyTexture(txr); - SDL_DestroyRenderer(rdr); + tt_map_free(); + TTF_CloseFont(ttfont); + SDL_DestroyTexture(tttxr); + SDL_DestroyRenderer(ttrdr); SDL_DestroyWindow(wdw); + Mix_HaltMusic(); + Mix_FreeMusic(ponpon); + Mix_CloseAudio(); + TTF_Quit(); SDL_Quit(); return 0; } diff --git a/src/map.c b/src/map.c new file mode 100644 index 0000000..13894d6 --- /dev/null +++ b/src/map.c @@ -0,0 +1,246 @@ +#include "map.h" + +#include +#include +#include +#include "room.h" +#include "globals.h" +#include "game.h" + + +static int readnum(FILE *f) +{ + int neg = fgetc(f) == '-'; + int num = fgetc(f) - '0'; + num *= 10; + num += fgetc(f) - '0'; + num *= 10; + num += fgetc(f) - '0'; + return (neg ? -1 : 1) * num; +} + +static SDL_Rect *newtile(int row, int col) +{ + SDL_Rect *r = malloc(sizeof(*r)); + r->x = 16 * col; + r->y = 16 * row; + r->h = r->w = 16; + return r; +} + +static void do_nothing(tt_body *b) +{} + +static void gribtake(tt_body *b) +{ + Mix_PlayMusic(ponpon, -1); + b->collision_act = do_nothing; + b->anim = 1; + b->txrrow = 0; + b->txrcol = 15; + magic = tt_gotofirstroom; +} + +static void loadroom(tt_room *r, FILE *f) +{ + int default_floor_id = fgetc(f) - '0'; + fgetc(f); + int i, j; + for (i = 0; i != 4; ++i) r->neighbours[i] = ttmap + fgetc(f); + r->bodies = 0; + r->bodies_count = 0; + fgetc(f); + for (i = 0; i != TT_ROOM_H; ++i) { + for (j = 0; j != TT_ROOM_W; ++j) { + int type = fgetc(f); + int id = fgetc(f); + int c = id; + if (id == ' ') id = '0'; + id -= '0'; + r->floor[i][j] = 0; + r->walls[i][j] = 0; + r->roof[i][j] = 0; + if (type == '@') { + ttplayer.room = r; + ttplayer.x = j * 32; + ttplayer.y = i * 32; + ttplayer.xrem = 0; + ttplayer.yrem = 0; + ttplayer.xwalk = 0; + ttplayer.ywalk = 0; + ttplayer.rem = 0; + ttplayer.money = 0; + ttplayer.keys = 0; + ttplayer.the_key = 0; + ttplayer.variant = 1; + r->floor[i][j] = newtile(0, default_floor_id); + } else if (type == '#') { + r->walls[i][j] = malloc(sizeof(tt_body)); + tt_body *b = r->walls[i][j]; + b->x = j * 32; + b->y = i * 32; + b->xrem = 0; + b->yrem = 0; + b->xvel = 0; + b->yvel = 0; + b->rem = 0; + b->txrrow = 1; + b->txrcol = id; + b->anim = 1; + b->rate = 1; + b->collision_act = do_nothing; + b->msg = 0; + b->msglen = 0; + } else if (type == 'M') { + r->floor[i][j] = newtile(0, default_floor_id); + r->bodies_count++; + r->bodies = realloc(r->bodies, + sizeof(tt_body) * r->bodies_count); + tt_body *b = r->bodies + r->bodies_count - 1; + b->x = j * 32; + b->y = i * 32; + b->xrem = 0; + b->yrem = 0; + b->xvel = 0; + b->yvel = 0; + b->rem = 0; + b->txrrow = 8; + b->txrcol = id; + b->anim = 4; + b->rate = 150 + (rand() % 50 - 25); + b->collision_act = gribtake; + b->msg = 0; + b->msglen = 0; + } else if (type == '^') { + r->floor[i][j] = newtile(0, default_floor_id); + r->walls[i][j] = malloc(sizeof(tt_body)); + tt_body *b = r->walls[i][j]; + b->x = j * 32; + b->y = i * 32; + b->xrem = 0; + b->yrem = 0; + b->xvel = 0; + b->yvel = 0; + b->rem = 0; + b->txrrow = 8; + b->txrcol = id; + b->anim = 4; + b->rate = 150 + (rand() % 50 - 25); + b->collision_act = do_nothing; + b->msg = 0; + b->msglen = 0; + } else if (type == '&') { + r->floor[i][j] = newtile(0, default_floor_id); + r->walls[i][j] = malloc(sizeof(tt_body)); + tt_body *b = r->walls[i][j]; + b->x = j * 32; + b->y = i * 32; + b->xrem = 0; + b->yrem = 0; + b->xvel = 0; + b->yvel = 0; + b->rem = 0; + b->txrrow = 10; + b->txrcol = id; + b->anim = 1; + b->rate = 100; + b->collision_act = do_nothing; + b->msg = 0; + b->msglen = 0; + } else if (type == '.') { + r->floor[i][j] = newtile(0, c == ' ' ? default_floor_id : id); + } else if (type == '*') { + r->walls[i][j] = malloc(sizeof(tt_body)); + tt_body *b = r->walls[i][j]; + b->x = j * 32; + b->y = i * 32; + b->xrem = 0; + b->yrem = 0; + b->xvel = 0; + b->yvel = 0; + b->rem = 0; + b->txrrow = 9; + b->txrcol = id; + b->anim = 4; + b->rate = 150 + (rand() % 50 - 25); + b->collision_act = do_nothing; + b->msg = 0; + b->msglen = 0; + } else if (type == 'X') { + r->floor[i][j] = newtile(0, default_floor_id); + r->roof[i][j] = newtile(5, id); + } else if (type == '=') { + r->floor[i][j] = newtile(0, default_floor_id); + r->bodies_count++; + r->bodies = realloc(r->bodies, + sizeof(tt_body) * r->bodies_count); + tt_body *b = r->bodies + r->bodies_count - 1; + b->x = j * 32; + b->y = i * 32; + b->xrem = 0; + b->yrem = 0; + b->rem = 0; + b->txrrow = 0; + b->txrcol = 15; + b->anim = 1; + b->rate = 100; + b->collision_act = do_nothing; + } else if (type == '$') { + r->floor[i][j] = newtile(0, default_floor_id); + r->bodies_count++; + r->bodies = realloc(r->bodies, + sizeof(tt_body) * r->bodies_count); + tt_body *b = r->bodies + r->bodies_count - 1; + b->x = j * 32; + b->y = i * 32; + b->xrem = 0; + b->yrem = 0; + b->rem = 0; + b->txrrow = 7; + b->txrcol = id; + b->anim = 4; + b->rate = 100 + (rand() % 50 - 25); + b->collision_act = 0; /* todo: kill */ + } + } + fgetc(f); + } + for (i = 0; i != r->bodies_count; ++i) { + r->bodies[i].yvel = readnum(f); + r->bodies[i].xvel = readnum(f); + r->bodies[i].msg = 0; + r->bodies[i].msglen = 0; + if (fgetc(f) == '=') { + int c = fgetc(f); + while (c != EOF && c != '\n') { + r->bodies[i].msglen++; + r->bodies[i].msg = realloc(r->bodies[i].msg, + r->bodies[i].msglen + 1); + r->bodies[i].msg[r->bodies[i].msglen - 1] = c; + c = fgetc(f); + } + r->bodies[i].msg[r->bodies[i].msglen] = 0; + } + } +} + +void tt_map_load() +{ + char path[] = "data/map/0"; + char r = '0'; + while (r != '~') { + path[9] = r; + FILE *roomf = fopen(path, "r"); + if (roomf) { + loadroom(ttmap + r, roomf); + fclose(roomf); + } + ++r; + } +} + + +void tt_map_free() +{ + return; +} diff --git a/src/map.h b/src/map.h new file mode 100644 index 0000000..e8da22d --- /dev/null +++ b/src/map.h @@ -0,0 +1,18 @@ +#ifndef TAKETHIS_INCLUDED_MAP +#define TAKETHIS_INCLUDED_MAP + + +/* room is identified by single char + * they are located in data/map/ + * roomfile has four neighbors at the first line + * after this indexed tiles go + * after them each line is speed of corresponding enemy in percents + * and optionaly after = message of this enemy */ + + +void tt_map_load(); + +void tt_map_free(); + + +#endif diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..10d5c5f --- /dev/null +++ b/src/player.c @@ -0,0 +1,61 @@ +#include "player.h" + +#include "globals.h" + + +void tt_player_draw() +{ + int i, j; + tt_room *r = ttplayer.room; + for (i = 0; i != TT_ROOM_H; ++i) { + for (j = 0; j != TT_ROOM_W; ++j) { + SDL_Rect d = { 14 + j * 32, 14 + i * 32, 32, 32 }; + SDL_Rect *f = r->floor[i][j]; + tt_body *w = r->walls[i][j]; + if (f) SDL_RenderCopy(ttrdr, tttxr, f, &d); + if (w) { + SDL_Rect s = { + (w->txrcol * w->anim + w->rem / w->rate % w->anim) * 16, + w->txrrow * 16, 16, 16 + }; + SDL_RenderCopy(ttrdr, tttxr, &s, &d); + } + } + } + for (i = 0; i != r->bodies_count; ++i) { + tt_body *b = r->bodies + i; + SDL_Rect s = { + (b->txrcol * b->anim + b->rem / b->rate % b->anim) * 16, + b->txrrow * 16, 16, 16 + }; + SDL_Rect d = { 14 + b->x, 14 + b->y, 32, 32 }; + SDL_RenderCopy(ttrdr, tttxr, &s, &d); + if (b->msg) { + SDL_Color c = { 255, 255, 255, 255 }; + SDL_Surface *s = TTF_RenderText_Blended(ttfont, b->msg, c); + SDL_Texture *t = SDL_CreateTextureFromSurface(ttrdr, s); + SDL_Rect dst = { 50 + b->x, 20 + b->y, s->w, s->h }; + SDL_RenderCopy(ttrdr, t, 0, &dst); + SDL_DestroyTexture(t); + SDL_FreeSurface(s); + } + } + + SDL_Rect d = { 14 + ttplayer.x, 14 + ttplayer.y, 32, 32 }; + int dir = 0; + if (ttplayer.xwalk == 1) dir = 6; + else if (ttplayer.xwalk == -1) dir = 2; + else if (ttplayer.ywalk == -1) dir = 4; + SDL_Rect s = { 16 * (dir + (ttplayer.rem / 100 % 2)), + 16 * (5 + ttplayer.variant), + 16, 16 }; + SDL_RenderCopy(ttrdr, tttxr, &s, &d); + + for (i = 0; i != TT_ROOM_H; ++i) { + for (j = 0; j != TT_ROOM_W; ++j) { + SDL_Rect d = { 14 + j * 32, 14 + i * 32, 32, 32 }; + SDL_Rect *roof = r->roof[i][j]; + if (roof) SDL_RenderCopy(ttrdr, tttxr, roof, &d); + } + } +} diff --git a/src/player.h b/src/player.h index 427c9d3..cc224ba 100644 --- a/src/player.h +++ b/src/player.h @@ -1,20 +1,29 @@ -#ifndef TAKETHIS_INCLUDED_ROOM -#define TAKETHIS_INCLUDED_ROOM +#ifndef TAKETHIS_INCLUDED_PLAYER +#define TAKETHIS_INCLUDED_PLAYER -typedef struct tt_room { - SDL_Rect *tiles[15][19]; - tt_body *bodies[15][19]; -} tt_room; +#include "room.h" -tt_room *tt_room_load(int num); +typedef struct tt_player { + tt_room *room; + int xwalk; + int ywalk; + int x; + int y; + int xrem; + int yrem; + int variant; + int rem; + int money; + int keys; + int the_key; +} tt_player; -void tt_room_draw(tt_room *room); -void tt_room_rtol(tt_room *right, tt_room *left, int permille); +void tt_player_walk(int delta); -void tt_room_utod(tt_room *up, tt_room *down, int permille); +void tt_player_draw(); #endif diff --git a/src/room.c b/src/room.c index 3d8d6c0..b45ada8 100644 --- a/src/room.c +++ b/src/room.c @@ -1,60 +1,27 @@ #include "room.h" -#include -#include "texture.h" - -static SDL_Rect *rect(int x, int y, int w, int h) -{ - SDL_Rect *res = malloc(sizeof(*res)); - res->x = x; - res->y = y; - res->w = w; - res->h = h; - return res; -} - -tt_room *tt_room_load(char c) +int tt_room_collide(tt_room *room, SDL_Rect *box) { - tt_room *res = malloc(sizeof(*res)); - char path[] = "data/rooms/0"; - path[11] = c; - FILE *f = fopen(path, "r"); + SDL_Rect r = { 0, 0, 32, 32 }; int i, j; for (i = 0; i != TT_ROOM_H; ++i) { + r.y = 32 * 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; + r.x = 32 * j; + if (SDL_HasIntersection(box, room->walls[i][j] ? &r : 0)) + return 1; } - 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 tt_room_out(tt_room *room, SDL_Rect *box) { - int i, 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 }; - SDL_RenderCopy(rdr, txr, src, &dst); - } - } - } + if (!box) return 0; + if (box->y < 0) return 1; + if (box->x + box->w > TT_ROOM_W * 32) return 2; + if (box->y + box->h > TT_ROOM_H * 32) return 3; + if (box->x < 0) return 4; + return 0; } diff --git a/src/room.h b/src/room.h index 9d6661a..b4567f2 100644 --- a/src/room.h +++ b/src/room.h @@ -3,29 +3,28 @@ #include +#include "body.h" -#define TT_ROOM_W 19 -#define TT_ROOM_H 15 +#define TT_ROOM_W 20 +#define TT_ROOM_H 16 typedef struct tt_room { - 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]; + SDL_Rect *floor[TT_ROOM_H][TT_ROOM_W]; + tt_body *walls[TT_ROOM_H][TT_ROOM_W]; + SDL_Rect *roof[TT_ROOM_H][TT_ROOM_W]; + tt_body *bodies; + int bodies_count; + struct tt_room *neighbours[4]; } tt_room; -tt_room *tt_room_load(char c); +void tt_room_draw_background(tt_room *room); +void tt_room_draw_foreground(tt_room *room); -void tt_room_draw(SDL_Renderer *rdr, SDL_Texture *txr, tt_room *room); +int tt_room_collide(tt_room *room, SDL_Rect *box); -/* -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); -*/ +int tt_room_out(tt_room *room, SDL_Rect *box); #endif diff --git a/src/texture.c b/src/texture.c deleted file mode 100644 index e046f6f..0000000 --- a/src/texture.c +++ /dev/null @@ -1,22 +0,0 @@ -#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/tiles.bmp"); - SDL_Texture *res = SDL_CreateTextureFromSurface(rdr, surf); - SDL_FreeSurface(surf); - return res; -} diff --git a/src/texture.h b/src/texture.h deleted file mode 100644 index 48ecdeb..0000000 --- a/src/texture.h +++ /dev/null @@ -1,23 +0,0 @@ -#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 -- cgit v1.2.3