mirror of
https://github.com/BreizhHardware/bloubloulespoissons.git
synced 2026-01-19 17:17:27 +01:00
Compare commits
16 Commits
Random-spa
...
new_menu
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f0ae6ac54 | |||
| 294d7503ed | |||
| bec39bbe3d | |||
| 82802cd33f | |||
|
|
beeb5fd187 | ||
| dd2c5c00c7 | |||
|
|
037a05b106 | ||
| 797e148f22 | |||
|
|
03737d9c3c | ||
| cf93e05955 | |||
|
|
4fddc26d91 | ||
|
|
cea9007684 | ||
| 566e066bdc | |||
|
|
682963e886 | ||
| 7d1ca2196b | |||
|
|
cbcc468fad |
@@ -55,4 +55,5 @@ endif()
|
||||
# Options pour la liaison statique en mode release
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
|
||||
target_sources(bloubloulespoissons PRIVATE resource.rc)
|
||||
endif()
|
||||
16
env.cpp
16
env.cpp
@@ -22,6 +22,22 @@ std::atomic<bool> game_running(false);
|
||||
std::atomic<bool> isPlayingOnline(false);
|
||||
std::atomic<bool> messageThreadRunning(false);
|
||||
std::atomic<bool> isHost(false);
|
||||
EventHandler eventHandler;
|
||||
std::atomic<bool> soundMuted(false);
|
||||
Mix_Music* backgroundMusic = nullptr;
|
||||
Mix_Music* menuMusic = nullptr;
|
||||
int MUSIC_CHANNEL = 0;
|
||||
int SOUND_CHANNEL = 1;
|
||||
|
||||
|
||||
void resetAll(){
|
||||
game_running = false;
|
||||
isPlayingOnline = false;
|
||||
messageThreadRunning = false;
|
||||
isHost = false;
|
||||
players.empty();
|
||||
players_server.empty();
|
||||
}
|
||||
|
||||
|
||||
bool initEnvironment(SDL_Renderer* renderer) {
|
||||
|
||||
10
env.h
10
env.h
@@ -5,12 +5,14 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include "event.h"
|
||||
|
||||
class Player;
|
||||
|
||||
@@ -36,8 +38,16 @@ extern std::atomic<bool> messageThreadRunning;
|
||||
extern std::vector<Player> players;
|
||||
extern std::vector<Player> players_server;
|
||||
extern std::atomic<bool> isHost;
|
||||
extern EventHandler eventHandler;
|
||||
extern std::atomic<bool> soundMuted;
|
||||
extern Mix_Music* backgroundMusic;
|
||||
extern Mix_Music* menuMusic;
|
||||
extern int MUSIC_CHANNEL;
|
||||
extern int SOUND_CHANNEL;
|
||||
|
||||
bool initEnvironment(SDL_Renderer* renderer);
|
||||
std::vector<SDL_Texture*> initTexture(SDL_Renderer* renderer);
|
||||
|
||||
void resetAll();
|
||||
|
||||
#endif // ENV_H
|
||||
34
event.h
Normal file
34
event.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef EVENTHANDLER_H
|
||||
#define EVENTHANDLER_H
|
||||
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
class EventHandler {
|
||||
public:
|
||||
using EventCallback = std::function<void()>;
|
||||
|
||||
template <typename EventCallback, typename... Args>
|
||||
|
||||
void registerEvent(const std::string& eventName, EventCallback callback, Args&&... args) {
|
||||
|
||||
eventCallbacks[eventName] = [callback, ...args = std::forward<Args>(args)]() {
|
||||
|
||||
callback(args.get()...);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void triggerEvent(const std::string& eventName) {
|
||||
if (eventCallbacks.find(eventName) != eventCallbacks.end()) {
|
||||
eventCallbacks[eventName]();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, EventCallback> eventCallbacks;
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
img/logo.ico
Normal file
BIN
img/logo.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 116 KiB |
BIN
img/logo.png
Normal file
BIN
img/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 31 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 317 KiB After Width: | Height: | Size: 306 KiB |
BIN
img/perdu.png
Normal file
BIN
img/perdu.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
129
main.cpp
129
main.cpp
@@ -24,6 +24,8 @@
|
||||
#include "network/networking_client.h"
|
||||
#include "shark.h"
|
||||
|
||||
#include "event.h"
|
||||
|
||||
|
||||
#include <system_error>
|
||||
|
||||
@@ -185,6 +187,15 @@ bool initSDL() {
|
||||
return false;
|
||||
}
|
||||
|
||||
Mix_AllocateChannels(16);
|
||||
|
||||
// Charger la musique du menu
|
||||
menuMusic = Mix_LoadMUS("../sounds/Menu.wav");
|
||||
if (menuMusic == nullptr) {
|
||||
std::cerr << "Erreur de chargement de la musique du menu: " << Mix_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow("BloubBloub les poissons",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
@@ -195,7 +206,7 @@ bool initSDL() {
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_Surface* iconSurface = IMG_Load("../img/mory.png");
|
||||
SDL_Surface* iconSurface = IMG_Load("../img/logo.png");
|
||||
if(iconSurface == nullptr) {
|
||||
std::cerr << "Erreur de chargement de l'icône: " << IMG_GetError() << std::endl;
|
||||
} else {
|
||||
@@ -306,6 +317,22 @@ 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();
|
||||
}
|
||||
|
||||
int main(int argc, char* args[]) {
|
||||
|
||||
SDL_SetHint(SDL_HINT_VIDEO_WAYLAND_ALLOW_LIBDECOR, "0");
|
||||
@@ -315,17 +342,23 @@ int main(int argc, char* args[]) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
Menu menu(renderer);
|
||||
menu.addPage("Main");
|
||||
menu.addPage("Multi");
|
||||
menu.addPage("Multi-Join");
|
||||
menu.changePage("Main");
|
||||
|
||||
|
||||
eventHandler.registerEvent("playerLost", onPlayerLost, std::ref(menu));
|
||||
|
||||
std::thread menu_thread = createThread("Menu thread", HandleMenuClick, std::ref(menu));
|
||||
std::thread quit_thread = createThread("Quit thread", handleQuitThread);
|
||||
|
||||
menu.addText("Main", (windowWidth/2) - 300, 50, 600, 100, "BloubBloub les poissons", 1024);
|
||||
|
||||
menu.addImage("Main", (windowWidth/2) - 100, 150, 200, 200, "../img/logo.png");
|
||||
|
||||
menu.addText("Multi-Join", (windowWidth/2) - 100, 50, 200, 100, "Join", 1024);
|
||||
|
||||
menu.addButton("Main", (windowWidth/2) - 100, windowHeight/2 - 25, 200, 50, "Solo", 1024, [](){
|
||||
@@ -399,6 +432,14 @@ int main(int argc, char* args[]) {
|
||||
|
||||
//menu.addButton((windowWidth/2) - 100, (windowHeight/2 + 25) + 50, 200, 50, "Multi", 1024);
|
||||
|
||||
menuMusic = Mix_LoadMUS("../sounds/Menu.wav");
|
||||
if (menuMusic != nullptr) {
|
||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
||||
if (Mix_PlayMusic(menuMusic, -1) == -1) {
|
||||
std::cerr << "Erreur de lecture de la musique du menu: " << Mix_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
while (running) {
|
||||
|
||||
handleQuit();
|
||||
@@ -448,6 +489,20 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
|
||||
|
||||
game_running = true;
|
||||
|
||||
Mix_HaltMusic();
|
||||
|
||||
backgroundMusic = Mix_LoadMUS("../sounds/Playing.wav");
|
||||
if (backgroundMusic == nullptr) {
|
||||
std::cerr << "Erreur de chargement de la musique: " << Mix_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
||||
if (Mix_PlayMusic(backgroundMusic, -1) == -1) {
|
||||
std::cerr << "Erreur de lecture de la musique: " << Mix_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
Mix_PlayMusic(backgroundMusic, -1);
|
||||
|
||||
std::vector<Kelp> kelps;
|
||||
std::vector<Rock> rocks;
|
||||
std::vector<Coral> corals;
|
||||
@@ -456,8 +511,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;
|
||||
@@ -507,7 +572,10 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
|
||||
std::cerr << "Exception caught 5: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << "All threads killed" << std::endl;
|
||||
running = false;
|
||||
// running = false;
|
||||
school.empty();
|
||||
players.clear();
|
||||
eventHandler.triggerEvent("playerLost");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -517,6 +585,21 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
|
||||
// return -1;
|
||||
// }
|
||||
game_running = true;
|
||||
|
||||
Mix_HaltMusic();
|
||||
|
||||
backgroundMusic = Mix_LoadMUS("../sounds/Playing.wav");
|
||||
if (backgroundMusic == nullptr) {
|
||||
std::cerr << "Erreur de chargement de la musique: " << Mix_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
||||
if (Mix_PlayMusic(backgroundMusic, -1) == -1) {
|
||||
std::cerr << "Erreur de lecture de la musique: " << Mix_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
Mix_PlayMusic(backgroundMusic, -1);
|
||||
|
||||
std::vector<Kelp> kelps;
|
||||
std::vector<Rock> rocks;
|
||||
std::vector<Coral> corals;
|
||||
@@ -613,8 +696,9 @@ 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;
|
||||
}
|
||||
|
||||
running = false;
|
||||
school.empty();
|
||||
players.clear();
|
||||
eventHandler.triggerEvent("playerLost");
|
||||
}
|
||||
else if (argc > 0 && argc < 65535 && args != "") {
|
||||
int port = 1234;
|
||||
@@ -674,7 +758,9 @@ 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;
|
||||
}
|
||||
running = false;
|
||||
school.empty();
|
||||
players.clear();
|
||||
eventHandler.triggerEvent("playerLost");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -701,6 +787,17 @@ void handleQuit() {
|
||||
}
|
||||
game_running = false;
|
||||
}
|
||||
|
||||
if (keystate[SDL_SCANCODE_M]) {
|
||||
soundMuted = !soundMuted;
|
||||
if (soundMuted) {
|
||||
Mix_Volume(-1, 0);
|
||||
Mix_VolumeMusic(0);
|
||||
} else {
|
||||
Mix_Volume(-1, MIX_MAX_VOLUME);
|
||||
Mix_VolumeMusic(MIX_MAX_VOLUME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void renderScene(std::vector<Player>& players, const std::vector<Kelp>& kelps, const std::vector<Rock>& rocks, const std::vector<Coral>& corals,Shark& shark) {
|
||||
@@ -805,6 +902,24 @@ void cleanup() {
|
||||
std::cerr << "Exception caught for DestroyWindow: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
if (backgroundMusic != nullptr) {
|
||||
Mix_FreeMusic(backgroundMusic);
|
||||
backgroundMusic = nullptr;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Exception caught for FreeMusic: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
if (menuMusic != nullptr) {
|
||||
Mix_FreeMusic(menuMusic);
|
||||
menuMusic = nullptr;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Exception caught for FreeMusic (menuMusic): " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
IMG_Quit();
|
||||
} catch (const std::exception& e) {
|
||||
|
||||
183
menu.cpp
183
menu.cpp
@@ -1,14 +1,98 @@
|
||||
#include "menu.h"
|
||||
|
||||
void Menu::draw(SDL_Renderer* renderer){
|
||||
|
||||
void drawRoundedRectWithGradient(SDL_Renderer* renderer, SDL_Rect rect, int radius, SDL_Color startColor, SDL_Color endColor, int gradientWidth) {
|
||||
for (int i = 0; i < gradientWidth; i++) {
|
||||
Uint8 r = startColor.r + i * (endColor.r - startColor.r) / gradientWidth;
|
||||
Uint8 g = startColor.g + i * (endColor.g - startColor.g) / gradientWidth;
|
||||
Uint8 b = startColor.b + i * (endColor.b - startColor.b) / gradientWidth;
|
||||
Uint8 a = startColor.a + i * (endColor.a - startColor.a) / gradientWidth;
|
||||
|
||||
SDL_Color gradientColor = {r, g, b, a};
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, gradientColor.r, gradientColor.g, gradientColor.b, gradientColor.a);
|
||||
SDL_Rect gradientRect = {
|
||||
rect.x - i,
|
||||
rect.y - i,
|
||||
rect.w + 2 * i,
|
||||
rect.h + 2 * i
|
||||
};
|
||||
|
||||
auto drawRoundedCorners = [&](int x, int y, int r) {
|
||||
for (int dy = -r; dy <= r; dy++) {
|
||||
for (int dx = -r; dx <= r; dx++) {
|
||||
if (dx * dx + dy * dy <= r * r) {
|
||||
SDL_RenderDrawPoint(renderer, x + dx, y + dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
drawRoundedCorners(gradientRect.x + radius, gradientRect.y + radius, radius);
|
||||
drawRoundedCorners(gradientRect.x + gradientRect.w - radius, gradientRect.y + radius, radius);
|
||||
drawRoundedCorners(gradientRect.x + radius, gradientRect.y + gradientRect.h - radius, radius);
|
||||
drawRoundedCorners(gradientRect.x + gradientRect.w - radius, gradientRect.y + gradientRect.h - radius, radius);
|
||||
|
||||
SDL_Rect top = {gradientRect.x + radius, gradientRect.y, gradientRect.w - 2 * radius, radius};
|
||||
SDL_Rect bottom = {gradientRect.x + radius, gradientRect.y + gradientRect.h - radius, gradientRect.w - 2 * radius, radius};
|
||||
SDL_Rect left = {gradientRect.x, gradientRect.y + radius, radius, gradientRect.h - 2 * radius};
|
||||
SDL_Rect right = {gradientRect.x + gradientRect.w - radius, gradientRect.y + radius, radius, gradientRect.h - 2 * radius};
|
||||
SDL_Rect center = {gradientRect.x + radius, gradientRect.y + radius, gradientRect.w - 2 * radius, gradientRect.h - 2 * radius};
|
||||
|
||||
SDL_RenderDrawRect(renderer, &top);
|
||||
SDL_RenderDrawRect(renderer, &bottom);
|
||||
SDL_RenderDrawRect(renderer, &left);
|
||||
SDL_RenderDrawRect(renderer, &right);
|
||||
}
|
||||
}
|
||||
|
||||
void drawRoundedRect(SDL_Renderer* renderer, SDL_Rect rect, int radius, SDL_Color color) {
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
||||
|
||||
auto drawRoundedCorners = [&](int x, int y, int r) {
|
||||
for (int dy = -r; dy <= r; dy++) {
|
||||
for (int dx = -r; dx <= r; dx++) {
|
||||
if (dx * dx + dy * dy <= r * r) {
|
||||
SDL_RenderDrawPoint(renderer, x + dx, y + dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
drawRoundedCorners(rect.x + radius, rect.y + radius, radius);
|
||||
drawRoundedCorners(rect.x + rect.w - radius, rect.y + radius, radius);
|
||||
drawRoundedCorners(rect.x + radius, rect.y + rect.h - radius, radius);
|
||||
drawRoundedCorners(rect.x + rect.w - radius, rect.y + rect.h - radius, radius);
|
||||
|
||||
SDL_Rect top = {rect.x + radius, rect.y, rect.w - 2 * radius, radius};
|
||||
SDL_Rect bottom = {rect.x + radius, rect.y + rect.h - radius, rect.w - 2 * radius, radius};
|
||||
SDL_Rect left = {rect.x, rect.y + radius, radius, rect.h - 2 * radius};
|
||||
SDL_Rect right = {rect.x + rect.w - radius, rect.y + radius, radius, rect.h - 2 * radius};
|
||||
SDL_Rect center = {rect.x + radius, rect.y + radius, rect.w - 2 * radius, rect.h - 2 * radius};
|
||||
|
||||
SDL_RenderFillRect(renderer, &top);
|
||||
SDL_RenderFillRect(renderer, &bottom);
|
||||
SDL_RenderFillRect(renderer, &left);
|
||||
SDL_RenderFillRect(renderer, &right);
|
||||
SDL_RenderFillRect(renderer, ¢er);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Menu::draw(SDL_Renderer* renderer) {
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, backgroundTxt, nullptr, &backgroundRect);
|
||||
if(pages.size() > 0){
|
||||
if(currentPage == -1 && pages.size() > 0){
|
||||
|
||||
if (pages.size() > 0) {
|
||||
if (currentPage == -1 && pages.size() > 0) {
|
||||
currentPage = 0;
|
||||
}
|
||||
|
||||
for(Text& text : pages[currentPage].texts){
|
||||
for (ImagePage image : pages[currentPage].images) {
|
||||
SDL_RenderCopy(renderer, image.image, nullptr, &image.rect);
|
||||
}
|
||||
|
||||
for (Text& text : pages[currentPage].texts) {
|
||||
SDL_RenderCopy(renderer, text.txt, nullptr, &text.rect);
|
||||
}
|
||||
|
||||
@@ -16,13 +100,21 @@ void Menu::draw(SDL_Renderer* renderer){
|
||||
SDL_GetMouseState(&mouseX, &mouseY);
|
||||
|
||||
bool hover = false;
|
||||
for(Button& button : pages[currentPage].buttons){
|
||||
for (Button& button : pages[currentPage].buttons) {
|
||||
// Activer le blending pour l'arrière-plan du bouton
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetRenderDrawColor(renderer, button.bgColor.r, button.bgColor.g, button.bgColor.b, button.bgColor.a);
|
||||
SDL_RenderFillRect(renderer, &button.rect);
|
||||
|
||||
// Dessiner la bordure du bouton
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
|
||||
SDL_SetRenderDrawColor(renderer, button.borderColor.r, button.borderColor.g, button.borderColor.b, button.borderColor.a);
|
||||
SDL_Rect borderRect = {button.rect.x - button.borderWidth, button.rect.y - button.borderWidth, button.rect.w + 2 * button.borderWidth, button.rect.h + 2 * button.borderWidth};
|
||||
SDL_RenderDrawRect(renderer, &borderRect);
|
||||
|
||||
SDL_RenderCopy(renderer, button.txt, nullptr, &button.txtRect);
|
||||
|
||||
if (button.isTextInput) {
|
||||
// Afficher le texte saisi
|
||||
TTF_Font* font_txt = TTF_OpenFont("../fonts/arial.ttf", 24);
|
||||
SDL_Surface* textSurface = TTF_RenderText_Solid(font_txt, button.inputText.c_str(), button.fontColor);
|
||||
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
|
||||
@@ -51,9 +143,16 @@ void Menu::draw(SDL_Renderer* renderer){
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
void Menu::handleClickedButton(){
|
||||
|
||||
|
||||
void Menu::handleClickedButton() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type == SDL_MOUSEBUTTONDOWN) {
|
||||
int mouseX, mouseY;
|
||||
SDL_GetMouseState(&mouseX, &mouseY);
|
||||
@@ -71,7 +170,7 @@ void Menu::handleClickedButton(){
|
||||
}
|
||||
}
|
||||
}
|
||||
}else if (event.type == SDL_TEXTINPUT) {
|
||||
} else if (event.type == SDL_TEXTINPUT) {
|
||||
if (activeTextInputIndex != -1) {
|
||||
Button& button = pages[currentPage].buttons[activeTextInputIndex];
|
||||
if (button.isTextInput) {
|
||||
@@ -109,27 +208,34 @@ void Menu::changePage(std::string title){
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::addButton(std::string page, int x, int y, int w, int h, std::string text, int size, std::function<void()> callback, bool isTextInput){
|
||||
for(Page& p : pages){
|
||||
if(p.title == page){
|
||||
void Menu::addButton(std::string page, int x, int y, int w, int h, std::string text, int size, std::function<void()> callback, bool isTextInput) {
|
||||
for (Page& p : pages) {
|
||||
if (p.title == page) {
|
||||
TTF_Font* font_txt = TTF_OpenFont("../fonts/arial.ttf", size);
|
||||
if (font_txt == nullptr) {
|
||||
std::cerr << "Erreur de chargement de la police: " << TTF_GetError() << std::endl;
|
||||
}
|
||||
Button button;
|
||||
button.rect = {x, y, w, h};
|
||||
int textWidth = w/2;
|
||||
int textHeight = h/1.5;
|
||||
button.txtRect = {x + (w/2) - (textWidth/2), y + (h/2) - (textHeight/2), textWidth, textHeight};
|
||||
int textWidth = w / 2;
|
||||
int textHeight = h / 1.5;
|
||||
button.txtRect = {x + (w / 2) - (textWidth / 2), y + (h / 2) - (textHeight / 2), textWidth, textHeight};
|
||||
button.fontColor = {255, 255, 255};
|
||||
button.bgColor = {0, 0, 255, 255};
|
||||
button.bgColor = {0, 0, 0, 75}; // Fond noir avec opacité de 13%
|
||||
button.borderColor = {40, 120, 122, 255}; // Couleur de la bordure
|
||||
button.borderWidth = 5; // Largeur de la bordure
|
||||
button.borderRadius = 17; // Rayon de la bordure
|
||||
button.isTextInput = isTextInput;
|
||||
|
||||
|
||||
SDL_Surface* textSurface = TTF_RenderText_Solid(font_txt, text.c_str(), button.fontColor);
|
||||
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
|
||||
SDL_FreeSurface(textSurface);
|
||||
button.txt = textTexture;
|
||||
button.callback = callback;
|
||||
button.startGradientColor = {40, 120, 122, 255};
|
||||
button.endGradientColor = {55, 171, 189, 255};
|
||||
button.gradientWidth = 10;
|
||||
|
||||
p.buttons.push_back(button);
|
||||
TTF_CloseFont(font_txt);
|
||||
}
|
||||
@@ -167,4 +273,49 @@ void Menu::addPage(std::string title){
|
||||
|
||||
std::vector<Button> Menu::getButtons(){
|
||||
return pages[currentPage].buttons;
|
||||
}
|
||||
|
||||
void Menu::addImage(std::string page, int x, int y, int w, int h, std::string path) {
|
||||
for (Page& p : pages) {
|
||||
if (p.title == page) {
|
||||
SDL_Surface* imageSurface = IMG_Load(path.c_str());
|
||||
if (imageSurface == nullptr) {
|
||||
std::cerr << "Erreur de chargement de l'image: " << IMG_GetError() << std::endl;
|
||||
return;
|
||||
}
|
||||
SDL_Texture* imageTexture = SDL_CreateTextureFromSurface(renderer, imageSurface);
|
||||
SDL_FreeSurface(imageSurface);
|
||||
SDL_Rect imageRect = {x, y, w, h};
|
||||
|
||||
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);
|
||||
}
|
||||
19
menu.h
19
menu.h
@@ -18,6 +18,12 @@ struct Button {
|
||||
std::function<void()> callback;
|
||||
bool isTextInput = false;
|
||||
std::string inputText;
|
||||
SDL_Color borderColor;
|
||||
int borderWidth;
|
||||
int borderRadius;
|
||||
SDL_Color startGradientColor;
|
||||
SDL_Color endGradientColor;
|
||||
int gradientWidth;
|
||||
};
|
||||
|
||||
struct Text {
|
||||
@@ -27,13 +33,22 @@ struct Text {
|
||||
SDL_Rect txtRect;
|
||||
};
|
||||
|
||||
struct ImagePage {
|
||||
SDL_Texture* image;
|
||||
SDL_Rect rect;
|
||||
};
|
||||
|
||||
struct Page {
|
||||
std::string title;
|
||||
std::vector<Button> buttons;
|
||||
std::vector<Text> texts;
|
||||
std::vector<ImagePage> images;
|
||||
};
|
||||
|
||||
|
||||
void drawRoundedRectWithGradient(SDL_Renderer* renderer, SDL_Rect rect, int radius, SDL_Color startColor, SDL_Color endColor, int gradientWidth);
|
||||
void drawRoundedRect(SDL_Renderer* renderer, SDL_Rect rect, int radius, SDL_Color color);
|
||||
|
||||
class Menu {
|
||||
private:
|
||||
SDL_Texture* backgroundTxt = nullptr;
|
||||
@@ -76,7 +91,11 @@ class Menu {
|
||||
|
||||
void addPage(std::string title);
|
||||
|
||||
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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
1
resource.rc
Normal file
1
resource.rc
Normal file
@@ -0,0 +1 @@
|
||||
IDI_ICON1 ICON "../img/logo.ico"
|
||||
17
shark.cpp
17
shark.cpp
@@ -22,6 +22,11 @@ Shark::Shark(const int x, const int y, const float vx, const float vy, const int
|
||||
std::cerr << "Erreur de chargement du son du requin: " << Mix_GetError() << std::endl;
|
||||
}
|
||||
|
||||
approachingSound = Mix_LoadWAV("../sounds/Shark-approching.wav");
|
||||
if (approachingSound == nullptr) {
|
||||
std::cerr << "Erreur de chargement du son d'approche du requin: " << Mix_GetError() << std::endl;
|
||||
}
|
||||
|
||||
lastSoundTime = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
@@ -69,6 +74,11 @@ void Shark::cycle() {
|
||||
if (isInView(player)) {
|
||||
checkNeighborhood(player, xpos_avg, ypos_avg, xvel_avg, yvel_avg, neighboring_player);
|
||||
checkCollision(player);
|
||||
Mix_PlayChannel(SOUND_CHANNEL, approachingSound, 0);
|
||||
}
|
||||
if (!isInView(player)) {
|
||||
// Cut the approach sound
|
||||
Mix_HaltChannel(SOUND_CHANNEL);
|
||||
}
|
||||
}
|
||||
if (neighboring_player > 0) {
|
||||
@@ -94,7 +104,7 @@ void Shark::cycle() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastSoundTime).count() > 15) {
|
||||
if (sharkSound != nullptr) {
|
||||
Mix_PlayChannel(-1, sharkSound, 0);
|
||||
Mix_PlayChannel(SOUND_CHANNEL, sharkSound, 0);
|
||||
}
|
||||
lastSoundTime = now;
|
||||
}
|
||||
@@ -117,6 +127,11 @@ Shark::~Shark() {
|
||||
Mix_FreeChunk(sharkSound);
|
||||
sharkSound = nullptr;
|
||||
}
|
||||
|
||||
if (approachingSound) {
|
||||
Mix_FreeChunk(approachingSound);
|
||||
approachingSound = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Shark::updatePosition(int newX, int newY) {
|
||||
|
||||
1
shark.h
1
shark.h
@@ -31,6 +31,7 @@ private:
|
||||
std::vector<Player> &players_list;
|
||||
|
||||
Mix_Chunk* sharkSound;
|
||||
Mix_Chunk* approachingSound;
|
||||
std::chrono::steady_clock::time_point lastSoundTime;
|
||||
std::chrono::steady_clock::time_point lastSendTime;
|
||||
|
||||
|
||||
BIN
sounds/Menu.wav
Normal file
BIN
sounds/Menu.wav
Normal file
Binary file not shown.
BIN
sounds/Playing.wav
Normal file
BIN
sounds/Playing.wav
Normal file
Binary file not shown.
BIN
sounds/Shark-approching.wav
Normal file
BIN
sounds/Shark-approching.wav
Normal file
Binary file not shown.
BIN
sounds/death.wav
Normal file
BIN
sounds/death.wav
Normal file
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user