Handling window resize and fixed spelling

This commit is contained in:
Maximilian Giller 2023-05-04 23:01:27 +02:00
parent 4dc9702747
commit bc0ad0b9bc
7 changed files with 63 additions and 28 deletions

View file

@ -3,12 +3,18 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#define DEVELOPER_MODE true
// FPS
#define FRAME_RATE 60 #define FRAME_RATE 60
#define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE)) #define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE))
// Window settings
#define ANTIALIASINGLEVEL 8 #define ANTIALIASINGLEVEL 8
#define KEY_REPEAT_ENABLED false
#define WOLRD_TO_ISO_SCALE 50.0f // Graphic settings
#define WORLD_TO_ISO_SCALE 50.0f
#endif //HOLESOME_CONFIG_H #endif //HOLESOME_CONFIG_H

View file

@ -12,7 +12,7 @@ IsometricCoordinates CoordinateTransformer::worldToIsometric(WorldCoordinates wo
Eigen::Vector3f worldCoordinatesVector; Eigen::Vector3f worldCoordinatesVector;
worldCoordinatesVector << worldCoordinates.x, worldCoordinates.y, worldCoordinates.z; worldCoordinatesVector << worldCoordinates.x, worldCoordinates.y, worldCoordinates.z;
Eigen::Vector3f isoCoordinatesVector = worldToIsometricMatrix * worldCoordinatesVector * WOLRD_TO_ISO_SCALE; Eigen::Vector3f isoCoordinatesVector = worldToIsometricMatrix * worldCoordinatesVector * WORLD_TO_ISO_SCALE;
return IsometricCoordinates( return IsometricCoordinates(
isoCoordinatesVector.x(), // x isoCoordinatesVector.x(), // x

View file

@ -5,17 +5,21 @@
#include "game.h" #include "game.h"
#include "../config.h" #include "../config.h"
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), gameObjects() { Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), gameObjects()
{
} }
void Game::run() { void Game::run()
{
sf::Clock clock; sf::Clock clock;
sf::Time TimeSinceLastUpdate = sf::seconds(0); sf::Time TimeSinceLastUpdate = sf::seconds(0);
while (window->isOpen()) { while (window->isOpen())
{
processEvents(); processEvents();
TimeSinceLastUpdate += clock.restart(); TimeSinceLastUpdate += clock.restart();
while (TimeSinceLastUpdate >= FRAME_TIME) { while (TimeSinceLastUpdate >= FRAME_TIME)
{
TimeSinceLastUpdate -= FRAME_TIME; TimeSinceLastUpdate -= FRAME_TIME;
update(); update();
@ -25,50 +29,73 @@ void Game::run() {
} }
} }
void Game::exit() { void Game::exit()
{
window->close(); window->close();
} }
void Game::drawFrame() { void Game::drawFrame()
{
window->clear(sf::Color::Black); window->clear(sf::Color::Black);
for (auto &gameObject: gameObjects) { for (auto &gameObject: gameObjects)
{
gameObject->draw(window.get()); gameObject->draw(window.get());
} }
window->display(); window->display();
} }
Game::~Game() { Game::~Game()
for (auto &gameObject: gameObjects) { {
for (auto &gameObject: gameObjects)
{
delete gameObject; delete gameObject;
} }
} }
void Game::addGameObject(GameObject *gameObject) { void Game::addGameObject(GameObject *gameObject)
{
gameObjects.push_back(gameObject); gameObjects.push_back(gameObject);
} }
void Game::update() { void Game::update()
for (auto &gameObject: gameObjects) { {
for (auto &gameObject: gameObjects)
{
gameObject->update(); gameObject->update();
} }
} }
void Game::processEvents()
void Game::processEvents() { {
sf::Event event; sf::Event event;
while (window->pollEvent(event)) { while (window->pollEvent(event))
switch (event.type) { {
switch (event.type)
{
case sf::Event::KeyPressed: case sf::Event::KeyPressed:
// Close game on Escape or Q in DEV Mode
if (DEVELOPER_MODE && (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q))
{
exit();
}
break; break;
case sf::Event::KeyReleased: case sf::Event::KeyReleased:
break; break;
case sf::Event::Closed: case sf::Event::Closed:
exit(); exit();
break; break;
case sf::Event::Resized:
handleWindowResize(event);
break;
default: default:
break; break;
} }
} }
} }
void Game::handleWindowResize(const sf::Event &event)
{
window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
}

View file

@ -10,28 +10,29 @@
#include "../logging/easylogging++.h" #include "../logging/easylogging++.h"
#include "game_object.h" #include "game_object.h"
class Game { class Game
private: {
std::shared_ptr<sf::RenderWindow> window;
std::vector<GameObject *> gameObjects;
public: public:
explicit Game(std::shared_ptr<sf::RenderWindow> window); explicit Game(std::shared_ptr<sf::RenderWindow> window);
~Game(); ~Game();
void run(); void run();
/***
* Stops the game and closes the window.
*/
void exit(); void exit();
void addGameObject(GameObject *gameObject); void addGameObject(GameObject *gameObject);
private:
std::shared_ptr<sf::RenderWindow> window;
std::vector<GameObject *> gameObjects;
void drawFrame(); void drawFrame();
void update(); void update();
void processEvents(); void processEvents();
void handleWindowResize(const sf::Event &event);
}; };

View file

@ -44,4 +44,5 @@ sf::ContextSettings GameFactory::getAdditionalSettings() {
void GameFactory::applyAdditionalWindowConfig(sf::RenderWindow *window) { void GameFactory::applyAdditionalWindowConfig(sf::RenderWindow *window) {
window->setFramerateLimit(FRAME_RATE); window->setFramerateLimit(FRAME_RATE);
window->setKeyRepeatEnabled(KEY_REPEAT_ENABLED);
} }

View file

@ -9,7 +9,7 @@
class GameFactory class GameFactory
{ {
public: public:
static std::shared_ptr<Game> createWindowed(const std::string &title, int width, int height); static std::shared_ptr<Game> createWindowed(const std::string &title, int width = 960, int height = 540);
static std::shared_ptr<Game> createFullscreen(const std::string &title); static std::shared_ptr<Game> createFullscreen(const std::string &title);
private: private:

View file

@ -10,7 +10,7 @@ int main(int argc, char *argv[])
{ {
START_EASYLOGGINGPP(argc, argv); START_EASYLOGGINGPP(argc, argv);
auto game = GameFactory::createFullscreen("Holesome"); auto game = GameFactory::createWindowed("Holesome");
game->addGameObject(new GridDebugLayer(0, 50, 0, 50)); game->addGameObject(new GridDebugLayer(0, 50, 0, 50));