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_object.h
|
||||||
src/game/game.cpp
|
src/game/game.cpp
|
||||||
src/game/game.h
|
src/game/game.h
|
||||||
src/util/easylogging++.cc
|
src/logging/easylogging++.cc
|
||||||
src/util/easylogging++.h
|
src/logging/easylogging++.h
|
||||||
src/coordinates/coordinate_transformer.cpp
|
src/coordinates/coordinate_transformer.cpp
|
||||||
src/coordinates/coordinate_transformer.h
|
src/coordinates/coordinate_transformer.h
|
||||||
src/coordinates/translated_coordinates.cpp
|
src/coordinates/translated_coordinates.cpp
|
||||||
|
@ -34,7 +34,7 @@ set(SOURCES
|
||||||
src/primitives/circle_object.cpp
|
src/primitives/circle_object.cpp
|
||||||
src/primitives/circle_object.h
|
src/primitives/circle_object.h
|
||||||
src/game/game_factory.cpp
|
src/game/game_factory.cpp
|
||||||
src/game/game_factory.hpp)
|
src/game/game_factory.hpp src/config.h)
|
||||||
|
|
||||||
set(PHYSICS_00_SOURCES
|
set(PHYSICS_00_SOURCES
|
||||||
src/prototypes/physics_00.cpp
|
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 <utility>
|
||||||
#include <SFML/Window/Event.hpp>
|
#include <SFML/Window/Event.hpp>
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
|
||||||
#include "game.h"
|
#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()
|
void Game::run() {
|
||||||
{
|
sf::Clock clock;
|
||||||
if (isRunning)
|
sf::Time TimeSinceLastUpdate = sf::seconds(0);
|
||||||
{
|
|
||||||
LOG(WARNING) << "Game is already running";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
isRunning = true;
|
while (window->isOpen()) {
|
||||||
while (isRunning)
|
processEvents();
|
||||||
{
|
TimeSinceLastUpdate += clock.restart();
|
||||||
renderFrame();
|
while (TimeSinceLastUpdate >= FRAME_TIME) {
|
||||||
|
TimeSinceLastUpdate -= FRAME_TIME;
|
||||||
|
|
||||||
// Process any events that have occurred since the last iteration
|
update();
|
||||||
sf::Event event{};
|
processEvents();
|
||||||
while (window->pollEvent(event))
|
|
||||||
{
|
|
||||||
// If the event is to close the window, then close it
|
|
||||||
if (event.type == sf::Event::Closed)
|
|
||||||
{
|
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
|
drawFrame();
|
||||||
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()
|
void Game::exit() {
|
||||||
{
|
|
||||||
isRunning = false;
|
|
||||||
window->close();
|
window->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::renderFrame()
|
void Game::drawFrame() {
|
||||||
{
|
window->clear(sf::Color::Black);
|
||||||
for (auto &gameObject: gameObjects)
|
|
||||||
{
|
for (auto &gameObject: gameObjects) {
|
||||||
gameObject->draw(*window, sf::RenderStates::Default);
|
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) {
|
||||||
|
gameObjects.push_back(gameObject);
|
||||||
|
}
|
||||||
|
|
||||||
//void Game::initWindow()
|
void Game::update() {
|
||||||
//{
|
for (auto &gameObject: gameObjects) {
|
||||||
// sf::ContextSettings settings;
|
gameObject->update();
|
||||||
// settings.antialiasingLevel = 8;
|
}
|
||||||
// Window.create(sf::VideoMode(SCREEN_W, SCREEN_H), "Ping & Pong", sf::Style::Default, settings);
|
}
|
||||||
//
|
|
||||||
// Window.setFramerateLimit(FRAMERATE);
|
|
||||||
//}
|
void Game::processEvents() {
|
||||||
//
|
sf::Event event;
|
||||||
//void Game::update()
|
while (window->pollEvent(event)) {
|
||||||
//{
|
switch (event.type) {
|
||||||
// if (bGameRun && tMultiBallTime.asSeconds() == -1)
|
case sf::Event::KeyPressed:
|
||||||
// {
|
break;
|
||||||
// for (std::list<CBall*>::iterator i = lBalls.begin(); i != lBalls.end(); ++i)
|
case sf::Event::KeyReleased:
|
||||||
// (*i)->update();
|
break;
|
||||||
//
|
case sf::Event::Closed:
|
||||||
// updateBars();
|
exit();
|
||||||
//
|
break;
|
||||||
// updatekBallGoal();
|
default:
|
||||||
// }
|
break;
|
||||||
// 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();
|
|
||||||
//}
|
|
||||||
|
|
|
@ -7,14 +7,13 @@
|
||||||
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "../util/easylogging++.h"
|
#include "../logging/easylogging++.h"
|
||||||
#include "game_object.h"
|
#include "game_object.h"
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<sf::RenderWindow> window;
|
std::shared_ptr<sf::RenderWindow> window;
|
||||||
std::vector<GameObject *> gameObjects;
|
std::vector<GameObject *> gameObjects;
|
||||||
bool isRunning = false;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Game(std::shared_ptr<sf::RenderWindow> window);
|
explicit Game(std::shared_ptr<sf::RenderWindow> window);
|
||||||
|
@ -28,7 +27,11 @@ public:
|
||||||
void exit();
|
void exit();
|
||||||
|
|
||||||
void addGameObject(GameObject *gameObject);
|
void addGameObject(GameObject *gameObject);
|
||||||
void renderFrame();
|
void drawFrame();
|
||||||
|
|
||||||
|
void update();
|
||||||
|
|
||||||
|
void processEvents();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,29 +4,44 @@
|
||||||
|
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include "game_factory.hpp"
|
#include "game_factory.hpp"
|
||||||
|
#include "../config.h"
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr<Game> GameFactory::createWindowed(const std::string &title, int width, int height)
|
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,
|
||||||
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);
|
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;
|
sf::VideoMode fullScreenMode;
|
||||||
|
|
||||||
auto availableModes = sf::VideoMode::getFullscreenModes();
|
auto availableModes = sf::VideoMode::getFullscreenModes();
|
||||||
if (availableModes.empty())
|
if (availableModes.empty()) {
|
||||||
{
|
|
||||||
LOG(INFO) << "No fullscreen modes available, falling back to Desktop Mode.";
|
LOG(INFO) << "No fullscreen modes available, falling back to Desktop Mode.";
|
||||||
fullScreenMode = sf::VideoMode::getDesktopMode();
|
fullScreenMode = sf::VideoMode::getDesktopMode();
|
||||||
} else
|
} else {
|
||||||
{
|
|
||||||
fullScreenMode = availableModes[0];
|
fullScreenMode = availableModes[0];
|
||||||
fullScreenMode.bitsPerPixel = sf::VideoMode::getDesktopMode().bitsPerPixel;
|
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);
|
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> createWindowed(const std::string &title, int width, int height);
|
||||||
static std::shared_ptr<Game> createFullscreen(const std::string &title);
|
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
|
#endif //HOLESOME_GAME_FACTORY_HPP
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
#include "game_object.h"
|
#include "game_object.h"
|
||||||
|
|
||||||
TranslatedCoordinates *GameObject::getCoordinates() const {
|
|
||||||
return coordinates.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
GameObject::GameObject() : coordinates(std::make_shared<TranslatedCoordinates>(WorldCoordinates(0, 0, 0))) {
|
GameObject::GameObject() : coordinates(std::make_shared<TranslatedCoordinates>(WorldCoordinates(0, 0, 0))) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,15 +4,15 @@
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <SFML/Graphics/Drawable.hpp>
|
#include <SFML/Graphics/Drawable.hpp>
|
||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include "../coordinates/translated_coordinates.h"
|
#include "../coordinates/translated_coordinates.h"
|
||||||
|
|
||||||
class GameObject : public sf::Drawable {
|
class GameObject {
|
||||||
public:
|
public:
|
||||||
GameObject();
|
GameObject();
|
||||||
|
|
||||||
void draw(sf::RenderTarget &target, sf::RenderStates states) const override = 0;
|
virtual void draw(sf::RenderWindow *window) const = 0;
|
||||||
|
virtual void update() const = 0;
|
||||||
TranslatedCoordinates* getCoordinates() const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<TranslatedCoordinates> coordinates;
|
std::shared_ptr<TranslatedCoordinates> coordinates;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "util/easylogging++.h"
|
#include "logging/easylogging++.h"
|
||||||
#include "game/game.h"
|
#include "game/game.h"
|
||||||
#include "primitives/circle_object.h"
|
#include "primitives/circle_object.h"
|
||||||
#include "game/game_factory.hpp"
|
#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);
|
sf::CircleShape circle(radius);
|
||||||
circle.setFillColor(color);
|
circle.setFillColor(color);
|
||||||
circle.setPosition(coordinates->getScreenCoordinates().x, coordinates->getScreenCoordinates().y);
|
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
|
#ifndef HOLESOME_CIRCLE_OBJECT_H
|
||||||
#define HOLESOME_CIRCLE_OBJECT_H
|
#define HOLESOME_CIRCLE_OBJECT_H
|
||||||
|
|
||||||
|
@ -13,7 +9,8 @@ class CircleObject : public GameObject {
|
||||||
public:
|
public:
|
||||||
CircleObject(int radius, sf::Color color);
|
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:
|
private:
|
||||||
int radius;
|
int radius;
|
||||||
|
|
Loading…
Reference in a new issue