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>
#define DEVELOPER_MODE true
// FPS
#define FRAME_RATE 60
#define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE))
// Window settings
#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

View file

@ -12,7 +12,7 @@ IsometricCoordinates CoordinateTransformer::worldToIsometric(WorldCoordinates wo
Eigen::Vector3f worldCoordinatesVector;
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(
isoCoordinatesVector.x(), // x

View file

@ -5,17 +5,21 @@
#include "game.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::Time TimeSinceLastUpdate = sf::seconds(0);
while (window->isOpen()) {
while (window->isOpen())
{
processEvents();
TimeSinceLastUpdate += clock.restart();
while (TimeSinceLastUpdate >= FRAME_TIME) {
while (TimeSinceLastUpdate >= FRAME_TIME)
{
TimeSinceLastUpdate -= FRAME_TIME;
update();
@ -25,50 +29,73 @@ void Game::run() {
}
}
void Game::exit() {
void Game::exit()
{
window->close();
}
void Game::drawFrame() {
void Game::drawFrame()
{
window->clear(sf::Color::Black);
for (auto &gameObject: gameObjects) {
for (auto &gameObject: gameObjects)
{
gameObject->draw(window.get());
}
window->display();
}
Game::~Game() {
for (auto &gameObject: gameObjects) {
Game::~Game()
{
for (auto &gameObject: gameObjects)
{
delete gameObject;
}
}
void Game::addGameObject(GameObject *gameObject) {
void Game::addGameObject(GameObject *gameObject)
{
gameObjects.push_back(gameObject);
}
void Game::update() {
for (auto &gameObject: gameObjects) {
void Game::update()
{
for (auto &gameObject: gameObjects)
{
gameObject->update();
}
}
void Game::processEvents() {
void Game::processEvents()
{
sf::Event event;
while (window->pollEvent(event)) {
switch (event.type) {
while (window->pollEvent(event))
{
switch (event.type)
{
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;
case sf::Event::KeyReleased:
break;
case sf::Event::Closed:
exit();
break;
case sf::Event::Resized:
handleWindowResize(event);
break;
default:
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 "game_object.h"
class Game {
private:
std::shared_ptr<sf::RenderWindow> window;
std::vector<GameObject *> gameObjects;
class Game
{
public:
explicit Game(std::shared_ptr<sf::RenderWindow> window);
~Game();
void run();
/***
* Stops the game and closes the window.
*/
void exit();
void addGameObject(GameObject *gameObject);
private:
std::shared_ptr<sf::RenderWindow> window;
std::vector<GameObject *> gameObjects;
void drawFrame();
void update();
void processEvents();
void handleWindowResize(const sf::Event &event);
};

View file

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

View file

@ -9,7 +9,7 @@
class GameFactory
{
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);
private:

View file

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