Skip to content

Commit

Permalink
Separated game logic into own class
Browse files Browse the repository at this point in the history
  • Loading branch information
eef-g committed Oct 23, 2023
1 parent 6b2286b commit 72965bf
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 17 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ FetchContent_Declare(SFML
GIT_TAG 2.6.x)
FetchContent_MakeAvailable(SFML)

add_executable(TheIsle src/main.cpp)
file(GLOB Isle_SRC
"src/*.cpp"
"src/*.hpp"
)

add_executable(TheIsle ${Isle_SRC})
target_link_libraries(TheIsle PRIVATE sfml-graphics)
target_compile_features(TheIsle PRIVATE cxx_std_17)

Expand Down
65 changes: 65 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "game.hpp"
// Constructor / Destructor
Game::Game() {
this->initVariables();
this->initWindow();
}

Game::~Game() {
delete this->window;
}

/*
Public Functions
*/

/// @brief Returns whether the game is running or not
const bool Game::running() const { return this->window->isOpen(); }

/// @brief Polls events from the window
void Game::pollEvents() {
// Event Polling
while (this->window->pollEvent(this->ev)) {
switch (this->ev.type) {
case sf::Event::Closed:
this->window->close();
break;
case sf::Event::KeyPressed:
if (this->ev.key.code == sf::Keyboard::Escape)
this->window->close();
break;
}
}
}

/// @brief Runs the game logic for each frame in the game loop
void Game::update() {
this->pollEvents();
}

/// @brief Runs the rendering logic for each frame in the game loop
void Game::render() {
// Clear the window
this->window->clear(sf::Color(255, 0, 0, 255));
// Render items

// Display items
this->window->display();
}

/*
Private Functions
*/


/// @brief Initializes the game's variables
void Game::initVariables() {
this->window = nullptr;
}

/// @brief Initializes the game's window
void Game::initWindow() {
this->videoMode.height = 480;
this->videoMode.width = 640;
window = new sf::RenderWindow(this->videoMode, "The Isle", sf::Style::Titlebar | sf::Style::Close | sf::Style::Resize);
}
42 changes: 42 additions & 0 deletions src/game.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef GAME_HPP
#define GAME_HPP

/*
The Game class acts as a container for all of the game's logic.
It will contain the game loop, and will be responsible for updating
and rendering all of the game's objects.
*/

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>


// Pro-tip: Press 'CRTL+.' to automatically create the function definition in the cpp file
class Game {
private:
// Variables
sf::RenderWindow* window;
sf::Event ev;
sf::VideoMode videoMode;

// Functions
void initVariables();
void initWindow();
public:
// Constructor
Game();
// Destructor
virtual ~Game();

// Accessors
const bool running() const;
// Functions
void pollEvents();
void update();
void render();

};
#endif
29 changes: 13 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#include "game.hpp"

int main()
{
auto window = sf::RenderWindow{ { 1920u, 1080u }, "CMake SFML Project" };
window.setFramerateLimit(144);
int main() {
// Initialize the game engine
Game game;

while (window.isOpen())
{
for (auto event = sf::Event{}; window.pollEvent(event);)
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
// Game loop
while (game.running()) {
// Update
game.update();

window.clear();
window.display();
// Render
game.render();
}

return 0;
}

0 comments on commit 72965bf

Please sign in to comment.