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
|
#include<SDL.h>
#include<SDL_image.h>
#include<string.h>
#include<SDL_ttf.h>
SDL_Window *fenetre;
SDL_Renderer *render;
TTF_Font *police;
SDL_Texture *texture_texte ;
void new_texture(SDL_Texture *texture,SDL_Surface *surface) //texture2 SDL_TEXTUREACCESS_STREAMING
{ SDL_Rect taille={0,0,surface->w,surface->h};
SDL_Surface *surface2;
void *mpixels ; int mpitch;
if(surface->format!=SDL_GetWindowSurface(fenetre)->format)
surface2=SDL_ConvertSurface(surface,SDL_GetWindowSurface(fenetre)->format,NULL);
else surface2=surface;
//SDL_LockTexture(texture,&taille,&mpixels,&mpitch);
// memcpy(mpixels,surface2->pixels,surface2->pitch*surface2->h);
// SDL_UnlockTexture(texture);
SDL_UpdateTexture(texture,&taille,surface2->pixels,surface2->pitch);
SDL_FreeSurface(surface2);
}
void print(char *texte,int c, int l, Uint8 r,Uint8 v, Uint8 b)
{ SDL_Color color={r,v,b};
SDL_Surface *surface_texte=TTF_RenderText_Solid(police,texte,color);
new_texture(texture_texte,surface_texte);
SDL_Rect rec_texte={0,0,surface_texte->w,surface_texte->h};
SDL_Rect position={c,l,surface_texte->w,surface_texte->h};
SDL_RenderCopy(render,texture_texte,&rec_texte,&position);
SDL_FreeSurface(surface_texte);
}
int main(int a,char **b)
{ SDL_Init(SDL_INIT_EVERYTHING);
fenetre=SDL_CreateWindow("SDL2",100,100,800,600,0);
render=SDL_CreateRenderer(fenetre,-1,SDL_RENDERER_ACCELERATED);
TTF_Init();
police=TTF_OpenFont("ARMY RUST.ttf",22);
texture_texte=SDL_CreateTexture(render,SDL_GetWindowPixelFormat(fenetre),SDL_TEXTUREACCESS_STREAMING,400,40);
print("essai de texte",400,0,255,255,0);
SDL_RenderPresent(render);
SDL_Delay(5000);
SDL_DestroyTexture(texture_texte);
SDL_DestroyWindow(fenetre);
SDL_DestroyRenderer(render);
SDL_Quit();
return 1;
} |
Partager