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
| // bibliotheques
#include <iostream>
#include <SDL.h>
#include <SDL_thread.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <SDL_ttf.h>
#include <SDL2_rotozoom.h>
#define IMG_PATH "menu_deroulant.svg"
// dimension ecran
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
// fonction principale
int main( int argc, char* args[] )
{
// declaration de variables
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
TTF_Font *arial = NULL;
SDL_Texture *Message_1, *Message_2;
SDL_Color Text_Bleu = {6, 49, 196};
std::string text_bouton_1 = " texte du bouton 1 ", text_bouton_2 = " texte du bouton 2 ";
SDL_Rect* clip = NULL;
double angle = 0.0;
SDL_Point* center = NULL;
SDL_RendererFlip flip = SDL_FLIP_NONE;
// dimensions de l image (SDL_Surface)
int mWidth_1, mWidth_2;
int mHeight_1, mHeight_2;
// coordonnées
int x_1, x_2;
int y_1, y_2;
// width et height de SDL_Texture
int w;
int h;
// initialisation SDL
SDL_Init( SDL_INIT_VIDEO );
// qualite de la mise a l echelle
SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );
// creation d une fenetre
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
// creation d'un SDL_Renderer (pinceau) utilisant l'acceleration materielle
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
// selection d'une couleur de nettoyage
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
// initialisation de la police
TTF_Init();
arial = TTF_OpenFont("arial.ttf", 12);
// nettoyage de l ecran <=> couleur de fond de la fenetre
SDL_SetRenderDrawColor( gRenderer,127, 255, 0, 1 );
SDL_RenderClear( gRenderer );
// construction du bouton 1 avec texte
// declaration initialisation d une structure utilisee pour contenir un ensemble de pixels
// ecriture du texte du bouton
SDL_Surface* surface_Message_1 = TTF_RenderText_Solid(arial, text_bouton_1.c_str(), Text_Bleu);
// conversion en texture d 1 SDL_Surface: dessin du texte
Message_1 = SDL_CreateTextureFromSurface(gRenderer, surface_Message_1);
// dimensions de l image
mWidth_1 = surface_Message_1->w;
mHeight_1 = surface_Message_1->h;
// coordonnees
x_1 = SCREEN_WIDTH / 8;
y_1 = SCREEN_HEIGHT / 8;
// definition du rectangle du bouton
SDL_Rect renderQuad_1 = { x_1, y_1, mWidth_1, mHeight_1 };
// definition couleur de remplissage du rectangle
SDL_SetRenderDrawColor( gRenderer, 255, 20, 147, 1 );
SDL_RenderFillRect( gRenderer, &renderQuad_1 );
// dessin de la fleche
// import de l image de la fleche
SDL_Texture *imgage_fleche = IMG_LoadTexture(gRenderer, IMG_PATH);
// width et height de la texture de la fleche
SDL_QueryTexture(imgage_fleche, NULL, NULL, &w, &h);
// localisation de la fleche
double mHeight_1_bis=mHeight_1;
double h_bis=h;
double coefficient = mHeight_1_bis/h_bis;
std::cout<<"coefficient="<<coefficient;
SDL_Rect renderQuad_fleche;
renderQuad_fleche.x = x_1+mWidth_1;
renderQuad_fleche.y = y_1;
renderQuad_fleche.w = w*coefficient;
renderQuad_fleche.h = mHeight_1;
// construction du bouton 2 avec texte
// declaration initialisation d une structure utilisee pour contenir un ensemble de pixels
// ecriture du texte du bouton
SDL_Surface* surface_Message_2 = TTF_RenderText_Solid(arial, text_bouton_2.c_str(), Text_Bleu);
// conversion en texture d 1 SDL_Surface: dessin du texte
Message_2 = SDL_CreateTextureFromSurface(gRenderer, surface_Message_2);
// dimensions de l image
mWidth_2 = surface_Message_2->w;
mHeight_2 = surface_Message_2->h;
// coordonnees
x_2 = x_1;
y_2 = y_1+mHeight_1;
// definition du rectangle du bouton
SDL_Rect renderQuad_2 = { x_2, y_2, mWidth_2, mHeight_2 };
// definition couleur de remplissage du rectangle
SDL_SetRenderDrawColor( gRenderer, 255, 20, 147, 1 );
SDL_RenderFillRect( gRenderer, &renderQuad_2 );
// structure stockant les information sur les evenements
SDL_Event e;
bool quit = false;
// gestion de l evenement quit
while( !quit )
{
// gerer les événements en file d attente
while( SDL_PollEvent( &e ) != 0 ) // <=> la queue des evenement n est pas vide
{
// si l utilisateur choisit quit
if( e.type == SDL_QUIT )
{
quit = true;
}
}
// Render to screen: dessin du bouton 1
SDL_RenderCopyEx( gRenderer, Message_1, clip, &renderQuad_1, angle, center, flip );
// Render to screen: dessin de la fleche
SDL_RenderCopy(gRenderer, imgage_fleche, NULL, &renderQuad_fleche);
//SDL_RenderCopy(gRenderer, img_fond_vaisseau, NULL, &rect_vaisseau);
// mise a jour de l affichage de la fenetre
SDL_RenderPresent( gRenderer );
}
// liberation font
TTF_CloseFont( arial );
arial = NULL;
// liberation fenetre
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
// quitter les SDL subsystems
TTF_Quit();
SDL_Quit();
return 0;
} |
Partager