From 99942f51efaa8639797e52d7b54d8b639f5d7733 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Wed, 24 Mar 2021 06:45:20 -0400 Subject: map: Render layer(s) --- diff --git a/src/map.c b/src/map.c index 314ae92..41d6af8 100644 --- a/src/map.c +++ b/src/map.c @@ -17,6 +17,7 @@ * along with Dodge Balls. If not, see . */ +#include #include #include #include @@ -504,6 +505,93 @@ _db_tmx_map_start(void *pv, const char *name, const char **attr) } } +static void +_db_map_render_layer(struct db_map *map, struct db_map_layer *layer, + SDL_Renderer *renderer, SDL_Texture *texture) +{ + int i; + int gid; + SDL_bool tile_found; + struct db_tileset *tileset; + int firstgid; + int lastgid; + int columns; + int tilewidth; + int tileheight; + SDL_Rect tile_rect; + SDL_Rect layer_rect; + + for (i = 0; i < map->w * map->h; ++i) { + gid = layer->tiles[i]; + if (gid == 0) { + continue; + } + tile_found = SDL_FALSE; + for (tileset = map->tileset_head; tileset != NULL; + tileset = db_tileset_next(tileset)) { + firstgid = db_tileset_firstgid(tileset); + lastgid = firstgid + db_tileset_tilecount(tileset); + if (gid >= firstgid && gid < lastgid) { + columns = db_tileset_columns (tileset); + tilewidth = db_tileset_tilewidth (tileset); + tileheight = db_tileset_tileheight(tileset); + gid -= firstgid; + tile_rect.x = gid % columns; + tile_rect.y = gid / columns; + tile_rect.w = tilewidth; + tile_rect.h = tileheight; + tile_rect.x *= tile_rect.w; + tile_rect.y *= tile_rect.h; + layer_rect.x = i % columns; + layer_rect.y = i / columns; + layer_rect.w = tilewidth; + layer_rect.h = tileheight; + layer_rect.x *= layer_rect.w; + layer_rect.y *= layer_rect.h; + db_dbg("Blitting %dx%d tile at (%d,%d) " + "onto layer at (%d,%d)...", + tile_rect.w, tile_rect.h, + tile_rect.x, tile_rect.y, + layer_rect.x, layer_rect.y); + SDL_RenderCopy(renderer, texture, + &tile_rect, &layer_rect); + tile_found = SDL_TRUE; + break; + } + } + if (!tile_found) { + db_warn("Tile with gid 0x%8.8x not found", gid); + } + } +} + +SDL_Texture * +db_map_render(struct db_map *map, SDL_Renderer *renderer) +{ + struct db_tileset *tileset; + SDL_Texture *texture; + SDL_Texture *old_target; + struct db_map_layer *layer; + + for (tileset = map->tileset_head; tileset != NULL; + tileset = db_tileset_next(tileset)) { + db_tileset_create_texture(tileset, renderer); + } + + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, map->w, map->h); + old_target = SDL_GetRenderTarget(renderer); + SDL_SetRenderTarget(renderer, texture); + + for (layer = map->layer_head; layer != NULL; layer = layer->next) { + _db_map_render_layer(map, layer, renderer, texture); + } + + SDL_SetRenderTarget(renderer, old_target); + + return texture; +} + struct db_map * db_map_new(const char *game_id, const char *level_id) { diff --git a/src/map.h b/src/map.h index a8ef337..2e6bd94 100644 --- a/src/map.h +++ b/src/map.h @@ -20,8 +20,11 @@ #ifndef DB_MAP_H_ #define DB_MAP_H_ +#include + struct db_map; struct db_map *db_map_new(const char *game_id, const char *level_id); +SDL_Texture *db_map_render(struct db_map *map, SDL_Renderer *renderer); #endif /* DB_MAP_H_ */ -- cgit v0.9.1