mirror of
https://github.com/BreizhHardware/bloubloulespoissons.git
synced 2026-01-18 16:47:31 +01:00
Fix huge memory leaks
This commit is contained in:
18
fish.cpp
18
fish.cpp
@@ -1,23 +1,11 @@
|
||||
#include "fish.h"
|
||||
|
||||
Fish::Fish(int x, int y, int vx, int vy, int width, int height, SDL_Renderer* renderer, const char* imagePath)
|
||||
: x(x), y(y), vx(vx), vy(vy), width(width), height(height), texture(nullptr) {
|
||||
if(imagePath == nullptr) {
|
||||
return;
|
||||
}
|
||||
SDL_Surface* surface = IMG_Load(imagePath);
|
||||
if (surface) {
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
SDL_FreeSurface(surface);
|
||||
} else {
|
||||
std::cerr << "Erreur de chargement de l'image: " << IMG_GetError() << std::endl;
|
||||
}
|
||||
Fish::Fish(int x, int y, int vx, int vy, int width, int height, SDL_Renderer* renderer, SDL_Texture* texture)
|
||||
: x(x), y(y), vx(vx), vy(vy), width(width), height(height), texture(texture) {
|
||||
}
|
||||
|
||||
Fish::~Fish() {
|
||||
if (texture) {
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
// On ne libère pas la texture ici car elle est partagée entre plusieurs poissons
|
||||
}
|
||||
|
||||
void Fish::move() {
|
||||
|
||||
2
fish.h
2
fish.h
@@ -14,7 +14,7 @@ const int ENV_HEIGHT = 1200;
|
||||
|
||||
class Fish {
|
||||
public:
|
||||
Fish(int x, int y, int vx, int vy, int width, int height, SDL_Renderer* renderer, const char* imagePath);
|
||||
Fish(int x, int y, int vx, int vy, int width, int height, SDL_Renderer* renderer, SDL_Texture* texture);
|
||||
~Fish();
|
||||
|
||||
void move();
|
||||
|
||||
BIN
img/mory-min.png
Normal file
BIN
img/mory-min.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 MiB |
45
main.cpp
45
main.cpp
@@ -65,32 +65,15 @@ int main(int argc, char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Charger l'image de la texture pour les poissons
|
||||
SDL_Surface* fishSurface = IMG_Load("../img/mory.png");
|
||||
if (fishSurface == nullptr) {
|
||||
std::cerr << "Erreur de chargement de l'image: " << IMG_GetError() << std::endl;
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_Texture* fishTexture = SDL_CreateTextureFromSurface(renderer, fishSurface);
|
||||
SDL_FreeSurface(fishSurface);
|
||||
if (fishTexture == nullptr) {
|
||||
std::cerr << "Erreur de création de la texture: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<Fish> fishes;
|
||||
for (int i = 0; i < 90; ++i) {
|
||||
fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5, renderer, nullptr);
|
||||
}
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5, renderer, "../img/mory.png");
|
||||
SDL_Surface* fishesSurface = IMG_Load("../img/mory-min.png");
|
||||
SDL_Texture* fishesTexture = SDL_CreateTextureFromSurface(renderer, fishesSurface);
|
||||
SDL_FreeSurface(fishesSurface);
|
||||
//for (int i = 0; i < 100; ++i) {
|
||||
// fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5, renderer, nullptr);
|
||||
//}
|
||||
for (int i = 0; i < 900; ++i) {
|
||||
fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5, renderer, fishesTexture);
|
||||
}
|
||||
|
||||
std::thread fishThread(updateFish, std::ref(fishes));
|
||||
@@ -143,15 +126,15 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
drawGradientBackground(renderer, offsetX, offsetY);
|
||||
|
||||
rock.draw(renderer);
|
||||
reef.draw(renderer);
|
||||
kelp.draw(renderer);
|
||||
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
for (auto& fish : fishes) {
|
||||
fish.draw(renderer);
|
||||
}
|
||||
|
||||
rock.draw(renderer);
|
||||
reef.draw(renderer);
|
||||
kelp.draw(renderer);
|
||||
|
||||
// Dessiner le joueur
|
||||
SDL_Rect playerRect = { playerX - offsetX, playerY - offsetY, 20, 20 };
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Blanc
|
||||
@@ -172,9 +155,11 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
|
||||
fishThread.join();
|
||||
SDL_DestroyTexture(fishTexture);
|
||||
SDL_FreeSurface(fishesSurface);
|
||||
SDL_DestroyTexture(fishesTexture);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
IMG_Quit();
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user