Simplified game structure and back to a working state
This commit is contained in:
parent
d55e3fd138
commit
1aea157aa1
13 changed files with 113 additions and 169 deletions
|
@ -22,8 +22,8 @@ set(SOURCES
|
|||
src/game/game_object.h
|
||||
src/game/game.cpp
|
||||
src/game/game.h
|
||||
src/util/easylogging++.cc
|
||||
src/util/easylogging++.h
|
||||
src/logging/easylogging++.cc
|
||||
src/logging/easylogging++.h
|
||||
src/coordinates/coordinate_transformer.cpp
|
||||
src/coordinates/coordinate_transformer.h
|
||||
src/coordinates/translated_coordinates.cpp
|
||||
|
@ -34,7 +34,7 @@ set(SOURCES
|
|||
src/primitives/circle_object.cpp
|
||||
src/primitives/circle_object.h
|
||||
src/game/game_factory.cpp
|
||||
src/game/game_factory.hpp)
|
||||
src/game/game_factory.hpp src/config.h)
|
||||
|
||||
set(PHYSICS_00_SOURCES
|
||||
src/prototypes/physics_00.cpp
|
||||
|
|
13
src/config.h
Normal file
13
src/config.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef HOLESOME_CONFIG_H
|
||||
#define HOLESOME_CONFIG_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
|
||||
#define FRAME_RATE 60
|
||||
#define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE))
|
||||
|
||||
#define ANTIALIASINGLEVEL 8
|
||||
|
||||
|
||||
#endif //HOLESOME_CONFIG_H
|
|
@ -1,162 +1,74 @@
|
|||
//
|
||||
// Created by max on 26.04.23.
|
||||
//
|
||||
|
||||
#include <utility>
|
||||
#include <SFML/Window/Event.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
|
||||
#include "game.h"
|
||||
#include "../config.h"
|
||||
|
||||
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), isRunning(false), gameObjects()
|
||||
{
|
||||
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), gameObjects() {
|
||||
}
|
||||
|
||||
void Game::run()
|
||||
{
|
||||
if (isRunning)
|
||||
{
|
||||
LOG(WARNING) << "Game is already running";
|
||||
return;
|
||||
}
|
||||
void Game::run() {
|
||||
sf::Clock clock;
|
||||
sf::Time TimeSinceLastUpdate = sf::seconds(0);
|
||||
|
||||
isRunning = true;
|
||||
while (isRunning)
|
||||
{
|
||||
renderFrame();
|
||||
while (window->isOpen()) {
|
||||
processEvents();
|
||||
TimeSinceLastUpdate += clock.restart();
|
||||
while (TimeSinceLastUpdate >= FRAME_TIME) {
|
||||
TimeSinceLastUpdate -= FRAME_TIME;
|
||||
|
||||
// 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();
|
||||
}
|
||||
update();
|
||||
processEvents();
|
||||
}
|
||||
drawFrame();
|
||||
}
|
||||
|
||||
// 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;
|
||||
void Game::exit() {
|
||||
window->close();
|
||||
}
|
||||
|
||||
void Game::renderFrame()
|
||||
{
|
||||
for (auto &gameObject: gameObjects)
|
||||
{
|
||||
gameObject->draw(*window, sf::RenderStates::Default);
|
||||
void Game::drawFrame() {
|
||||
window->clear(sf::Color::Black);
|
||||
|
||||
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) {
|
||||
gameObjects.push_back(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();
|
||||
//}
|
||||
void Game::update() {
|
||||
for (auto &gameObject: gameObjects) {
|
||||
gameObject->update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Game::processEvents() {
|
||||
sf::Event event;
|
||||
while (window->pollEvent(event)) {
|
||||
switch (event.type) {
|
||||
case sf::Event::KeyPressed:
|
||||
break;
|
||||
case sf::Event::KeyReleased:
|
||||
break;
|
||||
case sf::Event::Closed:
|
||||
exit();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,13 @@
|
|||
|
||||
|
||||
#include <memory>
|
||||
#include "../util/easylogging++.h"
|
||||
#include "../logging/easylogging++.h"
|
||||
#include "game_object.h"
|
||||
|
||||
class Game {
|
||||
private:
|
||||
std::shared_ptr<sf::RenderWindow> window;
|
||||
std::vector<GameObject *> gameObjects;
|
||||
bool isRunning = false;
|
||||
|
||||
public:
|
||||
explicit Game(std::shared_ptr<sf::RenderWindow> window);
|
||||
|
@ -28,7 +27,11 @@ public:
|
|||
void exit();
|
||||
|
||||
void addGameObject(GameObject *gameObject);
|
||||
void renderFrame();
|
||||
void drawFrame();
|
||||
|
||||
void update();
|
||||
|
||||
void processEvents();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -4,29 +4,44 @@
|
|||
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include "game_factory.hpp"
|
||||
#include "../config.h"
|
||||
|
||||
|
||||
std::shared_ptr<Game> GameFactory::createWindowed(const std::string &title, int width, int height)
|
||||
{
|
||||
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(width, height), title);
|
||||
std::shared_ptr<Game> GameFactory::createWindowed(const std::string &title, int width, int height) {
|
||||
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(width, height), title,
|
||||
sf::Style::Default,
|
||||
getAdditionalSettings());
|
||||
applyAdditionalWindowConfig(window.get());
|
||||
return std::make_shared<Game>(window);
|
||||
}
|
||||
|
||||
std::shared_ptr<Game> GameFactory::createFullscreen(const std::string &title)
|
||||
{
|
||||
std::shared_ptr<Game> GameFactory::createFullscreen(const std::string &title) {
|
||||
sf::VideoMode fullScreenMode;
|
||||
|
||||
auto availableModes = sf::VideoMode::getFullscreenModes();
|
||||
if (availableModes.empty())
|
||||
{
|
||||
if (availableModes.empty()) {
|
||||
LOG(INFO) << "No fullscreen modes available, falling back to Desktop Mode.";
|
||||
fullScreenMode = sf::VideoMode::getDesktopMode();
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
fullScreenMode = availableModes[0];
|
||||
fullScreenMode.bitsPerPixel = sf::VideoMode::getDesktopMode().bitsPerPixel;
|
||||
}
|
||||
|
||||
auto window = std::make_shared<sf::RenderWindow>(fullScreenMode, title, sf::Style::Fullscreen);
|
||||
auto window = std::make_shared<sf::RenderWindow>(fullScreenMode, title,
|
||||
sf::Style::Fullscreen,
|
||||
getAdditionalSettings());
|
||||
applyAdditionalWindowConfig(window.get());
|
||||
|
||||
return std::make_shared<Game>(window);
|
||||
}
|
||||
}
|
||||
|
||||
sf::ContextSettings GameFactory::getAdditionalSettings() {
|
||||
sf::ContextSettings settings = sf::ContextSettings();
|
||||
|
||||
settings.antialiasingLevel = ANTIALIASINGLEVEL;
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
void GameFactory::applyAdditionalWindowConfig(sf::RenderWindow *window) {
|
||||
window->setFramerateLimit(FRAME_RATE);
|
||||
}
|
||||
|
|
|
@ -12,6 +12,10 @@ public:
|
|||
static std::shared_ptr<Game> createWindowed(const std::string &title, int width, int height);
|
||||
static std::shared_ptr<Game> createFullscreen(const std::string &title);
|
||||
|
||||
private:
|
||||
static sf::ContextSettings getAdditionalSettings();
|
||||
|
||||
static void applyAdditionalWindowConfig(sf::RenderWindow *window);
|
||||
};
|
||||
|
||||
#endif //HOLESOME_GAME_FACTORY_HPP
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
#include "game_object.h"
|
||||
|
||||
TranslatedCoordinates *GameObject::getCoordinates() const {
|
||||
return coordinates.get();
|
||||
}
|
||||
|
||||
GameObject::GameObject() : coordinates(std::make_shared<TranslatedCoordinates>(WorldCoordinates(0, 0, 0))) {
|
||||
|
||||
}
|
||||
|
|
|
@ -4,15 +4,15 @@
|
|||
|
||||
#include <memory>
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include "../coordinates/translated_coordinates.h"
|
||||
|
||||
class GameObject : public sf::Drawable {
|
||||
class GameObject {
|
||||
public:
|
||||
GameObject();
|
||||
|
||||
void draw(sf::RenderTarget &target, sf::RenderStates states) const override = 0;
|
||||
|
||||
TranslatedCoordinates* getCoordinates() const;
|
||||
virtual void draw(sf::RenderWindow *window) const = 0;
|
||||
virtual void update() const = 0;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<TranslatedCoordinates> coordinates;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <SFML/Graphics.hpp>
|
||||
#include <iostream>
|
||||
#include "util/easylogging++.h"
|
||||
#include "logging/easylogging++.h"
|
||||
#include "game/game.h"
|
||||
#include "primitives/circle_object.h"
|
||||
#include "game/game_factory.hpp"
|
||||
|
|
|
@ -9,12 +9,16 @@ CircleObject::CircleObject(int radius, sf::Color color) : radius(radius), color(
|
|||
|
||||
}
|
||||
|
||||
void CircleObject::draw(sf::RenderTarget &target, sf::RenderStates states) const {
|
||||
void CircleObject::draw(sf::RenderWindow *window) const {
|
||||
sf::CircleShape circle(radius);
|
||||
circle.setFillColor(color);
|
||||
circle.setPosition(coordinates->getScreenCoordinates().x, coordinates->getScreenCoordinates().y);
|
||||
|
||||
target.draw(circle);
|
||||
window->draw(circle);
|
||||
}
|
||||
|
||||
void CircleObject::update() const {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
//
|
||||
// Created by max on 27.04.23.
|
||||
//
|
||||
|
||||
#ifndef HOLESOME_CIRCLE_OBJECT_H
|
||||
#define HOLESOME_CIRCLE_OBJECT_H
|
||||
|
||||
|
@ -13,7 +9,8 @@ class CircleObject : public GameObject {
|
|||
public:
|
||||
CircleObject(int radius, sf::Color color);
|
||||
|
||||
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
|
||||
void draw(sf::RenderWindow *window) const override;
|
||||
void update() const override;
|
||||
|
||||
private:
|
||||
int radius;
|
||||
|
|
Loading…
Reference in a new issue