Fix dangling pointer and minor things

This commit is contained in:
= 2024-01-20 01:14:01 +01:00
parent 905d20efb6
commit 911f51da78
2 changed files with 17 additions and 11 deletions

View File

@ -31,7 +31,7 @@ public:
color[3] = 255; color[3] = 255;
} }
PaddleDirection getPaddleDirection() const { [[nodiscard]] PaddleDirection getPaddleDirection() const {
if (movingUp != movingDown) if (movingUp != movingDown)
return PaddleDirection::NOT_MOVING; return PaddleDirection::NOT_MOVING;
else if (movingUp) else if (movingUp)

View File

@ -16,13 +16,13 @@
#include <iostream> #include <iostream>
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
// Windows
const char* defaultFontPath = "C:\\Windows\\Fonts\\Arial.ttf"; const char* defaultFontPath = "C:\\Windows\\Fonts\\Arial.ttf";
#elif defined(__linux__) #elif defined(__linux__)
const char *defaultFontPath = "/usr/share/fonts/truetype/DejaVuSans-Bold.ttf"; const char *defaultFontPath = "/usr/share/fonts/truetype/DejaVuSans-Bold.ttf";
#elif defined(__APPLE__) || defined(__MACH__)
const char *defaultFontPath = "/System/Library/Fonts/Supplemental/Arial.ttf";
#else #else
// Other platforms const char* defaultFontPath = nullptr;
const char* defaultFontPath = "path/to/a/default/font.ttf";
#endif #endif
@ -30,7 +30,7 @@ class Score {
private: private:
uint8_t leftScore, rightScore; uint8_t leftScore, rightScore;
const uint8_t MAX_SCORE; const uint8_t MAX_SCORE;
std::function<void(Side)> &whenWon; const std::function<void(Side)> whenWon;
TTF_Font *font; TTF_Font *font;
bool hasIncremented = false; bool hasIncremented = false;
@ -40,14 +40,19 @@ private:
SDL_Surface *surface = nullptr; SDL_Surface *surface = nullptr;
// Shadow // Shadow
const SDL_Color shadowColor = {243, 156, 18, 100}; // Black color for the shadow const SDL_Color shadowColor = {243, 156, 18, 100};
SDL_Surface *shadowSurface = nullptr; SDL_Surface *shadowSurface = nullptr;
const int shadowOffset = 3; const int shadowOffset = 3;
public: public:
explicit Score(uint8_t max_score, SDL_Point *screenSize, std::function<void(Side)> whenWon) : MAX_SCORE(max_score), explicit Score(uint8_t max_score, SDL_Point *screenSize, const std::function<void(Side)> whenWon) : MAX_SCORE(
whenWon(whenWon) { max_score), whenWon(whenWon) {
resetScore(); resetScore();
if (defaultFontPath == nullptr) {
std::cerr << "Font path is not set for this platform (null)" << std::endl;
exit(-1);
}
this->font = TTF_OpenFont(defaultFontPath, 42); this->font = TTF_OpenFont(defaultFontPath, 42);
if (font == nullptr) { if (font == nullptr) {
std::cerr << "Failed to load font: " << TTF_GetError() << std::endl; std::cerr << "Failed to load font: " << TTF_GetError() << std::endl;
@ -55,6 +60,7 @@ public:
} }
this->position = SDL_Rect{screenSize->x / 2 - 50, 10, 100, 40}; this->position = SDL_Rect{screenSize->x / 2 - 50, 10, 100, 40};
this->rightScore = this->leftScore = 0;
} }
~Score() { ~Score() {
@ -96,7 +102,7 @@ public:
if (shadowTexture != nullptr) { if (shadowTexture != nullptr) {
SDL_Rect shadowPosition = {position.x + shadowOffset, position.y + shadowOffset, position.w, SDL_Rect shadowPosition = {position.x + shadowOffset, position.y + shadowOffset, position.w,
position.h}; position.h};
SDL_RenderCopy(renderer, shadowTexture, NULL, &shadowPosition); SDL_RenderCopy(renderer, shadowTexture, nullptr, &shadowPosition);
SDL_DestroyTexture(shadowTexture); SDL_DestroyTexture(shadowTexture);
} }
} }
@ -105,7 +111,7 @@ public:
if (surface != nullptr) { if (surface != nullptr) {
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture != nullptr) { if (texture != nullptr) {
SDL_RenderCopy(renderer, texture, NULL, &position); SDL_RenderCopy(renderer, texture, nullptr, &position);
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
} }
} }