Jayjay Menu

This commit is contained in:
StevSpotted
2024-12-02 15:02:46 +01:00
parent 7edb45fd35
commit ce32838de7
5 changed files with 110 additions and 3 deletions

View File

@@ -19,6 +19,8 @@ add_executable(bloubloulespoissons main.cpp fish.cpp decors.cpp
env.cpp
player.h
player.cpp
menu.h
menu.cpp
)
# Lier SDL2 et SDL2_image

BIN
img/menu/background.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 317 KiB

View File

@@ -13,6 +13,7 @@
#include "camera.h"
#include "env.h"
#include "player.h"
#include "menu.h"
std::mutex mtx;
std::mutex coutMutex;
@@ -170,7 +171,17 @@ int main(int argc, char* args[]){
std::cerr << "Failed to initialize!" << std::endl;
return -1;
}
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);
Menu menu(renderer);
menu.addButton(300, 250, 200, 50, "Button", 10);
//std::thread quit_thread(handleQuitThread);
while (running) {
handleQuit();
menu.draw(renderer);
SDL_Delay(10);
}
//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;
}
@@ -204,14 +215,13 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
Player player = Player(windowWidth / 2, windowHeight / 2, 5, renderer);
std::thread player_thread(playerMovementThread, std::ref(player));
std::thread quit_thread(handleQuitThread);
while (running) {
renderScene(player, kelps, rocks, corals);
handleQuit();
SDL_Delay(10);
}
running = false;
quit_thread.join();
player_thread.join();
for (auto& thread : threads) {
thread.join();
@@ -282,6 +292,10 @@ void renderScene(Player player, const std::vector<Kelp>& kelps, const std::vecto
SDL_RenderPresent(renderer);
}
void rendererMenu(){
};
void cleanup() {
TTF_CloseFont(font);
TTF_Quit();

46
menu.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "menu.h"
void Menu::draw(SDL_Renderer* renderer){
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backgroundTxt, nullptr, &backgroundRect);
for(Button button : buttons){
SDL_SetRenderDrawColor(renderer, button.bgColor.r, button.bgColor.g, button.bgColor.b, button.bgColor.a);
SDL_RenderFillRect(renderer, &button.rect);
SDL_RenderCopy(renderer, button.txt, nullptr, &button.rect);
}
// int mouseX, mouseY;
// SDL_GetMouseState(&mouseX, &mouseY);
// if (mouseX >= buttonRect.x && mouseX <= buttonRect.x + buttonWidth &&
// mouseY >= buttonRect.y && mouseY <= buttonRect.y + buttonHeight) {
// SDL_Cursor* cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
// SDL_SetCursor(cursor);
// } else {
// SDL_Cursor* cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
// SDL_SetCursor(cursor);
// }
SDL_RenderPresent(renderer);
}
void Menu::addButton(int x, int y, int w, int h, std::string text, int size){
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};
button.fontColor = {255, 255, 255};
button.bgColor = {0, 0, 255, 255};
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;
buttons.push_back(button);
TTF_CloseFont(font_txt);
}

45
menu.h Normal file
View File

@@ -0,0 +1,45 @@
#include <iostream>
#include "env.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <vector>
#ifndef MENU_H
#define MENU_H
struct Button {
SDL_Rect rect;
SDL_Texture* txt;
SDL_Color fontColor;
SDL_Color bgColor;
};
class Menu {
private:
SDL_Texture* backgroundTxt = nullptr;
SDL_Rect backgroundRect = {0, 0, windowWidth, windowHeight};
std::string menuTitle = "BloubBloub les poissons";
std::vector<Button> buttons;
SDL_Renderer* renderer;
public:
Menu(SDL_Renderer* renderer){
this->renderer = renderer;
SDL_Surface* backgroundSurface = IMG_Load("../img/menu/background.png");
if (backgroundSurface == nullptr) {
std::cerr << "Erreur de chargement de l'image du menu: " << IMG_GetError() << std::endl;
} else {
backgroundTxt = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
SDL_FreeSurface(backgroundSurface);
}
};
void draw(SDL_Renderer* renderer);
void addButton(int x, int y, int w, int h, std::string text, int size);
};
#endif