debug-text.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  8. #include <SDL3/SDL.h>
  9. #include <SDL3/SDL_main.h>
  10. /* We will use this renderer to draw into this window every frame. */
  11. static SDL_Window *window = NULL;
  12. static SDL_Renderer *renderer = NULL;
  13. #define WINDOW_WIDTH 640
  14. #define WINDOW_HEIGHT 480
  15. /* This function runs once at startup. */
  16. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  17. {
  18. SDL_SetAppMetadata("Example Renderer Debug Texture", "1.0", "com.example.renderer-debug-text");
  19. if (!SDL_Init(SDL_INIT_VIDEO)) {
  20. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  21. return SDL_APP_FAILURE;
  22. }
  23. if (!SDL_CreateWindowAndRenderer("examples/renderer/debug-text", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
  24. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  25. return SDL_APP_FAILURE;
  26. }
  27. return SDL_APP_CONTINUE; /* carry on with the program! */
  28. }
  29. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  30. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  31. {
  32. if (event->type == SDL_EVENT_QUIT) {
  33. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  34. }
  35. return SDL_APP_CONTINUE; /* carry on with the program! */
  36. }
  37. /* This function runs once per frame, and is the heart of the program. */
  38. SDL_AppResult SDL_AppIterate(void *appstate)
  39. {
  40. const int charsize = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  41. /* as you can see from this, rendering draws over whatever was drawn before it. */
  42. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); /* black, full alpha */
  43. SDL_RenderClear(renderer); /* start with a blank canvas. */
  44. SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */
  45. SDL_RenderDebugText(renderer, 272, 100, "Hello world!");
  46. SDL_RenderDebugText(renderer, 224, 150, "This is some debug text.");
  47. SDL_SetRenderDrawColor(renderer, 51, 102, 255, SDL_ALPHA_OPAQUE); /* light blue, full alpha */
  48. SDL_RenderDebugText(renderer, 184, 200, "You can do it in different colors.");
  49. SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */
  50. SDL_SetRenderScale(renderer, 4.0f, 4.0f);
  51. SDL_RenderDebugText(renderer, 14, 65, "It can be scaled.");
  52. SDL_SetRenderScale(renderer, 1.0f, 1.0f);
  53. SDL_RenderDebugText(renderer, 64, 350, "This only does ASCII chars. So this laughing emoji won't draw: 🤣");
  54. SDL_RenderDebugTextFormat(renderer, (float) ((WINDOW_WIDTH - (charsize * 46)) / 2), 400, "(This program has been running for %" SDL_PRIu64 " seconds.)", SDL_GetTicks() / 1000);
  55. SDL_RenderPresent(renderer); /* put it all on the screen! */
  56. return SDL_APP_CONTINUE; /* carry on with the program! */
  57. }
  58. /* This function runs once at shutdown. */
  59. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  60. {
  61. /* SDL will clean up the window/renderer for us. */
  62. }