Merge pull request #22 from BreizhHardware/main

Main into new_menu
This commit is contained in:
Félix MARQUET
2024-12-12 15:28:07 +01:00
committed by GitHub
8 changed files with 84 additions and 12 deletions

View File

@@ -10,19 +10,23 @@ jobs:
build:
runs-on: ubuntu-latest
container:
image: fedora:latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up C++ environment
run: |
sudo apt-get update
sudo apt-get install -y gcc g++ build-essential
dnf install -y gcc gcc-c++ cmake make
- name: Install SDL2 dependencies
run: |
sudo apt-get update
sudo apt-get install -y libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev libsdl2-net-dev libsdl2-mixer-dev
dnf install -y SDL2 SDL2-devel SDL2_image SDL2_image-devel SDL2_ttf SDL2_ttf-devel SDL2_net SDL2_net-devel SDL2_mixer SDL2_mixer-devel
- name: Clean build directory
run: rm -rf build
- name: Create build directory
run: mkdir build

2
.gitignore vendored
View File

@@ -54,6 +54,8 @@ cmake-build-fedora
cmake-build-fedora/
cmake-build-debug-wsl
cmake-build-debug-wsl/
cmake-build-release-mingw
cmake-build-release-mingw/
# .vscode folder
.vscode/

View File

@@ -14,6 +14,7 @@ find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_net REQUIRED)
find_package(SDL2_mixer REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS} ${SDL2_NET_INCLUDE_DIRS})
@@ -36,7 +37,8 @@ add_executable(bloubloulespoissons main.cpp fish.cpp decors.cpp
shark.h
)
target_link_libraries(bloubloulespoissons ${SDL2_LIBRARIES} -lSDL2_image -lSDL2_ttf -lSDL2_net -lSDL2_mixer)
# Lier SDL2 et SDL2_image
target_link_libraries(bloubloulespoissons ${SDL2_LIBRARIES} SDL2::SDL2 SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf SDL2_net::SDL2_net SDL2_mixer::SDL2_mixer)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
@@ -48,4 +50,9 @@ if (WIN32)
set_target_properties(bloubloulespoissons PROPERTIES LINK_FLAGS "/SUBSYSTEM:CONSOLE")
endif()
target_link_libraries(bloubloulespoissons ws2_32)
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")
endif()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 43 KiB

View File

@@ -40,6 +40,7 @@ std::vector<Player> players_server;
struct ThreadInfo {
std::thread::id id;
std::string functionName;
bool isRunning;
};
std::vector<ThreadInfo> threadInfos;
@@ -62,6 +63,7 @@ void HandleMenuClick(Menu& menu);
void updateFishRange(std::vector<Fish>& school, int start, int end);
int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mais_une_des_fonctions_principale_meme_primordiale_du_projet_denomme_bloubloulespoissons(int argc, char* args[]);
int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mais_une_des_fonctions_principale_meme_primordiale_du_projet_denomme_bloubloulespoissons_mais_celle_ci_elle_lance_en_multijoueur(int argc, std::string args);
void checkThreads();
template <typename Function, typename... Args>
std::thread createThread(std::string key, Function&& func, Args&&... args) {
@@ -71,12 +73,28 @@ std::thread createThread(std::string key, Function&& func, Args&&... args) {
ThreadInfo info;
info.id = std::this_thread::get_id();
info.functionName = key;
info.isRunning = true;
{
std::lock_guard<std::mutex> lock(mtx);
threadInfos.push_back(info);
}
func(std::forward<Args>(args)...);
std::cout << "ThreadID = " << info.id << " ThreadFunction = " << info.functionName << std::endl;
try {
func(std::forward<Args>(args)...);
} catch (const std::exception& e) {
std::cerr << "Exception in thread " << key << ": " << e.what() << std::endl;
} catch (...) {
std::cerr << "Unknown exception in thread " << key << std::endl;
}
{
std::lock_guard<std::mutex> lock(mtx);
for (auto& threadInfo : threadInfos) {
if (threadInfo.id == std::this_thread::get_id()) {
threadInfo.isRunning = false;
break;
}
}
}
std::cout << "ThreadID = " << info.id << " ThreadFunction = " << info.functionName << " finished" << std::endl;
});
return thread;
} catch (const std::system_error& e) {
@@ -85,6 +103,17 @@ std::thread createThread(std::string key, Function&& func, Args&&... args) {
}
}
void checkThreads() {
std::lock_guard<std::mutex> lock(mtx);
for (const auto& threadInfo : threadInfos) {
if (threadInfo.isRunning) {
std::cout << "Thread " << threadInfo.functionName << " (ID: " << threadInfo.id << ") is still running." << std::endl;
} else {
std::cout << "Thread " << threadInfo.functionName << " (ID: " << threadInfo.id << ") has stopped." << std::endl;
}
}
}
void displayFPS(SDL_Renderer* renderer, TTF_Font* font, int fps) {
std::string fpsText = "FPS: " + std::to_string(fps);
SDL_Color color = {0, 255, 0}; // Green
@@ -246,8 +275,14 @@ void handleQuitThread() {
};
void HandleMenuClick(Menu& menu){
while (running) {
menu.handleClickedButton();
try {
while (menuRunning) {
menu.handleClickedButton();
}
} catch (const std::exception& e) {
std::cerr << "Exception in HandleMenuClick: " << e.what() << std::endl;
} catch (...) {
std::cerr << "Unknown exception in HandleMenuClick" << std::endl;
}
}
@@ -287,6 +322,7 @@ int main(int argc, char* args[]) {
menu.changePage("Main");
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);
@@ -362,7 +398,6 @@ int main(int argc, char* args[]) {
//menu.addButton((windowWidth/2) - 100, (windowHeight/2 + 25) + 50, 200, 50, "Multi", 1024);
std::thread quit_thread = createThread("Quit thread", handleQuitThread);
while (running) {
@@ -373,22 +408,33 @@ int main(int argc, char* args[]) {
}
}
}
std::cout << "Check Threads 1" << std::endl;
checkThreads();
try {
if (menu_thread.joinable())
menuRunning = false;
if (menu_thread.joinable()) {
menuRunning = false;
menu_thread.join();
}
} catch (const std::system_error& e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
checkThreads();
try{
if(quit_thread.joinable())
quit_thread.join();
}catch(const std::system_error& e){
std::cerr << "Exception caught 2: " << e.what() << std::endl;
}
if (!isPlayingOnline) {
cleanup();
}
std::cout << "Check Threads 2" << std::endl;
checkThreads();
//pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mais_une_des_fonctions_principale_meme_primordiale_du_projet_denomme_bloubloulespoissons(argc, args);
return 0;
}

View File

@@ -41,6 +41,7 @@ class Player {
bool onlineMovement();
int unifiedX = 0;
int unifiedY = 0;
int HITBOX = 50;
public:
Player(int x, int y, int playerSpeed, SDL_Renderer* renderer, int playerId) : x(x), y(y), playerBaseX(x), playerBaseY(y), playerSpeed(playerSpeed), playerId(playerId) {
playerPosForRender.x = x;
@@ -75,6 +76,8 @@ class Player {
return playerTexture;
}
int getHITBOX() const {return HITBOX;}
};
#endif

View File

@@ -55,6 +55,12 @@ void Shark::checkNeighborhood(Player& player, float &xpos_avg, float &ypos_avg,
}
}
void Shark::checkCollision(Player& player) {
if (player.getUnifiedX() + player.getHITBOX() >= x - HITBOX && player.getUnifiedX() - player.getHITBOX() <= x + HITBOX && player.getUnifiedY() + player.getHITBOX() >= y - HITBOX && player.getUnifiedY() - player.getHITBOX() <= y + HITBOX) {
game_running = false;
}
}
void Shark::cycle() {
int neighboring_player = 0;
@@ -62,6 +68,7 @@ void Shark::cycle() {
for (auto& player : players_list) {
if (isInView(player)) {
checkNeighborhood(player, xpos_avg, ypos_avg, xvel_avg, yvel_avg, neighboring_player);
checkCollision(player);
}
}
if (neighboring_player > 0) {

View File

@@ -21,6 +21,7 @@ private:
const float MAX_SPEED = 3;
const float MIN_SPEED = 0.6;
const float BIASVALUE = 0.001;
const int HITBOX = 50;
float x, y;
float vx, vy;
@@ -42,6 +43,7 @@ public:
float getY() const { return y; };
float getVx() const { return vx; };
float getVy() const { return vy; };
int getHITBOX() const { return HITBOX; };
@@ -52,6 +54,7 @@ public:
bool isInView(Player& player);
void checkNeighborhood(Player& player, float &xpos_avg, float &ypos_avg, float &xvel_avg, float &yvel_avg, int &neighboring_player);
void updatePosition(int newX, int newY);
void checkCollision(Player& player);
};