Hpp and better imports

This commit is contained in:
2025-08-12 17:36:08 +02:00
parent 2f401e8d32
commit 378e2d74cf
26 changed files with 63 additions and 82 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.idea
cmake-build-*
build
.cache

View File

@@ -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")
# 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.

View File

@@ -1,6 +1,8 @@
// SPDX-License-Identifier: BSD-2-Clause
#include "Game.h"
#include "Game.hpp"
#include <iostream>
#include <sstream>
Game::Game(SDL_Point screenSize) : SdlWrapper("Pong", screenSize, 60),
leftPaddle(new PlayerPaddle(&this->screenSize, Side::LEFT)),

View File

@@ -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

View File

@@ -1,6 +1,8 @@
// SPDX-License-Identifier: BSD-2-Clause
#include "SdlWrapper.h"
#include "SdlWrapper.hpp"
#include "icon.hpp"
#include <iostream>
SdlWrapper::SdlWrapper(const char *const title, const SDL_Point screenSize, const uint8_t fps) : fps(fps), screenSize(
screenSize) {

View File

@@ -2,11 +2,7 @@
#pragma
#include <string>
#include <SDL_ttf.h>
#include <iostream>
#include "SDL.h"
#include "icon.h"
class SdlWrapper {
private:

View File

@@ -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;

View File

@@ -2,12 +2,12 @@
#pragma once
#include "random"
#include "ctime"
#include "cmath"
#include <random>
#include <ctime>
#include <cmath>
#include <SDL_stdinc.h>
#include "SDL_rect.h"
#include "Bump.h"
#include "Bump.hpp"
inline double_t toRadians(double_t degrees) {
return degrees * M_PI / 100;

View File

@@ -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)

View File

@@ -2,13 +2,10 @@
#pragma once
#include "../Vec2d/Vec2d.h"
#include "PlayerPaddle.h"
#include "../Vec2d/Vec2d.hpp"
#include "PlayerPaddle.hpp"
#include <SDL2/SDL2_gfxPrimitives.h>
#include <iostream>
#include "optional"
#include "../text/Score.h"
#include "../text/Score.h"
#include "../text/Score.hpp"
class Ball {
private:

View File

@@ -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) {

View File

@@ -4,8 +4,8 @@
#include <SDL_rect.h>
#include <SDL_render.h>
#include "Side.h"
#include "../Vec2d/Bump.h"
#include "Side.hpp"
#include "../Vec2d/Bump.hpp"
class PlayerPaddle : public SDL_Rect {
private:

View File

@@ -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,

View File

@@ -1,6 +1,6 @@
// SPDX-License-Identifier: BSD-2-Clause
#include "Game.h"
#include "Game.hpp"
#include <SDL.h>

View File

@@ -1,7 +1,9 @@
// SPDX-License-Identifier: BSD-2-Clause
#include "OptionScreen.h"
#include "OptionScreen.hpp"
#include <chrono>
#include "../defaultfont.hpp"
#include <iostream>
int_least64_t getCurrentEpochTimeMillis() {

View File

@@ -2,8 +2,7 @@
#pragma once
#include "TextScreen.h"
#include "../VisibleObjects/Side.h"
#include "TextScreen.hpp"
class OptionScreen : public TextScreen {

View File

@@ -1,6 +1,7 @@
// SPDX-License-Identifier: BSD-2-Clause
#include "Score.h"
#include "Score.hpp"
#include <sstream>
Score::Score(SDL_Point *screenSize, uint8_t max_score) : maxScore_(max_score), leftScore(0), rightScore(0),
TextScreen("", screenSize, std::make_optional(

View File

@@ -3,17 +3,12 @@
#pragma once
#include "TextScreen.h"
#include <cstdint>
#include <SDL_render.h>
#include <functional>
#include "../VisibleObjects/Side.h"
#include "SDL_ttf.h"
#include "../defaultfont.h"
#include <string>
#include <iostream>
#include <optional>
#include <sstream>
#include "TextScreen.hpp"
#include "../VisibleObjects/Side.hpp"
class Score : public TextScreen {
private:

View File

@@ -1,6 +1,7 @@
// SPDX-License-Identifier: BSD-2-Clause
#include "ScrollOptionScreen.h"
#include "ScrollOptionScreen.hpp"
#include <iostream>
std::string textAppendHelper(std::string string, const std::string &toAppend) {
string.append("\nMax Score: ");

View File

@@ -3,7 +3,7 @@
#pragma once
#include "OptionScreen.h"
#include "OptionScreen.hpp"
class ScrollOptionScreen : public OptionScreen {

View File

@@ -3,8 +3,10 @@
#include <string>
#include <sstream>
#include <utility>
#include "TextScreen.h"
#include "optional"
#include <optional>
#include "TextScreen.hpp"
#include "../defaultfont.hpp"
#include <iostream>
std::vector<std::string> splitString(const std::string &string, const char &delim) {
int size = 0;

View File

@@ -4,11 +4,8 @@
#include <SDL_render.h>
#include <SDL_ttf.h>
#include <utility>
#include <vector>
#include <optional>
#include "../defaultfont.h"
#include "iostream"
struct Position {
SDL_Rect shadowPosition;