mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2026-02-04 08:19:21 +01:00
Update Screen system
Added FuelScreen (Working buttons) Added SellScreen (Nothing working)
This commit is contained in:
@@ -151,6 +151,7 @@
|
||||
<ClCompile Include="Animations\Animation.cpp"/>
|
||||
<ClCompile Include="Camera.cpp"/>
|
||||
<ClCompile Include="Game.cpp"/>
|
||||
<ClCompile Include="GameManager.cpp" />
|
||||
<ClCompile Include="GridSystem\WorldGridManager.cpp">
|
||||
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
@@ -301,6 +302,8 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="Gui\Button.cpp" />
|
||||
<ClCompile Include="Gui\Screen.cpp" />
|
||||
<ClCompile Include="Gui\Screens\FuelScreen.cpp" />
|
||||
<ClCompile Include="Gui\Screens\ScreenManager.cpp" />
|
||||
<ClCompile Include="Level.cpp">
|
||||
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
@@ -458,10 +461,13 @@
|
||||
<ClInclude Include="Animations\Animation.h"/>
|
||||
<ClInclude Include="Camera.h"/>
|
||||
<ClInclude Include="Game.h"/>
|
||||
<ClInclude Include="GameManager.h" />
|
||||
<ClInclude Include="GridSystem\WorldGridManager.h"/>
|
||||
<ClInclude Include="GridSystem\WorldTile.h"/>
|
||||
<ClInclude Include="Gui\Button.h" />
|
||||
<ClInclude Include="Gui\Screen.h" />
|
||||
<ClInclude Include="Gui\Screens\FuelScreen.h" />
|
||||
<ClInclude Include="Gui\Screens\ScreenManager.h" />
|
||||
<ClInclude Include="Level.h"/>
|
||||
<ClInclude Include="pch.h"/>
|
||||
<ClInclude Include="Player.h"/>
|
||||
|
||||
1
Game/GameManager.cpp
Normal file
1
Game/GameManager.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "GameManager.h"
|
||||
12
Game/GameManager.h
Normal file
12
Game/GameManager.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include "Player.h"
|
||||
|
||||
class GameManager
|
||||
{
|
||||
public:
|
||||
float balance{ 0.0f };
|
||||
float fuel{ 0.0f };
|
||||
|
||||
private:
|
||||
GameManager() = default;
|
||||
};
|
||||
@@ -1 +1,37 @@
|
||||
#include "Button.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "colors.h"
|
||||
#include "utils.h"
|
||||
Button::Button(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager): m_Position(pos), m_Size(size) {
|
||||
m_Texture = manager->GetTexture(filePath);
|
||||
if(size.x == 0 && size.y == 0) {
|
||||
m_Size = Point2f{float(m_Texture->GetWidth()), float(m_Texture->GetHeight())};
|
||||
}
|
||||
std::cout << "Button created" << '\n';
|
||||
}
|
||||
Button::~Button() {
|
||||
std::cout << "Button destroyed" << '\n';
|
||||
}
|
||||
void Button::Draw() const {
|
||||
Rectf dest = Rectf(m_Position, m_Size);
|
||||
Rectf src = Rectf(0, 0, m_Texture->GetWidth(), m_Texture->GetHeight());
|
||||
if(m_IsHovered) {
|
||||
m_Texture->Draw(dest, src, false);
|
||||
}
|
||||
}
|
||||
void Button::Update(float elapsedSec) {
|
||||
Point2f mousePos = utils::GetMousePos();
|
||||
Rectf buttonRect = Rectf(m_Position, m_Size);
|
||||
|
||||
this->m_IsHovered = utils::IsPointInRect(mousePos, buttonRect);
|
||||
m_IsPressed = m_IsHovered && utils::IsMouseButtonDown(SDL_BUTTON_LEFT);
|
||||
|
||||
if(m_IsPressed) {
|
||||
m_OnClick();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
#include "Texture.h"
|
||||
#include "../TextureManager.h"
|
||||
|
||||
class Button
|
||||
{
|
||||
public:
|
||||
Button() = default;
|
||||
Button(const std::string& filePath, Point2f pos, Point2f size);
|
||||
Button(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager);
|
||||
~Button();
|
||||
void Draw() const;
|
||||
void Update(float elapsedSec);
|
||||
|
||||
void SetOnClick(std::function<void(void)> onClick) {
|
||||
m_OnClick = onClick;
|
||||
}
|
||||
|
||||
private:
|
||||
Texture* m_Texture{ nullptr };
|
||||
@@ -16,4 +25,6 @@ private:
|
||||
|
||||
bool m_IsHovered{ false };
|
||||
bool m_IsPressed{ false };
|
||||
|
||||
std::function<void(void)> m_OnClick{ []() { std::cout << "Button not implemented" << std::endl; } };
|
||||
};
|
||||
|
||||
@@ -6,9 +6,16 @@ Screen::Screen(const std::string& filePath, Point2f pos, Point2f size,TextureMan
|
||||
Screen::~Screen() {
|
||||
}
|
||||
void Screen::Update(float elapsedSecs) {
|
||||
for(Button* b : m_Buttons) {
|
||||
b->Update(elapsedSecs);
|
||||
}
|
||||
}
|
||||
void Screen::Draw() const {
|
||||
Rectf dest = Rectf(m_Position, m_Size);
|
||||
Rectf src = Rectf(0,0, m_Background->GetWidth(), m_Background->GetHeight());
|
||||
m_Background->Draw(dest, src, false);
|
||||
|
||||
for(Button* b: m_Buttons) {
|
||||
b->Draw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
#include "Button.h"
|
||||
#include "structs.h"
|
||||
#include "Texture.h"
|
||||
#include "../TextureManager.h"
|
||||
@@ -9,18 +12,17 @@ public:
|
||||
Screen() = default;
|
||||
Screen(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager);
|
||||
|
||||
~Screen();
|
||||
virtual ~Screen();
|
||||
|
||||
void setActive(bool active) { m_Active = active; }
|
||||
void toggleActive() { m_Active = !m_Active; }
|
||||
void AddButton(Button* button) { m_Buttons.push_back(button); }
|
||||
|
||||
void Update(float elapsedSecs);
|
||||
void Draw() const;
|
||||
virtual void Update(float elapsedSecs);
|
||||
virtual void Draw() const;
|
||||
private:
|
||||
Point2f m_Position;
|
||||
Point2f m_Size;
|
||||
|
||||
Texture* m_Background{ nullptr };
|
||||
|
||||
bool m_Active{ false };
|
||||
std::vector<Button*> m_Buttons;
|
||||
};
|
||||
|
||||
10
Game/Gui/Screens/FuelScreen.cpp
Normal file
10
Game/Gui/Screens/FuelScreen.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "FuelScreen.h"
|
||||
FuelScreen::FuelScreen(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager): Screen(filePath, pos, size, manager)
|
||||
{
|
||||
}
|
||||
void FuelScreen::Draw() const {
|
||||
Screen::Draw();
|
||||
}
|
||||
void FuelScreen::Update(float elapsedSecs) {
|
||||
Screen::Update(elapsedSecs);
|
||||
}
|
||||
13
Game/Gui/Screens/FuelScreen.h
Normal file
13
Game/Gui/Screens/FuelScreen.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "../Screen.h"
|
||||
|
||||
class FuelScreen : public Screen
|
||||
{
|
||||
public:
|
||||
|
||||
FuelScreen(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager);
|
||||
|
||||
virtual void Draw() const override;
|
||||
virtual void Update(float elapsedSecs) override;
|
||||
|
||||
};
|
||||
77
Game/Gui/Screens/ScreenManager.cpp
Normal file
77
Game/Gui/Screens/ScreenManager.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "ScreenManager.h"
|
||||
|
||||
#include "FuelScreen.h"
|
||||
#include "utils.h"
|
||||
ScreenManager* ScreenManager::m_pInstance = nullptr;
|
||||
|
||||
Screen* ScreenManager::Fuel{ nullptr };
|
||||
Screen* ScreenManager::SellScreen{ nullptr };
|
||||
|
||||
ScreenManager* ScreenManager::GetInstance() {
|
||||
|
||||
if(m_pInstance == nullptr) {
|
||||
m_pInstance = new ScreenManager();
|
||||
}
|
||||
if(!m_pInstance->m_AreScreensInitialized) {
|
||||
m_pInstance->InitializeScreens(); //TODO: Ask if this a hack
|
||||
m_pInstance->m_AreScreensInitialized = true;
|
||||
}
|
||||
return m_pInstance;
|
||||
}
|
||||
|
||||
void ScreenManager::OpenScreen(Screen* screen) {
|
||||
if(m_IsScreenOpen == false) {
|
||||
m_currentScreen = screen;
|
||||
m_IsScreenOpen = true;
|
||||
}
|
||||
}
|
||||
void ScreenManager::CloseScreen() {
|
||||
if(m_IsScreenOpen == true) {
|
||||
m_IsScreenOpen = false;
|
||||
m_currentScreen = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
void ScreenManager::InitializeScreens() {
|
||||
Point2f fuelScreenSize = Point2f { 492, 396 };
|
||||
Point2f fuelScreenCenter = Point2f { utils::GetViewport().x / 2 - fuelScreenSize.x / 2, utils::GetViewport().y / 2 - fuelScreenSize.y / 2 };
|
||||
Fuel = new FuelScreen { "gui/fuel/background.png", fuelScreenCenter, fuelScreenSize, TextureManager::GetInstance() };
|
||||
|
||||
Point2f closeButtonOffset = Point2f { 460, 396 - 14 };
|
||||
Point2f closeButtonPos = fuelScreenCenter + closeButtonOffset;
|
||||
closeButtonPos.y -= 18;
|
||||
Button* closeFuelButton = new Button { "gui/close.png", closeButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
closeFuelButton->SetOnClick([this]() { CloseScreen(); });
|
||||
Fuel->AddButton(closeFuelButton);
|
||||
|
||||
Point2f oneDollarButtonPos = Point2f { 451, 287 };
|
||||
oneDollarButtonPos += fuelScreenCenter;
|
||||
Button* fiveDollarButton = new Button { "gui/fuel/5dollars.png", oneDollarButtonPos , Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(fiveDollarButton);
|
||||
|
||||
Point2f tenDollarButtonPos = oneDollarButtonPos + Point2f { 113, -1 };
|
||||
tenDollarButtonPos += fuelScreenCenter;
|
||||
Button* tenDollarButton = new Button { "gui/fuel/10dollars.png", tenDollarButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(tenDollarButton);
|
||||
|
||||
Point2f twentyFiveDollarButtonPos = oneDollarButtonPos + Point2f { 0, -89 };
|
||||
twentyFiveDollarButtonPos += fuelScreenCenter;
|
||||
Button* twentyFiveDollarButton = new Button { "gui/fuel/25dollars.png", twentyFiveDollarButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(twentyFiveDollarButton);
|
||||
|
||||
Point2f fiftyDollarButtonPos = twentyFiveDollarButtonPos + Point2f { 114, 0 };
|
||||
Button* fiftyDollarButton = new Button { "gui/fuel/50dollars.png", fiftyDollarButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(fiftyDollarButton);
|
||||
|
||||
Point2f fillTankButtonPos = Point2f { 450, 108 };
|
||||
fillTankButtonPos += fuelScreenCenter;
|
||||
Button* fillTankButton = new Button { "gui/fuel/fillTank.png", fillTankButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(fillTankButton);
|
||||
|
||||
Point2f sellScreenSize = Point2f { 533, 398 };
|
||||
Point2f sellScreenCenter = Point2f { utils::GetViewport().x / 2 - sellScreenSize.x / 2, utils::GetViewport().y / 2 - sellScreenSize.y / 2 };
|
||||
SellScreen = new Screen { "gui/sell/background.png", sellScreenCenter, sellScreenSize, TextureManager::GetInstance() };
|
||||
|
||||
//m_Button = Button { "gui/close.png", closeButtonPos, closeButtonSize, TextureManager::GetInstance() };
|
||||
}
|
||||
|
||||
31
Game/Gui/Screens/ScreenManager.h
Normal file
31
Game/Gui/Screens/ScreenManager.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include "../Screen.h"
|
||||
|
||||
|
||||
|
||||
class ScreenManager
|
||||
{
|
||||
public:
|
||||
static ScreenManager* GetInstance();
|
||||
|
||||
void OpenScreen(Screen* screen);
|
||||
void CloseScreen();
|
||||
|
||||
Screen* GetCurrentScreen() { return m_currentScreen; }
|
||||
|
||||
static ScreenManager* m_pInstance;
|
||||
|
||||
|
||||
static Screen* Fuel;
|
||||
static Screen* SellScreen;
|
||||
private:
|
||||
ScreenManager() = default;
|
||||
|
||||
void InitializeScreens();
|
||||
|
||||
bool m_IsScreenOpen{ false };
|
||||
Screen* m_currentScreen;
|
||||
|
||||
bool m_AreScreensInitialized{ false };
|
||||
|
||||
};
|
||||
@@ -14,7 +14,6 @@ private:
|
||||
~TextureManager();
|
||||
static TextureManager* m_pInstance;
|
||||
|
||||
private:
|
||||
std::map<std::string, Texture *> m_Textures {};
|
||||
|
||||
};
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "colors.h"
|
||||
#include "utils.h"
|
||||
#include "GridSystem/WorldTile.h"
|
||||
#include "Gui/Screens/ScreenManager.h"
|
||||
|
||||
|
||||
class GroundTileType;
|
||||
@@ -17,7 +18,8 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
|
||||
m_gridManager(WorldGridManager()),
|
||||
m_player(Player { Point2f { 0, 100 }, TextureManager::GetInstance() }),
|
||||
m_mousePos { 0, 0 },
|
||||
m_viewport(viewport) {
|
||||
m_viewport(viewport),
|
||||
m_screenManager(ScreenManager::GetInstance()) {
|
||||
// The grid is 34 x 50 big, the top center is 0,0 in world coords
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
@@ -44,8 +46,7 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
m_gridManager.GetTileAtIndex(x, 0)->SetTileType(Tiles::AIR);
|
||||
}
|
||||
Point2f screenCenterPos = Point2f { m_viewport.width / 2 - 492 / 2, m_viewport.height / 2 - 396 / 2 };
|
||||
m_screen = Screen{ "gui/fuel/background.png", screenCenterPos, Point2f { 492, 396 }, TextureManager::GetInstance() };
|
||||
|
||||
}
|
||||
WorldLevel::~WorldLevel() {
|
||||
//delete m_pTextTexture;
|
||||
@@ -81,6 +82,11 @@ void WorldLevel::Update(float elapsedSec) {
|
||||
t->SetTileType(Tiles::AIR);
|
||||
}
|
||||
|
||||
Screen* screen = m_screenManager->GetCurrentScreen();
|
||||
if (screen != nullptr) {
|
||||
screen->Update(elapsedSec);
|
||||
}
|
||||
|
||||
//Point2f playerPos = m_player.GetPosition();
|
||||
//Point2f newCameraPos = playerPos;
|
||||
//m_pCamera->SetPosition(newCameraPos);
|
||||
@@ -123,8 +129,10 @@ void WorldLevel::Draw() const {
|
||||
m_pCamera->EndRendering();
|
||||
|
||||
utils::FillRect(utils::GetMousePos(), 10, 10);
|
||||
|
||||
m_screen.Draw();
|
||||
const Screen* screen = m_screenManager->GetCurrentScreen();
|
||||
if (screen != nullptr) {
|
||||
screen->Draw();
|
||||
}
|
||||
}
|
||||
void WorldLevel::MouseMove(const Point2f& mousePos) {
|
||||
m_mousePos = mousePos;
|
||||
@@ -144,8 +152,29 @@ void WorldLevel::ProcessImGui() {
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
if (ImGui::BeginMenu(std::to_string(utils::isKeyPressed(SDL_SCANCODE_S)).c_str())) {
|
||||
if(ImGui::BeginMenu("Screens")) {
|
||||
if(ImGui::MenuItem("Open Fuel screen")) {
|
||||
ScreenManager::GetInstance()->OpenScreen(ScreenManager::Fuel);
|
||||
}
|
||||
|
||||
|
||||
if(ImGui::MenuItem("Open Sell screen")) {
|
||||
ScreenManager::GetInstance()->OpenScreen(ScreenManager::SellScreen);
|
||||
}
|
||||
|
||||
if(ImGui::MenuItem("Close Screen")) {
|
||||
ScreenManager::GetInstance()->CloseScreen();
|
||||
}
|
||||
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
Point2f screenPos = utils::GetMousePos();
|
||||
std::string mousePos = "Mouse Pos: (" + std::to_string(screenPos.x) + ", " + std::to_string(screenPos.y) + ")";
|
||||
if(ImGui::BeginMenu(mousePos.c_str())) {
|
||||
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
#include "Player.h"
|
||||
#include "utils.h"
|
||||
#include "GridSystem/WorldGridManager.h"
|
||||
#include "Gui/Button.h"
|
||||
#include "Gui/Screen.h"
|
||||
#include "Gui/Screens/ScreenManager.h"
|
||||
|
||||
|
||||
class WorldLevel : public Level
|
||||
@@ -35,10 +37,11 @@ private:
|
||||
|
||||
Rectf m_viewport;
|
||||
|
||||
Screen m_screen;
|
||||
ScreenManager* m_screenManager;
|
||||
|
||||
|
||||
// ImGui Vars
|
||||
bool m_ShowTextureManagerWindow { false };
|
||||
bool m_ShowCameraWindow { false };
|
||||
bool m_ShowPlayerInfo { true };
|
||||
bool m_ShowPlayerInfo { false };
|
||||
};
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
void StartHeapControl();
|
||||
void DumpMemoryLeaks();
|
||||
|
||||
Point2f Viewport { 900.f, 500.f };
|
||||
|
||||
int SDL_main(int argv, char** args) {
|
||||
srand(static_cast<unsigned int>(time(nullptr)));
|
||||
|
||||
StartHeapControl();
|
||||
|
||||
auto pGame { new Game { Window { "Motherload - Verhulst, Bram - 1DAEGD16E", 900.f, 500.f } } };
|
||||
auto pGame { new Game { Window { "Motherload - Verhulst, Bram - 1DAEGD16E", Viewport.x, Viewport.y } } };
|
||||
pGame->Run();
|
||||
delete pGame;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user