QUOTE: Never too old to chase dreams.

feat: Converted background to tiles & adjusted levels - freezo - A retro platform game

freezo

A retro platform game
git clone git@soophie.de:/srv/git/freezo
log | files | refs | readme

commit e72356172f35fd8c38100cf2e28121943057740a
parent d4baeee3307563972cd5cfaea0af4f16ddace4e6
Author: Sophie <info@soophie.de>
Date:   Mon, 16 Dec 2024 11:58:00 +0100

feat: Converted background to tiles & adjusted levels

Diffstat:
Minclude/tile.h | 6++++++
Minclude/util.h | 1+
Msrc/game.c | 1-
Msrc/level.c | 128++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Msrc/tile.c | 24++++++++++++++++++++++++
Msrc/util.c | 4++++
6 files changed, 119 insertions(+), 45 deletions(-)

diff --git a/include/tile.h b/include/tile.h @@ -14,6 +14,12 @@ typedef enum { TILE_STONE, TILE_SNOW, TILE_SAND, + TILE_BG_CHANDELIER, + TILE_BG_WINDOW, + TILE_BG_BANNER, + TILE_BG_BRICK_1, + TILE_BG_BRICK_2, + TILE_BG_BRICK_3, } tile_e; struct Tile { diff --git a/include/util.h b/include/util.h @@ -32,6 +32,7 @@ bool rect_collide(rect_t a, rect_t b); Texture texture_load(char *filename, int scale); Rectangle texture_rect(int x, int y, int width, int height); +Rectangle texture_rect_v(int x, int y, int width, int height, int size_x, int size_y); typedef enum { TEXT_ALIGNMENT_LEFT, diff --git a/src/game.c b/src/game.c @@ -147,7 +147,6 @@ void game_update(game_t *game) { void game_draw(game_t *game) { BeginDrawing(); ClearBackground(BLACK); - DrawTextureRec(game->assets.background, (Rectangle) { 0, 0, WINDOW_WIDTH * TILE_WIDTH, WINDOW_HEIGHT * TILE_HEIGHT }, (pos_t) { 0, 0 }, WHITE); if (game->level != LEVEL_NULL) { BeginMode2D(game->camera); for (int i = 0; i < game->tiles_len; i++) { diff --git a/src/level.c b/src/level.c @@ -9,16 +9,16 @@ const char *LEVEL_MAP_1 = { "........................" "........................" + "........k.......o......." + "..1....................." "........................" "........................" - "........................" - "........................" - "......e................." + "......e......3.........." "....xxxx................" - "........................" + ".....b.................." "...........--...xxx....." "................e......." - "...............--xxxxx.." + "...........2...--xxxxx.." "....p..................." "..xxxxxxxxx---xxxxxxxx.." "........................" @@ -28,55 +28,74 @@ const char *LEVEL_MAP_1 = { const char *LEVEL_MAP_2 = { "........................" "........................" + "......3....b............" "........................" "........................" - "........................" - "..............xxxx......" - "........s.e............." - "....xxxxxxxxxtxxx--x...." + "...............xxxx....." + "..o......s.e............" + ".......xxxxxxtxxx--xx..." ".............w.........." - ".............w...--....." + ".........2...w...--.1..." ".............w.........." ".........----x...--....." "....p...............e..." - "..ggggg....xxxxxxxxxxx.." + "..xxxxx....xxxxxxxxxxx.." "........................" "........................" }; const char *LEVEL_MAP_3 = { - "........................................" - "........................................" - "........................................" - "........................................" - "........................................" - "........................................" - "........................................" - "........................................" - "........................................" - "........................................" - "......e..................e...s.........." - "....ggggg...--...xxxxxxxxxxxxxxxxx......" - "........................................" - "............--.........................." - "....................e..................." - "............--...xxxxxxxxt-............." - ".........................w.............." - "............--...........w-............." - "..............e.......s..w..e..........." - "..........xxxxxxxxxxxxxxxxxxxxxxxt--x..." - ".................................w......" - ".................................w--...." - ".................................w......" - ".................................w--...." - "........................e........w......" - "....................xxtxxxtxx----x--...." - "....p.......e.........w...w............." - "..ggggg..-----...xxxxxxxxxxxxxxxxxxxxx.." - "........................................" - "........................................" + "........................" + "........................" + "...b.....1....e...k....." + "............-txx-......." + ".............w.........." + "............-w..-......." + "........s....w.........." + "..x---xxxxxxxtxxx--xx..." + "....e........w....e....." + "...---....2..w--txxx...." + "....3........w..w......." + "........-----w--x......." + "....p.......ew.....s...." + "..xxxx....xxxxxxxxxxxx.." + "........................" + "........................" }; +// const char *LEVEL_MAP_3 = { +// "........................................" +// "........................................" +// "........................................" +// "........................................" +// "........................................" +// "........................................" +// "........................................" +// "........................................" +// "........................................" +// "........................................" +// "......e..................e...s.........." +// "....xxxxx...--...xxxxxxxxxxxxxxxxx......" +// "........................................" +// "............--.........................." +// "....................e..................." +// "............--...xxxxxxxxt-............." +// ".........................w.............." +// "............--...........w-............." +// "..............e.......s..w..e..........." +// "..........xxxxxxxxxxxxxxxxxxxxxxxt--x..." +// ".................................w......" +// ".................................w--...." +// ".................................w......" +// ".................................w--...." +// "........................e........w......" +// "....................xxtxxxtxx----x--...." +// "....p.......e.........w...w............." +// "..xxxxx..-----...xxxxxxxxxxxxxxxxxxxxx.." +// "........................................" +// "........................................" +// }; + void level_generate(game_t *game, const char *map, int width, int height) { game->player = NULL; game->tiles = malloc(sizeof(tile_t) * width * height); @@ -118,6 +137,24 @@ void level_generate(game_t *game, const char *map, int width, int height) { case 'n': tile->type = TILE_SAND; break; + case 'k': + tile->type = TILE_BG_CHANDELIER; + break; + case 'o': + tile->type = TILE_BG_WINDOW; + break; + case 'b': + tile->type = TILE_BG_BANNER; + break; + case '1': + tile->type = TILE_BG_BRICK_1; + break; + case '2': + tile->type = TILE_BG_BRICK_2; + break; + case '3': + tile->type = TILE_BG_BRICK_3; + break; default: break; } @@ -182,9 +219,12 @@ void level_load(game_t *game, level_e type) { break; } case LEVEL_3: { - level_generate(game, LEVEL_MAP_3, 40, 30); - level->width = 40; - level->height = 30; + level_generate(game, LEVEL_MAP_3, 24, 16); + level->width = 24; + level->height = 16; + // level_generate(game, LEVEL_MAP_3, 40, 30); + // level->width = 40; + // level->height = 30; break; } } diff --git a/src/tile.c b/src/tile.c @@ -54,6 +54,30 @@ void tile_draw(tile_t *tile, game_t *game) { DrawTextureRec(game->assets.tiles, texture_rect(6, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); break; } + case TILE_BG_CHANDELIER: { + DrawTextureRec(game->assets.background, texture_rect_v(3, 5, 4, 3, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); + break; + } + case TILE_BG_WINDOW: { + DrawTextureRec(game->assets.background, texture_rect_v(8, 0, 4, 6, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); + break; + } + case TILE_BG_BANNER: { + DrawTextureRec(game->assets.background, texture_rect_v(3, 0, 3, 4, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); + break; + } + case TILE_BG_BRICK_1: { + DrawTextureRec(game->assets.background, texture_rect_v(0, 0, 2, 3, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); + break; + } + case TILE_BG_BRICK_2: { + DrawTextureRec(game->assets.background, texture_rect_v(0, 4, 2, 1, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); + break; + } + case TILE_BG_BRICK_3: { + DrawTextureRec(game->assets.background, texture_rect_v(0, 6, 2, 2, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); + break; + } } } diff --git a/src/util.c b/src/util.c @@ -66,6 +66,10 @@ Rectangle texture_rect(int x, int y, int width, int height) { return (Rectangle) { x * width, y * height, width, height }; } +Rectangle texture_rect_v(int x, int y, int width, int height, int size_x, int size_y) { + return (Rectangle) { x * size_x, y * size_y, width * size_x, height * size_y }; +} + void text_draw(pos_t pos, char *text, text_alignment_e alignment, game_t *game) { pos_t text_pos = pos;