Main Window Class
New Placeholder Spritesheet Timer Class Key Press And Small Fixes
This commit is contained in:
@@ -3,17 +3,97 @@
|
||||
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(/* args */);
|
||||
GameWindow(std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
|
||||
void BindDrawable(Drawable *, void (Drawable::*)(sf::RenderWindow *));
|
||||
void Update();
|
||||
bool IsOpen();
|
||||
~GameWindow();
|
||||
};
|
||||
|
||||
GameWindow::GameWindow(/* args */)
|
||||
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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user