delete src_original

This commit is contained in:
Crizomb 2025-06-12 09:19:04 +02:00
parent 2ace28d941
commit db51c6369a
7 changed files with 0 additions and 174 deletions

View file

@ -18,6 +18,5 @@ FetchContent_MakeAvailable(sfml)
set(CMAKE_CXX_STANDARD 23)
add_subdirectory(src_original)
add_subdirectory(src)

View file

@ -1,13 +0,0 @@
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})

View file

@ -1,38 +0,0 @@
#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

View file

@ -1,11 +0,0 @@
#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

View file

@ -1,6 +0,0 @@
template <typename T> std::string toString(const T &value) {
std::stringstream stream;
stream << value;
return stream.str();
}

View file

@ -1,99 +0,0 @@
#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;
}

View file

@ -1,6 +0,0 @@
#include "Game.hpp"
int main() {
Game game;
game.run();
}