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

fix problems arising from merge

parent 16795c19
Branches
No related tags found
No related merge requests found
......@@ -7,9 +7,9 @@ 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();
Spritesheet* spritesheet = engine->get_spritesheet();
SDL_Rect src;
src.x = static_cast<int>(id) * spritesheet->get_building_width();
......@@ -24,7 +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);
}
......
......@@ -35,7 +35,7 @@ class Building
BuildingId id;
BuildingFaction faction;
void render(Engine& engine, int scale);
void render(Engine* engine, int scale);
};
} // namespace advanced_wars
\ No newline at end of file
......@@ -10,16 +10,16 @@ Effect::Effect(int x, int y, EffectId id, bool repeat)
};
void Effect::render(Engine& engine, int scale)
void Effect::render(Engine* engine, int scale)
{
Spritesheet* spritesheet = engine.get_spritesheet();
Spritesheet* spritesheet = engine->get_spritesheet();
if (start == 0)
{
start = engine.get_stage();
start = engine->get_stage();
}
int step =
engine.get_stage() % spritesheet->get_effect_textures().at(static_cast<int>(id)).second;
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();
......@@ -34,15 +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)
bool Effect::is_finished(Engine* engine)
{
return !(
engine.get_stage() - start <=
engine.get_spritesheet()->get_effect_textures().at(static_cast<int>(id)).second ||
engine->get_stage() - start <=
engine->get_spritesheet()->get_effect_textures().at(static_cast<int>(id)).second ||
repeat);
}
......
......@@ -19,9 +19,9 @@ class Effect
public:
Effect(int x, int y, EffectId id, bool repeat);
void render(Engine& engine, int scale);
void render(Engine* engine, int scale);
bool is_finished(Engine& engine);
bool is_finished(Engine* engine);
int x;
int y;
......
#include "engine.hpp"
#include "SDL_events.h"
#include "SDL_timer.h"
#include "scene.hpp"
#include "spritesheet.hpp"
#include "window.hpp"
......@@ -20,40 +21,36 @@ Engine::Engine(Window& window) : window(window), quit(false)
this->sdl_renderer = SDL_CreateRenderer(
this->window.sdl_window(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags)) {
throw std::runtime_error(
"SDL_image could not initialize! SDL_image Error: " +
std::string(IMG_GetError()));
}
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()));
}
}
std::deque<SDL_Event> &Engine::events() { return this->_events; }
std::deque<SDL_Event>& Engine::events()
{
return this->_events;
}
void Engine::push_scene(std::shared_ptr<Scene> scene) {
void Engine::push_scene(std::shared_ptr<Scene> scene)
{
this->scenes.push_back(scene);
}
void Engine::return_to_menu() {
void Engine::return_to_menu()
{
// TODO: discuss if we outsource this to a separate function
// clear everything except the first scene
while (this->scenes.size() > 1) {
while (this->scenes.size() > 1)
{
this->scenes.pop_back();
}
}
std::optional<std::shared_ptr<Scene>> Engine::pop_scene() {
if (this->scenes.empty()) {
std::optional<std::shared_ptr<Scene>> Engine::pop_scene()
{
if (this->scenes.empty())
{
return std::nullopt;
}
std::shared_ptr<Scene> tmp = scenes.back();
......@@ -62,56 +59,11 @@ std::optional<std::shared_ptr<Scene>> Engine::pop_scene() {
return tmp;
}
void Engine::set_spritesheet(Spritesheet spritesheet) {
this->spritesheet = spritesheet;
}
void Engine::pump() {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
this->quit = true;
} else {
this->_events.push_back(e);
}
}
void Engine::exit() { this->quit = true; }
bool Engine::exited() { return this->quit; }
void Engine::render() {
if (SDL_RenderClear(this->sdl_renderer) != 0) {
throw std::runtime_error("Could not clear renderer: " +
std::string(SDL_GetError()));
}
if (scenes.empty()) {
SDL_RenderPresent(this->sdl_renderer);
return;
}
std::shared_ptr<Scene> currentScene = scenes.back();
if (!currentScene) {
SDL_RenderPresent(this->sdl_renderer);
return;
}
currentScene->render(this);
SDL_RenderPresent(this->sdl_renderer);
}
void Engine::set_spritesheet(Spritesheet& spritesheet)
{
this->spritesheet = &spritesheet;
}
Spritesheet* Engine::get_spritesheet()
{
return spritesheet.value();
}
void Engine::pump()
{
SDL_Event e;
......@@ -123,19 +75,19 @@ void Engine::pump()
}
else
{
this->events.push_back(e);
this->_events.push_back(e);
}
}
}
bool Engine::exited()
void Engine::exit()
{
return this->quit;
this->quit = true;
}
int Engine::get_stage()
bool Engine::exited()
{
return this->stage;
return this->quit;
}
void Engine::render()
......@@ -145,18 +97,34 @@ void Engine::render()
throw std::runtime_error("Could not clear renderer: " + std::string(SDL_GetError()));
}
if (!scene.has_value())
if (scenes.empty())
{
SDL_RenderPresent(this->sdl_renderer);
return;
}
stage = SDL_GetTicks() / 300;
std::shared_ptr<Scene> currentScene = scenes.back();
if (!currentScene)
{
SDL_RenderPresent(this->sdl_renderer);
return;
}
this->scene.value()->render(*this, this->events);
currentScene->render(this);
SDL_RenderPresent(this->sdl_renderer);
}
int Engine::get_stage()
{
return SDL_GetTicks() / 300;
}
Spritesheet* Engine::get_spritesheet()
{
return spritesheet.value();
}
SDL_Renderer* Engine::renderer()
{
return this->sdl_renderer;
......
......@@ -56,9 +56,9 @@ class Engine
private:
Window& window;
SDL_Renderer* sdl_renderer;
std::optional<Scene*> scene;
std::vector<std::shared_ptr<Scene>> scenes;
std::optional<Spritesheet*> spritesheet;
std::vector<SDL_Event> events;
std::deque<SDL_Event> _events;
bool quit;
int stage;
};
......
#include "level.hpp"
#include "SDL_error.h"
#include "building.hpp"
#include "effect.hpp"
#include "engine.hpp"
#include "spritesheet.hpp"
#include "ui/contextmenu.hpp"
#include "ui/pausemenu.hpp"
#include "unit.hpp"
#include <SDL.h>
#include <iostream>
#include <string>
#include "ui/pausemenu.hpp"
#include "ui/contextmenu.hpp"
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), context_menu(ContextMenu()), context_menu_active(false)
: name(name), width(width), height(height), tiles(tiles), context_menu(ContextMenu()),
context_menu_active(false), id(0)
{
context_menu.setOptions({"Move", "Info", "Wait"});
......@@ -42,12 +43,12 @@ Level::Level(
}
};
void Level::render(Engine& engine, std::vector<SDL_Event>& events)
void Level::render(Engine* engine)
{
const int RENDERING_SCALE = 3;
// Iterate over all events
while (!events.empty())
while (!engine->events().empty())
{
handleEvent(engine, engine->events().at(0));
engine->events().pop_front();
......@@ -91,53 +92,53 @@ void Level::render(Engine& engine, std::vector<SDL_Event>& events)
this->remove_effect(id);
}
// Set background color for renderer
if (SDL_SetRenderDrawColor(engine.renderer(), 255, 0, 0, 0))
if (context_menu_active)
{
std::cout << "Could not set render draw color: " << SDL_GetError() << std::endl;
}
SDL_RenderClear(engine->renderer());
if(context_menu_active) {
context_menu.render(engine);
}
}
void Level::handleEvent(Engine *engine, SDL_Event &event) {
void Level::handleEvent(Engine* engine, SDL_Event& event)
{
// Handle events for the level
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_ESCAPE) {
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
{
// Pause the game
std::cout << "Pausing game..." << std::endl;
SDL_Texture *currentTexture = SDL_CreateTexture(engine->renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 800, 600);
SDL_Texture* currentTexture = SDL_CreateTexture(
engine->renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 800, 600);
PauseMenu pauseMenu(0, currentTexture);
engine->push_scene(std::make_shared<PauseMenu>(pauseMenu));
}
if(context_menu_active){
if(event.key.keysym.sym == SDLK_DOWN) {
if (context_menu_active)
{
if (event.key.keysym.sym == SDLK_DOWN)
{
context_menu.handleEvent(event);
}
if(event.key.keysym.sym == SDLK_UP) {
if (event.key.keysym.sym == SDLK_UP)
{
context_menu.handleEvent(event);
}
if(event.key.keysym.sym == SDLK_RETURN) {
if(context_menu.getSelectedOption() == "Wait"){
if (event.key.keysym.sym == SDLK_RETURN)
{
if (context_menu.getSelectedOption() == "Wait")
{
context_menu_active = false;
}
}
}
}
if(event.type == SDL_MOUSEBUTTONDOWN) {
if (event.type == SDL_MOUSEBUTTONDOWN)
{
context_menu.update(event.button.x, event.button.y);
context_menu_active = true;
}
}
int Level::add_building(Building building)
{
buildings.insert({id, building});
......@@ -186,5 +187,4 @@ Effect Level::remove_effect(int id)
return value;
}
} // namespace advanced_wars
\ No newline at end of file
......@@ -5,12 +5,12 @@
#include "engine.hpp"
#include "scene.hpp"
#include "tile.hpp"
#include "ui/contextmenu.hpp"
#include "unit.hpp"
#include <SDL.h>
#include <string>
#include <unordered_map>
#include <vector>
#include "ui/contextmenu.hpp"
namespace advanced_wars
{
......@@ -25,7 +25,7 @@ class Level : public Scene
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);
void render(Engine* engine);
void handleEvent(Engine* engine, SDL_Event& event);
......
#include "building.hpp"
#include "effect.hpp"
#include "engine.hpp"
#include "spritesheet.hpp"
#include "ui/menu.hpp"
#include "ui/contextmenu.hpp"
#include <memory>
#include "tile.hpp"
#include "unit.hpp"
#include "ui/menu.hpp"
#include "window.hpp"
#include <SDL2/SDL.h>
#include <SDL_image.h>
#include <cstddef>
#include <memory>
#include <stdexcept>
#include <vector>
......@@ -34,13 +29,15 @@ int main()
Window window("Advanced Wars", 960, 960);
Engine engine(window);
// render main menu
Spritesheet spritesheet("/media/data/rust/sprite-extractor/spritesheet.h5", engine);
engine.set_spritesheet(spritesheet);
std::shared_ptr<Menu> menu = std::make_shared<Menu>(0);
std::shared_ptr<ContextMenu> context_menu = std::make_shared<ContextMenu>();
context_menu->setOptions({"Move", "Info", "Wait"});
std::string basePath = SDL_GetBasePath();
std::string relativePath = "assets/main_background.png";
std::string fullPath = basePath + relativePath;
......@@ -48,87 +45,6 @@ int main()
engine.push_scene(menu);
// Construct a level
std::vector<Tile> tiles;
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 20; x++)
{
tiles.push_back(Tile(TileId::PLAIN, x, y));
}
}
engine.set_spritesheet(spritesheet);
// Fill the edges with water
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);
// Horizontal
tiles.at(n) = Tile(TileId::WATER, n, 0);
tiles.at(19 * 20 + n) = Tile(TileId::WATER, n, 19);
}
// Make the edges cliffs
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);
// Horizontal
tiles.at(20 + n) = Tile(TileId::CLIFF_BOTTOM, n, 1);
tiles.at(18 * 20 + n) = Tile(TileId::CLIFF_TOP, n, 18);
}
// Fix the corners
tiles.at(20 + 1) = Tile(TileId::CLIFF_CORNER_TOP_LEFT, 1, 1);
tiles.at(20 + 18) = Tile(TileId::CLIFF_CORNER_TOP_RIGHT, 18, 1);
tiles.at(18 * 20 + 1) = Tile(TileId::CLIFF_CORNER_BOTTOM_LEFT, 1, 18);
tiles.at(18 * 20 + 18) = Tile(TileId::CLIFF_CORNER_BOTTOM_RIGHT, 18, 18);
// Buildings
std::vector<Building> buildings;
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);
buildings.push_back(Building(3 + x, 3 + 2 * y, id, faction));
}
}
// 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)));
}
}
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);
Spritesheet spritesheet("/media/data/rust/sprite-extractor/spritesheet.h5", engine);
engine.set_spritesheet(spritesheet);
while (!engine.exited())
{
engine.pump();
......
......@@ -12,7 +12,7 @@ class Engine;
class Scene
{
public:
virtual void render(Engine& engine, std::vector<SDL_Event>& events) = 0;
virtual void render(Engine* engine) = 0;
};
} // namespace advanced_wars
......@@ -7,8 +7,6 @@
*/
#include "spritesheet.hpp"
#include <SDL_render.h>
#include "SDL_surface.h"
#include "engine.hpp"
#include "highfive/H5File.hpp"
#include <SDL_image.h>
......
......@@ -10,12 +10,12 @@ Tile::Tile(TileId id, int x, int y)
};
void Tile::render(Engine& engine, int scale)
void Tile::render(Engine* engine, int scale)
{
Spritesheet* spritesheet = engine.get_spritesheet();
Spritesheet* spritesheet = engine->get_spritesheet();
int step =
engine.get_stage() % spritesheet->get_tile_textures().at(static_cast<int>(id)).second;
engine->get_stage() % spritesheet->get_tile_textures().at(static_cast<int>(id)).second;
SDL_Rect src;
src.x = step * spritesheet->get_tile_width();
......@@ -30,7 +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);
}
......
......@@ -48,7 +48,7 @@ class Tile
int x;
int y;
void render(Engine& engine, int scale);
void render(Engine* engine, int scale);
};
} // namespace advanced_wars
\ No newline at end of file
#include "menu.hpp"
#include "../building.hpp"
#include "../level.hpp"
#include "../spritesheet.hpp"
#include "../tile.hpp"
#include "../unit.hpp"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <iostream>
#include <string>
#include "../level.hpp"
#include "../building.hpp"
#include "../unit.hpp"
#include "../tile.hpp"
#include "../spritesheet.hpp"
namespace advanced_wars {
namespace advanced_wars
{
Menu::Menu(int selectedOption)
: selectedOption(selectedOption),
options({"Start Game", "Options", "Exit"}), backgroundTexture(nullptr) {
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
std::cerr << "Failed to initialize SDL_image: " << IMG_GetError()
<< std::endl;
: selectedOption(selectedOption), options({"Start Game", "Options", "Exit"}),
backgroundTexture(nullptr)
{
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
{
std::cerr << "Failed to initialize SDL_image: " << IMG_GetError() << std::endl;
}
}
Menu::~Menu() {
if (backgroundTexture) {
Menu::~Menu()
{
if (backgroundTexture)
{
SDL_DestroyTexture(backgroundTexture);
}
IMG_Quit();
};
void Menu::render(Engine *engine) {
void Menu::render(Engine* engine)
{
// Iterate over all events
while (!engine->events().empty()) {
while (!engine->events().empty())
{
SDL_Event event = engine->events().at(0);
engine->events().pop_front();
handleEvent(engine, event);
}
if (backgroundTexture) {
if (backgroundTexture)
{
SDL_RenderCopy(engine->renderer(), backgroundTexture, nullptr, nullptr);
} else {
}
else
{
SDL_SetRenderDrawColor(engine->renderer(), 0, 0, 0, 255);
SDL_RenderClear(engine->renderer());
}
if (TTF_Init() == -1) {
if (TTF_Init() == -1)
{
std::cerr << "Failed to initialize TTF: " << TTF_GetError() << std::endl;
return;
}
......@@ -53,47 +63,47 @@ void Menu::render(Engine *engine) {
std::string relativePath = "assets/ARCADECLASSIC.TTF";
std::string fullPath = basePath + relativePath;
TTF_Font* titleFont = TTF_OpenFont(fullPath.c_str(), 48);
if (!titleFont) {
std::cerr << "Failed to load title font: " << fullPath << TTF_GetError()
<< std::endl;
if (!titleFont)
{
std::cerr << "Failed to load title font: " << fullPath << TTF_GetError() << std::endl;
return;
}
TTF_Font* menuFont = TTF_OpenFont(fullPath.c_str(), 24);
if (!menuFont) {
if (!menuFont)
{
TTF_CloseFont(titleFont);
std::cerr << "Failed to load menu font: " << fullPath << TTF_GetError()
<< std::endl;
std::cerr << "Failed to load menu font: " << fullPath << TTF_GetError() << std::endl;
return;
}
SDL_Color white = {255, 255, 255, 255};
SDL_Color yellow = {255, 255, 0, 255};
SDL_Surface *titleSurface =
TTF_RenderText_Solid(titleFont, "Advanced Wars", white);
if (titleSurface) {
SDL_Texture *titleTexture =
SDL_CreateTextureFromSurface(engine->renderer(), titleSurface);
SDL_Rect titleRect = {static_cast<int>((800 - titleSurface->w) / 2), 50,
titleSurface->w, titleSurface->h};
SDL_Surface* titleSurface = TTF_RenderText_Solid(titleFont, "Advanced Wars", white);
if (titleSurface)
{
SDL_Texture* titleTexture = SDL_CreateTextureFromSurface(engine->renderer(), titleSurface);
SDL_Rect titleRect = {
static_cast<int>((800 - titleSurface->w) / 2), 50, titleSurface->w, titleSurface->h};
SDL_RenderCopy(engine->renderer(), titleTexture, nullptr, &titleRect);
SDL_DestroyTexture(titleTexture);
SDL_FreeSurface(titleSurface);
}
for (size_t i = 0; i < options.size(); ++i) {
for (size_t i = 0; i < options.size(); ++i)
{
SDL_Surface* textSurface = TTF_RenderText_Solid(
menuFont, options[i].c_str(), (i == selectedOption) ? yellow : white);
if (!textSurface) {
if (!textSurface)
{
continue;
}
SDL_Texture *textTexture =
SDL_CreateTextureFromSurface(engine->renderer(), textSurface);
SDL_Rect textRect = {static_cast<int>((800 - textSurface->w) / 2),
static_cast<int>(150 + i * 50), textSurface->w,
textSurface->h};
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(engine->renderer(), textSurface);
SDL_Rect textRect = {
static_cast<int>((800 - textSurface->w) / 2), static_cast<int>(150 + i * 50),
textSurface->w, textSurface->h};
SDL_RenderCopy(engine->renderer(), textTexture, nullptr, &textRect);
SDL_DestroyTexture(textTexture);
......@@ -107,49 +117,120 @@ void Menu::render(Engine *engine) {
SDL_RenderPresent(engine->renderer());
}
void Menu::handleEvent(Engine *engine, SDL_Event &event) {
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_DOWN) {
void Menu::handleEvent(Engine* engine, SDL_Event& event)
{
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_DOWN)
{
selectedOption = (selectedOption + 1) % options.size();
} else if (event.key.keysym.sym == SDLK_UP) {
}
else if (event.key.keysym.sym == SDLK_UP)
{
selectedOption = (selectedOption - 1 + options.size()) % options.size();
} else if (event.key.keysym.sym == SDLK_RETURN) {
if (options[selectedOption] == "Exit") {
}
else if (event.key.keysym.sym == SDLK_RETURN)
{
if (options[selectedOption] == "Exit")
{
std::cout << "Exiting game..." << std::endl;
engine->exit();
} else if (options[selectedOption] == "Start Game") {
}
else if (options[selectedOption] == "Start Game")
{
std::cout << "Starting game..." << std::endl;
/* TODO REMOVE THIS BOILERPLATE CODE BEFORE MERGE */
// Construct a level
std::vector<Tile> tiles;
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 20; x++)
{
tiles.push_back(Tile(TileId::PLAIN, x, y));
}
}
Level level("Osnabrück", 20, 20, std::vector<Tile>(),
std::vector<Building>(), std::vector<Unit>());
// Fill the edges with water
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);
// Horizontal
tiles.at(n) = Tile(TileId::WATER, n, 0);
tiles.at(19 * 20 + n) = Tile(TileId::WATER, n, 19);
}
engine->push_scene(std::make_shared<advanced_wars::Level>(level));
// Make the edges cliffs
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);
std::string basePath = SDL_GetBasePath();
std::string relativePath = "assets/main_background.png";
std::string fullPath = basePath + relativePath;
Spritesheet spritesheet(fullPath, *engine);
// Horizontal
tiles.at(20 + n) = Tile(TileId::CLIFF_BOTTOM, n, 1);
tiles.at(18 * 20 + n) = Tile(TileId::CLIFF_TOP, n, 18);
}
engine->set_spritesheet(spritesheet);
// Fix the corners
tiles.at(20 + 1) = Tile(TileId::CLIFF_CORNER_TOP_LEFT, 1, 1);
tiles.at(20 + 18) = Tile(TileId::CLIFF_CORNER_TOP_RIGHT, 18, 1);
tiles.at(18 * 20 + 1) = Tile(TileId::CLIFF_CORNER_BOTTOM_LEFT, 1, 18);
tiles.at(18 * 20 + 18) = Tile(TileId::CLIFF_CORNER_BOTTOM_RIGHT, 18, 18);
/* END OF BOILERPLATE CODE */
// Buildings
std::vector<Building> buildings;
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);
buildings.push_back(Building(3 + x, 3 + 2 * y, id, faction));
}
}
} else if (options[selectedOption] == "Options") {
// 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)));
}
}
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)});
std::shared_ptr<Level> level =
std::make_shared<Level>("Osnabrück", 20, 20, tiles, buildings, units, effects);
engine->push_scene(level);
}
else if (options[selectedOption] == "Options")
{
std::cout << "Opening options..." << std::endl;
}
}
}
}
void Menu::loadBackground(SDL_Renderer *renderer,
const std::string &imagePath) {
void Menu::loadBackground(SDL_Renderer* renderer, const std::string& imagePath)
{
// Lade das Hintergrundbild
SDL_Surface* backgroundSurface = IMG_Load(imagePath.c_str());
if (!backgroundSurface) {
std::cerr << "Failed to load background image: " << IMG_GetError()
<< std::endl;
if (!backgroundSurface)
{
std::cerr << "Failed to load background image: " << IMG_GetError() << std::endl;
return;
}
......@@ -159,9 +240,9 @@ void Menu::loadBackground(SDL_Renderer *renderer,
SDL_FreeSurface(backgroundSurface); // Oberfläche freigeben, da sie nicht mehr
// benötigt wird
if (!backgroundTexture) {
std::cerr << "Failed to create background texture: " << SDL_GetError()
<< std::endl;
if (!backgroundTexture)
{
std::cerr << "Failed to create background texture: " << SDL_GetError() << std::endl;
}
}
......
......@@ -8,11 +8,11 @@ Unit::Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state)
};
void Unit::render(Engine& engine, int scale)
void Unit::render(Engine* engine, int scale)
{
Spritesheet* spritesheet = engine.get_spritesheet();
Spritesheet* spritesheet = engine->get_spritesheet();
int step = engine.get_stage() % spritesheet->get_unit_textures()
int step = engine->get_stage() % spritesheet->get_unit_textures()
.at(static_cast<int>(faction))
.at(static_cast<int>(id))
.at(static_cast<int>(state))
......@@ -34,7 +34,7 @@ void Unit::render(Engine& engine, int scale)
dst.h = spritesheet->get_unit_height() * scale;
SDL_RenderCopyEx(
engine.renderer(),
engine->renderer(),
spritesheet->get_unit_textures()
.at(static_cast<int>(faction))
.at(static_cast<int>(id))
......@@ -59,7 +59,7 @@ void Unit::render(Engine& engine, int scale)
dst.h = spritesheet->get_unit_moving_height() * scale;
SDL_RenderCopyEx(
engine.renderer(),
engine->renderer(),
spritesheet->get_unit_textures()
.at(static_cast<int>(faction))
.at(static_cast<int>(id))
......
......@@ -62,7 +62,7 @@ class Unit
public:
Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state);
void render(Engine& engine, int scale);
void render(Engine* engine, int scale);
private:
int x;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment