diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/map.c | 42 | ||||
-rw-r--r-- | src/tileset.c | 69 | ||||
-rw-r--r-- | src/tileset.h | 9 |
3 files changed, 46 insertions, 74 deletions
@@ -511,12 +511,6 @@ _db_map_render_layer(struct db_map *map, struct db_map_layer *layer, { int i; int gid; - SDL_bool tile_found; - struct db_tileset *tileset; - int firstgid; - int lastgid; - int columns; - SDL_Rect tile_rect; SDL_Rect layer_rect; for (i = 0; i < map->w * map->h; ++i) { @@ -524,45 +518,11 @@ _db_map_render_layer(struct db_map *map, struct db_map_layer *layer, if (gid == 0) { continue; } - tile_found = SDL_FALSE; layer_rect.x = map->tw * (i % map->w); layer_rect.y = map->th * (i / map->w); layer_rect.w = map->tw; layer_rect.h = map->th; - 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); - gid -= firstgid; - tile_rect.x = map->tw * (gid % columns); - tile_rect.y = map->th * (gid / columns); - tile_rect.w = map->tw; - tile_rect.h = map->th; - db_dbg("Blitting %dx%d tile at (%d,%d) " - "onto %dx%d area of layer at " - "(%d,%d)...", - tile_rect.w, tile_rect.h, - tile_rect.x, tile_rect.y, - layer_rect.w, layer_rect.h, - layer_rect.x, layer_rect.y); - if (SDL_RenderCopy(renderer, db_tileset_texture( - tileset, - renderer), - &tile_rect, &layer_rect) - != 0) { - db_err("Failed to copy texture (%s)", - SDL_GetError()); - return -1; - } - tile_found = SDL_TRUE; - break; - } - } - if (!tile_found) { - db_warn("Tile with gid 0x%8.8x not found", gid); - } + db_tile_render(map->tileset_head, renderer, gid, &layer_rect); } return 0; diff --git a/src/tileset.c b/src/tileset.c index f63fa0a..afe0bde 100644 --- a/src/tileset.c +++ b/src/tileset.c @@ -422,32 +422,8 @@ db_tileset_new(const char *game_id, const char *file, int firstgid, return tileset; } -int -db_tileset_tilecount(struct db_tileset *tileset) -{ - return tileset->tc; -} - -int -db_tileset_columns(struct db_tileset *tileset) -{ - return tileset->cols; -} - -int -db_tileset_firstgid(struct db_tileset *tileset) -{ - return tileset->firstgid; -} - -struct db_tileset * -db_tileset_next(struct db_tileset *tileset) -{ - return tileset->next; -} - -SDL_Texture * -db_tileset_texture(struct db_tileset *tileset, SDL_Renderer *renderer) +static SDL_Texture * +_db_tileset_texture(struct db_tileset *tileset, SDL_Renderer *renderer) { if (tileset->texture == NULL) { tileset->texture = SDL_CreateTextureFromSurface(renderer, @@ -455,3 +431,44 @@ db_tileset_texture(struct db_tileset *tileset, SDL_Renderer *renderer) } return tileset->texture; } + +int +db_tile_render(struct db_tileset *tileset, SDL_Renderer *renderer, int gid, + SDL_Rect *dstrect) +{ + SDL_bool tile_found; + SDL_Rect srcrect; + + tile_found = SDL_FALSE; + for (; tileset != NULL; tileset = tileset->next) { + if (gid >= tileset->firstgid && + gid < tileset->firstgid + tileset->tc) { + gid -= tileset->firstgid; + srcrect.x = tileset->tw * (gid % tileset->cols); + srcrect.y = tileset->th * (gid / tileset->cols); + srcrect.w = tileset->tw; + srcrect.h = tileset->th; + db_dbg("Blitting %dx%d tile at (%d,%d) onto %dx%d area " + "at (%d,%d)...", + srcrect.w, srcrect.h, + srcrect.x, srcrect.y, + dstrect->w, dstrect->h, + dstrect->x, dstrect->y); + if (SDL_RenderCopy(renderer, _db_tileset_texture( + tileset, renderer), + &srcrect, dstrect) + != 0) { + db_err("Failed to copy texture (%s)", + SDL_GetError()); + return -1; + } + tile_found = SDL_TRUE; + break; + } + } + if (!tile_found) { + db_warn("Tile with gid 0x%8.8x not found", gid); + return 0; + } + return 1; +} diff --git a/src/tileset.h b/src/tileset.h index 79b8bc7..652e1a3 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -27,12 +27,7 @@ struct db_tileset; struct db_tileset *db_tileset_new(const char *game_id, const char *file, int firstgid, int tilewidth, int tileheight, struct db_tileset *prev); -int db_tileset_tilecount (struct db_tileset *tileset) __attribute__((__pure__)); -int db_tileset_columns (struct db_tileset *tileset) __attribute__((__pure__)); -int db_tileset_firstgid (struct db_tileset *tileset) __attribute__((__pure__)); -struct db_tileset *db_tileset_next(struct db_tileset *tileset) - __attribute__((__pure__)); -SDL_Texture *db_tileset_texture(struct db_tileset *tileset, - SDL_Renderer *renderer); +int db_tile_render(struct db_tileset *tileset, SDL_Renderer *renderer, int gid, + SDL_Rect *dstrect); #endif /* DB_TILESET_H_ */ |