Initial GUI

This commit is contained in:
Asrın Doğan 2020-03-01 14:13:23 +03:00
parent 99305779d5
commit 9c4dfcf5ec
7 changed files with 315 additions and 4 deletions

View File

@ -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

23
.vscode/settings.json vendored
View File

@ -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"
}

59
GUITest/GUIElements.hpp Normal file
View File

@ -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()
{
}

27
GUITest/GUIWindow.hpp Normal file
View File

@ -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()
{
}

14
GUITest/SynGame.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef SynClasses
#define SynClasses
#define DegToRad 0.0174533
#include <SFML/Graphics.hpp>
#include <sstream>
#include <vector>
#include <iostream>
#include <cmath>
#include "Window.hpp"
#include "GUIWindow.hpp"
#include "GUIElements.hpp"
#endif

181
GUITest/Window.hpp Normal file
View File

@ -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

11
GUITest/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "SynGame.hpp"
int main()
{
GUIWindow window(960, 540, "GUI Test");
while (window.IsOpen())
window.Update();
return 0;
}