Skip to content
Snippets Groups Projects
Commit 88f1189a authored by Frederik Alexander Keens's avatar Frederik Alexander Keens
Browse files

Merge branch 'win_end_screen' into 'main'

Win end screen

See merge request !33
parents d1d187b3 63fa5a76
Branches
No related tags found
2 merge requests!33Win end screen,!29Merge main into box2d to implement physics
......@@ -106,7 +106,8 @@ void Engine::render()
std::shared_ptr<Scene> currentScene = m_scenes.back();
currentScene->render(*this);
SDL_SetRenderDrawColor(this->m_SDLRenderer, 0, 0, 0, 255);
// prevent epilepsy
SDL_SetRenderDrawColor(this->m_SDLRenderer, 128, 128, 128, 128);
SDL_RenderPresent(this->m_SDLRenderer);
}
......
......@@ -7,8 +7,8 @@
#include "Unit.hpp"
#include "highfive/H5File.hpp"
#include "ui/Contextmenu.hpp"
#include "ui/Pausemenu.hpp"
#include "ui/Helpmenu.hpp"
#include "ui/Pausemenu.hpp"
#include <SDL.h>
#include <algorithm>
#include <boost/property_tree/ptree.hpp>
......@@ -179,8 +179,10 @@ int Level::selectBuilding(int tileX, int tileY)
void Level::handleEvent(Engine& engine, SDL_Event& event)
{
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_h) {
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_h)
{
toggle_Helpmenu = !toggle_Helpmenu;
}
}
......@@ -355,10 +357,10 @@ void Level::render(Engine& engine)
}
m_currentPos.render(engine);
if(toggle_Helpmenu) {
if (toggle_Helpmenu)
{
m_helpMenu.render(engine);
}
}
int Level::addBuilding(Building building)
......@@ -420,6 +422,7 @@ void Level::changeTurn()
m_turnQ.push(temp);
m_turnQ.front().startTurn(m_units, m_buildings);
m_currentPos.setMarkerColor(m_turnQ.front().getFaction());
}
void Level::handleRecruitingEvent(Engine& engine, SDL_Event& event)
......
#include "Endscreen.hpp"
#include "Menu.hpp"
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <iostream>
namespace advanced_wars
{
Endscreen::Endscreen(Player& player) : m_moenyLeft(player.getMoney())
{
std::cout << "Player faction: " << static_cast<int>(player.getFaction()) << std::endl;
switch (player.getFaction())
{
case UnitFaction::UBLUE:
m_color = {0, 0, 255, 255};
m_playerString = "Blue";
break;
case UnitFaction::UGREEN:
m_color = {0, 255, 0, 255};
m_playerString = "Green";
break;
case UnitFaction::UPURPLE:
m_color = {255, 0, 255, 255};
m_playerString = "Purple";
break;
case UnitFaction::URED:
m_color = {255, 0, 0, 255};
m_playerString = "Red";
break;
case UnitFaction::UYELLOW:
m_color = {255, 255, 0, 255};
m_playerString = "Yellow";
break;
default:
break;
}
}
void Endscreen::render(Engine& engine)
{
std::string basePath = SDL_GetBasePath();
SDL_Color white = {255, 255, 255, 255};
SDL_Color yellow = {255, 255, 0, 255};
if (TTF_Init() == -1)
{
std::cerr << "Failed to initialize TTF: " << TTF_GetError() << std::endl;
return;
}
// Draw Background
std::string relativePath = "res/main_background.png";
std::string fullPath = SDL_GetBasePath() + relativePath;
SDL_Surface* backgroundSurface = IMG_Load(fullPath.c_str());
if (!backgroundSurface)
{
std::cerr << "Failed to load background image: " << IMG_GetError() << std::endl;
return;
}
SDL_Texture* backgroundTexture =
SDL_CreateTextureFromSurface(engine.renderer(), backgroundSurface);
SDL_RenderCopy(engine.renderer(), backgroundTexture, nullptr, nullptr);
// Draw Foreground
relativePath = "res/ARCADECLASSIC.TTF";
fullPath = basePath + relativePath;
TTF_Font* titleFont = TTF_OpenFont(fullPath.c_str(), 48);
std::string titleText = "Player " + m_playerString + " won!";
SDL_Surface* titleSurface = TTF_RenderText_Solid(titleFont, titleText.c_str(), m_color);
if (titleSurface)
{
SDL_Texture* titleTexture = SDL_CreateTextureFromSurface(engine.renderer(), titleSurface);
SDL_Rect titleRect = {
static_cast<int>((960 - titleSurface->w) / 2), 50, titleSurface->w, titleSurface->h};
SDL_RenderCopy(engine.renderer(), titleTexture, nullptr, &titleRect);
SDL_DestroyTexture(titleTexture);
SDL_FreeSurface(titleSurface);
}
TTF_Font* menuFont = TTF_OpenFont(fullPath.c_str(), 24);
std::string moneyText = "Money left* " + std::to_string(m_moenyLeft);
SDL_Surface* statsSurface = TTF_RenderText_Solid(menuFont, moneyText.c_str(), white);
if (statsSurface)
{
SDL_Texture* statsTexture = SDL_CreateTextureFromSurface(engine.renderer(), statsSurface);
SDL_Rect titleRect = {
static_cast<int>((800 - statsSurface->w) / 2), 150, statsSurface->w, statsSurface->h};
SDL_RenderCopy(engine.renderer(), statsTexture, nullptr, &titleRect);
SDL_DestroyTexture(statsTexture);
SDL_FreeSurface(statsSurface);
}
SDL_Surface* optionSurface = TTF_RenderText_Solid(menuFont, "Back to Menu", yellow);
if (optionSurface)
{
SDL_Texture* optionTexture = SDL_CreateTextureFromSurface(engine.renderer(), optionSurface);
SDL_Rect titleRect = {
static_cast<int>((960 - optionSurface->w) / 2), 400, optionSurface->w,
optionSurface->h};
SDL_RenderCopy(engine.renderer(), optionTexture, nullptr, &titleRect);
SDL_DestroyTexture(optionTexture);
SDL_FreeSurface(optionSurface);
}
// Free resources
SDL_FreeSurface(backgroundSurface);
SDL_DestroyTexture(backgroundTexture);
}
void Endscreen::handleEvent(Engine& engine, SDL_Event& event)
{
if (event.key.keysym.sym == SDLK_RETURN)
{
engine.returnToMenu();
}
}
} // namespace advanced_wars
#pragma once
#include "../Player.hpp"
#include "../Scene.hpp"
namespace advanced_wars
{
class Endscreen : public Scene
{
private:
SDL_Color m_color;
int m_moenyLeft;
std::string m_playerString;
public:
Endscreen(Player& player);
void render(Engine& engine);
void handleEvent(Engine& engine, SDL_Event& event);
};
} // namespace advanced_wars
......@@ -4,7 +4,7 @@
namespace advanced_wars
{
TileMarker::TileMarker(int renderingScale, int tileX, int tileY, int levelWidth, int levelHeight)
: m_renderingScale(renderingScale), m_width(16), m_height(16)
: m_renderingScale(renderingScale), m_width(16), m_height(16), m_markerColor({255, 0, 0, 255})
{
int tileSize = 16 * renderingScale;
m_x = tileX * tileSize;
......@@ -15,7 +15,8 @@ TileMarker::TileMarker(int renderingScale, int tileX, int tileY, int levelWidth,
void TileMarker::render(Engine& engine)
{
SDL_SetRenderDrawColor(engine.renderer(), 255, 0, 0, 255);
SDL_SetRenderDrawColor(
engine.renderer(), m_markerColor.r, m_markerColor.g, m_markerColor.b, m_markerColor.a);
SDL_Rect box = {m_x, m_y, m_width, m_height};
SDL_RenderFillRect(engine.renderer(), &box);
}
......@@ -88,4 +89,27 @@ void TileMarker::setPosition(int tileX, int tileY)
m_y = tileY * 16 * m_renderingScale + (16 * m_renderingScale - m_height);
}
void TileMarker::setMarkerColor(UnitFaction faction)
{
switch (faction)
{
case UnitFaction::URED:
m_markerColor = {255, 0, 0, 255};
break;
case UnitFaction::UBLUE:
m_markerColor = {0, 0, 255, 255};
break;
case UnitFaction::UGREEN:
m_markerColor = {0, 255, 0, 255};
break;
case UnitFaction::UYELLOW:
m_markerColor = {255, 255, 0, 255};
break;
case UnitFaction::UPURPLE:
m_markerColor = {255, 0, 255, 255};
break;
default:
break;
}
}
} // namespace advanced_wars
#pragma once
#include "../Player.hpp"
#include "../Scene.hpp"
namespace advanced_wars
......@@ -21,6 +22,8 @@ class TileMarker : public Scene
void setPosition(int x, int y);
void setMarkerColor(UnitFaction faction);
private:
int m_x;
int m_y;
......@@ -29,5 +32,6 @@ class TileMarker : public Scene
int m_height;
int m_levelHeight;
int m_levelWidth;
SDL_Color m_markerColor;
};
} // namespace advanced_wars
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment