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

add support for buildings

parent 771d3b7f
No related branches found
No related tags found
1 merge request!5Spritesheet
#include "building.hpp" #include "building.hpp"
#include "common.h"
Building::Building(int x, int y): x(x), y(y) {}; Building::Building(int x, int y, BuildingId id, Faction faction)
: x(x), y(y), id(id), faction(faction) {};
#ifndef BUILDING_HPP #pragma once
#define BUILDING_HPP
#include "common.h"
enum BuildingId {
HEADQUARTER = 0,
CITY = 1,
FACTORY = 2,
PORT = 3,
SATELLITE = 4,
};
class Building { class Building {
public: public:
Building(int x, int y); Building(int x, int y, BuildingId id, Faction faction);
private:
// Position
int x; int x;
int y; int y;
BuildingId id;
Faction faction;
}; };
#endif
#pragma once
enum Faction {
RED = 0,
BLUE = 1,
YELLOW = 2,
GREEN = 3,
PURPLE = 4,
NEUTRAL = 5,
};
...@@ -32,9 +32,11 @@ void Level::render(Engine &engine, std::vector<SDL_Event> &events) { ...@@ -32,9 +32,11 @@ void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
int stage = SDL_GetTicks() / 300; int stage = SDL_GetTicks() / 300;
Spritesheet *spritesheet = engine.get_spritesheet();
// Tiles
for (int y = 0; y < this->height; y++) { for (int y = 0; y < this->height; y++) {
for (int x = 0; x < this->width; x++) { for (int x = 0; x < this->width; x++) {
Spritesheet *spritesheet = engine.get_spritesheet();
SDL_Rect dst; SDL_Rect dst;
dst.x = x * spritesheet->get_tile_width() * RENDERING_SCALE; dst.x = x * spritesheet->get_tile_width() * RENDERING_SCALE;
...@@ -55,6 +57,18 @@ void Level::render(Engine &engine, std::vector<SDL_Event> &events) { ...@@ -55,6 +57,18 @@ void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
} }
} }
// Buildings
for (Building building : buildings) {
SDL_Rect dst;
dst.x = building.x * spritesheet->get_tile_width() * RENDERING_SCALE;
dst.y = (building.y - 1) * spritesheet->get_tile_height() * RENDERING_SCALE;
dst.w = spritesheet->get_building_width() * RENDERING_SCALE;
dst.h = spritesheet->get_building_height() * RENDERING_SCALE;
spritesheet->render_building(engine.renderer(), building.id,
building.faction, &dst);
}
// Set background color for renderer // Set background color for renderer
if (SDL_SetRenderDrawColor(engine.renderer(), 255, 0, 0, 0)) { if (SDL_SetRenderDrawColor(engine.renderer(), 255, 0, 0, 0)) {
std::cout << "Could not set render draw color: " << SDL_GetError() std::cout << "Could not set render draw color: " << SDL_GetError()
......
#include "building.hpp"
#include "common.h"
#include "engine.hpp" #include "engine.hpp"
#include "level.hpp" #include "level.hpp"
#include "spritesheet.hpp" #include "spritesheet.hpp"
#include "tile.hpp" #include "tile.hpp"
#include "window.hpp" #include "window.hpp"
#include <cstddef>
#include <vector> #include <vector>
using namespace advanced_wars; using namespace advanced_wars;
...@@ -43,8 +46,19 @@ int main() { ...@@ -43,8 +46,19 @@ int main() {
tiles.at(18 * 20 + 1) = Tile(TileId::CLIFF_CORNER_BOTTOM_LEFT); tiles.at(18 * 20 + 1) = Tile(TileId::CLIFF_CORNER_BOTTOM_LEFT);
tiles.at(18 * 20 + 18) = Tile(TileId::CLIFF_CORNER_BOTTOM_RIGHT); tiles.at(18 * 20 + 18) = Tile(TileId::CLIFF_CORNER_BOTTOM_RIGHT);
Level level("Osnabrück", 20, 20, tiles, std::vector<Building>(), // Buildings
std::vector<Unit>()); std::vector<Building> buildings;
for (int y = 0; y < 6; y++) {
for (int x = 0; x < 5; x++) {
BuildingId id = static_cast<BuildingId>(x);
Faction faction = static_cast<Faction>(y);
buildings.push_back(Building(3 + x, 3 + 2 * y, id, faction));
}
}
Level level("Osnabrück", 20, 20, tiles, buildings, std::vector<Unit>());
engine.set_scene(level); engine.set_scene(level);
......
#include "spritesheet.hpp" #include "spritesheet.hpp"
#include "SDL_pixels.h" #include "SDL_pixels.h"
#include "SDL_surface.h" #include "SDL_surface.h"
#include "building.hpp"
#include "common.h"
#include "engine.hpp" #include "engine.hpp"
#include "highfive/H5File.hpp" #include "highfive/H5File.hpp"
#include "highfive/highfive.hpp" #include "highfive/highfive.hpp"
...@@ -9,6 +11,7 @@ ...@@ -9,6 +11,7 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <stdexcept> #include <stdexcept>
#include <string>
#include <vector> #include <vector>
namespace advanced_wars { namespace advanced_wars {
...@@ -17,6 +20,7 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) { ...@@ -17,6 +20,7 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
HighFive::File file(path, HighFive::File::ReadOnly); HighFive::File file(path, HighFive::File::ReadOnly);
// Tiles
HighFive::DataSet tile_frames_ds = file.getDataSet("tiles/frames"); HighFive::DataSet tile_frames_ds = file.getDataSet("tiles/frames");
HighFive::DataSet tile_num_frames_ds = file.getDataSet("tiles/num_frames"); HighFive::DataSet tile_num_frames_ds = file.getDataSet("tiles/num_frames");
...@@ -29,14 +33,14 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) { ...@@ -29,14 +33,14 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
tile_num_frames_ds.read(tile_num_frames); tile_num_frames_ds.read(tile_num_frames);
std::vector<uint32_t> buffer(16 * 16 * tile_frames.size(), 0); std::vector<uint32_t> tile_buffer(16 * 16 * tile_frames.size(), 0);
for (size_t n = 0; n < tile_frames.size(); n++) { for (size_t n = 0; n < tile_frames.size(); n++) {
for (size_t y = 0; y < 16; y++) { for (size_t y = 0; y < 16; y++) {
for (size_t x = 0; x < 16; x++) { for (size_t x = 0; x < 16; x++) {
size_t index = (y * tile_frames.size() * 16) + (n * 16 + x); size_t index = (y * tile_frames.size() * 16) + (n * 16 + x);
buffer.at(index) = tile_frames.at(n).at(y).at(x); tile_buffer.at(index) = tile_frames.at(n).at(y).at(x);
} }
} }
} }
...@@ -48,24 +52,76 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) { ...@@ -48,24 +52,76 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
count += tile_num_frames.at(n); count += tile_num_frames.at(n);
} }
texture = SDL_CreateTexture(engine.renderer(), SDL_PIXELFORMAT_RGBA8888, tile_texture = SDL_CreateTexture(engine.renderer(), SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STATIC, count * 16, 16); SDL_TEXTUREACCESS_STATIC, count * 16, 16);
if (texture == nullptr) { if (tile_texture == nullptr) {
throw std::runtime_error("Fehler beim Erstellen der Textur: " + throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Tiles: " +
std::string(SDL_GetError())); std::string(SDL_GetError()));
} }
if (SDL_UpdateTexture(texture, NULL, buffer.data(), if (SDL_UpdateTexture(tile_texture, NULL, tile_buffer.data(),
count * 16 * sizeof(int32_t)) != 0) { count * 16 * sizeof(int32_t)) != 0) {
throw std::runtime_error("Fehler beim updaten der Textur: " + throw std::runtime_error("Fehler beim updaten der Textur für die Tiles: " +
std::string(SDL_GetError())); std::string(SDL_GetError()));
} }
this->tile_width = 16; this->tile_width = 16;
this->tile_height = 16; this->tile_height = 16;
// Buildings
std::vector<std::string> factions(
{"red", "blue", "yellow", "green", "purple", "neutral"});
for (std::string faction : factions) {
HighFive::DataSet buildings_ds = file.getDataSet("buildings/" + faction);
std::vector<std::vector<std::vector<uint32_t>>> buildings_frames;
buildings_ds.read(buildings_frames);
std::vector<uint32_t> building_buffer(32 * 16 * buildings_frames.size(), 0);
for (size_t n = 0; n < buildings_frames.size(); n++) {
for (size_t y = 0; y < 32; y++) {
for (size_t x = 0; x < 16; x++) {
size_t index = (y * buildings_frames.size() * 16) + (n * 16 + x);
building_buffer.at(index) =
buildings_frames.at(n).at(32 - y - 1).at(x);
}
}
}
SDL_Texture *tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
buildings_frames.size() * 16, 32);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
if (tmp == nullptr) {
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Tiles: " +
std::string(SDL_GetError()));
} }
if (SDL_UpdateTexture(tmp, NULL, building_buffer.data(),
buildings_frames.size() * 16 * sizeof(int32_t)) !=
0) {
throw std::runtime_error(
"Fehler beim updaten der Textur für die Tiles: " +
std::string(SDL_GetError()));
}
this->building_textures.push_back(tmp);
}
this->building_width = 16;
this->building_height = 32;
}
// Tiles
int Spritesheet::get_tile_steps(int tile) { return tiles.at(tile).second; } int Spritesheet::get_tile_steps(int tile) { return tiles.at(tile).second; }
int Spritesheet::get_tile_width() { return tile_width; } int Spritesheet::get_tile_width() { return tile_width; }
...@@ -89,10 +145,28 @@ int Spritesheet::render_tile(SDL_Renderer *renderer, int tile, int step, ...@@ -89,10 +145,28 @@ int Spritesheet::render_tile(SDL_Renderer *renderer, int tile, int step,
src.w = tile_width; src.w = tile_width;
src.h = tile_height; src.h = tile_height;
return SDL_RenderCopyEx(renderer, texture, &src, rect, 0, NULL, return SDL_RenderCopyEx(renderer, tile_texture, &src, rect, 0, NULL,
SDL_FLIP_NONE); SDL_FLIP_NONE);
} }
Spritesheet::~Spritesheet() { SDL_DestroyTexture(texture); } // Buildings
int Spritesheet::get_building_width() { return this->building_width; }
int Spritesheet::get_building_height() { return this->building_height; }
int Spritesheet::render_building(SDL_Renderer *renderer, BuildingId id,
Faction faction, SDL_Rect *rect) {
SDL_Rect src;
src.x = static_cast<int>(id) * this->get_building_width();
src.y = 0;
src.w = this->get_building_width();
src.h = this->get_building_height();
return SDL_RenderCopyEx(renderer,
building_textures[static_cast<int>(faction)], &src,
rect, 0, NULL, SDL_FLIP_NONE);
}
Spritesheet::~Spritesheet() { SDL_DestroyTexture(tile_texture); }
} // namespace advanced_wars } // namespace advanced_wars
\ No newline at end of file
#pragma once #pragma once
#include <SDL_render.h> #include "building.hpp"
#include "common.h"
#include <SDL.h> #include <SDL.h>
#include <SDL_render.h>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -29,15 +31,26 @@ public: ...@@ -29,15 +31,26 @@ public:
int render_tile(SDL_Renderer *renderer, int tile, int step, SDL_Rect *rect); int render_tile(SDL_Renderer *renderer, int tile, int step, SDL_Rect *rect);
// Units: TODO
// Buildings: TODO // Buildings: TODO
int get_building_width();
int get_building_height();
int render_building(SDL_Renderer *renderer, BuildingId id, Faction faction,
SDL_Rect *rect);
// Units: TODO
private: private:
SDL_Texture *texture;
// Tiles // Tiles
SDL_Texture *tile_texture;
int tile_width; int tile_width;
int tile_height; int tile_height;
std::vector<std::pair<int, int>> tiles; std::vector<std::pair<int, int>> tiles;
// Buildings
std::vector<SDL_Texture *> building_textures;
int building_width;
int building_height;
}; };
} // namespace advanced_wars } // namespace advanced_wars
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment