SFML_Experiments/3DTest/Window.hpp

132 lines
2.8 KiB
C++
Raw Normal View History

2020-01-08 14:45:47 +03:00
#include "SynGame.hpp"
class Window
{
2020-01-08 16:19:25 +03:00
protected:
2020-01-08 14:45:47 +03:00
sf::RenderWindow window;
sf::Event event;
sf::Vector2u size;
2020-01-08 16:19:25 +03:00
sf::Vector2u windowedSize;
2020-01-08 14:45:47 +03:00
std::string title;
sf::Uint32 style;
Timer timer;
bool isFocused;
2020-01-08 16:19:25 +03:00
bool fullscreen;
2020-01-08 14:45:47 +03:00
public:
2020-01-08 16:19:25 +03:00
Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
virtual void CreateWindow();
virtual void CloseWindow();
virtual void Update();
2020-01-08 14:45:47 +03:00
void SetFrameRate(int = 0);
void SetTitle(std::string);
2020-01-08 16:19:25 +03:00
virtual void SetSize(unsigned int, unsigned int);
2020-01-08 14:45:47 +03:00
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;
2020-01-08 16:19:25 +03:00
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F))
{
fullscreen = !fullscreen;
CloseWindow();
CreateWindow();
}
2020-01-08 14:45:47 +03:00
}
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;
2020-01-08 16:19:25 +03:00
windowedSize.x = width;
windowedSize.y = height;
2020-01-08 14:45:47 +03:00
isFocused = true;
2020-01-08 16:19:25 +03:00
fullscreen = false;
2020-01-08 14:45:47 +03:00
SetFrameRate();
CreateWindow();
}
void Window::CreateWindow()
{
if(window.isOpen())
return;
2020-01-08 16:19:25 +03:00
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);
}
2020-01-08 14:45:47 +03:00
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);
2020-01-08 16:19:25 +03:00
sf::Vector2i pos = window.getPosition();
CloseWindow();
2020-01-08 14:45:47 +03:00
window.setSize(size);
2020-01-08 16:19:25 +03:00
CreateWindow();
window.setPosition(pos);
2020-01-08 14:45:47 +03:00
}
bool Window::IsOpen()
{
return window.isOpen();
}