Skip to content
Snippets Groups Projects
Commit d08404f5 authored by David Maul's avatar David Maul :crab:
Browse files

switch to hierarchical tile representation

parent 6c379dfe
No related branches found
No related tags found
1 merge request!5Spritesheet
......@@ -74,7 +74,7 @@ int main() {
}
}
std::vector<Effect> effects({Effect(3, 15, EffectId::LAND_EXPLOSION, false),
std::vector<Effect> effects({Effect(3, 15, EffectId::LAND_EXPLOSION, true),
Effect(5, 15, EffectId::AIR_EXPLOSION, true),
Effect(5, 18, EffectId::NAVAL_EXPLOSION, true)});
......
......@@ -17,17 +17,42 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
HighFive::File file(path, HighFive::File::ReadOnly);
// Tiles
HighFive::DataSet tile_frames_ds = file.getDataSet("tiles/frames");
HighFive::DataSet tile_num_frames_ds = file.getDataSet("tiles/num_frames");
std::vector<std::string> tiles({"plain",
"water",
"forest",
"mountain",
"bridge_horizontal",
"bridge_vertical",
"street_horizontal",
"street_vertical",
"street_crossing",
"street_junction_right",
"street_junction_left",
"street_junction_down",
"street_junction_up",
"street_corner_top_left",
"street_corner_top_right",
"street_corner_bottom_left",
"street_corner_bottom_right",
"riff",
"cliff_top",
"cliff_bottom",
"cliff_left",
"cliff_right",
"cliff_corner_top_left",
"cliff_corner_top_right",
"cliff_corner_bottom_left",
"cliff_corner_bottom_right",
"cliff_inverse_corner_top_left",
"cliff_inverse_corner_top_right",
"cliff_inverse_corner_bottom_left",
"cliff_inverse_corner_bottom_right"});
for (size_t tile_idx = 0; tile_idx < tiles.size(); tile_idx++) {
HighFive::DataSet units_ds = file.getDataSet("tiles/" + tiles[tile_idx]);
std::vector<std::vector<std::vector<uint32_t>>> tile_frames;
tile_frames_ds.read(tile_frames);
std::vector<uint32_t> tile_num_frames;
tile_num_frames_ds.read(tile_num_frames);
units_ds.read(tile_frames);
std::vector<uint32_t> tile_buffer(16 * 16 * tile_frames.size(), 0);
......@@ -36,33 +61,34 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
for (size_t x = 0; x < 16; x++) {
size_t index = (y * tile_frames.size() * 16) + (n * 16 + x);
tile_buffer.at(index) = tile_frames.at(n).at(y).at(x);
tile_buffer.at(index) = tile_frames.at(n).at(16 - y - 1).at(x);
}
}
}
int count = 0;
for (size_t n = 0; n < tile_num_frames.size(); n++) {
this->tiles.push_back(std::pair(count, tile_num_frames.at(n)));
count += tile_num_frames.at(n);
}
SDL_Texture *tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
tile_frames.size() * 16, 16);
tile_texture = SDL_CreateTexture(engine.renderer(), SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STATIC, count * 16, 16);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
if (tile_texture == nullptr) {
if (tmp == nullptr) {
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Tiles: " +
"Fehler beim Erstellen der Textur für die Units: " +
std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(tile_texture, NULL, tile_buffer.data(),
count * 16 * sizeof(int32_t)) != 0) {
throw std::runtime_error("Fehler beim updaten der Textur für die Tiles: " +
if (SDL_UpdateTexture(tmp, NULL, tile_buffer.data(),
tile_frames.size() * 16 * sizeof(int32_t)) != 0) {
throw std::runtime_error(
"Fehler beim updaten der Textur für die Units: " +
std::string(SDL_GetError()));
}
tile_textures.push_back(
std::pair<SDL_Texture *, int>(tmp, tile_frames.size()));
}
this->tile_width = 16;
this->tile_height = 16;
......@@ -297,15 +323,14 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
}
// Tiles
int Spritesheet::get_tile_steps(int tile) { return tiles.at(tile).second; }
int Spritesheet::get_tile_width() { return tile_width; }
int Spritesheet::get_tile_height() { return tile_height; }
std::vector<std::pair<int, int>> Spritesheet::get_tiles() { return tiles; }
SDL_Texture *Spritesheet::get_tile_texture() { return tile_texture; }
std::vector<std::pair<SDL_Texture *, int>> &Spritesheet::get_tile_textures() {
return tile_textures;
}
// Buildings
int Spritesheet::get_building_width() { return this->building_width; }
......@@ -339,6 +364,23 @@ std::vector<std::pair<SDL_Texture *, int>> &Spritesheet::get_effect_textures() {
return this->effect_textures;
}
Spritesheet::~Spritesheet() { SDL_DestroyTexture(tile_texture); }
Spritesheet::~Spritesheet() {
for (std::pair<SDL_Texture *, int> tile_texture : tile_textures) {
SDL_DestroyTexture(tile_texture.first);
}
for (SDL_Texture *building_texture : building_textures) {
SDL_DestroyTexture(building_texture);
}
for (std::vector<std::vector<std::pair<SDL_Texture *, int>>> faction :
unit_textures) {
for (std::vector<std::pair<SDL_Texture *, int>> unit : faction) {
for (std::pair<SDL_Texture *, int> state : unit) {
SDL_DestroyTexture(state.first);
}
}
}
}
} // namespace advanced_wars
\ No newline at end of file
......@@ -21,15 +21,12 @@ public:
Spritesheet &operator=(const Spritesheet &) = delete;
// Tiles
int get_tile_steps(int tile);
int get_tile_width();
int get_tile_height();
std::vector<std::pair<int, int>> get_tiles();
SDL_Texture *get_tile_texture();
std::vector<std::pair<SDL_Texture *, int>> &get_tile_textures();
// Buildings
int get_building_width();
......@@ -59,10 +56,9 @@ public:
private:
// Tiles
SDL_Texture *tile_texture;
int tile_width;
int tile_height;
std::vector<std::pair<int, int>> tiles;
std::vector<std::pair<SDL_Texture *, int>> tile_textures;
// Buildings
std::vector<SDL_Texture *> building_textures;
......
......@@ -12,22 +12,19 @@ Tile::Tile(TileId id, int x, int y)
void Tile::render(Engine &engine, int scale) {
Spritesheet *spritesheet = engine.get_spritesheet();
int max_steps = spritesheet->get_tile_steps(static_cast<int>(id));
int step = engine.get_stage() % max_steps;
int step = engine.get_stage() %
spritesheet->get_tile_textures().at(static_cast<int>(id)).second;
if (step >= max_steps || step < 0) {
if (step >=
spritesheet->get_tile_textures().at(static_cast<int>(id)).second ||
step < 0) {
throw std::runtime_error("Tried to access step " + std::to_string(step) +
" for tile " + std::to_string(this->id));
}
std::vector<std::pair<int, int>> tiles = spritesheet->get_tiles();
int offset = tiles.at(id).first;
SDL_Rect src;
src.x = offset * spritesheet->get_tile_width() +
step * spritesheet->get_tile_width();
src.x = step * spritesheet->get_tile_width();
src.y = 0;
src.w = spritesheet->get_tile_width();
src.h = spritesheet->get_tile_height();
......@@ -38,7 +35,9 @@ void Tile::render(Engine &engine, int scale) {
dest.w = spritesheet->get_tile_width() * scale;
dest.h = spritesheet->get_tile_height() * scale;
SDL_RenderCopyEx(engine.renderer(), spritesheet->get_tile_texture(), &src,
SDL_RenderCopyEx(
engine.renderer(),
spritesheet->get_tile_textures().at(static_cast<int>(id)).first, &src,
&dest, 0, NULL, SDL_FLIP_NONE);
}
......
......@@ -8,25 +8,34 @@ namespace advanced_wars {
enum TileId {
PLAIN = 0,
WATER = 1,
STREET_HORIZONTAL = 2,
STREET_VERTICAL = 3,
STREET_CORNER_TOP_LEFT = 4,
STREET_CORNER_TOP_RIGHT = 5,
STREET_CORNER_BOTTOM_LEFT = 6,
STREET_CORNER_BOTTOM_RIGHT = 7,
RIFF = 8,
CLIFF_TOP = 9,
CLIFF_BOTTOM = 10,
CLIFF_LEFT = 11,
CLIFF_RIGHT = 12,
CLIFF_CORNER_TOP_LEFT = 13,
CLIFF_CORNER_TOP_RIGHT = 14,
CLIFF_CORNER_BOTTOM_LEFT = 15,
CLIFF_CORNER_BOTTOM_RIGHT = 16,
CLIFF_INVERSE_CORNER_TOP_LEFT = 17,
CLIFF_INVERSE_CORNER_TOP_RIGHT = 18,
CLIFF_INVERSE_CORNER_BOTTOM_LEFT = 19,
CLIFF_INVERSE_CORNER_BOTTOM_RIGHT = 20,
FOREST = 2,
MOUNTAIN = 3,
BRIDGE_HORIZONTAL = 4,
BRIDGE_VERTICAL = 5,
STREET_HORIZONTAL = 6,
STREET_VERTICAL = 7,
STREET_CROSSING = 8,
STREET_JUNCTION_RIGHT = 9,
STREET_JUNCTION_LEFT = 10,
STREET_JUNCTION_DOWN = 11,
STREET_JUNCTION_UP = 12,
STREET_CORNER_TOP_LEFT = 13,
STREET_CORNER_TOP_RIGHT = 14,
STREET_CORNER_BOTTOM_LEFT = 15,
STREET_CORNER_BOTTOM_RIGHT = 16,
RIFF = 17,
CLIFF_TOP = 18,
CLIFF_BOTTOM = 19,
CLIFF_LEFT = 20,
CLIFF_RIGHT = 21,
CLIFF_CORNER_TOP_LEFT = 22,
CLIFF_CORNER_TOP_RIGHT = 23,
CLIFF_CORNER_BOTTOM_LEFT = 24,
CLIFF_CORNER_BOTTOM_RIGHT = 25,
CLIFF_INVERSE_CORNER_TOP_LEFT = 26,
CLIFF_INVERSE_CORNER_TOP_RIGHT = 27,
CLIFF_INVERSE_CORNER_BOTTOM_LEFT = 28,
CLIFF_INVERSE_CORNER_BOTTOM_RIGHT = 29,
};
class Tile {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment