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

add scene stack

parent 2636f461
No related branches found
No related tags found
1 merge request!14Refactoring CMakeLists.txt
#include "engine.hpp"
#include <SDL_render.h>
#include "SDL_events.h"
#include "scene.hpp"
#include "spritesheet.hpp"
#include "window.hpp"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_render.h>
#include <deque>
#include <memory>
#include <optional>
#include <stdexcept>
#include <vector>
namespace advanced_wars {
......@@ -34,7 +37,22 @@ Engine::Engine(Window &window) : window(window), quit(false) {
}
}
void Engine::set_scene(Scene &scene) { this->scene = &scene; }
std::deque<SDL_Event> &Engine::events() { return this->_events; }
void Engine::push_scene(std::shared_ptr<Scene> scene) {
this->scenes.push_back(scene);
}
std::optional<std::shared_ptr<Scene>> Engine::pop_scene() {
if (!this->scenes.empty()) {
return std::nullopt;
} else {
std::shared_ptr<Scene> tmp = scenes.at(scenes.size() - 1);
this->scenes.pop_back();
return std::optional<std::shared_ptr<Scene>>(tmp);
}
}
void Engine::set_spritesheet(Spritesheet spritesheet) {
this->spritesheet = spritesheet;
......@@ -46,11 +64,13 @@ void Engine::pump() {
if (e.type == SDL_QUIT) {
this->quit = true;
} else {
this->events.push_back(e);
this->_events.push_back(e);
}
}
}
void Engine::exit() { this->quit = true; }
bool Engine::exited() { return this->quit; }
void Engine::render() {
......@@ -59,11 +79,11 @@ void Engine::render() {
std::string(SDL_GetError()));
}
if (!scene.has_value()) {
if (scenes.empty()) {
return;
}
this->scene.value()->render(this->sdl_renderer, this->events);
this->scenes.at(scenes.size() - 1)->render(this);
SDL_RenderPresent(this->sdl_renderer);
}
......
......
#pragma once
#include <SDL_render.h>
#include "SDL_events.h"
#include "scene.hpp"
#include "spritesheet.hpp"
#include "window.hpp"
#include <SDL.h>
#include <SDL_render.h>
#include <deque>
#include <memory>
#include <optional>
#include <vector>
namespace advanced_wars {
// Forward declaration
class Scene;
/**
* @brief The main window of the game
*/
......@@ -22,9 +27,15 @@ public:
bool exited();
void exit();
void pump();
void set_scene(Scene &scene);
void push_scene(std::shared_ptr<Scene> scene);
std::optional<std::shared_ptr<Scene>> pop_scene();
std::deque<SDL_Event> &events();
void set_spritesheet(Spritesheet spritesheet);
......@@ -37,9 +48,9 @@ public:
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;
};
......
......
#include "level.hpp"
#include "building.hpp"
#include "unit.hpp"
#include <string>
#include <iostream>
#include <SDL.h>
#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): name(name), width(width), height(height), buildings(buildings), units(units) {};
Level::Level(std::string name, int width, int height, std::vector<Tile> tiles,
std::vector<Building> buildings, std::vector<Unit> units)
: name(name), width(width), height(height), buildings(buildings),
units(units) {};
void Level::render(SDL_Renderer *renderer, std::vector<SDL_Event> &events) {
void Level::render(Engine *engine) {
// Iterate over all events
while (!events.empty()) {
events.erase(events.begin());
while (!engine->events().empty()) {
engine->events().pop_front();
}
// Set background color for renderer
if(SDL_SetRenderDrawColor(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;
}
}
}
} // namespace advanced_wars
......@@ -18,7 +18,7 @@ public:
Level(std::string name, int width, int height, std::vector<Tile> tiles,
std::vector<Building> buildings, std::vector<Unit> units);
void render(SDL_Renderer *renderer, std::vector<SDL_Event> &events);
void render(Engine *events);
private:
std::string name;
......
......
#include "engine.hpp"
#include "level.hpp"
#include "spritesheet.hpp"
#include "window.hpp"
#include <vector>
#include "ui/Menu.hpp"
#include "window.hpp"
#include <memory>
using namespace advanced_wars;
......@@ -15,16 +14,16 @@ int main() {
// render main menu
Menu menu(0);
std::shared_ptr<Menu> menu = std::make_shared<Menu>(0);
std::string basePath = SDL_GetBasePath();
std::string relativePath = "assets/main_background.png";
std::string fullPath = basePath + relativePath;
menu.loadBackground(engine.renderer(), fullPath.c_str());
menu->loadBackground(engine.renderer(), fullPath.c_str());
engine.set_scene(menu);
/* Level level("Osnabrück", 20, 20, std::vector<Tile>(), std::vector<Building>(),
std::vector<Unit>());
engine.push_scene(menu);
/* Level level("Osnabrück", 20, 20, std::vector<Tile>(),
std::vector<Building>(), std::vector<Unit>());
engine.set_scene(level);
......
......
#pragma once
#include "engine.hpp"
#include <SDL.h>
#include <vector>
namespace advanced_wars {
// Forward declaration
class Engine;
class Scene {
public:
virtual void render(SDL_Renderer *renderer,
std::vector<SDL_Event> &events) = 0;
virtual void render(Engine *engine) = 0;
};
} // namespace advanced_wars
#include "Menu.hpp"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <iostream>
#include <vector>
#include <string>
#include <SDL_image.h>
#include "Menu.hpp"
namespace advanced_wars
{
namespace advanced_wars {
Menu::Menu(int selectedOption)
: selectedOption(selectedOption),
options({"Start Game", "Options", "Exit"}),
backgroundTexture(nullptr)
{
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;
std::cerr << "Failed to initialize SDL_image: " << IMG_GetError()
<< std::endl;
}
}
......@@ -26,19 +23,20 @@ namespace advanced_wars
IMG_Quit();
};
void Menu::render(SDL_Renderer *renderer, std::vector<SDL_Event> &events) {
void Menu::render(Engine *engine) {
if (events.size() > 0) {
SDL_Event event = events.back();
events.pop_back();
handleEvent(event);
// Iterate over all events
while (!engine->events().empty()) {
SDL_Event event = engine->events().at(0);
engine->events().pop_front();
handleEvent(engine, event);
}
if (backgroundTexture) {
SDL_RenderCopy(renderer, backgroundTexture, nullptr, nullptr);
SDL_RenderCopy(engine->renderer(), backgroundTexture, nullptr, nullptr);
} else {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(engine->renderer(), 0, 0, 0, 255);
SDL_RenderClear(engine->renderer());
}
if (TTF_Init() == -1) {
......@@ -51,38 +49,47 @@ namespace advanced_wars
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;
std::cerr << "Failed to load title font: " << fullPath << TTF_GetError()
<< std::endl;
return;
}
TTF_Font *menuFont = TTF_OpenFont(fullPath.c_str(), 24);
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);
SDL_Surface *titleSurface =
TTF_RenderText_Solid(titleFont, "Advanced Wars", white);
if (titleSurface) {
SDL_Texture* titleTexture = SDL_CreateTextureFromSurface(renderer, titleSurface);
SDL_Rect titleRect = {static_cast<int>((800 - titleSurface->w) / 2), 50, titleSurface->w, titleSurface->h};
SDL_RenderCopy(renderer, titleTexture, nullptr, &titleRect);
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) {
SDL_Surface* textSurface = TTF_RenderText_Solid(menuFont, options[i].c_str(), (i == selectedOption) ? yellow : white);
SDL_Surface *textSurface = TTF_RenderText_Solid(
menuFont, options[i].c_str(), (i == selectedOption) ? yellow : white);
if (!textSurface) {
continue;
}
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = {static_cast<int>((800 - textSurface->w) / 2), static_cast<int>(150 + i * 50), textSurface->w, textSurface->h};
SDL_RenderCopy(renderer, textTexture, nullptr, &textRect);
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);
SDL_FreeSurface(textSurface);
......@@ -92,11 +99,10 @@ namespace advanced_wars
TTF_CloseFont(menuFont);
TTF_Quit();
SDL_RenderPresent(renderer);
SDL_RenderPresent(engine->renderer());
}
void Menu::handleEvent(SDL_Event& event) {
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();
......@@ -105,28 +111,32 @@ namespace advanced_wars
} else if (event.key.keysym.sym == SDLK_RETURN) {
if (options[selectedOption] == "Exit") {
std::cout << "Exiting game..." << std::endl;
// Exit logic here
engine->exit();
}
}
}
}
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;
std::cerr << "Failed to load background image: " << IMG_GetError()
<< std::endl;
return;
}
// Erstelle eine Textur aus der Oberfläche und speichere sie als Klassenmitglied
// Erstelle eine Textur aus der Oberfläche und speichere sie als
// Klassenmitglied
backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
SDL_FreeSurface(backgroundSurface); // Oberfläche freigeben, da sie nicht mehr benötigt wird
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;
std::cerr << "Failed to create background texture: " << SDL_GetError()
<< std::endl;
}
}
}
} // namespace advanced_wars
#ifndef MENU_SYSTEM_HPP
#define MENU_SYSTEM_HPP
#include "../scene.hpp"
#include <SDL.h>
#include <array>
#include <iostream>
#include <vector>
#include <string>
#include <array>
#include "../scene.hpp"
#include <vector>
namespace advanced_wars {
......@@ -17,18 +17,17 @@ private:
SDL_Texture *backgroundTexture;
public:
Menu(int selectedOption);
void render(SDL_Renderer *renderer, std::vector<SDL_Event> &events);
void render(Engine *engine);
void handleEvent(SDL_Event& event);
void handleEvent(Engine *engine, SDL_Event &event);
void loadBackground(SDL_Renderer *renderer, const std::string &imagePath);
~Menu();
};
}
} // namespace advanced_wars
#endif // MENU_SYSTEM_HPP
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment