From 378e2d74cfa46073d4b4d71b257fb09f2624a528 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Tue, 12 Aug 2025 17:36:08 +0200 Subject: [PATCH] Hpp and better imports --- .gitignore | 3 +- CMakeLists.txt | 46 +++++++------------ src/Game.cpp | 4 +- src/{Game.h => Game.hpp} | 16 +++---- src/SdlWrapper.cpp | 4 +- src/{SdlWrapper.h => SdlWrapper.hpp} | 4 -- src/Vec2d/{Bump.h => Bump.hpp} | 0 src/Vec2d/Vec2d.cpp | 2 +- src/Vec2d/{Vec2d.h => Vec2d.hpp} | 10 ++-- src/VisibleObjects/Ball.cpp | 2 +- src/VisibleObjects/{Ball.h => Ball.hpp} | 9 ++-- src/VisibleObjects/PlayerPaddle.cpp | 2 +- .../{PlayerPaddle.h => PlayerPaddle.hpp} | 4 +- src/VisibleObjects/{Side.h => Side.hpp} | 0 src/{defaultfont.h => defaultfont.hpp} | 0 src/icon.cpp | 2 +- src/{icon.h => icon.hpp} | 0 src/main.cpp | 2 +- src/text/OptionScreen.cpp | 4 +- src/text/{OptionScreen.h => OptionScreen.hpp} | 3 +- src/text/Score.cpp | 3 +- src/text/{Score.h => Score.hpp} | 11 ++--- src/text/ScrollOptionScreen.cpp | 3 +- ...lOptionScreen.h => ScrollOptionScreen.hpp} | 2 +- src/text/TextScreen.cpp | 6 ++- src/text/{TextScreen.h => TextScreen.hpp} | 3 -- 26 files changed, 63 insertions(+), 82 deletions(-) rename src/{Game.h => Game.hpp} (69%) rename src/{SdlWrapper.h => SdlWrapper.hpp} (89%) rename src/Vec2d/{Bump.h => Bump.hpp} (100%) rename src/Vec2d/{Vec2d.h => Vec2d.hpp} (84%) rename src/VisibleObjects/{Ball.h => Ball.hpp} (84%) rename src/VisibleObjects/{PlayerPaddle.h => PlayerPaddle.hpp} (92%) rename src/VisibleObjects/{Side.h => Side.hpp} (100%) rename src/{defaultfont.h => defaultfont.hpp} (100%) rename src/{icon.h => icon.hpp} (100%) rename src/text/{OptionScreen.h => OptionScreen.hpp} (90%) rename src/text/{Score.h => Score.hpp} (75%) rename src/text/{ScrollOptionScreen.h => ScrollOptionScreen.hpp} (92%) rename src/text/{TextScreen.h => TextScreen.hpp} (95%) diff --git a/.gitignore b/.gitignore index b28ecc6..6364293 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea cmake-build-* -build \ No newline at end of file +build +.cache diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b3cc9a..c4654f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,52 +6,36 @@ set(CMAKE_CXX_STANDARD 17) # Option for enabling optimizations option(ENABLE_OPTIMIZATIONS "Enable compiler optimizations" OFF) -# Base SDL2 -find_package(SDL2 REQUIRED) +# Use pkg-config +find_package(PkgConfig REQUIRED) + +pkg_check_modules(SDL2 REQUIRED sdl2) include_directories(${SDL2_INCLUDE_DIRS}) +link_directories(${SDL2_LIBRARY_DIRS}) -# SDL2 Gfx -find_library(SDL2_GFX_LIBRARY NAMES SDL2_gfx SDL2_gfxd libSDL2_gfx) -if (NOT SDL2_GFX_LIBRARY) - message(FATAL_ERROR "SDL2_gfx not found") -endif () +pkg_check_modules(SDL2_GFX REQUIRED SDL2_gfx) +include_directories(${SDL2_GFX_INCLUDE_DIRS}) +link_directories(${SDL2_GFX_LIBRARY_DIRS}) -find_library(SDL2_TTF_LIBRARY NAMES SDL2_ttf SDL2_TTF SDL2TTF) -if (NOT SDL2_TTF_LIBRARY) - message(FATAL_ERROR "SDL2_TTF not found") -endif () +pkg_check_modules(SDL2_TTF REQUIRED SDL2_ttf) +include_directories(${SDL2_TTF_INCLUDE_DIRS}) +link_directories(${SDL2_TTF_LIBRARY_DIRS}) - -# Define the executable target before linking libraries add_executable(Pong src/main.cpp - src/SdlWrapper.h src/SdlWrapper.cpp - src/Game.h src/Game.cpp - src/VisibleObjects/Ball.h src/VisibleObjects/Ball.cpp - src/Vec2d/Vec2d.h src/Vec2d/Vec2d.cpp - src/Vec2d/Bump.h - src/VisibleObjects/PlayerPaddle.h src/VisibleObjects/PlayerPaddle.cpp - src/VisibleObjects/Side.h - src/text/TextScreen.h src/text/TextScreen.cpp - src/defaultfont.h src/defaultfont.cpp - src/icon.h src/icon.cpp - src/text/OptionScreen.h src/text/OptionScreen.cpp - src/text/Score.h src/text/Score.cpp src/text/ScrollOptionScreen.cpp - src/text/ScrollOptionScreen.h ) -# Now link the libraries to the target -target_link_libraries(Pong ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARY} ${SDL2_TTF_LIBRARY}) +target_link_libraries(Pong ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARIES} ${SDL2_TTF_LIBRARIES}) # Set compiler optimization flags if (ENABLE_OPTIMIZATIONS) @@ -59,7 +43,11 @@ if (ENABLE_OPTIMIZATIONS) set_target_properties(Pong PROPERTIES LINK_SEARCH_START_STATIC 1) set_target_properties(Pong PROPERTIES LINK_SEARCH_END_STATIC 1) set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") - set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") + + # Apply static linking options only for GCC + if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") + endif() # Make sure that a terminal window doesn't launch under Windows. # We will still launch the terminal if we haven't compiled with optimizations. diff --git a/src/Game.cpp b/src/Game.cpp index 00faf41..0260142 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,6 +1,8 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "Game.h" +#include "Game.hpp" +#include +#include Game::Game(SDL_Point screenSize) : SdlWrapper("Pong", screenSize, 60), leftPaddle(new PlayerPaddle(&this->screenSize, Side::LEFT)), diff --git a/src/Game.h b/src/Game.hpp similarity index 69% rename from src/Game.h rename to src/Game.hpp index 2b0694a..765de3e 100644 --- a/src/Game.h +++ b/src/Game.hpp @@ -2,15 +2,13 @@ #pragma -#include "SdlWrapper.h" -#include "SDL.h" -#include "VisibleObjects/Ball.h" -#include "VisibleObjects/PlayerPaddle.h" -#include "text/Score.h" -#include "text/TextScreen.h" -#include "text/OptionScreen.h" -#include "text/Score.h" -#include "text/ScrollOptionScreen.h" +#include "SdlWrapper.hpp" +#include "VisibleObjects/Ball.hpp" +#include "VisibleObjects/PlayerPaddle.hpp" +#include "text/Score.hpp" +#include "text/OptionScreen.hpp" +#include "text/Score.hpp" +#include "text/ScrollOptionScreen.hpp" enum class GameState { START_SCREEN, GAME, END_SCREEN diff --git a/src/SdlWrapper.cpp b/src/SdlWrapper.cpp index 70db2d8..f7aa0d8 100644 --- a/src/SdlWrapper.cpp +++ b/src/SdlWrapper.cpp @@ -1,6 +1,8 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "SdlWrapper.h" +#include "SdlWrapper.hpp" +#include "icon.hpp" +#include SdlWrapper::SdlWrapper(const char *const title, const SDL_Point screenSize, const uint8_t fps) : fps(fps), screenSize( screenSize) { diff --git a/src/SdlWrapper.h b/src/SdlWrapper.hpp similarity index 89% rename from src/SdlWrapper.h rename to src/SdlWrapper.hpp index e902474..1ceeb6f 100644 --- a/src/SdlWrapper.h +++ b/src/SdlWrapper.hpp @@ -2,11 +2,7 @@ #pragma -#include #include -#include -#include "SDL.h" -#include "icon.h" class SdlWrapper { private: diff --git a/src/Vec2d/Bump.h b/src/Vec2d/Bump.hpp similarity index 100% rename from src/Vec2d/Bump.h rename to src/Vec2d/Bump.hpp diff --git a/src/Vec2d/Vec2d.cpp b/src/Vec2d/Vec2d.cpp index 9e67577..a42e4d0 100644 --- a/src/Vec2d/Vec2d.cpp +++ b/src/Vec2d/Vec2d.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "Vec2d.h" +#include "Vec2d.hpp" Vec2d::Vec2d(float_t hypotenuse) : hypotenuse(hypotenuse) { std::random_device rd; diff --git a/src/Vec2d/Vec2d.h b/src/Vec2d/Vec2d.hpp similarity index 84% rename from src/Vec2d/Vec2d.h rename to src/Vec2d/Vec2d.hpp index c437212..e7a1e80 100644 --- a/src/Vec2d/Vec2d.h +++ b/src/Vec2d/Vec2d.hpp @@ -2,12 +2,12 @@ #pragma once -#include "random" -#include "ctime" -#include "cmath" +#include +#include +#include +#include -#include "SDL_rect.h" -#include "Bump.h" +#include "Bump.hpp" inline double_t toRadians(double_t degrees) { return degrees * M_PI / 100; diff --git a/src/VisibleObjects/Ball.cpp b/src/VisibleObjects/Ball.cpp index c327a6e..7a6b90c 100644 --- a/src/VisibleObjects/Ball.cpp +++ b/src/VisibleObjects/Ball.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "Ball.h" +#include "Ball.hpp" Ball::Ball(const SDL_Point *screen, const PlayerPaddle *leftPaddle, const PlayerPaddle *rightPaddle, Score *score) diff --git a/src/VisibleObjects/Ball.h b/src/VisibleObjects/Ball.hpp similarity index 84% rename from src/VisibleObjects/Ball.h rename to src/VisibleObjects/Ball.hpp index 50047e3..6f93b62 100644 --- a/src/VisibleObjects/Ball.h +++ b/src/VisibleObjects/Ball.hpp @@ -2,13 +2,10 @@ #pragma once -#include "../Vec2d/Vec2d.h" -#include "PlayerPaddle.h" +#include "../Vec2d/Vec2d.hpp" +#include "PlayerPaddle.hpp" #include -#include -#include "optional" -#include "../text/Score.h" -#include "../text/Score.h" +#include "../text/Score.hpp" class Ball { private: diff --git a/src/VisibleObjects/PlayerPaddle.cpp b/src/VisibleObjects/PlayerPaddle.cpp index d032f2e..510a898 100644 --- a/src/VisibleObjects/PlayerPaddle.cpp +++ b/src/VisibleObjects/PlayerPaddle.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "PlayerPaddle.h" +#include "PlayerPaddle.hpp" PlayerPaddle::PlayerPaddle(const SDL_Point *screen, const Side side) : SDL_Rect(), screen(screen) { diff --git a/src/VisibleObjects/PlayerPaddle.h b/src/VisibleObjects/PlayerPaddle.hpp similarity index 92% rename from src/VisibleObjects/PlayerPaddle.h rename to src/VisibleObjects/PlayerPaddle.hpp index 90001b6..b79672a 100644 --- a/src/VisibleObjects/PlayerPaddle.h +++ b/src/VisibleObjects/PlayerPaddle.hpp @@ -4,8 +4,8 @@ #include #include -#include "Side.h" -#include "../Vec2d/Bump.h" +#include "Side.hpp" +#include "../Vec2d/Bump.hpp" class PlayerPaddle : public SDL_Rect { private: diff --git a/src/VisibleObjects/Side.h b/src/VisibleObjects/Side.hpp similarity index 100% rename from src/VisibleObjects/Side.h rename to src/VisibleObjects/Side.hpp diff --git a/src/defaultfont.h b/src/defaultfont.hpp similarity index 100% rename from src/defaultfont.h rename to src/defaultfont.hpp diff --git a/src/icon.cpp b/src/icon.cpp index 9ddbb2f..46c6c84 100644 --- a/src/icon.cpp +++ b/src/icon.cpp @@ -1,5 +1,5 @@ // This is the bytes of ./icon.bmp -#include "icon.h" +#include "icon.hpp" const unsigned char icon[] = { 0x42, 0x4d, 0x9a, 0x76, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x00, 0x00, 0x00, 0x7c, 0x00, diff --git a/src/icon.h b/src/icon.hpp similarity index 100% rename from src/icon.h rename to src/icon.hpp diff --git a/src/main.cpp b/src/main.cpp index 18022fa..eba035f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "Game.h" +#include "Game.hpp" #include diff --git a/src/text/OptionScreen.cpp b/src/text/OptionScreen.cpp index 9fa68f5..4f1a1ff 100644 --- a/src/text/OptionScreen.cpp +++ b/src/text/OptionScreen.cpp @@ -1,7 +1,9 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "OptionScreen.h" +#include "OptionScreen.hpp" #include +#include "../defaultfont.hpp" +#include int_least64_t getCurrentEpochTimeMillis() { diff --git a/src/text/OptionScreen.h b/src/text/OptionScreen.hpp similarity index 90% rename from src/text/OptionScreen.h rename to src/text/OptionScreen.hpp index 5428e85..471f7cc 100644 --- a/src/text/OptionScreen.h +++ b/src/text/OptionScreen.hpp @@ -2,8 +2,7 @@ #pragma once -#include "TextScreen.h" -#include "../VisibleObjects/Side.h" +#include "TextScreen.hpp" class OptionScreen : public TextScreen { diff --git a/src/text/Score.cpp b/src/text/Score.cpp index 9e77504..742612a 100644 --- a/src/text/Score.cpp +++ b/src/text/Score.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "Score.h" +#include "Score.hpp" +#include Score::Score(SDL_Point *screenSize, uint8_t max_score) : maxScore_(max_score), leftScore(0), rightScore(0), TextScreen("", screenSize, std::make_optional( diff --git a/src/text/Score.h b/src/text/Score.hpp similarity index 75% rename from src/text/Score.h rename to src/text/Score.hpp index a07a30a..50682cb 100644 --- a/src/text/Score.h +++ b/src/text/Score.hpp @@ -3,17 +3,12 @@ #pragma once -#include "TextScreen.h" #include #include -#include -#include "../VisibleObjects/Side.h" -#include "SDL_ttf.h" -#include "../defaultfont.h" -#include -#include #include -#include + +#include "TextScreen.hpp" +#include "../VisibleObjects/Side.hpp" class Score : public TextScreen { private: diff --git a/src/text/ScrollOptionScreen.cpp b/src/text/ScrollOptionScreen.cpp index 9552ce3..f47ccdd 100644 --- a/src/text/ScrollOptionScreen.cpp +++ b/src/text/ScrollOptionScreen.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: BSD-2-Clause -#include "ScrollOptionScreen.h" +#include "ScrollOptionScreen.hpp" +#include std::string textAppendHelper(std::string string, const std::string &toAppend) { string.append("\nMax Score: "); diff --git a/src/text/ScrollOptionScreen.h b/src/text/ScrollOptionScreen.hpp similarity index 92% rename from src/text/ScrollOptionScreen.h rename to src/text/ScrollOptionScreen.hpp index 045aef2..00631f5 100644 --- a/src/text/ScrollOptionScreen.h +++ b/src/text/ScrollOptionScreen.hpp @@ -3,7 +3,7 @@ #pragma once -#include "OptionScreen.h" +#include "OptionScreen.hpp" class ScrollOptionScreen : public OptionScreen { diff --git a/src/text/TextScreen.cpp b/src/text/TextScreen.cpp index 3b67514..ac8e5d4 100644 --- a/src/text/TextScreen.cpp +++ b/src/text/TextScreen.cpp @@ -3,8 +3,10 @@ #include #include #include -#include "TextScreen.h" -#include "optional" +#include +#include "TextScreen.hpp" +#include "../defaultfont.hpp" +#include std::vector splitString(const std::string &string, const char &delim) { int size = 0; diff --git a/src/text/TextScreen.h b/src/text/TextScreen.hpp similarity index 95% rename from src/text/TextScreen.h rename to src/text/TextScreen.hpp index 6138668..41bf2f0 100644 --- a/src/text/TextScreen.h +++ b/src/text/TextScreen.hpp @@ -4,11 +4,8 @@ #include #include -#include #include #include -#include "../defaultfont.h" -#include "iostream" struct Position { SDL_Rect shadowPosition;