Skip to content
Snippets Groups Projects
Commit bdd4865d authored by Frederik's avatar Frederik
Browse files

format main with clang-format

parent d844288f
Branches
No related tags found
1 merge request!11Introduce clang rulefile for united style
#include "building.hpp"
#include "spritesheet.hpp"
namespace advanced_wars {
namespace advanced_wars
{
Building::Building(int x, int y, BuildingId id, BuildingFaction faction)
: x(x), y(y), id(id), faction(faction){};
void Building::render(Engine &engine, int scale) {
void Building::render(Engine& engine, int scale)
{
Spritesheet* spritesheet = engine.get_spritesheet();
SDL_Rect src;
......@@ -22,8 +24,7 @@ void Building::render(Engine &engine, int scale) {
dst.h = spritesheet->get_building_height() * scale;
SDL_RenderCopyEx(
engine.renderer(),
spritesheet->get_building_textures()[static_cast<int>(faction)], &src,
engine.renderer(), spritesheet->get_building_textures()[static_cast<int>(faction)], &src,
&dst, 0, NULL, SDL_FLIP_NONE);
}
......
......@@ -3,9 +3,11 @@
#include "engine.hpp"
#include "scene.hpp"
namespace advanced_wars {
namespace advanced_wars
{
enum class BuildingFaction {
enum class BuildingFaction
{
RED = 0,
BLUE = 1,
YELLOW = 2,
......@@ -14,7 +16,8 @@ enum class BuildingFaction {
NEUTRAL = 5,
};
enum class BuildingId {
enum class BuildingId
{
HEADQUARTER = 0,
CITY = 1,
FACTORY = 2,
......@@ -22,7 +25,8 @@ enum class BuildingId {
SATELLITE = 4,
};
class Building {
class Building
{
public:
Building(int x, int y, BuildingId id, BuildingFaction faction);
......
......@@ -2,25 +2,27 @@
#include "spritesheet.hpp"
#include <vector>
namespace advanced_wars {
namespace advanced_wars
{
Effect::Effect(int x, int y, EffectId id, bool repeat)
: x(x), y(y), id(id), repeat(repeat), start(0){
};
void Effect::render(Engine &engine, int scale) {
void Effect::render(Engine& engine, int scale)
{
Spritesheet* spritesheet = engine.get_spritesheet();
if (start == 0) {
if (start == 0)
{
start = engine.get_stage();
}
int step = engine.get_stage() %
spritesheet->get_effect_textures().at(static_cast<int>(id)).second;
int step =
engine.get_stage() % spritesheet->get_effect_textures().at(static_cast<int>(id)).second;
SDL_Rect src;
src.x = step * spritesheet->get_effect_width() +
step * spritesheet->get_effect_height();
src.x = step * spritesheet->get_effect_width() + step * spritesheet->get_effect_height();
src.y = 0;
src.w = spritesheet->get_effect_width();
src.h = spritesheet->get_effect_height();
......@@ -32,16 +34,15 @@ void Effect::render(Engine &engine, int scale) {
dest.h = spritesheet->get_effect_height() * scale;
SDL_RenderCopyEx(
engine.renderer(),
spritesheet->get_effect_textures().at(static_cast<int>(id)).first, &src,
engine.renderer(), spritesheet->get_effect_textures().at(static_cast<int>(id)).first, &src,
&dest, 0, NULL, SDL_FLIP_NONE);
}
bool Effect::is_finished(Engine &engine) {
return !(engine.get_stage() - start <= engine.get_spritesheet()
->get_effect_textures()
.at(static_cast<int>(id))
.second ||
bool Effect::is_finished(Engine& engine)
{
return !(
engine.get_stage() - start <=
engine.get_spritesheet()->get_effect_textures().at(static_cast<int>(id)).second ||
repeat);
}
......
......@@ -2,9 +2,11 @@
#include "engine.hpp"
namespace advanced_wars {
namespace advanced_wars
{
enum class EffectId {
enum class EffectId
{
LAND_EXPLOSION = 0,
AIR_EXPLOSION = 1,
NAVAL_EXPLOSION = 2,
......@@ -12,7 +14,8 @@ enum class EffectId {
SUBMARINE_APPEAR = 4
};
class Effect {
class Effect
{
public:
Effect(int x, int y, EffectId id, bool repeat);
......
......@@ -8,50 +8,71 @@
#include <stdexcept>
#include <vector>
namespace advanced_wars {
namespace advanced_wars
{
Engine::Engine(Window &window) : window(window), quit(false) {
Engine::Engine(Window& window) : window(window), quit(false)
{
this->sdl_renderer =
SDL_CreateRenderer(this->window.sdl_window(), -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
this->sdl_renderer = SDL_CreateRenderer(
this->window.sdl_window(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (sdl_renderer == nullptr) {
throw std::runtime_error("SDL could not generate renderer: " +
std::string(SDL_GetError()));
if (sdl_renderer == nullptr)
{
throw std::runtime_error("SDL could not generate renderer: " + std::string(SDL_GetError()));
}
}
void Engine::set_scene(Scene &scene) { this->scene = &scene; }
void Engine::set_scene(Scene& scene)
{
this->scene = &scene;
}
void Engine::set_spritesheet(Spritesheet &spritesheet) {
void Engine::set_spritesheet(Spritesheet& spritesheet)
{
this->spritesheet = &spritesheet;
}
Spritesheet *Engine::get_spritesheet() { return spritesheet.value(); }
Spritesheet* Engine::get_spritesheet()
{
return spritesheet.value();
}
void Engine::pump() {
void Engine::pump()
{
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
this->quit = true;
} else {
}
else
{
this->events.push_back(e);
}
}
}
bool Engine::exited() { return this->quit; }
bool Engine::exited()
{
return this->quit;
}
int Engine::get_stage() { return this->stage; }
int Engine::get_stage()
{
return this->stage;
}
void Engine::render() {
if (SDL_RenderClear(this->sdl_renderer) != 0) {
throw std::runtime_error("Could not clear renderer: " +
std::string(SDL_GetError()));
void Engine::render()
{
if (SDL_RenderClear(this->sdl_renderer) != 0)
{
throw std::runtime_error("Could not clear renderer: " + std::string(SDL_GetError()));
}
if (!scene.has_value()) {
if (!scene.has_value())
{
return;
}
......@@ -62,9 +83,13 @@ void Engine::render() {
SDL_RenderPresent(this->sdl_renderer);
}
SDL_Renderer *Engine::renderer() { return this->sdl_renderer; }
SDL_Renderer* Engine::renderer()
{
return this->sdl_renderer;
}
Engine::~Engine() {
Engine::~Engine()
{
SDL_DestroyRenderer(sdl_renderer);
IMG_Quit();
SDL_Quit();
......
......@@ -8,12 +8,14 @@
#include <optional>
#include <vector>
namespace advanced_wars {
namespace advanced_wars
{
/**
* @brief The main window of the game
*/
class Engine {
class Engine
{
public:
Engine(Window& window);
......
......@@ -9,111 +9,133 @@
#include <iostream>
#include <string>
namespace advanced_wars {
namespace advanced_wars
{
Level::Level(std::string name, int width, int height, std::vector<Tile> tiles,
std::vector<Building> buildings, std::vector<Unit> units,
std::vector<Effect> effects)
: name(name), width(width), height(height), tiles(tiles), id(0) {
Level::Level(
std::string name, int width, int height, std::vector<Tile> tiles,
std::vector<Building> buildings, std::vector<Unit> units, std::vector<Effect> effects)
: name(name), width(width), height(height), tiles(tiles), id(0)
{
for (Building building : buildings) {
for (Building building : buildings)
{
this->add_building(building);
}
for (Unit unit : units) {
for (Unit unit : units)
{
this->add_unit(unit);
}
for (Effect effect : effects) {
for (Effect effect : effects)
{
this->add_effect(effect);
}
if ((size_t)(width * height) != tiles.size()) {
if ((size_t)(width * height) != tiles.size())
{
throw std::runtime_error("level tile mismatch");
}
};
void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
void Level::render(Engine& engine, std::vector<SDL_Event>& events)
{
const int RENDERING_SCALE = 3;
// Iterate over all events
while (!events.empty()) {
while (!events.empty())
{
events.erase(events.begin());
}
// Tiles
for (Tile &tile : tiles) {
for (Tile& tile : tiles)
{
tile.render(engine, RENDERING_SCALE);
}
// Buildings
for (auto &[id, building] : buildings) {
for (auto& [id, building] : buildings)
{
building.render(engine, RENDERING_SCALE);
}
// Units
for (auto &[id, unit] : units) {
for (auto& [id, unit] : units)
{
unit.render(engine, RENDERING_SCALE);
}
// Effects
std::vector<int> effects_to_remove;
for (auto &[id, effect] : effects) {
if (effect.is_finished(engine)) {
for (auto& [id, effect] : effects)
{
if (effect.is_finished(engine))
{
effects_to_remove.push_back(id);
} else {
}
else
{
effect.render(engine, RENDERING_SCALE);
}
}
// Remove finished effects after iteration
for (int id : effects_to_remove) {
for (int id : effects_to_remove)
{
this->remove_effect(id);
}
// Set background color for renderer
if (SDL_SetRenderDrawColor(engine.renderer(), 255, 0, 0, 0)) {
std::cout << "Could not set render draw color: " << SDL_GetError()
<< std::endl;
if (SDL_SetRenderDrawColor(engine.renderer(), 255, 0, 0, 0))
{
std::cout << "Could not set render draw color: " << SDL_GetError() << std::endl;
}
}
int Level::add_building(Building building) {
int Level::add_building(Building building)
{
buildings.insert({id, building});
id += 1;
return id - 1;
}
Building Level::remove_building(int id) {
Building Level::remove_building(int id)
{
Building value = buildings.at(id);
buildings.erase(id);
return value;
}
int Level::add_unit(Unit unit) {
int Level::add_unit(Unit unit)
{
units.insert({id, unit});
id += 1;
return id - 1;
}
Unit Level::remove_unit(int id) {
Unit Level::remove_unit(int id)
{
Unit value = units.at(id);
units.erase(id);
return value;
}
int Level::add_effect(Effect effect) {
int Level::add_effect(Effect effect)
{
effects.insert({id, effect});
id += 1;
return id - 1;
}
Effect Level::remove_effect(int id) {
Effect Level::remove_effect(int id)
{
Effect value = effects.at(id);
effects.erase(id);
......
......@@ -11,16 +11,18 @@
#include <unordered_map>
#include <vector>
namespace advanced_wars {
namespace advanced_wars
{
/**
* @brief The main window of the game
*/
class Level : public Scene {
class Level : public Scene
{
public:
Level(std::string name, int width, int height, std::vector<Tile> tiles,
std::vector<Building> buildings, std::vector<Unit> units,
std::vector<Effect>);
Level(
std::string name, int width, int height, std::vector<Tile> tiles,
std::vector<Building> buildings, std::vector<Unit> units, std::vector<Effect>);
void render(Engine& engine, std::vector<SDL_Event>& events);
......
......@@ -14,18 +14,19 @@
using namespace advanced_wars;
int main() {
int main()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
throw std::runtime_error("SDL could not initialize: " +
std::string(SDL_GetError()));
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
throw std::runtime_error("SDL could not initialize: " + std::string(SDL_GetError()));
}
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags)) {
if (!(IMG_Init(imgFlags) & imgFlags))
{
throw std::runtime_error(
"SDL_image could not initialize! SDL_image Error: " +
std::string(IMG_GetError()));
"SDL_image could not initialize! SDL_image Error: " + std::string(IMG_GetError()));
}
Window window("Advanced Wars", 960, 960);
......@@ -34,14 +35,17 @@ int main() {
// Construct a level
std::vector<Tile> tiles;
for (int y = 0; y < 20; y++) {
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 20; x++)
{
tiles.push_back(Tile(TileId::PLAIN, x, y));
}
}
// Fill the edges with water
for (size_t n = 0; n < 20; n++) {
for (size_t n = 0; n < 20; n++)
{
// Vertical
tiles.at(n * 20) = Tile(TileId::WATER, 0, n);
tiles.at(n * 20 + 19) = Tile(TileId::WATER, 19, n);
......@@ -51,7 +55,8 @@ int main() {
}
// Make the edges cliffs
for (size_t n = 1; n < 19; n++) {
for (size_t n = 1; n < 19; n++)
{
// Vertical
tiles.at(n * 20 + 1) = Tile(TileId::CLIFF_RIGHT, 1, n);
tiles.at(n * 20 + 18) = Tile(TileId::CLIFF_LEFT, 18, n);
......@@ -70,8 +75,10 @@ int main() {
// Buildings
std::vector<Building> buildings;
for (int y = 0; y < 6; y++) {
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 6; y++)
{
for (int x = 0; x < 5; x++)
{
BuildingId id = static_cast<BuildingId>(x);
BuildingFaction faction = static_cast<BuildingFaction>(y);
......@@ -82,14 +89,18 @@ int main() {
// Units
std::vector<Unit> units;
for (int y = 0; y < 19; y++) {
for (int x = 0; x < 6; x++) {
units.push_back(Unit(x + 9, y + 2, UnitFaction::URED,
static_cast<UnitId>(y), static_cast<UnitState>(x)));
for (int y = 0; y < 19; y++)
{
for (int x = 0; x < 6; x++)
{
units.push_back(Unit(
x + 9, y + 2, UnitFaction::URED, static_cast<UnitId>(y),
static_cast<UnitState>(x)));
}
}
std::vector<Effect> effects({Effect(3, 15, EffectId::LAND_EXPLOSION, false),
std::vector<Effect> effects(
{Effect(3, 15, EffectId::LAND_EXPLOSION, false),
Effect(5, 15, EffectId::AIR_EXPLOSION, true),
Effect(5, 18, EffectId::NAVAL_EXPLOSION, true)});
......@@ -97,12 +108,12 @@ int main() {
engine.set_scene(level);
Spritesheet spritesheet("/media/data/rust/sprite-extractor/spritesheet.h5",
engine);
Spritesheet spritesheet("/media/data/rust/sprite-extractor/spritesheet.h5", engine);
engine.set_spritesheet(spritesheet);
while (!engine.exited()) {
while (!engine.exited())
{
engine.pump();
engine.render();
}
......
......@@ -3,12 +3,14 @@
#include <SDL.h>
#include <vector>
namespace advanced_wars {
namespace advanced_wars
{
// Forward declaration
class Engine;
class Scene {
class Scene
{
public:
virtual void render(Engine& engine, std::vector<SDL_Event>& events) = 0;
};
......
......@@ -10,14 +10,17 @@
#include <string>
#include <vector>
namespace advanced_wars {
namespace advanced_wars
{
Spritesheet::Spritesheet(std::string path, Engine &engine) {
Spritesheet::Spritesheet(std::string path, Engine& engine)
{
HighFive::File file(path, HighFive::File::ReadOnly);
// Tiles
std::vector<std::string> tiles({"plain",
std::vector<std::string> tiles(
{"plain",
"water",
"forest",
"mountain",
......@@ -48,7 +51,8 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
"cliff_inverse_corner_bottom_left",
"cliff_inverse_corner_bottom_right"});
for (size_t tile_idx = 0; tile_idx < tiles.size(); tile_idx++) {
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;
......@@ -56,9 +60,12 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
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 y = 0; y < 16; y++) {
for (size_t x = 0; x < 16; x++) {
for (size_t n = 0; n < tile_frames.size(); n++)
{
for (size_t y = 0; y < 16; y++)
{
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(16 - y - 1).at(x);
......@@ -72,21 +79,20 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
if (tmp == nullptr) {
if (tmp == nullptr)
{
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Units: " +
std::string(SDL_GetError()));
"Fehler beim Erstellen der Textur für die Units: " + std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(tmp, NULL, tile_buffer.data(),
tile_frames.size() * 16 * sizeof(int32_t)) != 0) {
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()));
"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()));
tile_textures.push_back(std::pair<SDL_Texture*, int>(tmp, tile_frames.size()));
}
this->tile_width = 16;
......@@ -96,7 +102,8 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
std::vector<std::string> building_factions(
{"red", "blue", "yellow", "green", "purple", "neutral"});
for (std::string faction : building_factions) {
for (std::string faction : building_factions)
{
HighFive::DataSet buildings_ds = file.getDataSet("buildings/" + faction);
std::vector<std::vector<std::vector<uint32_t>>> buildings_frames;
......@@ -105,13 +112,15 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
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++) {
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);
building_buffer.at(index) = buildings_frames.at(n).at(32 - y - 1).at(x);
}
}
}
......@@ -122,18 +131,19 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
if (tmp == nullptr) {
if (tmp == nullptr)
{
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Buildings: " +
std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(tmp, NULL, building_buffer.data(),
buildings_frames.size() * 16 * sizeof(int32_t)) !=
0) {
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 Buildings: " +
std::string(SDL_GetError()));
"Fehler beim updaten der Textur für die Buildings: " + std::string(SDL_GetError()));
}
this->building_textures.push_back(tmp);
......@@ -143,35 +153,32 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
this->building_height = 32;
// Units
std::vector<std::string> unit_factions(
{"red", "blue", "green", "yellow", "purple"});
std::vector<std::string> unit_factions({"red", "blue", "green", "yellow", "purple"});
std::vector<std::string> units(
{"infantery", "mechanized_infantery", "recon", "medium_tank",
"heavy_tank", "neo_tank", "apc", "anti_air_tank", "artillery",
"rocket_artillery", "anti_air_missile_launcher", "fighter", "bomber",
"battle_helicopter", "transport_helicopter", "battleship", "cruiser",
{"infantery", "mechanized_infantery", "recon", "medium_tank", "heavy_tank", "neo_tank",
"apc", "anti_air_tank", "artillery", "rocket_artillery", "anti_air_missile_launcher",
"fighter", "bomber", "battle_helicopter", "transport_helicopter", "battleship", "cruiser",
"lander", "submarine"});
std::vector<std::string> unit_states({"idle", "unavailable"});
std::vector<std::string> unit_movement_states(
{"left", "right", "down", "up"});
std::vector<std::string> unit_movement_states({"left", "right", "down", "up"});
for (size_t faction_idx = 0; faction_idx < unit_factions.size();
faction_idx++) {
for (size_t faction_idx = 0; faction_idx < unit_factions.size(); faction_idx++)
{
std::string faction = unit_factions.at(faction_idx);
// Create entry for units for in a faction
unit_textures.push_back(
std::vector<std::vector<std::pair<SDL_Texture *, int>>>());
unit_textures.push_back(std::vector<std::vector<std::pair<SDL_Texture*, int>>>());
for (size_t unit_idx = 0; unit_idx < units.size(); unit_idx++) {
for (size_t unit_idx = 0; unit_idx < units.size(); unit_idx++)
{
std::string unit = units.at(unit_idx);
// Create entry for states for a unit
unit_textures.at(faction_idx)
.push_back(std::vector<std::pair<SDL_Texture *, int>>());
unit_textures.at(faction_idx).push_back(std::vector<std::pair<SDL_Texture*, int>>());
for (size_t state_idx = 0; state_idx < unit_states.size(); state_idx++) {
for (size_t state_idx = 0; state_idx < unit_states.size(); state_idx++)
{
std::string unit_state = unit_states.at(state_idx);
HighFive::DataSet units_ds =
......@@ -182,9 +189,12 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
std::vector<uint32_t> unit_buffer(16 * 16 * unit_frames.size(), 0);
for (size_t n = 0; n < unit_frames.size(); n++) {
for (size_t y = 0; y < 16; y++) {
for (size_t x = 0; x < 16; x++) {
for (size_t n = 0; n < unit_frames.size(); n++)
{
for (size_t y = 0; y < 16; y++)
{
for (size_t x = 0; x < 16; x++)
{
size_t index = (y * unit_frames.size() * 16) + (n * 16 + x);
unit_buffer.at(index) = unit_frames.at(n).at(16 - y - 1).at(x);
......@@ -193,19 +203,22 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
}
SDL_Texture* tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STATIC, unit_frames.size() * 16, 16);
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
unit_frames.size() * 16, 16);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
if (tmp == nullptr) {
if (tmp == nullptr)
{
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Units: " +
std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(tmp, NULL, unit_buffer.data(),
unit_frames.size() * 16 * sizeof(int32_t)) != 0) {
if (SDL_UpdateTexture(
tmp, NULL, unit_buffer.data(), unit_frames.size() * 16 * sizeof(int32_t)) !=
0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Units: " +
std::string(SDL_GetError()));
......@@ -216,11 +229,10 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
.push_back(std::pair<SDL_Texture*, int>(tmp, unit_frames.size()));
}
for (size_t movement_state_idx = 0;
movement_state_idx < unit_movement_states.size();
movement_state_idx++) {
std::string movement_state =
unit_movement_states.at(movement_state_idx);
for (size_t movement_state_idx = 0; movement_state_idx < unit_movement_states.size();
movement_state_idx++)
{
std::string movement_state = unit_movement_states.at(movement_state_idx);
HighFive::DataSet units_ds = file.getDataSet(
"units/" + faction + "/" + unit + "/movement/" + movement_state);
......@@ -230,9 +242,12 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
std::vector<uint32_t> unit_buffer(24 * 24 * unit_frames.size(), 0);
for (size_t n = 0; n < unit_frames.size(); n++) {
for (size_t y = 0; y < 24; y++) {
for (size_t x = 0; x < 24; x++) {
for (size_t n = 0; n < unit_frames.size(); n++)
{
for (size_t y = 0; y < 24; y++)
{
for (size_t x = 0; x < 24; x++)
{
size_t index = (y * unit_frames.size() * 24) + (n * 24 + x);
unit_buffer.at(index) = unit_frames.at(n).at(24 - y - 1).at(x);
......@@ -241,19 +256,22 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
}
SDL_Texture* tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STATIC, unit_frames.size() * 24, 24);
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
unit_frames.size() * 24, 24);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
if (tmp == nullptr) {
if (tmp == nullptr)
{
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Units: " +
std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(tmp, NULL, unit_buffer.data(),
unit_frames.size() * 24 * sizeof(int32_t)) != 0) {
if (SDL_UpdateTexture(
tmp, NULL, unit_buffer.data(), unit_frames.size() * 24 * sizeof(int32_t)) !=
0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Units: " +
std::string(SDL_GetError()));
......@@ -272,22 +290,25 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
this->unit_moving_height = 24;
// Effects
std::vector<std::string> effects({"land_explosion", "air_explosion",
"naval_explosion", "submarine_hide",
std::vector<std::string> effects(
{"land_explosion", "air_explosion", "naval_explosion", "submarine_hide",
"submarine_appear"});
for (size_t effect_idx = 0; effect_idx < effects.size(); effect_idx++) {
HighFive::DataSet effect_ds =
file.getDataSet("effects/" + effects[effect_idx]);
for (size_t effect_idx = 0; effect_idx < effects.size(); effect_idx++)
{
HighFive::DataSet effect_ds = file.getDataSet("effects/" + effects[effect_idx]);
std::vector<std::vector<std::vector<uint32_t>>> effect_frames;
effect_ds.read(effect_frames);
std::vector<uint32_t> effect_buffer(32 * 32 * effect_frames.size(), 0);
for (size_t n = 0; n < effect_frames.size(); n++) {
for (size_t y = 0; y < 32; y++) {
for (size_t x = 0; x < 32; x++) {
for (size_t n = 0; n < effect_frames.size(); n++)
{
for (size_t y = 0; y < 32; y++)
{
for (size_t x = 0; x < 32; x++)
{
size_t index = (y * effect_frames.size() * 32) + (n * 32 + x);
effect_buffer.at(index) = effect_frames.at(n).at(32 - y - 1).at(x);
......@@ -301,21 +322,20 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
if (tmp == nullptr) {
if (tmp == nullptr)
{
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Effects: " +
std::string(SDL_GetError()));
"Fehler beim Erstellen der Textur für die Effects: " + std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(tmp, NULL, effect_buffer.data(),
effect_frames.size() * 32 * sizeof(int32_t)) != 0) {
if (SDL_UpdateTexture(
tmp, NULL, effect_buffer.data(), effect_frames.size() * 32 * sizeof(int32_t)) != 0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Tiles: " +
std::string(SDL_GetError()));
"Fehler beim updaten der Textur für die Tiles: " + std::string(SDL_GetError()));
}
effect_textures.push_back(
std::pair<SDL_Texture *, int>(tmp, effect_frames.size()));
effect_textures.push_back(std::pair<SDL_Texture*, int>(tmp, effect_frames.size()));
}
this->effect_width = 32;
......@@ -324,59 +344,98 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
// Tiles
int Spritesheet::get_tile_width() { return tile_width; }
int Spritesheet::get_tile_width()
{
return tile_width;
}
int Spritesheet::get_tile_height() { return tile_height; }
int Spritesheet::get_tile_height()
{
return tile_height;
}
std::vector<std::pair<SDL_Texture *, int>> &Spritesheet::get_tile_textures() {
std::vector<std::pair<SDL_Texture*, int>>& Spritesheet::get_tile_textures()
{
return tile_textures;
}
// Buildings
int Spritesheet::get_building_width() { return this->building_width; }
int Spritesheet::get_building_width()
{
return this->building_width;
}
int Spritesheet::get_building_height() { return this->building_height; }
int Spritesheet::get_building_height()
{
return this->building_height;
}
std::vector<SDL_Texture *> &Spritesheet::get_building_textures() {
std::vector<SDL_Texture*>& Spritesheet::get_building_textures()
{
return building_textures;
}
// Units
int Spritesheet::get_unit_width() { return this->unit_width; }
int Spritesheet::get_unit_width()
{
return this->unit_width;
}
int Spritesheet::get_unit_height() { return this->unit_height; }
int Spritesheet::get_unit_height()
{
return this->unit_height;
}
int Spritesheet::get_unit_moving_width() { return this->unit_moving_width; }
int Spritesheet::get_unit_moving_width()
{
return this->unit_moving_width;
}
int Spritesheet::get_unit_moving_height() { return this->unit_moving_height; }
int Spritesheet::get_unit_moving_height()
{
return this->unit_moving_height;
}
std::vector<std::vector<std::vector<std::pair<SDL_Texture*, int>>>>&
Spritesheet::get_unit_textures() {
Spritesheet::get_unit_textures()
{
return this->unit_textures;
}
// Effects
int Spritesheet::get_effect_width() { return this->effect_width; }
int Spritesheet::get_effect_width()
{
return this->effect_width;
}
int Spritesheet::get_effect_height() { return this->effect_height; }
int Spritesheet::get_effect_height()
{
return this->effect_height;
}
std::vector<std::pair<SDL_Texture *, int>> &Spritesheet::get_effect_textures() {
std::vector<std::pair<SDL_Texture*, int>>& Spritesheet::get_effect_textures()
{
return this->effect_textures;
}
Spritesheet::~Spritesheet() {
for (std::pair<SDL_Texture *, int> tile_texture : tile_textures) {
Spritesheet::~Spritesheet()
{
for (std::pair<SDL_Texture*, int> tile_texture : tile_textures)
{
SDL_DestroyTexture(tile_texture.first);
}
for (SDL_Texture *building_texture : building_textures) {
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) {
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);
}
}
......
......@@ -5,12 +5,14 @@
#include <string>
#include <vector>
namespace advanced_wars {
namespace advanced_wars
{
// Forward declaration
class Engine;
class Spritesheet {
class Spritesheet
{
public:
Spritesheet(std::string path, Engine& engine);
......@@ -44,8 +46,7 @@ public:
int get_unit_moving_height();
std::vector<std::vector<std::vector<std::pair<SDL_Texture *, int>>>> &
get_unit_textures();
std::vector<std::vector<std::vector<std::pair<SDL_Texture*, int>>>>& get_unit_textures();
// Effects
int get_effect_width();
......@@ -66,8 +67,7 @@ private:
int building_height;
// Units
std::vector<std::vector<std::vector<std::pair<SDL_Texture *, int>>>>
unit_textures;
std::vector<std::vector<std::vector<std::pair<SDL_Texture*, int>>>> unit_textures;
int unit_width;
int unit_height;
int unit_moving_width;
......
......@@ -2,18 +2,20 @@
#include "spritesheet.hpp"
#include <vector>
namespace advanced_wars {
namespace advanced_wars
{
Tile::Tile(TileId id, int x, int y)
: id(id), x(x), y(y){
};
void Tile::render(Engine &engine, int scale) {
void Tile::render(Engine& engine, int scale)
{
Spritesheet* spritesheet = engine.get_spritesheet();
int step = engine.get_stage() %
spritesheet->get_tile_textures().at(static_cast<int>(id)).second;
int step =
engine.get_stage() % spritesheet->get_tile_textures().at(static_cast<int>(id)).second;
SDL_Rect src;
src.x = step * spritesheet->get_tile_width();
......@@ -28,8 +30,7 @@ void Tile::render(Engine &engine, int scale) {
dest.h = spritesheet->get_tile_height() * scale;
SDL_RenderCopyEx(
engine.renderer(),
spritesheet->get_tile_textures().at(static_cast<int>(id)).first, &src,
engine.renderer(), spritesheet->get_tile_textures().at(static_cast<int>(id)).first, &src,
&dest, 0, NULL, SDL_FLIP_NONE);
}
......
......@@ -3,9 +3,11 @@
#include "engine.hpp"
#include "scene.hpp"
namespace advanced_wars {
namespace advanced_wars
{
enum class TileId {
enum class TileId
{
PLAIN = 0,
WATER = 1,
FOREST = 2,
......@@ -38,7 +40,8 @@ enum class TileId {
CLIFF_INVERSE_CORNER_BOTTOM_RIGHT = 29,
};
class Tile {
class Tile
{
public:
Tile(TileId id, int x, int y);
TileId id;
......
#include "unit.hpp"
namespace advanced_wars {
namespace advanced_wars
{
Unit::Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state)
: x(x), y(y), faction(faction), id(id), state(state){
};
void Unit::render(Engine &engine, int scale) {
void Unit::render(Engine& engine, int scale)
{
Spritesheet* spritesheet = engine.get_spritesheet();
int step = engine.get_stage() % spritesheet->get_unit_textures()
......@@ -16,7 +18,8 @@ void Unit::render(Engine &engine, int scale) {
.at(static_cast<int>(state))
.second;
if (state == UnitState::IDLE || state == UnitState::UNAVAILABLE) {
if (state == UnitState::IDLE || state == UnitState::UNAVAILABLE)
{
SDL_Rect src;
src.x = step * spritesheet->get_unit_width();
......@@ -30,14 +33,17 @@ void Unit::render(Engine &engine, int scale) {
dst.w = spritesheet->get_unit_width() * scale;
dst.h = spritesheet->get_unit_height() * scale;
SDL_RenderCopyEx(engine.renderer(),
SDL_RenderCopyEx(
engine.renderer(),
spritesheet->get_unit_textures()
.at(static_cast<int>(faction))
.at(static_cast<int>(id))
.at(static_cast<int>(state))
.first,
&src, &dst, 0, NULL, SDL_FLIP_NONE);
} else {
}
else
{
// The moving states have a resolution of 24x24 instead of 16x16 and need to
// be handled separately
SDL_Rect src;
......@@ -52,7 +58,8 @@ void Unit::render(Engine &engine, int scale) {
dst.w = spritesheet->get_unit_moving_width() * scale;
dst.h = spritesheet->get_unit_moving_height() * scale;
SDL_RenderCopyEx(engine.renderer(),
SDL_RenderCopyEx(
engine.renderer(),
spritesheet->get_unit_textures()
.at(static_cast<int>(faction))
.at(static_cast<int>(id))
......
......@@ -2,9 +2,11 @@
#include "engine.hpp"
namespace advanced_wars {
namespace advanced_wars
{
enum class UnitFaction {
enum class UnitFaction
{
URED = 0,
UBLUE = 1,
UGREEN = 2,
......@@ -12,7 +14,8 @@ enum class UnitFaction {
UPURPLE = 4,
};
enum class UnitId {
enum class UnitId
{
INFANTERY = 0,
MECHANIZED_INFANTERY = 1,
RECON = 2,
......@@ -34,7 +37,8 @@ enum class UnitId {
SUBMARINE = 18,
};
enum class UnitState {
enum class UnitState
{
IDLE = 0,
UNAVAILABLE = 1,
MOVEMENTLEFT = 2,
......@@ -43,7 +47,8 @@ enum class UnitState {
MOVEMENTUP = 5,
};
enum class MovementType {
enum class MovementType
{
FOOT = 0,
TIRES = 1,
TREAD = 2,
......@@ -52,7 +57,8 @@ enum class MovementType {
LANDER = 5,
};
class Unit {
class Unit
{
public:
Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state);
......
#include "window.hpp"
#include <stdexcept>
namespace advanced_wars {
namespace advanced_wars
{
Window::Window(std::string title, int w, int h) {
Window::Window(std::string title, int w, int h)
{
/// Init width and height
width = w;
height = h;
// Generate SDL main window
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, width, height,
window = SDL_CreateWindow(
title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height,
SDL_WINDOW_SHOWN);
if (window == nullptr) {
throw std::runtime_error("SDL window could not be generated: " +
std::string(SDL_GetError()));
if (window == nullptr)
{
throw std::runtime_error(
"SDL window could not be generated: " + std::string(SDL_GetError()));
}
}
int Window::w() { return width; }
int Window::w()
{
return width;
}
int Window::h() { return height; }
int Window::h()
{
return height;
}
SDL_Window *Window::sdl_window() { return window; }
SDL_Window* Window::sdl_window()
{
return window;
}
Window::~Window() {
if (window) {
Window::~Window()
{
if (window)
{
SDL_DestroyWindow(window);
window = nullptr;
}
......
......@@ -3,12 +3,14 @@
#include <SDL.h>
#include <string>
namespace advanced_wars {
namespace advanced_wars
{
/**
* @brief The main window of the game
*/
class Window {
class Window
{
public:
/***
* Creates a main window with given \ref title, width \ref w and height \ref h
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment