init
This commit is contained in:
commit
f698a38c7e
585 changed files with 118338 additions and 0 deletions
13
SimpleGame/src_original/CMakeLists.txt
Normal file
13
SimpleGame/src_original/CMakeLists.txt
Normal file
|
@ -0,0 +1,13 @@
|
|||
add_executable(simpleGameOriginal
|
||||
Source/Game.cpp Source/Main.cpp
|
||||
)
|
||||
|
||||
target_include_directories(simpleGameOriginal
|
||||
PRIVATE
|
||||
Include
|
||||
)
|
||||
|
||||
target_link_libraries(simpleGameOriginal PUBLIC sfml-graphics)
|
||||
|
||||
# The following command is executed only when cmake is executed
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../media DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
38
SimpleGame/src_original/Include/Game.hpp
Normal file
38
SimpleGame/src_original/Include/Game.hpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
#ifndef BOOK_GAME_HPP
|
||||
#define BOOK_GAME_HPP
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
class Game {
|
||||
public:
|
||||
Game();
|
||||
Game(const Game &) = delete;
|
||||
Game &operator=(const Game &) = delete;
|
||||
void run();
|
||||
|
||||
private:
|
||||
void processEvents();
|
||||
void update(sf::Time elapsedTime);
|
||||
void render();
|
||||
|
||||
void updateStatistics(sf::Time elapsedTime);
|
||||
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
|
||||
|
||||
static const float PlayerSpeed;
|
||||
static const sf::Time TimePerFrame;
|
||||
|
||||
sf::RenderWindow mWindow{sf::VideoMode({640, 480}), "SFML Application"};
|
||||
sf::Texture mTexture;
|
||||
sf::CircleShape mTarget;
|
||||
sf::Font mFont;
|
||||
sf::Text mStatisticsText{mFont};
|
||||
sf::Time mStatisticsUpdateTime;
|
||||
|
||||
std::size_t mStatisticsNumFrames{0};
|
||||
bool mIsMovingUp{false};
|
||||
bool mIsMovingDown{false};
|
||||
bool mIsMovingRight{false};
|
||||
bool mIsMovingLeft{false};
|
||||
};
|
||||
|
||||
#endif // BOOK_GAME_HPP
|
11
SimpleGame/src_original/Include/StringHelpers.hpp
Normal file
11
SimpleGame/src_original/Include/StringHelpers.hpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef BOOK_STRINGHELPERS_HPP
|
||||
#define BOOK_STRINGHELPERS_HPP
|
||||
|
||||
#include <sstream>
|
||||
|
||||
// Since std::to_string doesn't work on MinGW we have to implement
|
||||
// our own to support all platforms.
|
||||
template <typename T> std::string toString(const T &value);
|
||||
|
||||
#include "StringHelpers.inl"
|
||||
#endif // BOOK_STRINGHELPERS_HPP
|
6
SimpleGame/src_original/Include/StringHelpers.inl
Normal file
6
SimpleGame/src_original/Include/StringHelpers.inl
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
template <typename T> std::string toString(const T &value) {
|
||||
std::stringstream stream;
|
||||
stream << value;
|
||||
return stream.str();
|
||||
}
|
99
SimpleGame/src_original/Source/Game.cpp
Normal file
99
SimpleGame/src_original/Source/Game.cpp
Normal file
|
@ -0,0 +1,99 @@
|
|||
#include "Game.hpp"
|
||||
#include "StringHelpers.hpp"
|
||||
|
||||
const float Game::PlayerSpeed = 100.f;
|
||||
const sf::Time Game::TimePerFrame = sf::seconds(1.f / 60.f);
|
||||
|
||||
Game::Game() {
|
||||
mTarget.setRadius(50.f);
|
||||
mTarget.setFillColor(sf::Color::Cyan);
|
||||
mTarget.setPosition({100.f, 100.f});
|
||||
|
||||
assert(mFont.openFromFile("media/Sansation.ttf"));
|
||||
// We do not need to do mStatisticsText.setFont(mFont); as mStatisticsText is
|
||||
// initialized with a reference to mFont
|
||||
mStatisticsText.setPosition({5.f, 5.f});
|
||||
mStatisticsText.setCharacterSize(10);
|
||||
}
|
||||
|
||||
void Game::run() {
|
||||
sf::Clock clock;
|
||||
sf::Time timeSinceLastUpdate = sf::Time::Zero;
|
||||
while (mWindow.isOpen()) {
|
||||
sf::Time elapsedTime = clock.restart();
|
||||
timeSinceLastUpdate += elapsedTime;
|
||||
while (timeSinceLastUpdate > TimePerFrame) {
|
||||
timeSinceLastUpdate -= TimePerFrame;
|
||||
|
||||
processEvents();
|
||||
update(TimePerFrame);
|
||||
}
|
||||
|
||||
updateStatistics(elapsedTime);
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
void Game::processEvents() {
|
||||
while (const std::optional event = mWindow.pollEvent()) {
|
||||
if (const auto *keyPressed = event->getIf<sf::Event::KeyPressed>()) {
|
||||
handlePlayerInput(keyPressed->code, true);
|
||||
} else if (const auto *keyReleased =
|
||||
event->getIf<sf::Event::KeyReleased>()) {
|
||||
handlePlayerInput(keyReleased->code, false);
|
||||
} else if (event->is<sf::Event::Closed>()) {
|
||||
mWindow.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game::update(sf::Time elapsedTime) {
|
||||
sf::Vector2f movement(0.f, 0.f);
|
||||
if (mIsMovingUp)
|
||||
movement.y -= PlayerSpeed;
|
||||
if (mIsMovingDown)
|
||||
movement.y += PlayerSpeed;
|
||||
if (mIsMovingLeft)
|
||||
movement.x -= PlayerSpeed;
|
||||
if (mIsMovingRight)
|
||||
movement.x += PlayerSpeed;
|
||||
|
||||
mTarget.move(movement * elapsedTime.asSeconds());
|
||||
}
|
||||
|
||||
void Game::render() {
|
||||
mWindow.clear();
|
||||
mWindow.draw(mTarget);
|
||||
mWindow.draw(mStatisticsText);
|
||||
mWindow.display();
|
||||
}
|
||||
|
||||
void Game::updateStatistics(const sf::Time elapsedTime) {
|
||||
mStatisticsUpdateTime += elapsedTime;
|
||||
mStatisticsNumFrames += 1;
|
||||
|
||||
if (mStatisticsUpdateTime >= sf::seconds(1.0f)) {
|
||||
mStatisticsText.setString(
|
||||
"Frames / Second = " + toString(mStatisticsNumFrames) + "\n" +
|
||||
"Time / Update = " +
|
||||
toString(mStatisticsUpdateTime.asMicroseconds() /
|
||||
mStatisticsNumFrames) +
|
||||
" us");
|
||||
|
||||
mStatisticsUpdateTime -= sf::seconds(1.0f);
|
||||
mStatisticsNumFrames = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Game::handlePlayerInput(const sf::Keyboard::Key key,
|
||||
const bool isPressed) {
|
||||
using enum sf::Keyboard::Key;
|
||||
if (key == Z)
|
||||
mIsMovingUp = isPressed;
|
||||
else if (key == S)
|
||||
mIsMovingDown = isPressed;
|
||||
else if (key == Q)
|
||||
mIsMovingLeft = isPressed;
|
||||
else if (key == D)
|
||||
mIsMovingRight = isPressed;
|
||||
}
|
6
SimpleGame/src_original/Source/Main.cpp
Normal file
6
SimpleGame/src_original/Source/Main.cpp
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "Game.hpp"
|
||||
|
||||
int main() {
|
||||
Game game;
|
||||
game.run();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue