hello.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include <SDL3/SDL.h>
  11. #include <SDL3/SDL_main.h>
  12. int main(int argc, char *argv[])
  13. {
  14. SDL_Window *window = NULL;
  15. SDL_Renderer *renderer = NULL;
  16. const char *message = "Hello World!";
  17. int w = 0, h = 0;
  18. float x, y;
  19. bool done = false;
  20. /* Create the window */
  21. if (!SDL_CreateWindowAndRenderer("Hello World", 0, 0, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
  22. SDL_Log("Couldn't create window and renderer: %s\n", SDL_GetError());
  23. return 1;
  24. }
  25. while (!done) {
  26. SDL_Event event;
  27. /* Handle events */
  28. while (SDL_PollEvent(&event)) {
  29. if (event.type == SDL_EVENT_KEY_DOWN ||
  30. event.type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
  31. event.type == SDL_EVENT_QUIT) {
  32. done = true;
  33. }
  34. }
  35. /* Center the message */
  36. SDL_GetWindowSize(window, &w, &h);
  37. x = (float)(w - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(message)) / 2;
  38. y = (float)(h - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
  39. /* Draw the message */
  40. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  41. SDL_RenderClear(renderer);
  42. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  43. SDL_RenderDebugText(renderer, x, y, message);
  44. SDL_RenderPresent(renderer);
  45. }
  46. SDL_Quit();
  47. return 0;
  48. }