Long-Starlight-Snake-Game/GameWindow.hpp

116 lines
2.8 KiB
C++

#include "SynEngine.hpp"
Timer GameManager::timer;
class GameWindow
{
private:
sf::Event event;
sf::Vector2i windowDimensions;
std::string title;
sf::Uint32 style;
sf::RenderWindow window;
Timer *timer;
GameConfiguration *config;
IDrawable *drawable;
void (IDrawable::*draw)(sf::RenderWindow *);
IBehaviour *behaviour;
void (IBehaviour::*behaviourUpdate)();
bool isFocused;
bool fullscreen;
void CreateWindow();
void CloseWindow();
public:
GameWindow(std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
void BindDrawable(IDrawable *, void (IDrawable::*)(sf::RenderWindow *));
void BindBehaviour(IBehaviour *, void (IBehaviour::*)());
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());
if(behaviour)
((behaviour)->*(behaviourUpdate))();
if(drawable)
((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 = &GameManager::timer;
timer -> ResetTimer();
}
void GameWindow::CloseWindow()
{
if(!window.isOpen())
return;
window.close();
}
void GameWindow::BindDrawable(IDrawable *drawable, void (IDrawable::*draw)(sf::RenderWindow *))
{
this -> drawable = drawable;
this -> draw = draw;
}
void GameWindow::BindBehaviour(IBehaviour *behaviour, void (IBehaviour::*behaviourUpdate)())
{
this -> behaviour = behaviour;
this -> behaviourUpdate = behaviourUpdate;
}
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();
}