-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |