diff options
-rw-r--r-- | src/level.c | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/src/level.c b/src/level.c index 9febd7c..cc15c53 100644 --- a/src/level.c +++ b/src/level.c @@ -28,6 +28,8 @@ struct db_level { char *game_id; char *level_id; struct db_level *next; + struct db_map *map; + SDL_Texture *map_texture; struct db_tileset *tilesets; int player_x; int player_y; @@ -75,47 +77,50 @@ db_level_new(const char *game_id, const char *level_id, struct db_level *prev) return level; } -int -db_level_play(SDL_Renderer *renderer, struct db_level *level) +static void +_db_level_render(SDL_Renderer *renderer, struct db_level *level) { - struct db_map *map; - SDL_Texture *map_texture; - SDL_Rect dstrect; + SDL_Rect dstrect; - db_dbg("Playing %s/%s", level->game_id, level->level_id); + SDL_RenderClear(renderer); - map = db_map_new(level->game_id, level->level_id); - if (map == NULL) { - return -1; - } - map_texture = db_map_render(map, renderer); - level->tilesets = db_map_get_tilesets(map); - db_map_get_player(map, &level->player_x, &level->player_y, - &level->player_r, &level->player_gid); - db_map_get_target(map, &level->target_x, &level->target_y, - &level->target_r, &level->target_gid); - level->balls = db_map_get_balls(map); + SDL_RenderCopy(renderer, level->map_texture, NULL, NULL); - if (SDL_RenderClear(renderer) != 0) { - db_err("Failed to clear screen (%s)", SDL_GetError()); - return -1; - } - if (SDL_RenderCopy(renderer, map_texture, NULL, NULL) != 0) { - db_err("Failed to copy texture (%s)", SDL_GetError()); - return -1; - } dstrect.x = level->player_x - level->player_r; dstrect.y = level->player_y - level->player_r; dstrect.w = level->player_r * 2; dstrect.h = level->player_r * 2; db_tile_render(level->tilesets, renderer, level->player_gid, &dstrect); + dstrect.x = level->target_x - level->target_r; dstrect.y = level->target_y - level->target_r; dstrect.w = level->target_r * 2; dstrect.h = level->target_r * 2; db_tile_render(level->tilesets, renderer, level->target_gid, &dstrect); + db_balls_render(level->balls, renderer); + SDL_RenderPresent(renderer); +} + +int +db_level_play(SDL_Renderer *renderer, struct db_level *level) +{ + db_dbg("Playing %s/%s", level->game_id, level->level_id); + + level->map = db_map_new(level->game_id, level->level_id); + if (level->map == NULL) { + return -1; + } + level->map_texture = db_map_render(level->map, renderer); + level->tilesets = db_map_get_tilesets(level->map); + db_map_get_player(level->map, &level->player_x, &level->player_y, + &level->player_r, &level->player_gid); + db_map_get_target(level->map, &level->target_x, &level->target_y, + &level->target_r, &level->target_gid); + level->balls = db_map_get_balls(level->map); + + _db_level_render(renderer, level); SDL_Delay(1000); return 0; |