Atlas - debug-text.c

Home / ext / SDL / examples / renderer / 18-debug-text Lines: 1 | Size: 3331 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 * This example creates an SDL window and renderer, and then draws some text 3 * using SDL_RenderDebugText() every frame. 4 * 5 * This code is public domain. Feel free to use it for any purpose! 6 */ 7 8#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 9#include <SDL3/SDL.h> 10#include <SDL3/SDL_main.h> 11 12/* We will use this renderer to draw into this window every frame. */ 13static SDL_Window *window = NULL; 14static SDL_Renderer *renderer = NULL; 15 16#define WINDOW_WIDTH 640 17#define WINDOW_HEIGHT 480 18 19/* This function runs once at startup. */ 20SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 21{ 22 SDL_SetAppMetadata("Example Renderer Debug Texture", "1.0", "com.example.renderer-debug-text"); 23 24 if (!SDL_Init(SDL_INIT_VIDEO)) { 25 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 26 return SDL_APP_FAILURE; 27 } 28 29 if (!SDL_CreateWindowAndRenderer("examples/renderer/debug-text", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 30 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 31 return SDL_APP_FAILURE; 32 } 33 SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); 34 35 return SDL_APP_CONTINUE; /* carry on with the program! */ 36} 37 38/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 39SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 40{ 41 if (event->type == SDL_EVENT_QUIT) { 42 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 43 } 44 return SDL_APP_CONTINUE; /* carry on with the program! */ 45} 46 47/* This function runs once per frame, and is the heart of the program. */ 48SDL_AppResult SDL_AppIterate(void *appstate) 49{ 50 const int charsize = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE; 51 52 /* as you can see from this, rendering draws over whatever was drawn before it. */ 53 SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); /* black, full alpha */ 54 SDL_RenderClear(renderer); /* start with a blank canvas. */ 55 56 SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */ 57 SDL_RenderDebugText(renderer, 272, 100, "Hello world!"); 58 SDL_RenderDebugText(renderer, 224, 150, "This is some debug text."); 59 60 SDL_SetRenderDrawColor(renderer, 51, 102, 255, SDL_ALPHA_OPAQUE); /* light blue, full alpha */ 61 SDL_RenderDebugText(renderer, 184, 200, "You can do it in different colors."); 62 SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */ 63 64 SDL_SetRenderScale(renderer, 4.0f, 4.0f); 65 SDL_RenderDebugText(renderer, 14, 65, "It can be scaled."); 66 SDL_SetRenderScale(renderer, 1.0f, 1.0f); 67 SDL_RenderDebugText(renderer, 64, 350, "This only does ASCII chars. So this laughing emoji won't draw: 🤣"); 68 69 SDL_RenderDebugTextFormat(renderer, (float) ((WINDOW_WIDTH - (charsize * 46)) / 2), 400, "(This program has been running for %" SDL_PRIu64 " seconds.)", SDL_GetTicks() / 1000); 70 71 SDL_RenderPresent(renderer); /* put it all on the screen! */ 72 73 return SDL_APP_CONTINUE; /* carry on with the program! */ 74} 75 76/* This function runs once at shutdown. */ 77void SDL_AppQuit(void *appstate, SDL_AppResult result) 78{ 79 /* SDL will clean up the window/renderer for us. */ 80} 81 82
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.