diff options
Diffstat (limited to 'src/sdl')
-rw-r--r-- | src/sdl/draw.c | 159 |
1 files changed, 121 insertions, 38 deletions
diff --git a/src/sdl/draw.c b/src/sdl/draw.c index ab4803e..4449b52 100644 --- a/src/sdl/draw.c +++ b/src/sdl/draw.c @@ -29,6 +29,27 @@ get_cell_rect( } static void +on_size( + const sok_ctx_t * const ctx, + const sok_pos_t level_size, + void * const data +) { + draw_ctx_t * const draw_ctx = data; + const size_t cell_size = get_cell_size(draw_ctx); + + // get renderer size + int renderer_x, renderer_y; + if (SDL_GetRendererOutputSize(draw_ctx->renderer, &renderer_x, &renderer_y)) { + die("SDL_GetRendererOutputSize(): %s", SDL_GetError()); + } + + // calculate renderer offset + draw_ctx->render_ofs_x = (renderer_x - level_size.x * cell_size) / 2; + draw_ctx->render_ofs_y = (renderer_y - level_size.y * cell_size) / 2; +} + +#ifdef DRAW_SPRITES +static void draw_cell( draw_ctx_t * const draw_ctx, const sok_pos_t pos, @@ -36,51 +57,113 @@ draw_cell( ) { const SDL_Rect rect = get_cell_rect(draw_ctx, pos); -#ifdef DRAW_SPRITES SDL_Texture *tex = draw_ctx->sprites[sprite]; if (SDL_RenderCopy(draw_ctx->renderer, tex, NULL, &rect)) { die("SDL_RenderCopy(): %s", SDL_GetError()); } -#else - if (SDL_RenderFillRect(draw_ctx->renderer, &rect)) { - die("SDL_RenderFillRect(): %s", SDL_GetError()); - } -#endif /* DRAW_SPRITES */ } static bool -draw_on_size( +draw_sprites_on_size( const sok_ctx_t * const ctx, const sok_pos_t level_size, void * const data ) { + on_size(ctx, level_size, data); draw_ctx_t * const draw_ctx = data; - const size_t cell_size = get_cell_size(draw_ctx); - // get renderer size - int renderer_x, renderer_y; - if (SDL_GetRendererOutputSize(draw_ctx->renderer, &renderer_x, &renderer_y)) { - die("SDL_GetRendererOutputSize(): %s", SDL_GetError()); - } - - // calculate renderer offset - draw_ctx->render_ofs_x = (renderer_x - level_size.x * cell_size) / 2; - draw_ctx->render_ofs_y = (renderer_y - level_size.y * cell_size) / 2; - -#ifdef DRAW_SPRITES + // draw floor for (size_t y = 0; y < level_size.y; y++) { for (size_t x = 0; x < level_size.x; x++) { const sok_pos_t pos = { x, y }; draw_cell(draw_ctx, pos, SPRITE_FLOOR); } } -#endif /* DRAW_SPRITES */ return true; } static bool -draw_on_walls_start( +draw_sprites_on_wall( + const sok_ctx_t * const ctx, + const sok_pos_t pos, + void * const data +) { + draw_ctx_t * const draw_ctx = data; + draw_cell(draw_ctx, pos, SPRITE_WALL); + return true; +} + +static bool +draw_sprites_on_goal( + const sok_ctx_t * const ctx, + const sok_pos_t pos, + const bool has_player, + const bool has_box, + void * const data +) { + draw_ctx_t * const draw_ctx = data; + draw_cell(draw_ctx, pos, SPRITE_GOAL); + return true; +} + +static bool +draw_sprites_on_home( + const sok_ctx_t * const ctx, + const sok_pos_t pos, + const bool has_goal, + void * const data +) { + draw_ctx_t * const draw_ctx = data; + draw_cell(draw_ctx, pos, SPRITE_HOME); + return true; +} + +static bool +draw_sprites_on_box( + const sok_ctx_t * const ctx, + const sok_pos_t pos, + const bool has_goal, + void * const data +) { + draw_ctx_t * const draw_ctx = data; + draw_cell(draw_ctx, pos, SPRITE_BOX); + return true; +} + +static const sok_ctx_walk_cbs_t +DRAW_CBS = { + .on_size = draw_sprites_on_size, + .on_wall = draw_sprites_on_wall, + .on_goal = draw_sprites_on_goal, + .on_home = draw_sprites_on_home, + .on_box = draw_sprites_on_box, +}; +#else /* !DRAW_SPRITES */ +static void +fill_cell( + draw_ctx_t * const draw_ctx, + const sok_pos_t pos +) { + const SDL_Rect rect = get_cell_rect(draw_ctx, pos); + + if (SDL_RenderFillRect(draw_ctx->renderer, &rect)) { + die("SDL_RenderFillRect(): %s", SDL_GetError()); + } +} + +static bool +draw_colors_on_size( + const sok_ctx_t * const ctx, + const sok_pos_t level_size, + void * const data +) { + on_size(ctx, level_size, data); + return true; +} + +static bool +draw_colors_on_walls_start( const sok_ctx_t * const ctx, void * const data ) { @@ -91,29 +174,28 @@ draw_on_walls_start( } static bool -draw_on_wall( +draw_colors_on_wall( const sok_ctx_t * const ctx, const sok_pos_t pos, void * const data ) { draw_ctx_t * const draw_ctx = data; - draw_cell(draw_ctx, pos, SPRITE_WALL); + fill_cell(draw_ctx, pos); return true; } static bool -draw_on_goals_start( +draw_colors_on_goals_start( const sok_ctx_t * const ctx, void * const data ) { draw_ctx_t * const draw_ctx = data; set_color(draw_ctx->renderer, COLOR_GOAL); - return true; } static bool -draw_on_goal( +draw_colors_on_goal( const sok_ctx_t * const ctx, const sok_pos_t pos, const bool has_player, @@ -122,13 +204,13 @@ draw_on_goal( ) { draw_ctx_t * const draw_ctx = data; - draw_cell(draw_ctx, pos, SPRITE_GOAL); + fill_cell(draw_ctx, pos); return true; } static bool -draw_on_home( +draw_colors_on_home( const sok_ctx_t * const ctx, const sok_pos_t pos, const bool has_goal, @@ -137,13 +219,13 @@ draw_on_home( draw_ctx_t * const draw_ctx = data; set_color(draw_ctx->renderer, has_goal ? COLOR_HOME_GOAL : COLOR_HOME); - draw_cell(draw_ctx, pos, SPRITE_HOME); + fill_cell(draw_ctx, pos); return true; } static bool -draw_on_box( +draw_colors_on_box( const sok_ctx_t * const ctx, const sok_pos_t pos, const bool has_goal, @@ -152,21 +234,22 @@ draw_on_box( draw_ctx_t * const draw_ctx = data; set_color(draw_ctx->renderer, has_goal ? COLOR_BOX_GOAL : COLOR_BOX); - draw_cell(draw_ctx, pos, SPRITE_BOX); + fill_cell(draw_ctx, pos); return true; } static const sok_ctx_walk_cbs_t DRAW_CBS = { - .on_size = draw_on_size, - .on_walls_start = draw_on_walls_start, - .on_wall = draw_on_wall, - .on_goals_start = draw_on_goals_start, - .on_goal = draw_on_goal, - .on_home = draw_on_home, - .on_box = draw_on_box, + .on_size = draw_colors_on_size, + .on_walls_start = draw_colors_on_walls_start, + .on_wall = draw_colors_on_wall, + .on_goals_start = draw_colors_on_goals_start, + .on_goal = draw_colors_on_goal, + .on_home = draw_colors_on_home, + .on_box = draw_colors_on_box, }; +#endif /* DRAW_SPRITES */ void draw( |