main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "SDL2_wip.c"
  2. #include "SDL2_wip.h"
  3. #include "SDL_render.h"
  4. #include "SDL_stdinc.h"
  5. #include "SDL_video.h"
  6. #include <SDL.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. int
  10. circle_of_lines (SDL_Renderer *renderer, Uint8 fg, Uint8 bg, int midx, int midy,
  11. int rad_in, int rad_out, int iter, float thick_start,
  12. float thick_step)
  13. {
  14. filledRectRaw (renderer, midx - rad_out, midy - rad_out, midx + rad_out,
  15. midy + rad_out, bg, bg, bg, 255);
  16. float rad_i = rad_in;
  17. float rad_o = rad_out;
  18. float thickness = thick_start;
  19. for (float i = 0; i < 6.28; i = i + 6.28 / iter)
  20. {
  21. float x1 = rad_i * cos (i);
  22. float y1 = rad_i * sin (i);
  23. float x2 = rad_o * cos (i);
  24. float y2 = rad_o * sin (i);
  25. line (renderer, x1 + midx, y1 + midy, x2 + midx, y2 + midy, thickness, 0,
  26. fg, fg, fg, 255);
  27. thickness += thick_step;
  28. }
  29. return 0;
  30. }
  31. int
  32. drawRegPolyTest (SDL_Renderer *renderer)
  33. {
  34. float cx = 400;
  35. float cy = 400;
  36. float rad = 200;
  37. int num_sides = 5;
  38. regularPolygon (renderer, cx, cy, rad, num_sides, 20, 0, 255, 0, 0, 0, 0, 255,
  39. 255, 255, 255);
  40. }
  41. int
  42. drawFanGeometryTest (SDL_Renderer *renderer)
  43. {
  44. float x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
  45. x1 = 50;
  46. y1 = 50;
  47. x2 = 700;
  48. y2 = 50;
  49. x3 = 700;
  50. y3 = 700;
  51. x4 = 100;
  52. y4 = 600;
  53. x5 = 200;
  54. y5 = 700;
  55. #define num_vert 4
  56. float x_arr[num_vert] = { x1, x2, x3, x4 };
  57. float y_arr[num_vert] = { y1, y2, y3, y4 };
  58. _renderFanPolygon (renderer, 400, 400, x_arr, y_arr, num_vert, 255, 255, 255,
  59. 255);
  60. }
  61. int
  62. drawAARectTest (SDL_Renderer *renderer)
  63. {
  64. float x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
  65. x1 = 50;
  66. y1 = 50;
  67. x2 = 700;
  68. y2 = 700;
  69. float thickness = 30.0;
  70. aaRect (renderer, x1, y1, x2, y2, 0, 0, 255, 0, 255, 255, 1, 255, 255, 255,
  71. 255);
  72. return 0;
  73. }
  74. int
  75. drawPolylineTest (SDL_Renderer *renderer)
  76. {
  77. float x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
  78. x1 = 50;
  79. y1 = 400;
  80. x2 = 100;
  81. y2 = 250;
  82. x3 = 400;
  83. y3 = 20;
  84. x4 = 200;
  85. y4 = 400;
  86. x5 = 700;
  87. y5 = 700;
  88. float w = 3;
  89. #define poly_num_vert 6
  90. float x_arr[poly_num_vert] = { x1, x2, x3, x4, x5, x1 };
  91. float y_arr[poly_num_vert] = { y1, y2, y3, y4, y5, y1 };
  92. polyline (renderer, x_arr, y_arr, num_vert, 2.0, 0, 0, 255, 255, 255, 255);
  93. if (0)
  94. {
  95. rectRaw (renderer, x1 - w, y1 - w, x1 + w, y1 + w, 255, 0, 0, 255);
  96. rectRaw (renderer, x2 - w, y2 - w, x2 + w, y2 + w, 255, 0, 0, 255);
  97. rectRaw (renderer, x3 - w, y3 - w, x3 + w, y3 + w, 255, 0, 0, 255);
  98. rectRaw (renderer, x4 - w, y4 - w, x4 + w, y4 + w, 255, 0, 0, 255);
  99. }
  100. return 0;
  101. }
  102. int
  103. draw (SDL_Renderer *renderer)
  104. {
  105. // circle_of_lines (renderer, 255, 0, 400, 400, 40, 300, 40, 1, 0.2);
  106. // circle_of_lines (renderer, 0, 255, 600, 200, 40, 200, 16, 0.6 * 20, 0.6);
  107. // circle_of_lines (renderer, 255, 0, 200, 600, 100, 200, 14, 0.6 *
  108. // 36, 1.2); circle_of_lines (renderer, 255, 0, 600, 600, 120, 200, 10, 0.6
  109. // * 60 + 1.2 * 14, 2.4);
  110. // drawPolylineTest (renderer);
  111. // drawAARectTest (renderer);
  112. // drawFanGeometryTest (renderer);
  113. drawRegPolyTest (renderer);
  114. }
  115. int
  116. main (int argc, char **argv)
  117. {
  118. SDL_SetHint (SDL_HINT_RENDER_SCALE_QUALITY, "2");
  119. SDL_SetHint (SDL_HINT_RENDER_LINE_METHOD, "3");
  120. SDL_SetHint (SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
  121. SDL_Init (SDL_INIT_VIDEO);
  122. SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
  123. SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, 16);
  124. SDL_Window *win = SDL_CreateWindow ("Rendering Geometry", 0, 0, 800, 800,
  125. SDL_WINDOW_ALLOW_HIGHDPI);
  126. if (!win)
  127. {
  128. SDL_Quit ();
  129. return 2;
  130. }
  131. SDL_Renderer *renderer
  132. = SDL_CreateRenderer (win, -1, SDL_RENDERER_ACCELERATED);
  133. if (!renderer)
  134. {
  135. SDL_DestroyWindow (win);
  136. return 3;
  137. }
  138. int render_w, render_h;
  139. int window_w, window_h;
  140. float scale_x, scale_y;
  141. SDL_GetRendererOutputSize (renderer, &render_w, &render_h);
  142. SDL_GetWindowSize (win, &window_w, &window_h);
  143. scale_x = (float)(render_w) / (float)(window_w);
  144. scale_y = (float)(render_h) / (float)(window_h);
  145. printf ("%.6f", scale_x);
  146. printf ("%.6f", scale_y);
  147. SDL_RenderSetScale (renderer, scale_x, scale_y);
  148. SDL_bool running = SDL_TRUE;
  149. SDL_Event ev;
  150. while (running)
  151. {
  152. while (SDL_PollEvent (&ev))
  153. {
  154. if (ev.type == SDL_QUIT)
  155. {
  156. running = SDL_FALSE;
  157. }
  158. }
  159. SDL_SetRenderDrawColor (renderer, 0, 0, 0, 255);
  160. SDL_RenderClear (renderer);
  161. draw (renderer);
  162. SDL_RenderPresent (renderer);
  163. }
  164. SDL_DestroyRenderer (renderer);
  165. SDL_DestroyWindow (win);
  166. SDL_Quit ();
  167. return 0;
  168. }