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
|
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
#include "SDL_ttf.h"
SDL_Surface* affichage;
TTF_Font* Font;
SDL_Color TextColor;
void initSDL(int width, int height);
void attendreTouche(void);
void actualiser(void);
void dessiner();
int main(int argc, char** argv)
{
initSDL(600,800);
dessiner();
actualiser();
attendreTouche();
return EXIT_SUCCESS;
}
void initSDL(int width, int height)
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Erreur à l'initialisation de la SDL : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
atexit(SDL_Quit);
affichage = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
if (affichage == NULL) {
fprintf(stderr, "Impossible d'activer le mode graphique : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_WM_SetCaption("Image", NULL);
/* Initialisation de SDL_ttf */
if(TTF_Init() < 0 )
{
printf("Impossible d'initialiser SDL_ttf : %s", TTF_GetError());
return;
}
/* Charge une police depuis un fichier, avec une taille de point à 50 */
Font = TTF_OpenFont("comic.ttf", 50);
/* Si la police est nulle, il y a eu une erreur */
if(!Font)
{
printf("Erreur de création de la police : %s", TTF_GetError());
return;
}
/* Couleur du texte (ici rouge) */
TextColor.r = 255;
TextColor.g = 0;
TextColor.b = 0;
}
void attendreTouche(void)
{
SDL_Event event;
do
SDL_WaitEvent(&event);
while (event.type != SDL_QUIT && event.type != SDL_KEYDOWN);
}
void actualiser(void)
{
SDL_UpdateRect(affichage, 0, 0, 0, 0);
}
void dessiner()
{
SDL_Rect DstRect;
SDL_Surface* TextSurface;
TextSurface = (SDL_Surface*) malloc (10000000);
TextSurface = TTF_RenderText_Solid(Font, "Salut tout le monde !", TextColor);; /*Unhandled exception at 0x00331d3b in autreTest.exe: 0xC0000005: Access violation reading location 0x00000000.*/
/* On rend un texte sur une surface SDL, en utilisant notre police et notre couleur */
/* Si la surface est nulle, il y a eu une erreur */
if(!TextSurface)
{
printf("Erreur de rendu du texte : %s", TTF_GetError());
return;
}
/* On peut maintenant blitter notre surface comme n'importe quelle autre */
DstRect.x = 0;
DstRect.y = 0;
DstRect.w = TextSurface->w;
DstRect.h = TextSurface->h;
/* Affiche toute la surface en 0,0 */
SDL_BlitSurface(TextSurface, NULL, affichage, &DstRect);
} |
Partager