1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
| // instruction préprocesseur: recopier le code qui se trouve dans le ficher
//========================================================================
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdio.h>
#include <time.h> // srand()
#include <sstream>
#include <regex>
#include <string> // classe std::string, fonction std::stof
#include <iostream> // fonction std::cout
#include <list>
// declaration, initialisation de variables
//=========================================
// dimension ecran
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 640;
// The window we'll be rendering to
SDL_Window* gWindow = NULL;
// The window renderer
SDL_Renderer* gRenderer = NULL;
// Globally used font
TTF_Font *font = NULL;
SDL_Color couleur_texte = {6, 49, 196};
// affichage cadre plus texte
SDL_Rect* clip = NULL;
double angle = 0.0;
SDL_Point* center = NULL;
SDL_RendererFlip flip = SDL_FLIP_NONE;
// definition d'une classe pour créer des objets rectangles à l'écran
//===================================================================
class cadre_A
{
// données privées: non accessible de l'extérieur de la classe
private:
// données, fonctions membres publiques
public:
std::string texte;
SDL_Surface* surface_texte;
SDL_Texture* texture_texte;
SDL_Rect texture_bouton;
// dimensions texture
int texture_width;
int texture_height;
// rectangle du cadre
//SDL_Rect rectangle_cadre = {int x_rectangle_cadre, int y_rectangle_cadre, int width_rectangle_cadre,int height_rectangle_cadre};
SDL_Rect rectangle_cadre;
int x_rectangle_cadre;
int y_rectangle_cadre;
int width_rectangle_cadre;
int height_rectangle_cadre;
// constructeur par défaut
// cadre_A(); syntaxe non valide
cadre_A() = default; // ou cadre_A() {};
// destructeur
~cadre_A();
// déclaration de fonctions membres
// render texte
void render_texte(std::string texte_bis);
// definition rectangle cadre
void definition_rectangle_cadre (int x_rectangle_cadre_bis, int y_rectangle_cadre_bis, int width_rectangle_cadre_bis, int height_rectangle_cadre_bis);
// affichage cadre
void affichage_cadre_plus_texte ();
// méthode qui gère le clic souris X et Y
void proceder_click_souris(int x_bis, int y_bis, cadre_A cadre_suivant_bis);
};
// defintion de fonctions membres
cadre_A::~cadre_A()
{
//Deallocate
SDL_DestroyTexture( texture_texte );
texture_texte = NULL;
texture_width = 0;
texture_height = 0;
}
void cadre_A::definition_rectangle_cadre (int x_rectangle_cadre_bis, int y_rectangle_cadre_bis, int width_rectangle_cadre_bis, int height_rectangle_cadre_bis)
{
x_rectangle_cadre = x_rectangle_cadre_bis;
y_rectangle_cadre = y_rectangle_cadre_bis;
width_rectangle_cadre = width_rectangle_cadre_bis;
height_rectangle_cadre = height_rectangle_cadre_bis;
SDL_Rect rectangle_cadre = { x_rectangle_cadre, y_rectangle_cadre, width_rectangle_cadre, height_rectangle_cadre };
SDL_SetRenderDrawColor( gRenderer, 255, 20, 147, 1 );
SDL_RenderFillRect( gRenderer, &rectangle_cadre );
}
void cadre_A::render_texte(std::string texte_bis)
{
texte = texte_bis;
surface_texte = TTF_RenderText_Blended_Wrapped(font, texte.c_str(), couleur_texte, this->width_rectangle_cadre);
texture_texte = SDL_CreateTextureFromSurface( gRenderer, surface_texte);
texture_width = surface_texte->w;
texture_height = surface_texte->h;
}
void cadre_A::affichage_cadre_plus_texte ()
{
//Set rendering space and render to screen
SDL_Rect texture_bouton = { x_rectangle_cadre+(width_rectangle_cadre-texture_width)/2, y_rectangle_cadre+(height_rectangle_cadre-texture_height)/2, texture_width, texture_height };
// definition couleur de remplissage du rectangle bouton
SDL_SetRenderDrawColor( gRenderer, 255, 20, 147, 1 );
SDL_RenderFillRect( gRenderer, &texture_bouton );
//Render to screen
SDL_RenderCopyEx( gRenderer, texture_texte, clip, &texture_bouton, angle, center, flip );
}
void cadre_A::proceder_click_souris(int x_bis, int y_bis, cadre_A cadre_suivant_bis)
{
if((
x_bis > cadre_suivant_bis.x_rectangle_cadre &
x_bis < cadre_suivant_bis.x_rectangle_cadre + cadre_suivant_bis.width_rectangle_cadre &
y_bis > cadre_suivant_bis.y_rectangle_cadre &
y_bis < cadre_suivant_bis.y_rectangle_cadre + cadre_suivant_bis.height_rectangle_cadre
))
{
// Si x et y sont des coordonnées inscrites dans le rectangle d'une instance de cadre_A alors agir en conséquence
std::cout << "suivant = true" << std::endl;
// instructions ...
}
}
// fonction principale
//====================
int main( int argc, char* args[] )
{
// declaration de variables
//=========================
int height_cadre_bouton = SCREEN_HEIGHT/14;
int marge_verticale1 = 30;
int width1 = 200;
// declaration des 3 variables
int nombre1;
// constantes
const int MAX=10;
const int MIN=1;
// fonction qui donne un chiffre au hasard
srand(time(NULL));
//Initialize SDL
SDL_Init( SDL_INIT_VIDEO );
//Set texture filtering to linear
SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
//Create vsynced renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer,127, 255, 0, 1 );
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
IMG_Init( imgFlags );
//Initialize SDL_ttf
TTF_Init();
//Open the font
font = TTF_OpenFont( "arial.ttf", 25 );
//Render the prompt
bool bouton_suivant_clique = false;
nombre1=(rand()%(MAX-MIN+1))+MIN;
std::string nombre1_string = std::to_string(nombre1);
//Clear screen
SDL_SetRenderDrawColor( gRenderer,127, 255, 0, 1 );
SDL_RenderClear( gRenderer );
cadre_A cadre_calcul;
cadre_calcul.definition_rectangle_cadre (0, 0, width1, height_cadre_bouton);
cadre_calcul.render_texte(nombre1_string);
cadre_calcul.affichage_cadre_plus_texte();
cadre_A cadre_suivant;
cadre_suivant.definition_rectangle_cadre (0, cadre_calcul.y_rectangle_cadre+marge_verticale1, width1, height_cadre_bouton);
cadre_suivant.render_texte("Suivant");
cadre_suivant.affichage_cadre_plus_texte();
// tableaux dynamiques de bouton
std::list<cadre_A > list_cadre_A;
list_cadre_A.push_back(cadre_calcul);
list_cadre_A.push_back(cadre_suivant);
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
while( !quit )
{
//The rerender text flag
bool renderText = false;
bool bouton_egal_clique = false;
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
else if( /*e.type == SDL_MOUSEMOTION ||*/ e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP )
{
//Get mouse position
int x, y;
SDL_GetMouseState( &x, &y );
//Check if mouse is in button
bool inside = true;
// appel de la methode de vérification du clic du bouton suivant
cadre_A::proceder_click_souris(x, y, cadre_suivant);
}
}
//Clear screen
SDL_SetRenderDrawColor( gRenderer,127, 255, 0, 1 );
SDL_RenderClear( gRenderer );
cadre_calcul.definition_rectangle_cadre (0, 0, width1, height_cadre_bouton);
cadre_calcul.affichage_cadre_plus_texte();
cadre_suivant.definition_rectangle_cadre (0, cadre_calcul.y_rectangle_cadre+height_cadre_bouton+marge_verticale1, width1, height_cadre_bouton);
cadre_suivant.affichage_cadre_plus_texte();
//Update screen
SDL_RenderPresent( gRenderer );
}
//Free resources and close SDL
//Free global font
TTF_CloseFont( font );
font = NULL;
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
TTF_Quit();
IMG_Quit();
SDL_Quit();
return 0;
} |
Partager