Bonjour à tous, je viens vers vous aujourd’hui car j’ai un problème lorsque je veux utiliser un while dans SDL2, je souhaite faire un bouton, ce que j’ai fais, qui en créer deux autres quand on appuie dessus, je me suis dis quand je clique sur le premier bouton je vais faire un while qui tant qu’on a pas appuyé sur un des nouveaux bouton ne va rien faire, c’set ce que j’ai essayé mais quand j’appuie sur le bouton en question le logiciel arrête de répondre, voilà mon code :
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <cstdlib>
#include <string>
#include <iostream>
#define TTF_GetError SDL_GetError
extern DECLSPEC int SDLCALL TTF_Init(void);
template<typename T>
constexpr T WIDTHSCREEN{1280};
template<typename T>
constexpr T HEIGHTSCREEN{720};
int main (int argc, char* argv[])
{
if (TTF_Init() < 0) //TTF-Init
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[DEBUG] > %s", TTF_GetError());
return EXIT_FAILURE;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) //Init_video
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[DEBUG] > %s", SDL_GetError());
return EXIT_FAILURE;
}
bool isOpen = true; //Booléenne pour l'app
SDL_Event events; //events
SDL_Window* pWindow = SDL_CreateWindow("SDL_CreateTexture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTHSCREEN<int>, HEIGHTSCREEN<int>, SDL_WINDOW_RESIZABLE); //la fenetre
SDL_Renderer* pRenderer = SDL_CreateRenderer(pWindow, -1, 0); //Le rendu
SDL_Renderer* pHistorique = NULL; //inutile (pour plus tard)
TTF_Font* font = TTF_OpenFont("D:\\font\\bebas-neue_fr.allfont.net.ttf", 40); //Font
SDL_Surface* text = TTF_RenderText_Blended(font, "Burger Truck", SDL_Color{ 255, 255, 255, 255 }); //Tous
SDL_Surface* burger1_surface = TTF_RenderText_Blended(font, "Burger 1", SDL_Color{ 255, 255, 255, 255 }); //Les
SDL_Surface* burger2_surface = TTF_RenderText_Blended(font, "Burger 2", SDL_Color{ 255, 255, 255, 255 }); //textes
SDL_Surface* burger3_surface = TTF_RenderText_Blended(font, "Burger 3", SDL_Color{ 255, 255, 255, 255 }); //du
SDL_Surface* burger4_surface = TTF_RenderText_Blended(font, "Burger 4", SDL_Color{ 255, 255, 255, 255 }); //programme
SDL_Surface* boisson_surface = TTF_RenderText_Blended(font, "boisson", SDL_Color{255, 255, 255, 255});
SDL_Surface* supplement_surface = TTF_RenderText_Blended(font, "supplement", SDL_Color{255, 255, 255, 255});
SDL_Surface* soda_surface = TTF_RenderText_Blended(font, "Soda", SDL_Color{255, 255, 255, 255});
SDL_Surface* biere_surface = TTF_RenderText_Blended(font, "Biere", SDL_Color{255, 255, 255, 255});
SDL_Surface* reset_surface = TTF_RenderText_Blended(font, "clear", SDL_Color{255, 255, 255, 255});
SDL_Texture* burger_text = SDL_CreateTextureFromSurface(pRenderer, text);
SDL_Texture* burger1_text = SDL_CreateTextureFromSurface(pRenderer, burger1_surface); //On convertir les surfaces en textures
SDL_Texture* burger1_text_h = SDL_CreateTextureFromSurface(pHistorique, burger1_surface);
SDL_Texture* burger2_text = SDL_CreateTextureFromSurface(pRenderer, burger2_surface);
SDL_Texture* burger3_text = SDL_CreateTextureFromSurface(pRenderer, burger3_surface);
SDL_Texture* burger4_text = SDL_CreateTextureFromSurface(pRenderer, burger4_surface);
SDL_Texture* boisson_text = SDL_CreateTextureFromSurface(pRenderer, boisson_surface);
SDL_Texture* supplement_texture = SDL_CreateTextureFromSurface(pRenderer, supplement_surface);
SDL_Texture* reset_text = SDL_CreateTextureFromSurface(pRenderer, reset_surface);
SDL_Texture* soda_text = SDL_CreateTextureFromSurface(pRenderer, soda_surface);
SDL_Texture* biere_text = SDL_CreateTextureFromSurface(pRenderer, biere_surface);
SDL_FreeSurface(soda_surface); SDL_FreeSurface(biere_surface); SDL_FreeSurface(reset_surface); SDL_FreeSurface(burger1_surface); SDL_FreeSurface(burger2_surface);
SDL_FreeSurface(burger3_surface); SDL_FreeSurface(burger4_surface); SDL_FreeSurface(boisson_surface); SDL_FreeSurface(supplement_surface); //on libère de la mémoire
pHistorique = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED); //inutile (pour plus tard)
//On fait les rectangles des positions
SDL_Rect center;
SDL_Rect burger;
SDL_Rect burger1;
SDL_Rect burger2;
SDL_Rect burger3;
SDL_Rect burger4;
SDL_Rect boisson;
SDL_Rect supplement;
SDL_Rect reset;
SDL_Rect soda;
SDL_Rect biere;
//On récupère les dimensiosn des textures
SDL_QueryTexture(burger_text, nullptr, nullptr, ¢er.w, ¢er.h); // Récupere la dimension de la texture
SDL_QueryTexture(burger_text, nullptr, nullptr, &burger.w, &burger.h); //Titre Burger Truck
SDL_QueryTexture(burger1_text, nullptr, nullptr, &burger1.w, &burger1.h);
SDL_QueryTexture(burger2_text, nullptr, nullptr, &burger2.w, &burger2.h);
SDL_QueryTexture(burger3_text, nullptr, nullptr, &burger3.w, &burger3.h);
SDL_QueryTexture(burger4_text, nullptr, nullptr, &burger4.w, &burger4.h);
SDL_QueryTexture(boisson_text, NULL, NULL, &boisson.w, &boisson.h);
SDL_QueryTexture(supplement_texture, NULL, NULL, &supplement.w, &supplement.h);
SDL_QueryTexture(reset_text, NULL, NULL, &reset.w, &reset.h);
SDL_QueryTexture(soda_text, NULL, NULL, &soda.w, &soda.h);
SDL_QueryTexture(biere_text, NULL, NULL, &biere.w, &biere.h);
//Emplacement du burger 1
burger1.x = WIDTHSCREEN<int> / 4 - burger1.w;
burger1.y = HEIGHTSCREEN<int> / 3 - burger1.h;
//Emplacement du burger 2
burger2.x = WIDTHSCREEN<int> / 4 - burger2.w;
burger2.y = HEIGHTSCREEN<int> / 2 + burger2.h;
//Emplacement du burger 3
burger3.x = WIDTHSCREEN<int> / 2 + burger3.w * 3;
burger3.y = HEIGHTSCREEN<int> / 3 - burger1.h;
//Emplacement du burger 4
burger4.x = WIDTHSCREEN<int> / 2 + burger4.w * 3;
burger4.y = HEIGHTSCREEN<int> / 2 + burger4.h;
//Position du texte Burger Truck
burger.x = WIDTHSCREEN<int> / 2 - burger.w / 2;
burger.y = 15;
// Centre la texture sur l'écran
center.x = WIDTHSCREEN<int> / 2 - center.w / 2;
center.y = HEIGHTSCREEN<int> / 2 - center.h / 2;
//Position du boisson
boisson.x = WIDTHSCREEN<int> / 2 + boisson.w * 3.5;
boisson.y = HEIGHTSCREEN<int> / 2 + boisson.h * 3;
//Position de supplement
supplement.x = WIDTHSCREEN<int> / 4 - supplement.w;
supplement.y = HEIGHTSCREEN<int> / 2 + boisson.h * 3;
//Position du reset
reset.x = WIDTHSCREEN<int> / 2 - reset.w / 2;
reset.y = HEIGHTSCREEN<int> / 2 - reset.h / 2;
//Emplacement du soda
soda.x = WIDTHSCREEN<int> / 2 - soda.w;
soda.y = HEIGHTSCREEN<int> / 2 - soda.h / 2;
//Emplacement du biere
biere.x = WIDTHSCREEN<int> / 2 + biere.w;
biere.y = HEIGHTSCREEN<int> / 2 - biere.h / 2;
int hauteur_historique = burger1.h; //inutile (pour plus tard)
double total = 0; //Total
while (isOpen) //Boucle de open
{
SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 255); //Couleur noir
SDL_RenderClear(pRenderer); //Pot de peinture
SDL_Surface* total_suface = TTF_RenderText_Blended(font, ("Total: " + std::to_string(total) + "e").c_str(), SDL_Color{255, 255, 255 ,255}); //Affichage du total
SDL_Texture* total_text = SDL_CreateTextureFromSurface(pRenderer, total_suface); //Créasion de la texture
SDL_Rect total_rect; //Dimension de total
SDL_QueryTexture(total_text, NULL, NULL, &total_rect.w, &total_rect.h); //récupération de la dimension de la texture
total_rect.x = WIDTHSCREEN<int> / 2 - total_rect.w / 2; //x
total_rect.y = HEIGHTSCREEN<int> - 75; //y
SDL_Rect historique; //inutile (pour plus tard)
historique.x = 0;
historique.y = 0 + hauteur_historique;
historique.w = 100;
historique.h = 50;
while (SDL_PollEvent(&events))
{
switch (events.type)
{
case SDL_QUIT:
isOpen = false; //Quitter avec la croix rouge
SDL_Quit();
break;
case SDL_MOUSEBUTTONUP:
if (events.button.y > HEIGHTSCREEN<int> / 3 - burger1.h && //Bouton burger 1
events.button.y <= HEIGHTSCREEN<int> / 3 &&
events.button.x > WIDTHSCREEN<int> / 4 - burger1.w &&
events.button.x <= WIDTHSCREEN<int> / 4)
{
total += 9;
break;
}
if (events.button.y > HEIGHTSCREEN<int> / 2 + burger2.h && //Bouton burger 2
events.button.y <= HEIGHTSCREEN<int> / 2 + burger2.h*2 &&
events.button.x > WIDTHSCREEN<int> / 4 - burger1.w &&
events.button.x <= WIDTHSCREEN<int> / 4)
{
total += 10;
break;
}
if (events.button.y > HEIGHTSCREEN<int> / 3 - burger3.h && //bouton burger 3
events.button.y <= HEIGHTSCREEN<int> / 3 &&
events.button.x > WIDTHSCREEN<int> / 2 + burger3.w * 3 &&
events.button.x <= WIDTHSCREEN<int> / 2 + burger3.w * 4)
{
total += 11;
break;
}
if (events.button.y > HEIGHTSCREEN<int> / 2 + burger4.h && //bouton burger 4
events.button.y <= HEIGHTSCREEN<int> / 2 + burger4.h * 2 &&
events.button.x > WIDTHSCREEN<int> / 2 + burger4.w * 3 &&
events.button.x <= WIDTHSCREEN<int> / 2 + burger4.w * 4)
{
total += 12;
break;
}
if (events.button.y > HEIGHTSCREEN<int> / 2 + boisson.h * 3 && //bouton boisson
events.button.y <= HEIGHTSCREEN<int> / 2 + boisson.h * 4 &&
events.button.x > WIDTHSCREEN<int> / 2 + burger4.w * 3 &&
events.button.x <= WIDTHSCREEN<int> / 2 + burger4.w * 4)
{
SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 255); //on chosit noir
SDL_RenderClear(pRenderer); //On n'ettoie la fenetre tout en noir
SDL_SetRenderDrawColor(pRenderer, 255, 255, 255, 255); //On prend du blanc
SDL_RenderCopy(pRenderer, soda_text, NULL, &soda); //On affiche soda
SDL_RenderCopy(pRenderer, biere_text, NULL, &biere); //On affiche bière
SDL_RenderDrawRect(pRenderer, &soda); //rectangle de soda
SDL_RenderDrawRect(pRenderer, &biere); //rectangle de bière
bool boisson_open = true; //variable du while
while (boisson_open == true)
{
if (events.button.y > HEIGHTSCREEN<int> / 2 - soda.h / 2 &&
events.button.y <= HEIGHTSCREEN<int> / 2 + soda.h / 2 &&
events.button.x > WIDTHSCREEN<int> / 2 - soda.w / 2 &&
events.button.x <= WIDTHSCREEN<int> / 2 + soda.w / 2)
{
total += 1;
boisson_open == false;
break;
}
if (events.button.y > HEIGHTSCREEN<int> / 2 - biere.h / 2 &&
events.button.y <= HEIGHTSCREEN<int> / 2 + biere.h / 2 &&
events.button.x > WIDTHSCREEN<int> / 2 + biere.w &&
events.button.x <= HEIGHTSCREEN<int> / 2 + biere.w * 2)
{
total += 2;
boisson_open == false;
break;
}
}
break;
}
if (events.button.y > HEIGHTSCREEN<int> / 2 + boisson.h * 3 && //Bouton supplement
events.button.y <= HEIGHTSCREEN<int> / 2 + boisson.h * 4 &&
events.button.x > WIDTHSCREEN<int> / 4 - burger1.w &&
events.button.x <= WIDTHSCREEN<int> / 4)
{
total += 1.5;
break;
}
if(events.button.y > HEIGHTSCREEN<int> / 2 - reset.h / 2 && //Bouton reset
events.button.y <= HEIGHTSCREEN<int> / 2 + reset.h / 2 &&
events.button.x > WIDTHSCREEN<int> / 2 - reset.w / 2 &&
events.button.x <= WIDTHSCREEN<int> / 2 + reset.w / 2)
total = 0;
break;
}
}
SDL_SetRenderDrawColor(pRenderer, 255, 255, 255, 255); //Blanc
//Affichange du texte
SDL_RenderCopy(pRenderer, burger_text, NULL, &burger);
SDL_RenderCopy(pRenderer, burger1_text, NULL, &burger1);
SDL_RenderCopy(pRenderer, burger2_text, NULL, &burger2);
SDL_RenderCopy(pRenderer, burger3_text, NULL, &burger3);
SDL_RenderCopy(pRenderer, burger4_text, NULL, &burger4);
SDL_RenderCopy(pRenderer, total_text, NULL, &total_rect);
SDL_RenderCopy(pRenderer, boisson_text, NULL, &boisson);
SDL_RenderCopy(pRenderer, supplement_texture, NULL, &supplement);
SDL_RenderCopy(pRenderer, reset_text, NULL, &reset);
//Affichage des rectangles
SDL_RenderDrawRect(pRenderer, &burger1);
SDL_RenderDrawRect(pRenderer, &burger2);
SDL_RenderDrawRect(pRenderer, &burger3);
SDL_RenderDrawRect(pRenderer, &burger4);
SDL_RenderDrawRect(pRenderer, &boisson);
SDL_RenderDrawRect(pRenderer, &supplement);
SDL_RenderDrawRect(pRenderer, &reset);
//Rendus
SDL_RenderPresent(pRenderer);
//SDL_RenderPresent(pHistorique);
SDL_FreeSurface(total_suface);
SDL_DestroyTexture(total_text);
}
TTF_Quit();
SDL_FreeSurface(text);
TTF_CloseFont(font);
SDL_DestroyWindow(pWindow);
SDL_DestroyRenderer(pRenderer);
SDL_Quit();
return EXIT_SUCCESS;
}
}
+0
-0