|
1 | 1 | // Names: Hugo, Jaime, Jay, Leo |
2 | | -// Last Modified: 05/17/25 |
| 2 | +// Last Modified: 08/30/25 |
3 | 3 | // Purpose: MEOWSTRO |
4 | 4 | // |
5 | 5 | // |
@@ -68,7 +68,7 @@ int main(int argc, char** argv) |
68 | 68 | stats.resetStats(); |
69 | 69 | } |
70 | 70 | } |
71 | | - window.~RenderWindow(); |
| 71 | + // Destructor will be called automatically when window goes out of scope |
72 | 72 |
|
73 | 73 | TTF_Quit(); |
74 | 74 | SDL_Quit(); |
@@ -144,6 +144,13 @@ void mainMenu(RenderWindow &window, bool &gameRunning, SDL_Event &event) |
144 | 144 | window.render(quit); |
145 | 145 | window.display(); |
146 | 146 | } |
| 147 | + |
| 148 | + SDL_DestroyTexture(quitTexture); |
| 149 | + SDL_DestroyTexture(startTexture); |
| 150 | + SDL_DestroyTexture(logoTexture); |
| 151 | + SDL_DestroyTexture(logoCatTexture); |
| 152 | + SDL_DestroyTexture(selectedTexture); |
| 153 | + |
147 | 154 | logoFont.unload(); |
148 | 155 | startFont.unload(); |
149 | 156 | quitFont.unload(); |
@@ -238,9 +245,18 @@ void gameLoop(RenderWindow& window, bool& gameRunning, SDL_Event& event, GameSta |
238 | 245 |
|
239 | 246 | double currentTime = SDL_GetTicks() - songStartTime; //calculates the delay by comparing the current ticks and when the song starts |
240 | 247 |
|
241 | | - std::string strNum = formatScore(stats.getScore()); |
242 | | - numberTexture = numberFont.renderText(window.getRenderer(), strNum, { 0, 0, 0, 255 }); |
243 | | - number.setTexture(numberTexture); |
| 248 | + // Only update score texture when score actually changes to prevent memory leaks |
| 249 | + static int lastScore = -1; |
| 250 | + int currentScore = stats.getScore(); |
| 251 | + if (currentScore != lastScore) { |
| 252 | + if (lastScore != -1) { |
| 253 | + SDL_DestroyTexture(numberTexture); // Clean up old texture |
| 254 | + } |
| 255 | + std::string strNum = formatScore(currentScore); |
| 256 | + numberTexture = numberFont.renderText(window.getRenderer(), strNum, { 0, 0, 0, 255 }); |
| 257 | + number.setTexture(numberTexture); |
| 258 | + lastScore = currentScore; |
| 259 | + } |
244 | 260 | handX = fisher.getX() + 135; |
245 | 261 | handY = fisher.getY() + 50; |
246 | 262 |
|
@@ -428,9 +444,24 @@ void gameLoop(RenderWindow& window, bool& gameRunning, SDL_Event& event, GameSta |
428 | 444 | SDL_Delay(75); |
429 | 445 | } |
430 | 446 | player.stopBackgroundMusic(); |
431 | | - player.~Audio(); |
| 447 | + |
| 448 | + // Clean up game loop textures to prevent memory leaks |
| 449 | + for (int i = 0; i < NUM_FISH_TEXTURES; i++) { |
| 450 | + SDL_DestroyTexture(fishTextures[i]); |
| 451 | + } |
| 452 | + SDL_DestroyTexture(oceanTexture); |
| 453 | + SDL_DestroyTexture(boatTexture); |
| 454 | + SDL_DestroyTexture(fisherTexture); |
| 455 | + SDL_DestroyTexture(hookTexture); |
| 456 | + SDL_DestroyTexture(scoreTexture); |
| 457 | + SDL_DestroyTexture(numberTexture); |
| 458 | + SDL_DestroyTexture(perfectHitTexture); |
| 459 | + SDL_DestroyTexture(goodHitTexture); |
| 460 | + |
432 | 461 | scoreFont.unload(); |
433 | 462 | numberFont.unload(); |
| 463 | + perfectHitFont.unload(); |
| 464 | + goodHitFont.unload(); |
434 | 465 | } |
435 | 466 |
|
436 | 467 | void endScreen(RenderWindow& window, bool& gameRunning, SDL_Event& event, GameStats& stats) |
@@ -539,6 +570,22 @@ void endScreen(RenderWindow& window, bool& gameRunning, SDL_Event& event, GameSt |
539 | 570 | window.render(numMisses); |
540 | 571 | window.display(); |
541 | 572 | } |
| 573 | + |
| 574 | + // Clean up end screen textures to prevent memory leaks |
| 575 | + SDL_DestroyTexture(statsTexture); |
| 576 | + SDL_DestroyTexture(scoreTexture); |
| 577 | + SDL_DestroyTexture(numberTexture); |
| 578 | + SDL_DestroyTexture(hitsTexture); |
| 579 | + SDL_DestroyTexture(numHitsTexture); |
| 580 | + SDL_DestroyTexture(accuracyTexture); |
| 581 | + SDL_DestroyTexture(accPercentTexture); |
| 582 | + SDL_DestroyTexture(missTexture); |
| 583 | + SDL_DestroyTexture(numMissTexture); |
| 584 | + SDL_DestroyTexture(quitTexture); |
| 585 | + SDL_DestroyTexture(retryTexture); |
| 586 | + SDL_DestroyTexture(logoTexture); |
| 587 | + SDL_DestroyTexture(selectedTexture); |
| 588 | + SDL_DestroyTexture(logoCatTexture); |
542 | 589 | } |
543 | 590 |
|
544 | 591 | std::string formatScore(int score) |
|
0 commit comments