Merge pull request #32 from BreizhHardware/new_menu

New menu
This commit is contained in:
Félix MARQUET
2024-12-12 17:36:43 +01:00
committed by GitHub
8 changed files with 77 additions and 7 deletions

11
env.cpp
View File

@@ -22,6 +22,17 @@ std::atomic<bool> game_running(false);
std::atomic<bool> isPlayingOnline(false);
std::atomic<bool> messageThreadRunning(false);
std::atomic<bool> isHost(false);
EventHandler eventHandler;
void resetAll(){
game_running = false;
isPlayingOnline = false;
messageThreadRunning = false;
isHost = false;
players.empty();
players_server.empty();
}
bool initEnvironment(SDL_Renderer* renderer) {

4
env.h
View File

@@ -11,6 +11,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "event.h"
class Player;
@@ -36,8 +37,11 @@ extern std::atomic<bool> messageThreadRunning;
extern std::vector<Player> players;
extern std::vector<Player> players_server;
extern std::atomic<bool> isHost;
extern EventHandler eventHandler;
bool initEnvironment(SDL_Renderer* renderer);
std::vector<SDL_Texture*> initTexture(SDL_Renderer* renderer);
void resetAll();
#endif // ENV_H

BIN
img/perdu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@@ -309,13 +309,21 @@ void updateShark(Shark &shark) {
}
void onPlayerLost(Menu &menu){
//Affiche drawLost par dessus le jeu
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
menu.drawLost(renderer);
SDL_RenderPresent(renderer);
Mix_Chunk* deathSound = Mix_LoadWAV("../sounds/death.wav");
Mix_PlayChannel(-1, deathSound, 0);
std::this_thread::sleep_for(std::chrono::seconds(8));
Mix_FreeChunk(deathSound);
menuRunning = true;
menu.changePage("Main");
menu.show();
resetAll();
}
EventHandler eventHandler;
int main(int argc, char* args[]) {
SDL_SetHint(SDL_HINT_VIDEO_WAYLAND_ALLOW_LIBDECOR, "0");
@@ -472,8 +480,18 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);
for (int i = 0; i < FISH_NUMBER ; ++i) {
school.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, 0.1, 0.1, school, i, 75, 75, renderer, rand() % 2 == 0 ? 1 : 0, fishTextures[rand() % fishCount]);
for (int i = 0; i < FISH_NUMBER; ++i) {
int x = rand() % ENV_WIDTH;
int y = rand() % ENV_HEIGHT;
double speedX = 0.1;
double speedY = 0.1;
std::cout << fishCount << std::endl;
int textureIndex = rand() % fishCount;
int gender = rand() % 2 == 0 ? 1 : 0;
std::cout << "Creating fish " << i << " at (" << x << ", " << y << ") with texture " << textureIndex << " and gender " << gender << std::endl;
school.emplace_back(x, y, speedX, speedY, school, i, 75, 75, renderer, gender, fishTextures[textureIndex]);
}
std::ranges::sort(school, Fish::SortByX);
std::vector<std::thread> fish_threads;
@@ -524,6 +542,8 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
}
std::cout << "All threads killed" << std::endl;
// running = false;
school.empty();
players.clear();
eventHandler.triggerEvent("playerLost");
return 0;
}
@@ -630,7 +650,8 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
} catch (const std::system_error& e) {
std::cerr << "Exception caught 5: " << e.what() << std::endl;
}
school.empty();
players.clear();
eventHandler.triggerEvent("playerLost");
}
else if (argc > 0 && argc < 65535 && args != "") {
@@ -691,6 +712,8 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
} catch (const std::system_error& e) {
std::cerr << "Exception caught 4: " << e.what() << std::endl;
}
school.empty();
players.clear();
eventHandler.triggerEvent("playerLost");
}
}

View File

@@ -290,4 +290,32 @@ void Menu::addImage(std::string page, int x, int y, int w, int h, std::string pa
p.images.push_back(ImagePage(imageTexture, imageRect));
}
}
}
void Menu::drawLost(SDL_Renderer* renderer) {
// Charger l'image perdu.png
SDL_Surface* lostSurface = IMG_Load("../img/perdu.png");
if (lostSurface == nullptr) {
std::cerr << "Erreur de chargement de l'image perdu.png: " << IMG_GetError() << std::endl;
return;
}
SDL_Texture* lostTexture = SDL_CreateTextureFromSurface(renderer, lostSurface);
SDL_FreeSurface(lostSurface);
// Définir la transparence (alpha) de l'image
SDL_SetTextureBlendMode(lostTexture, SDL_BLENDMODE_BLEND);
SDL_SetTextureAlphaMod(lostTexture, 128); // 128 pour 50% de transparence
// Obtenir les dimensions de la fenêtre
int windowWidth, windowHeight;
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);
// Définir le rectangle de destination pour l'image
SDL_Rect destRect = {0, 0, windowWidth, windowHeight};
// Dessiner l'image
SDL_RenderCopy(renderer, lostTexture, nullptr, &destRect);
// Détruire la texture après utilisation
SDL_DestroyTexture(lostTexture);
}

2
menu.h
View File

@@ -94,6 +94,8 @@ class Menu {
void addImage(std::string page, int x, int y, int w, int h, std::string path);
std::vector<Button> getButtons();
void drawLost(SDL_Renderer* renderer);
};
#endif

View File

@@ -37,8 +37,10 @@ void sendMessage(TCPsocket socket, const std::string& message) {
int result = SDLNet_TCP_Send(socket, &len, sizeof(len));
if (result < sizeof(len)) {
std::cerr << "SDLNet_TCP_Send failed: " << SDLNet_GetError() << std::endl;
std::cerr << "Closing the game ..." << std::endl;
game_running = false;
//std::cerr << "Closing the game ..." << std::endl;
//game_running = false;
std::cout << "Return to menu..." << std::endl;
eventHandler.triggerEvent("playerLost");
return;
}

BIN
sounds/death.wav Normal file

Binary file not shown.