From 9c4dfcf5ecf7fc22212b5b60772debd0bac5c37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asr=C4=B1n=20Do=C4=9Fan?= Date: Sun, 1 Mar 2020 14:13:23 +0300 Subject: [PATCH] Initial GUI --- .vscode/c_cpp_properties.json | 4 +- .vscode/settings.json | 23 ++++- GUITest/GUIElements.hpp | 59 +++++++++++ GUITest/GUIWindow.hpp | 27 +++++ GUITest/SynGame.hpp | 14 +++ GUITest/Window.hpp | 181 ++++++++++++++++++++++++++++++++++ GUITest/main.cpp | 11 +++ 7 files changed, 315 insertions(+), 4 deletions(-) create mode 100644 GUITest/GUIElements.hpp create mode 100644 GUITest/GUIWindow.hpp create mode 100644 GUITest/SynGame.hpp create mode 100644 GUITest/Window.hpp create mode 100644 GUITest/main.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index d6b1617..de09f8a 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -12,8 +12,8 @@ "_UNICODE" ], "windowsSdkVersion": "10.0.17763.0", - "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe", - "intelliSenseMode": "msvc-x64" + "compilerPath": "C:/MinGW/bin/g++.exe", + "intelliSenseMode": "gcc-x86" } ], "version": 4 diff --git a/.vscode/settings.json b/.vscode/settings.json index 7960908..b25e0b5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -51,6 +51,25 @@ "iostream": "cpp", "list": "cpp", "unordered_set": "cpp", - "xhash": "cpp" - } + "xhash": "cpp", + "*.tcc": "cpp", + "atomic": "cpp", + "array": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cstdarg": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "functional": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "fstream": "cpp", + "concepts": "cpp", + "queue": "cpp" + }, + "C_Cpp.errorSquiggles": "Enabled" } \ No newline at end of file diff --git a/GUITest/GUIElements.hpp b/GUITest/GUIElements.hpp new file mode 100644 index 0000000..e621d9e --- /dev/null +++ b/GUITest/GUIElements.hpp @@ -0,0 +1,59 @@ +#include "SynGame.hpp" + +class SynButton +{ + private: + sf::Text textDisplay; + std::string text; + static sf::Font font; + int x; + int y; + int width; + int height; + void (*action)(int); + int *value; + public: + SynButton(std::string, int, int, int, int); + void Bind(void (*)(int), int *); + bool IsMouseOver(int, int); + void Click(); + ~SynButton(); +}; + +SynButton::SynButton(std::string text, int x, int y, int width, int height) +{ + textDisplay.setFont(font); + textDisplay.setString(text); + this -> x = x; + this -> y = y; + this -> width = width; + this -> height = height; +} + +void SynButton::Bind(void (*action)(int), int *value) +{ + this -> action = action; + this -> value = value; +} + +bool SynButton::IsMouseOver(int mouseX, int mouseY) +{ + bool isOver; + int halfWidth = width / 2; + int halfHeight = height / 2; + isOver = mouseX <= x + halfWidth && + mouseX >= x - halfWidth && + mouseY <= y + halfHeight && + mouseY >= y - halfHeight; + return isOver; +} + +void SynButton::Click() +{ + if(value) + action(*value); +} + +SynButton::~SynButton() +{ +} diff --git a/GUITest/GUIWindow.hpp b/GUITest/GUIWindow.hpp new file mode 100644 index 0000000..80d516c --- /dev/null +++ b/GUITest/GUIWindow.hpp @@ -0,0 +1,27 @@ +#include "SynGame.hpp" + +class GUIWindow : public Window +{ + private: + public: + GUIWindow(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close); + void Update(); + ~GUIWindow(); +}; + +GUIWindow::GUIWindow(unsigned int width, unsigned int height, std::string title, sf::Uint32 style) : Window(width, height, title, style) +{ + +} + +void GUIWindow::Update() +{ + if(!isFocused) + return; + + Window::Update(); +} + +GUIWindow::~GUIWindow() +{ +} diff --git a/GUITest/SynGame.hpp b/GUITest/SynGame.hpp new file mode 100644 index 0000000..f52362b --- /dev/null +++ b/GUITest/SynGame.hpp @@ -0,0 +1,14 @@ +#ifndef SynClasses + #define SynClasses + + #define DegToRad 0.0174533 + + #include + #include + #include + #include + #include + #include "Window.hpp" + #include "GUIWindow.hpp" + #include "GUIElements.hpp" +#endif diff --git a/GUITest/Window.hpp b/GUITest/Window.hpp new file mode 100644 index 0000000..3ac1b79 --- /dev/null +++ b/GUITest/Window.hpp @@ -0,0 +1,181 @@ +#include "SynGame.hpp" + +#pragma region Timer + class Timer + { + private: + sf::Clock clock; + float deltaTime; + float timePassed; + public: + Timer(); + void ResetTimer(); + void UpdateTime(); + float GetDeltaTime(); + float GetTimePassed(); + }; + + Timer::Timer() + { + clock.restart(); + timePassed = 0.0; + deltaTime = 0.0; + } + + void Timer::ResetTimer() + { + clock.restart(); + timePassed = 0.0; + deltaTime = 0.0; + } + + void Timer::UpdateTime() + { + float newTimePassed = clock.getElapsedTime().asSeconds(); + + deltaTime = newTimePassed - timePassed; + timePassed = newTimePassed; + } + + float Timer::GetDeltaTime() + { + return deltaTime; + } + + float Timer::GetTimePassed() + { + return timePassed; + } +#pragma endregion + +#pragma region Window + class Window + { + protected: + sf::RenderWindow window; + sf::Event event; + sf::Vector2u size; + sf::Vector2u windowedSize; + std::string title; + sf::Uint32 style; + Timer timer; + bool isFocused; + bool fullscreen; + virtual void Update(); + public: + Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close); + virtual void CreateWindow(); + virtual void CloseWindow(); + void SetFrameRate(int = 0); + void SetTitle(std::string); + virtual void SetSize(unsigned int, unsigned int); + bool IsOpen(); + }; + + void Window::Update() + { + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + CloseWindow(); + else if (event.type == sf::Event::LostFocus) + isFocused = false; + else if (event.type == sf::Event::GainedFocus) + isFocused = true; + if(sf::Keyboard::isKeyPressed(sf::Keyboard::F)) + { + fullscreen = !fullscreen; + CloseWindow(); + CreateWindow(); + } + } + + if(!isFocused) + return; + + timer.UpdateTime(); + } + + Window::Window(unsigned int width, unsigned int height, std::string title, sf::Uint32 style) + { + this -> size.x = width; + this -> size.y = height; + this -> title = title; + this -> style = style; + + windowedSize.x = width; + windowedSize.y = height; + + isFocused = true; + fullscreen = false; + SetFrameRate(); + CreateWindow(); + } + + void Window::CreateWindow() + { + if(window.isOpen()) + return; + + if(!fullscreen) + { + size = windowedSize; + sf::VideoMode videoMode(windowedSize.x, windowedSize.y); + window.create(videoMode, title, style); + } + else + { + sf::VideoMode videoMode(sf::VideoMode::getDesktopMode()); + size.x = videoMode.width; + size.y = videoMode.height; + window.create(videoMode, title, sf::Style::Fullscreen); + } + + timer.ResetTimer(); + } + + void Window::CloseWindow() + { + if(!window.isOpen()) + return; + + window.close(); + } + + void Window::SetFrameRate(int rate) + { + if(rate == 0) + { + window.setVerticalSyncEnabled(true); + return; + } + + if(rate < 0) + window.setFramerateLimit(10000); + else + window.setFramerateLimit(rate); + + window.setVerticalSyncEnabled(false); + } + + void Window::SetTitle(std::string title) + { + this -> title = title; + window.setTitle(title); + } + + void Window::SetSize(unsigned int width, unsigned int height) + { + size = sf::Vector2u(width, height); + sf::Vector2i pos = window.getPosition(); + CloseWindow(); + window.setSize(size); + CreateWindow(); + window.setPosition(pos); + } + + bool Window::IsOpen() + { + return window.isOpen(); + } +#pragma endregion diff --git a/GUITest/main.cpp b/GUITest/main.cpp new file mode 100644 index 0000000..af1d295 --- /dev/null +++ b/GUITest/main.cpp @@ -0,0 +1,11 @@ +#include "SynGame.hpp" + +int main() +{ + GUIWindow window(960, 540, "GUI Test"); + + while (window.IsOpen()) + window.Update(); + + return 0; +} \ No newline at end of file