162 lines
3.5 KiB
C++
162 lines
3.5 KiB
C++
//
|
|
// Created by max on 26.04.23.
|
|
//
|
|
|
|
#include <utility>
|
|
#include <SFML/Window/Event.hpp>
|
|
#include <SFML/Graphics/RenderWindow.hpp>
|
|
|
|
#include "game.h"
|
|
|
|
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), isRunning(false), gameObjects()
|
|
{
|
|
}
|
|
|
|
void Game::run()
|
|
{
|
|
if (isRunning)
|
|
{
|
|
LOG(WARNING) << "Game is already running";
|
|
return;
|
|
}
|
|
|
|
isRunning = true;
|
|
while (isRunning)
|
|
{
|
|
renderFrame();
|
|
|
|
// Process any events that have occurred since the last iteration
|
|
sf::Event event{};
|
|
while (window->pollEvent(event))
|
|
{
|
|
// If the event is to close the window, then close it
|
|
if (event.type == sf::Event::Closed)
|
|
{
|
|
exit();
|
|
}
|
|
|
|
if (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q)
|
|
{
|
|
exit();
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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();
|
|
// }
|
|
}
|
|
|
|
void Game::exit()
|
|
{
|
|
isRunning = false;
|
|
window->close();
|
|
}
|
|
|
|
void Game::renderFrame()
|
|
{
|
|
for (auto &gameObject: gameObjects)
|
|
{
|
|
gameObject->draw(*window, sf::RenderStates::Default);
|
|
}
|
|
|
|
window->display();
|
|
}
|
|
|
|
Game::~Game()
|
|
{
|
|
for (auto &gameObject: gameObjects)
|
|
{
|
|
delete gameObject;
|
|
}
|
|
}
|
|
|
|
|
|
//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();
|
|
//}
|