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

add support for effects

parent cad058de
No related branches found
No related tags found
1 merge request!5Spritesheet
#include "effect.hpp"
#include "spritesheet.hpp"
#include <vector>
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) {
Spritesheet *spritesheet = engine.get_spritesheet();
if (start == 0) {
start = engine.get_stage();
}
int step = engine.get_stage() %
spritesheet->get_effect_textures().at(static_cast<int>(id)).second;
if (engine.get_stage() - start <=
spritesheet->get_effect_textures().at(static_cast<int>(id)).second ||
repeat) {
SDL_Rect src;
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();
SDL_Rect dest;
dest.x = (x * spritesheet->get_tile_width() * scale) - 8;
dest.y = (y * spritesheet->get_tile_height() * scale) - 8;
dest.w = spritesheet->get_effect_width() * scale;
dest.h = spritesheet->get_effect_height() * scale;
SDL_RenderCopyEx(
engine.renderer(),
spritesheet->get_effect_textures().at(static_cast<int>(id)).first, &src,
&dest, 0, NULL, SDL_FLIP_NONE);
}
}
} // namespace advanced_wars
\ No newline at end of file
#pragma once
#include "engine.hpp"
namespace advanced_wars {
enum EffectId {
LAND_EXPLOSION = 0,
AIR_EXPLOSION = 1,
NAVAL_EXPLOSION = 2,
SUBMARINE_HIDE = 3,
SUBMARINE_APPEAR = 4
};
class Effect {
public:
Effect(int x, int y, EffectId id, bool repeat);
void render(Engine &engine, int scale);
int x;
int y;
EffectId id;
bool repeat;
int start;
};
} // namespace advanced_wars
\ No newline at end of file
#include "engine.hpp"
#include <SDL_render.h>
#include "scene.hpp"
#include "spritesheet.hpp"
#include "window.hpp"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_render.h>
#include <stdexcept>
#include <vector>
......
#pragma once
#include <SDL_render.h>
#include "scene.hpp"
#include "spritesheet.hpp"
#include "window.hpp"
#include <SDL.h>
#include <SDL_render.h>
#include <optional>
#include <vector>
......
#include "level.hpp"
#include "SDL_error.h"
#include "building.hpp"
#include "effect.hpp"
#include "engine.hpp"
#include "spritesheet.hpp"
#include "unit.hpp"
......@@ -11,9 +12,10 @@
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<Building> buildings, std::vector<Unit> units,
std::vector<Effect> effects)
: name(name), width(width), height(height), tiles(tiles),
buildings(buildings), units(units) {
buildings(buildings), units(units), effects(effects) {
if ((size_t)(width * height) != tiles.size()) {
throw std::runtime_error("level tile mismatch");
......@@ -29,20 +31,25 @@ void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
}
// Tiles
for (Tile tile : tiles) {
for (Tile &tile : tiles) {
tile.render(engine, RENDERING_SCALE);
}
// Buildings
for (Building building : buildings) {
for (Building &building : buildings) {
building.render(engine, RENDERING_SCALE);
}
// Units
for (Unit unit : units) {
for (Unit &unit : units) {
unit.render(engine, RENDERING_SCALE);
}
// Effects
for (Effect &effect : effects) {
effect.render(engine, RENDERING_SCALE);
}
// Set background color for renderer
if (SDL_SetRenderDrawColor(engine.renderer(), 255, 0, 0, 0)) {
std::cout << "Could not set render draw color: " << SDL_GetError()
......
#pragma once
#include "building.hpp"
#include "effect.hpp"
#include "engine.hpp"
#include "scene.hpp"
#include "tile.hpp"
......@@ -17,7 +18,8 @@ namespace advanced_wars {
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<Building> buildings, std::vector<Unit> units,
std::vector<Effect>);
void render(Engine &engine, std::vector<SDL_Event> &events);
......@@ -28,6 +30,7 @@ private:
std::vector<Tile> tiles;
std::vector<Building> buildings;
std::vector<Unit> units;
std::vector<Effect> effects;
};
} // namespace advanced_wars
#include "building.hpp"
#include "effect.hpp"
#include "engine.hpp"
#include "level.hpp"
#include "spritesheet.hpp"
......@@ -64,7 +65,6 @@ int main() {
}
// Units
std::vector<Unit> units;
for (int y = 0; y < 19; y++) {
......@@ -74,7 +74,11 @@ int main() {
}
}
Level level("Osnabrück", 20, 20, tiles, buildings, units);
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)});
Level level("Osnabrück", 20, 20, tiles, buildings, units, effects);
engine.set_scene(level);
......
......@@ -244,6 +244,56 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
this->unit_height = 16;
this->unit_moving_width = 24;
this->unit_moving_height = 24;
// Effects
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]);
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++) {
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);
}
}
}
SDL_Texture *tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
effect_frames.size() * 32, 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, 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()));
}
effect_textures.push_back(
std::pair<SDL_Texture *, int>(tmp, effect_frames.size()));
}
this->effect_width = 32;
this->effect_height = 32;
}
// Tiles
......@@ -280,6 +330,15 @@ Spritesheet::get_unit_textures() {
return this->unit_textures;
}
// Effects
int Spritesheet::get_effect_width() { return this->effect_width; }
int Spritesheet::get_effect_height() { return this->effect_height; }
std::vector<std::pair<SDL_Texture *, int>> &Spritesheet::get_effect_textures() {
return this->effect_textures;
}
Spritesheet::~Spritesheet() { SDL_DestroyTexture(tile_texture); }
} // namespace advanced_wars
\ No newline at end of file
......@@ -50,6 +50,13 @@ public:
std::vector<std::vector<std::vector<std::pair<SDL_Texture *, int>>>> &
get_unit_textures();
// Effects
int get_effect_width();
int get_effect_height();
std::vector<std::pair<SDL_Texture *, int>> &get_effect_textures();
private:
// Tiles
SDL_Texture *tile_texture;
......@@ -69,5 +76,10 @@ private:
int unit_height;
int unit_moving_width;
int unit_moving_height;
// Effects
std::vector<std::pair<SDL_Texture *, int>> effect_textures;
int effect_width;
int effect_height;
};
} // namespace advanced_wars
#include "unit.hpp"
#include <iostream>
namespace advanced_wars {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment