From fb9df2ae08cfd00a4329a330ca53ccccd8bf9351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20MARQUET?= Date: Sun, 8 Dec 2024 17:26:03 +0100 Subject: [PATCH] Start adding capability to see other players --- CMakeLists.txt | 2 ++ display.cpp | 36 ++++++++++++++++++++++++++++++++++++ display.h | 17 +++++++++++++++++ main.cpp | 2 ++ 4 files changed, 57 insertions(+) create mode 100644 display.cpp create mode 100644 display.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 28bb5bf..7894ee6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,8 @@ add_executable(bloubloulespoissons main.cpp fish.cpp decors.cpp network/networking.h network/networking_client.cpp network/networking_client.h + display.cpp + display.h ) # Lier SDL2 et SDL2_image diff --git a/display.cpp b/display.cpp new file mode 100644 index 0000000..f2b5c16 --- /dev/null +++ b/display.cpp @@ -0,0 +1,36 @@ +// +// Created by BreizhHardware on 08/12/2024. +// + +#include "display.h" + +double calculateDistance(int x1, int y1, int x2, int y2) { + return std::sqrt(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2)); +} + +void displayNearbyPlayers(SDL_Renderer* renderer, TTF_Font* font, Player& currentPlayer, std::vector& players, double threshold) { + int currentX = currentPlayer.getUnifiedX(); + int currentY = currentPlayer.getUnifiedY(); + int offsetY = 90; // Starting Y position for displaying nearby players + + for (auto& player : players) { + if (&player == ¤tPlayer) continue; // Skip the current player + + int playerX = player.getUnifiedX(); + int playerY = player.getUnifiedY(); + double distance = calculateDistance(currentX, currentY, playerX, playerY); + + if (distance < threshold) { + std::string nearbyPlayerText = "Nearby Player: (" + std::to_string(playerX) + ", " + std::to_string(playerY) + ")"; + SDL_Color textColor = {0, 255, 0}; + SDL_Surface* textSurface = TTF_RenderText_Solid(font, nearbyPlayerText.c_str(), textColor); + SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + SDL_Rect textRect = {10, offsetY, textSurface->w, textSurface->h}; + SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); + SDL_FreeSurface(textSurface); + SDL_DestroyTexture(textTexture); + + offsetY += 20; // Move down for the next player + } + } +} \ No newline at end of file diff --git a/display.h b/display.h new file mode 100644 index 0000000..9c97dd9 --- /dev/null +++ b/display.h @@ -0,0 +1,17 @@ +// +// Created by BreizhHardware on 08/12/2024. +// + +#ifndef DISPLAY_H +#define DISPLAY_H +#include +#include +#include +#include +#include +#include "player.h" + +double calculateDistance(int x1, int y1, int x2, int y2); +void displayNearbyPlayers(SDL_Renderer* renderer, TTF_Font* font, Player& currentPlayer, std::vector& players, double threshold); + +#endif //DISPLAY_H diff --git a/main.cpp b/main.cpp index 5ef7bda..210fd4f 100644 --- a/main.cpp +++ b/main.cpp @@ -12,6 +12,7 @@ #include "fish.h" #include "decors.h" #include "camera.h" +#include "display.h" #include "env.h" #include "player.h" #include "menu.h" @@ -599,6 +600,7 @@ void renderScene(std::vector& players, const std::vector& kelps, c int unifiedX = player.getUnifiedX(); int unifiedY = player.getUnifiedY(); displayUnifiedPlayerCoord(renderer, font, unifiedX, unifiedY); + displayNearbyPlayers(renderer, font, player, players, 200.0); } SDL_RenderPresent(renderer);