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
| #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
void renderText(SDL_Renderer* renderer, const char* text, int const size, const SDL_Rect text_rect, const SDL_Color text_color)
{
#ifdef __linux__
const char* font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf";
#elif _WIN32
const char* font_path = "C:\\Windows\\Fonts\\Arial.ttf";
#endif
TTF_Font* font = TTF_OpenFont(font_path, size);
if (!font) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_ttf - %s", TTF_GetError()); return; }
SDL_Surface* text_surface = TTF_RenderText_Blended(font, text, text_color);
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
if (text_color.a!=255) SDL_SetTextureAlphaMod(text_texture, text_color.a);
SDL_RenderCopy(renderer, text_texture, NULL, &text_rect);
SDL_DestroyTexture(text_texture);
SDL_FreeSurface(text_surface);
TTF_CloseFont(font);
}
int main(int argc, char** argv)
{
//(void)argc; (void)argv;
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
SDL_Window* win = SDL_CreateWindow("Exemple pour fuite de mémoire avec SDL2_ttf", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
SDL_Renderer* ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
SDL_Event event;
SDL_bool running = SDL_TRUE;
while (running)
{
while (SDL_PollEvent(&event))
{
running = event.type != SDL_QUIT;
}
renderText(ren, "Coucou les devs !", 22, (SDL_Rect){ (640-256)/2, (480-64)/2, 256, 64 }, (SDL_Color){ 255, 255, 255, 255 });
SDL_RenderPresent(ren);
}
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
TTF_Quit();
SDL_Quit();
return 0;
} |
Partager