diff --git a/.idea/.idea.Motherload/.idea/workspace.xml b/.idea/.idea.Motherload/.idea/workspace.xml index 17e454f..6d98451 100644 --- a/.idea/.idea.Motherload/.idea/workspace.xml +++ b/.idea/.idea.Motherload/.idea/workspace.xml @@ -11,18 +11,23 @@ - - - - + + + + + + - - + + + + + @@ -59,6 +64,429 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - { + "keyToString": { + "C++ Project.Game.executor": "Run", + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "ignore.virus.scanning.warn.message": "true", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "settings.editor.selected.configurable": "CppClangTidyOptionsId", + "vue.rearranger.settings.migration": "true" }, - "keyToStringList": { - "rider.external.source.directories": [ - "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache", - "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache", - "C:\\Users\\Bram\\AppData\\Local\\Symbols\\src" + "keyToStringList": { + "rider.external.source.directories": [ + "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache", + "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache", + "C:\\Users\\Bram\\AppData\\Local\\Symbols\\src" ] } -}]]> +} @@ -175,7 +603,13 @@ - + + + + + + + - @@ -204,7 +646,15 @@ + + + + + + diff --git a/Engine/structs.cpp b/Engine/structs.cpp index 8891027..041d288 100644 --- a/Engine/structs.cpp +++ b/Engine/structs.cpp @@ -1,6 +1,8 @@ #include "base.h" #include "structs.h" +#include + //----------------------------------------------------------------- // Window Constructors //----------------------------------------------------------------- @@ -52,6 +54,10 @@ Point2f operator/(float right, const Point2f& left) { Point2f operator*(float right, const Point2f& left) { return Point2f{ right * left.x, right * left.y }; } +std::ostream& operator<<(std::ostream& os, const Point2f& p) { + os << "Point2f( " << p.x << ", " << p.y << " )" << std::endl; + return os; +} //----------------------------------------------------------------- // Rectf Constructors //----------------------------------------------------------------- diff --git a/Engine/structs.h b/Engine/structs.h index cd60f17..c71ee55 100644 --- a/Engine/structs.h +++ b/Engine/structs.h @@ -26,6 +26,7 @@ struct Point2f Point2f operator*( const Point2f& other ) const; Point2f operator/( float other ) const; Point2f operator-( const Point2f& other ) const; + float x; float y; @@ -34,6 +35,8 @@ struct Point2f Point2f operator/(float right, const Point2f& left); Point2f operator*(float right, const Point2f& left); +std::ostream& operator<<(std::ostream& os, const Point2f& p); + struct Rectf { Rectf( ); diff --git a/Engine/utils.cpp b/Engine/utils.cpp index dcbbd8f..3a5483e 100644 --- a/Engine/utils.cpp +++ b/Engine/utils.cpp @@ -705,6 +705,17 @@ Point2f utils::GetMousePos() { //TODO: make the screen size a global or something return Point2f { float(x), float(500.f - y) }; } +bool utils::IsMouseButtonDown(int button) { + const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr); + if (pStates & SDL_BUTTON(button)) { + return true; + } + return false; +} +static Point2f ViewportSize{ 900.f, 500.f }; //TODO: somehow move this (Ask teacher) +Point2f utils::GetViewport() { + return ViewportSize; +} bool utils::isMouseDown(int button) { const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr); diff --git a/Engine/utils.h b/Engine/utils.h index 9316bb2..8b10de0 100644 --- a/Engine/utils.h +++ b/Engine/utils.h @@ -112,5 +112,10 @@ namespace utils bool isKeyUp(int keycode); Point2f GetMousePos(); + bool IsMouseButtonDown(int button); + + + Point2f GetViewport(); + } \ No newline at end of file diff --git a/Game/Game.vcxproj b/Game/Game.vcxproj index 6b2f98f..e7c7d7d 100644 --- a/Game/Game.vcxproj +++ b/Game/Game.vcxproj @@ -151,6 +151,7 @@ + MultiThreadedDebugDll EnableFastChecks @@ -301,6 +302,8 @@ + + MultiThreadedDebugDll EnableFastChecks @@ -458,10 +461,13 @@ + + + diff --git a/Game/GameManager.cpp b/Game/GameManager.cpp new file mode 100644 index 0000000..8faed90 --- /dev/null +++ b/Game/GameManager.cpp @@ -0,0 +1 @@ +#include "GameManager.h" diff --git a/Game/GameManager.h b/Game/GameManager.h new file mode 100644 index 0000000..0a34524 --- /dev/null +++ b/Game/GameManager.h @@ -0,0 +1,12 @@ +#pragma once +#include "Player.h" + +class GameManager +{ +public: + float balance{ 0.0f }; + float fuel{ 0.0f }; + +private: + GameManager() = default; +}; diff --git a/Game/Gui/Button.cpp b/Game/Gui/Button.cpp index b079d40..4ab2924 100644 --- a/Game/Gui/Button.cpp +++ b/Game/Gui/Button.cpp @@ -1 +1,37 @@ #include "Button.h" + +#include + +#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(); + } +} + + + diff --git a/Game/Gui/Button.h b/Game/Gui/Button.h index 240a95e..a7a134c 100644 --- a/Game/Gui/Button.h +++ b/Game/Gui/Button.h @@ -1,13 +1,22 @@ #pragma once +#include +#include + #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 onClick) { + m_OnClick = onClick; + } private: Texture* m_Texture{ nullptr }; @@ -16,4 +25,6 @@ private: bool m_IsHovered{ false }; bool m_IsPressed{ false }; + + std::function m_OnClick{ []() { std::cout << "Button not implemented" << std::endl; } }; }; diff --git a/Game/Gui/Screen.cpp b/Game/Gui/Screen.cpp index 6bbb512..38457a1 100644 --- a/Game/Gui/Screen.cpp +++ b/Game/Gui/Screen.cpp @@ -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(); + } } diff --git a/Game/Gui/Screen.h b/Game/Gui/Screen.h index 7f11fca..8c9d1a9 100644 --- a/Game/Gui/Screen.h +++ b/Game/Gui/Screen.h @@ -1,4 +1,7 @@ #pragma once +#include + +#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 m_Buttons; }; diff --git a/Game/Gui/Screens/FuelScreen.cpp b/Game/Gui/Screens/FuelScreen.cpp new file mode 100644 index 0000000..6656efa --- /dev/null +++ b/Game/Gui/Screens/FuelScreen.cpp @@ -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); +} diff --git a/Game/Gui/Screens/FuelScreen.h b/Game/Gui/Screens/FuelScreen.h new file mode 100644 index 0000000..c2e5702 --- /dev/null +++ b/Game/Gui/Screens/FuelScreen.h @@ -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; + +}; diff --git a/Game/Gui/Screens/ScreenManager.cpp b/Game/Gui/Screens/ScreenManager.cpp new file mode 100644 index 0000000..0bf7c33 --- /dev/null +++ b/Game/Gui/Screens/ScreenManager.cpp @@ -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() }; +} + diff --git a/Game/Gui/Screens/ScreenManager.h b/Game/Gui/Screens/ScreenManager.h new file mode 100644 index 0000000..25d01b0 --- /dev/null +++ b/Game/Gui/Screens/ScreenManager.h @@ -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 }; + +}; \ No newline at end of file diff --git a/Game/TextureManager.h b/Game/TextureManager.h index 9fd604d..01c815b 100644 --- a/Game/TextureManager.h +++ b/Game/TextureManager.h @@ -14,7 +14,6 @@ private: ~TextureManager(); static TextureManager* m_pInstance; -private: std::map m_Textures {}; }; diff --git a/Game/WorldLevel.cpp b/Game/WorldLevel.cpp index b93d667..5e5679a 100644 --- a/Game/WorldLevel.cpp +++ b/Game/WorldLevel.cpp @@ -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(); } diff --git a/Game/WorldLevel.h b/Game/WorldLevel.h index 9082302..6b420a7 100644 --- a/Game/WorldLevel.h +++ b/Game/WorldLevel.h @@ -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 }; }; diff --git a/Game/main.cpp b/Game/main.cpp index dc2efff..e73ddf6 100644 --- a/Game/main.cpp +++ b/Game/main.cpp @@ -6,12 +6,14 @@ void StartHeapControl(); void DumpMemoryLeaks(); +Point2f Viewport { 900.f, 500.f }; + int SDL_main(int argv, char** args) { srand(static_cast(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;