100 lines
2.3 KiB
C++
100 lines
2.3 KiB
C++
#include "SynEngine.hpp"
|
|
|
|
class GameWindow
|
|
{
|
|
private:
|
|
sf::Event event;
|
|
sf::Vector2i windowDimensions;
|
|
std::string title;
|
|
sf::Uint32 style;
|
|
sf::RenderWindow window;
|
|
Timer timer;
|
|
GameConfiguration *config;
|
|
Drawable *drawable;
|
|
void (Drawable::*draw)(sf::RenderWindow *);
|
|
bool isFocused;
|
|
bool fullscreen;
|
|
void CreateWindow();
|
|
void CloseWindow();
|
|
public:
|
|
GameWindow(std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
|
|
void BindDrawable(Drawable *, void (Drawable::*)(sf::RenderWindow *));
|
|
void Update();
|
|
bool IsOpen();
|
|
~GameWindow();
|
|
};
|
|
|
|
void GameWindow::Update()
|
|
{
|
|
while (window.pollEvent(event))
|
|
switch (event.type)
|
|
{
|
|
case sf::Event::Closed: CloseWindow(); break;
|
|
case sf::Event::LostFocus: isFocused = false; break;
|
|
case sf::Event::GainedFocus: isFocused = true; break;
|
|
case sf::Event::KeyPressed:
|
|
if(event.key.code == sf::Keyboard::Key::Escape)
|
|
CloseWindow();
|
|
GameManager::KeyPress(event.key.code);
|
|
break;
|
|
}
|
|
|
|
timer.UpdateTime();
|
|
|
|
if(!isFocused)
|
|
return;
|
|
|
|
window.clear(config -> GetBackgroundColor());
|
|
((drawable)->*(draw))(&window);
|
|
|
|
window.display();
|
|
}
|
|
|
|
void GameWindow::CreateWindow()
|
|
{
|
|
if(window.isOpen())
|
|
return;
|
|
|
|
sf::VideoMode videoMode(windowDimensions.x, windowDimensions.y);
|
|
window.create(videoMode, title, fullscreen ? sf::Style::Fullscreen : style);
|
|
window.setVerticalSyncEnabled(true);
|
|
window.setFramerateLimit(60);
|
|
timer.ResetTimer();
|
|
}
|
|
|
|
void GameWindow::CloseWindow()
|
|
{
|
|
if(!window.isOpen())
|
|
return;
|
|
|
|
window.close();
|
|
}
|
|
|
|
void GameWindow::BindDrawable(Drawable *drawable, void (Drawable::*draw)(sf::RenderWindow *))
|
|
{
|
|
this -> drawable = drawable;
|
|
this -> draw = draw;
|
|
}
|
|
|
|
GameWindow::GameWindow(std::string title, sf::Uint32 style)
|
|
{
|
|
config = GameManager::GetConfig();
|
|
windowDimensions = config -> GetScreenDimensions();
|
|
fullscreen = config -> IsFullscreen();
|
|
this -> style = style;
|
|
this -> title = title;
|
|
isFocused = true;
|
|
|
|
CreateWindow();
|
|
}
|
|
|
|
bool GameWindow::IsOpen()
|
|
{
|
|
return window.isOpen();
|
|
}
|
|
|
|
GameWindow::~GameWindow()
|
|
{
|
|
CloseWindow();
|
|
}
|