1
0

affine-textures.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* affine-textures.c ... */
  2. /*
  3. * This example creates an SDL window and renderer, and then draws a cube
  4. * using affine-transformed textures every frame.
  5. *
  6. * This code is public domain. Feel free to use it for any purpose!
  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. /* We will use this renderer to draw into this window every frame. */
  12. static SDL_Window *window = NULL;
  13. static SDL_Renderer *renderer = NULL;
  14. static SDL_Texture *texture = NULL;
  15. static int texture_width = 0;
  16. static int texture_height = 0;
  17. #define WINDOW_WIDTH 640
  18. #define WINDOW_HEIGHT 480
  19. /* This function runs once at startup. */
  20. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  21. {
  22. SDL_Surface *surface = NULL;
  23. char *png_path = NULL;
  24. SDL_SetAppMetadata("Example Renderer Affine Textures", "1.0", "com.example.renderer-affine-textures");
  25. if (!SDL_Init(SDL_INIT_VIDEO)) {
  26. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  27. return SDL_APP_FAILURE;
  28. }
  29. if (!SDL_CreateWindowAndRenderer("examples/renderer/affine-textures", 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. /* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D
  35. engines refer to these as "sprites." We'll do a static texture (upload once, draw many
  36. times) with data from a bitmap file. */
  37. /* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
  38. Load a .png into a surface, move it to a texture from there. */
  39. SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
  40. surface = SDL_LoadPNG(png_path);
  41. if (!surface) {
  42. SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
  43. return SDL_APP_FAILURE;
  44. }
  45. SDL_free(png_path); /* done with this, the file is loaded. */
  46. texture_width = surface->w;
  47. texture_height = surface->h;
  48. texture = SDL_CreateTextureFromSurface(renderer, surface);
  49. if (!texture) {
  50. SDL_Log("Couldn't create static texture: %s", SDL_GetError());
  51. return SDL_APP_FAILURE;
  52. }
  53. SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
  54. return SDL_APP_CONTINUE; /* carry on with the program! */
  55. }
  56. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  57. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  58. {
  59. if (event->type == SDL_EVENT_QUIT) {
  60. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  61. }
  62. return SDL_APP_CONTINUE; /* carry on with the program! */
  63. }
  64. /* This function runs once per frame, and is the heart of the program. */
  65. SDL_AppResult SDL_AppIterate(void *appstate)
  66. {
  67. const float x0 = 0.5f * WINDOW_WIDTH;
  68. const float y0 = 0.5f * WINDOW_HEIGHT;
  69. const float px = SDL_min(WINDOW_WIDTH, WINDOW_HEIGHT) / SDL_sqrtf(3.0f);
  70. const Uint64 now = SDL_GetTicks();
  71. const float rad = (((float) ((int) (now % 2000))) / 2000.0f) * SDL_PI_F * 2;
  72. const float cos = SDL_cosf(rad);
  73. const float sin = SDL_sinf(rad);
  74. const float k[3] = { 3.0f / SDL_sqrtf(50.0f), 4.0f / SDL_sqrtf(50.0f), 5.0f / SDL_sqrtf(50.0f)};
  75. float mat[9] = {
  76. cos + (1.0f-cos)*k[0]*k[0], -sin*k[2] + (1.0f-cos)*k[0]*k[1], sin*k[1] + (1.0f-cos)*k[0]*k[2],
  77. sin*k[2] + (1.0f-cos)*k[0]*k[1], cos + (1.0f-cos)*k[1]*k[1], -sin*k[0] + (1.0f-cos)*k[1]*k[2],
  78. -sin*k[1] + (1.0f-cos)*k[0]*k[2], sin*k[0] + (1.0f-cos)*k[1]*k[2], cos + (1.0f-cos)*k[2]*k[2],
  79. };
  80. float corners[16];
  81. int i;
  82. for (i = 0; i < 8; i++) {
  83. const float x = (i & 1) ? -0.5f : 0.5f;
  84. const float y = (i & 2) ? -0.5f : 0.5f;
  85. const float z = (i & 4) ? -0.5f : 0.5f;
  86. corners[0 + 2*i] = mat[0]*x + mat[1]*y + mat[2]*z;
  87. corners[1 + 2*i] = mat[3]*x + mat[4]*y + mat[5]*z;
  88. }
  89. SDL_SetRenderDrawColor(renderer, 0x42, 0x87, 0xf5, SDL_ALPHA_OPAQUE); // light blue background.
  90. SDL_RenderClear(renderer);
  91. for (i = 1; i < 7; i++) {
  92. const int dir = 3 & ((i & 4) ? ~i : i);
  93. const int odd = (i & 1) ^ ((i & 2) >> 1) ^ ((i & 4) >> 2);
  94. if (0 < (odd ? 1.0f : -1.0f) * mat[5 + dir]) continue;
  95. int origin_index = (1 << ((dir - 1) % 3));
  96. int right_index = (1 << ((dir + odd) % 3)) | origin_index;
  97. int down_index = (1 << ((dir + (odd^1)) % 3)) | origin_index;
  98. if (!odd) {
  99. origin_index ^= 7;
  100. right_index ^= 7;
  101. down_index ^= 7;
  102. }
  103. SDL_FPoint origin, right, down;
  104. origin.x = x0 + px*corners[0 + 2*origin_index];
  105. origin.y = y0 + px*corners[1 + 2*origin_index];
  106. right.x = x0 + px*corners[0 + 2*right_index];
  107. right.y = y0 + px*corners[1 + 2*right_index];
  108. down.x = x0 + px*corners[0 + 2*down_index];
  109. down.y = y0 + px*corners[1 + 2*down_index];
  110. SDL_RenderTextureAffine(renderer, texture, NULL, &origin, &right, &down);
  111. }
  112. SDL_RenderPresent(renderer);
  113. return SDL_APP_CONTINUE;
  114. }
  115. /* This function runs once at shutdown. */
  116. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  117. {
  118. SDL_DestroyTexture(texture);
  119. /* SDL will clean up the window/renderer for us. */
  120. }