diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-03-24 06:45:20 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-03-24 09:36:41 (EDT) |
commit | 99942f51efaa8639797e52d7b54d8b639f5d7733 (patch) | |
tree | 7b4a77ab4f90762eb9ffc3c6157fd371c6f57b05 /src | |
parent | 9a54ab34a2f524a4c1798c7065e86928868bd223 (diff) | |
download | dodge-balls-99942f51efaa8639797e52d7b54d8b639f5d7733.zip dodge-balls-99942f51efaa8639797e52d7b54d8b639f5d7733.tar.gz dodge-balls-99942f51efaa8639797e52d7b54d8b639f5d7733.tar.bz2 |
map: Render layer(s)
Diffstat (limited to 'src')
-rw-r--r-- | src/map.c | 88 | ||||
-rw-r--r-- | src/map.h | 3 |
2 files changed, 91 insertions, 0 deletions
@@ -17,6 +17,7 @@ * along with Dodge Balls. If not, see <http://www.gnu.org/licenses/>. */ +#include <SDL.h> #include <expat.h> #include <stdio.h> #include <stdlib.h> @@ -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) { @@ -20,8 +20,11 @@ #ifndef DB_MAP_H_ #define DB_MAP_H_ +#include <SDL.h> + 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_ */ |