holesome/src/game/game.cpp

163 lines
3.5 KiB
C++
Raw Normal View History

2023-04-26 22:48:15 +02:00
//
// Created by max on 26.04.23.
//
#include <utility>
2023-04-27 23:05:19 +02:00
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
2023-04-26 22:48:15 +02:00
#include "game.h"
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), isRunning(false), gameObjects()
{
2023-04-26 22:48:15 +02:00
}
void Game::run()
{
if (isRunning)
{
2023-04-27 23:05:19 +02:00
LOG(WARNING) << "Game is already running";
2023-04-26 22:48:15 +02:00
return;
}
isRunning = true;
while (isRunning)
{
2023-04-27 23:05:19 +02:00
renderFrame();
2023-04-26 22:48:15 +02:00
2023-04-27 23:05:19 +02:00
// Process any events that have occurred since the last iteration
sf::Event event{};
while (window->pollEvent(event))
{
2023-04-27 23:05:19 +02:00
// If the event is to close the window, then close it
if (event.type == sf::Event::Closed)
{
2023-04-27 23:05:19 +02:00
exit();
}
if (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q)
{
2023-04-27 23:05:19 +02:00
exit();
}
}
2023-04-26 22:48:15 +02:00
}
2023-05-03 01:33:14 +02:00
// sf::Clock clock;
// sf::Time TimeSinceLastUpdate = sf::seconds(0);
//
// while (Window.isOpen())
// {
// processEvents();
// TimeSinceLastUpdate += clock.restart();
// while (TimeSinceLastUpdate >= FPS_TIME)
// {
// TimeSinceLastUpdate -= FPS_TIME;
//
// update();
// processEvents();
// }
// render();
// }
2023-04-26 22:48:15 +02:00
}
void Game::exit()
{
2023-04-26 22:48:15 +02:00
isRunning = false;
window->close();
2023-04-27 23:05:19 +02:00
}
void Game::renderFrame()
{
for (auto &gameObject: gameObjects)
{
gameObject->draw(*window, sf::RenderStates::Default);
2023-04-27 23:05:19 +02:00
}
window->display();
2023-04-27 23:05:19 +02:00
}
Game::~Game()
{
for (auto &gameObject: gameObjects)
{
2023-04-27 23:05:19 +02:00
delete gameObject;
}
}
2023-05-03 01:33:14 +02:00
//void Game::initWindow()
//{
// sf::ContextSettings settings;
// settings.antialiasingLevel = 8;
// Window.create(sf::VideoMode(SCREEN_W, SCREEN_H), "Ping & Pong", sf::Style::Default, settings);
//
// Window.setFramerateLimit(FRAMERATE);
//}
//
//void Game::update()
//{
// if (bGameRun && tMultiBallTime.asSeconds() == -1)
// {
// for (std::list<CBall*>::iterator i = lBalls.begin(); i != lBalls.end(); ++i)
// (*i)->update();
//
// updateBars();
//
// updatekBallGoal();
// }
// updateScoreString();
//
// updateMultiBall();
//}
//
//void Game::processEvents()
//{
// sf::Event event;
// while (Window.pollEvent(event))
// {
// switch (event.type)
// {
// case sf::Event::KeyPressed:
// {
// //World.handleInput(event.key.code, true);
// }break;
// case sf::Event::KeyReleased:
// {
// if (event.key.code == sf::Keyboard::P)
// bGameRun = !bGameRun;
// //World.handleInput(event.key.code, false);
// }break;
// case sf::Event::Closed:
// Window.close();
// break;
// }
// }
//}
//
//void Game::render()
//{
// Window.clear();
//
// //Bg
// Window.draw(Background);
//
// //Bar
// for (std::list<CBar*>::iterator i = lBars.begin(); i != lBars.end(); ++i)
// (*i)->draw(&Window);
//
// //Ball
// if ((int)(tMultiBallTime.asSeconds() * 10) % 10 < 5.f)
// for (std::list<CBall*>::iterator i = lBalls.begin(); i != lBalls.end(); ++i)
// (*i)->draw(&Window);
//
// //Score
// for (int i = 0; i < 2; i++)
// Window.draw(sScoreString[i]);
//
// //pausemenu
// if (!bGameRun)
// PauseMenu.draw(&Window);
//
// Window.display();
//}