Skip to content
Snippets Groups Projects
Unverified Commit eb5b3fd1 authored by David Hermann's avatar David Hermann
Browse files

Closing #10

Implementing possibility to render background in main menu
parent f42e8ccf
Branches
No related tags found
1 merge request!14Refactoring CMakeLists.txt
......@@ -17,8 +17,12 @@ int main() {
MainMenu menu(0);
engine.set_scene(menu);
std::string basePath = SDL_GetBasePath();
std::string relativePath = "assets/main_background.png";
std::string fullPath = basePath + relativePath;
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>());
......
......@@ -3,6 +3,7 @@
#include <iostream>
#include <vector>
#include <string>
#include <SDL_image.h>
#include "Menu.hpp"
namespace advanced_wars
......@@ -10,13 +11,19 @@ namespace advanced_wars
MainMenu::MainMenu(int selectedOption)
: selectedOption(selectedOption),
options({"Start Game", "Options", "Exit"})
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;
}
}
MainMenu::~MainMenu() {
if (backgroundTexture) {
SDL_DestroyTexture(backgroundTexture);
}
IMG_Quit();
};
void MainMenu::render(SDL_Renderer *renderer, std::vector<SDL_Event> &events) {
......@@ -28,9 +35,14 @@ namespace advanced_wars
}
// Clear the screen with a background color
if (backgroundTexture) {
SDL_RenderCopy(renderer, backgroundTexture, nullptr, nullptr);
} else {
std::cout << "No background texture loaded" << std::endl;
// Falls kein Hintergrundbild vorhanden ist, cleare mit Schwarz
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
}
// Initialize SDL_TTF if not already done
if (TTF_Init() == -1) {
......@@ -101,4 +113,22 @@ namespace advanced_wars
}
}
void MainMenu::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;
return;
}
// 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
if (!backgroundTexture) {
std::cerr << "Failed to create background texture: " << SDL_GetError() << std::endl;
}
}
}
......@@ -20,6 +20,7 @@ class MainMenu : public Scene {
private:
size_t selectedOption;
std::array<std::string, 3> options;
SDL_Texture* backgroundTexture;
public:
......@@ -29,6 +30,8 @@ public:
void handleEvent(SDL_Event& event);
void loadBackground(SDL_Renderer *renderer, const std::string& imagePath);
~MainMenu();
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment