affine-textures.c 5.3 KB

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