Main Window Class

New Placeholder Spritesheet
Timer Class
Key Press
And Small Fixes
This commit is contained in:
Syntriax 2020-06-08 23:59:42 +03:00
parent 7b2841235d
commit 3b8affae37
8 changed files with 164 additions and 21 deletions

View File

@ -10,8 +10,8 @@ class Cell : public Drawable
bool CreateSprite(); bool CreateSprite();
public: public:
Cell(); Cell();
Cell(unsigned int, unsigned int, unsigned int = 1); Cell(unsigned int, unsigned int, unsigned int = 0);
void Set(unsigned int, unsigned int, unsigned int = 1); void Set(unsigned int, unsigned int, unsigned int = 0);
void UpdateSprite(unsigned int); void UpdateSprite(unsigned int);
void UpdateRotation(unsigned); void UpdateRotation(unsigned);
void Draw(sf::RenderWindow *); void Draw(sf::RenderWindow *);
@ -47,7 +47,8 @@ bool Cell::CreateSprite()
void Cell::UpdateSprite(unsigned int index) void Cell::UpdateSprite(unsigned int index)
{ {
if(index == spriteIndex) return; if(index == spriteIndex) return;
sprite.setTextureRect(GameManager::GetConfig() -> GetSpriteRect(index)); spriteIndex = index;
sprite.setTextureRect(GameManager::GetConfig() -> GetSpriteRect(spriteIndex));
} }
void Cell::UpdateRotation(unsigned direction) void Cell::UpdateRotation(unsigned direction)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -28,6 +28,7 @@ class GameConfiguration
public: public:
GameConfiguration(); GameConfiguration();
GameConfiguration(std::string = std::string()); GameConfiguration(std::string = std::string());
bool IsFullscreen();
float GetSpriteSizeMultiplier(); float GetSpriteSizeMultiplier();
float GetAudioVolume(); float GetAudioVolume();
sf::Color GetBackgroundColor(); sf::Color GetBackgroundColor();
@ -190,18 +191,15 @@ void GameConfiguration::WriteValuesToFile(std::string path)
void GameConfiguration::LoadDefaultValues() void GameConfiguration::LoadDefaultValues()
{ {
windowDimensions = sf::Vector2i(800, 600); windowDimensions = sf::Vector2i(800, 600);
// !! Placeholder values! Change after real sprites are added! !!
spriteSizeMultiplier = 1.0;
backgroundColor = sf::Color::White; backgroundColor = sf::Color::White;
gridSize = sf::Vector2i(10, 10); gridSize = sf::Vector2i(10, 10);
LoadSpriteSheet("Data\\SpriteSheet.png"); LoadSpriteSheet("Data\\SpriteSheet.png");
// !! Placeholder values! Change after real sprites are added! !! spriteSheetCellSize = sf::Vector2i(64, 64);
spriteSheetCellSize = sf::Vector2i(50, 50); spriteSizeMultiplier = 1.0;
fullscreen = false;
fullscreen = false;
audioVolume = 1.0; audioVolume = 1.0;
} }
@ -234,6 +232,11 @@ sf::IntRect GameConfiguration::GetSpriteRect(unsigned int index)
return spriteSheetRect; return spriteSheetRect;
} }
bool GameConfiguration::IsFullscreen()
{
return fullscreen;
}
sf::Vector2i GameConfiguration::GetGridSize() sf::Vector2i GameConfiguration::GetGridSize()
{ {
return gridSize; return gridSize;

View File

@ -5,13 +5,27 @@ class GameManager
{ {
private: private:
static GameConfiguration *config; static GameConfiguration *config;
static unsigned lastInputDirection;
public: public:
static void KeyPress(sf::Keyboard::Key);
static void SetConfig(GameConfiguration &); static void SetConfig(GameConfiguration &);
static GameConfiguration *GetConfig(); static GameConfiguration *GetConfig();
}; };
GameConfiguration *GameManager::config = NULL; GameConfiguration *GameManager::config = NULL;
unsigned GameManager::lastInputDirection = 0;
void GameManager::KeyPress(sf::Keyboard::Key keycode)
{
switch (keycode)
{
case sf::Keyboard::Key::Right: case sf::Keyboard::Key::D: GameManager::lastInputDirection = 0; break;
case sf::Keyboard::Key::Down: case sf::Keyboard::Key::S: GameManager::lastInputDirection = 1; break;
case sf::Keyboard::Key::Left: case sf::Keyboard::Key::A: GameManager::lastInputDirection = 2; break;
case sf::Keyboard::Key::Up: case sf::Keyboard::Key::W: GameManager::lastInputDirection = 3; break;
}
}
void GameManager::SetConfig(GameConfiguration &config) void GameManager::SetConfig(GameConfiguration &config)
{ {

View File

@ -3,17 +3,97 @@
class GameWindow class GameWindow
{ {
private: private:
sf::Event event;
sf::Vector2i windowDimensions; sf::Vector2i windowDimensions;
std::string title;
sf::Uint32 style;
sf::RenderWindow window; sf::RenderWindow window;
Timer timer;
GameConfiguration *config;
Drawable *drawable;
void (Drawable::*draw)(sf::RenderWindow *);
bool isFocused;
bool fullscreen;
void CreateWindow();
void CloseWindow();
public: 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::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() GameWindow::~GameWindow()
{ {
CloseWindow();
} }

View File

@ -5,6 +5,7 @@
#include <fstream> #include <fstream>
#include <cmath> #include <cmath>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "Timer.hpp"
#include "GameManager.hpp" #include "GameManager.hpp"
#include "GameConfiguration.hpp" #include "GameConfiguration.hpp"
#include "Drawable.hpp" #include "Drawable.hpp"

47
Timer.hpp Normal file
View File

@ -0,0 +1,47 @@
#include "SynEngine.hpp"
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;
}

View File

@ -4,20 +4,17 @@ int main(int argc, char const *argv[])
{ {
GameConfiguration config = GameConfiguration("Configurations"); GameConfiguration config = GameConfiguration("Configurations");
GameManager::SetConfig(config); GameManager::SetConfig(config);
GameWindow window("Long Starlight");
Grid grid = Grid(); Grid grid = Grid();
grid.SetGrid(); grid.SetGrid();
window.BindDrawable(&grid, &Drawable::Draw);
sf::RenderWindow render(sf::VideoMode(800, 600), "Test");
if (render.isOpen()) // Quick Test
{
render.clear(config.GetBackgroundColor());
grid.UpdateCell(0, 0, 2); grid.UpdateCell(0, 0, 2);
grid.UpdateCell(0, 1, 2, 1); grid.UpdateCell(0, 1, 2, 1);
grid.Draw(&render);
render.display(); while (window.IsOpen())
sf::sleep(sf::milliseconds(5000)); window.Update();
}
std::cout << "/)(\\"; std::cout << "/)(\\";
return 0; return 0;