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

Refactor Building Rendering from Spritesheet to Buildings themselves

parent a130421a
No related branches found
No related tags found
1 merge request!5Spritesheet
#include "building.hpp"
#include "common.h"
#include "spritesheet.hpp"
namespace advanced_wars
{
Building::Building(int x, int y, BuildingId id, Faction faction)
: x(x), y(y), id(id), faction(faction) {};
void Building::render(Engine &engine, std::vector<SDL_Event> &events)
{
Spritesheet* spritesheet = engine.get_spritesheet();
SDL_Rect src;
src.x = id * spritesheet->get_building_width();
src.y = 0;
src.w = spritesheet->get_building_width();
src.h = spritesheet->get_building_height();
SDL_Rect dst;
dst.x = x * spritesheet->get_tile_width() * 3;
dst.y = (y - 1) * spritesheet->get_tile_height() * 3;
dst.w = spritesheet->get_building_width() * 3;
dst.h = spritesheet->get_building_height() * 3;
SDL_RenderCopyEx(engine.renderer(), spritesheet->get_building_textures()[faction], &src, &dst, 0, NULL, SDL_FLIP_NONE);
}
} // namespace advanced_wars
\ No newline at end of file
#pragma once
#include "common.h"
#include "scene.hpp"
#include "engine.hpp"
namespace advanced_wars {
enum BuildingId {
HEADQUARTER = 0,
......@@ -10,7 +14,7 @@ enum BuildingId {
SATELLITE = 4,
};
class Building {
class Building : public Scene{
public:
Building(int x, int y, BuildingId id, Faction faction);
......@@ -18,4 +22,8 @@ public:
int y;
BuildingId id;
Faction faction;
void render(Engine &engine, std::vector<SDL_Event> &events) override;
};
} // namespace advanced_wars
\ No newline at end of file
......@@ -28,12 +28,6 @@ void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
events.erase(events.begin());
}
size_t idx = 0;
int stage = SDL_GetTicks() / 300;
Spritesheet *spritesheet = engine.get_spritesheet();
// Tiles
for(Tile t : tiles)
{
......@@ -41,15 +35,9 @@ 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);
for (Building building : buildings)
{
building.render(engine, events);
}
// Set background color for renderer
......
......@@ -137,19 +137,11 @@ 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);
std::vector<SDL_Texture*> Spritesheet::get_building_textures() {
return building_textures;
}
Spritesheet::~Spritesheet() { SDL_DestroyTexture(tile_texture); }
} // namespace advanced_wars
\ No newline at end of file
#pragma once
#include "building.hpp"
#include "common.h"
#include <SDL.h>
#include <SDL_render.h>
......@@ -38,8 +37,7 @@ public:
int get_building_height();
int render_building(SDL_Renderer *renderer, BuildingId id, Faction faction,
SDL_Rect *rect);
std::vector<SDL_Texture*> get_building_textures();
// Units: TODO
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment