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
| // bibliotheques
#include <SDL.h>
#include <SDL_thread.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <SDL_ttf.h>
// dimension ecran
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
// declaration de variables
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
TTF_Font *arial = NULL;
// fonction principale
int main( int argc, char* args[] )
{
// 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 );
// Initialise le chargement du format PNG
int imgFlags = IMG_INIT_PNG;
IMG_Init( imgFlags );
// initialisation de la police
TTF_Init();
TTF_Font* arial = TTF_OpenFont("arial.ttf", 120);
// Render text
SDL_Texture* mTexture;
// declaration initialisation de la couleur
SDL_Color Text_Bleu = {6, 49, 196};
// declaration initialisation d une chaine de caractere
std::string text_bouton = "a";
// declaration initialisation d une structure utilisee pour contenir un ensemble de pixels
// ecriture du texte:
// - avec TTF_RenderText_Solid:
SDL_Surface* surfaceMessage1 = TTF_RenderText_Solid(arial, text_bouton.c_str(), Text_Bleu);
// on convertit cette surface en SDL_Texture
mTexture = SDL_CreateTextureFromSurface( gRenderer, surfaceMessage1 );
// dimensions de l image
int mWidth = surfaceMessage1->w;
int mHeight = surfaceMessage1->h;
// nettoyage de l ecran <=> couleur de fond de la fenetre
SDL_SetRenderDrawColor( gRenderer,127, 255, 0, 1 );
SDL_RenderClear( gRenderer );
// dessin (Render) du cadre courant
// definition d 1 rectangle
SDL_Rect fillRect1 = { SCREEN_WIDTH / 8, SCREEN_HEIGHT / 8, SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4 };
// definition couleur de remplissage du rectangle
SDL_SetRenderDrawColor( gRenderer, 255, 20, 147, 1 );
SDL_RenderFillRect( gRenderer, &fillRect1 );
// mise a jour de l affichage de la fenetre
SDL_RenderPresent( gRenderer );
// conversion en texture d 1 SDL_Surface
// dessin du texte
SDL_Texture* Message1 = SDL_CreateTextureFromSurface(gRenderer, surfaceMessage1);
// liberation des ressources liees au sprite
SDL_FreeSurface( surfaceMessage1 );
// definition de rectangle
SDL_Rect Message_rect1;
Message_rect1.x = fillRect1.x; // controls the rect's x coordinate
Message_rect1.y = fillRect1.y; // controls the rect's y coordinte
Message_rect1.w = fillRect1.w; // controls the width of the rect
Message_rect1.h = fillRect1.h; // controls the height of the rect
// copie du sprite (sprite = une image (un tableau contenant des couleurs))
// copie du texte dans le rectangle
//SDL_RenderCopy(gRenderer, Message1, NULL, &Message_rect1);
int x = fillRect1.x;
int y = fillRect1.y;
SDL_Rect* clip = NULL;
double angle = 0.0;
SDL_Point* center = NULL;
SDL_RendererFlip flip = SDL_FLIP_NONE;
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
renderQuad.w = clip->w;
renderQuad.h = clip->h;
SDL_RenderCopyEx( gRenderer, mTexture, clip, &renderQuad, angle, center, flip );
// mise a jour de l affichage de la fenetre
SDL_RenderPresent( gRenderer );
// 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;
}
}
}
// 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