display.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * 3D Engine
  3. * display.c:
  4. * Based on pikuma.com 3D software renderer in C
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 01/03/2021.
  8. */
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11. #include <SDL.h>
  12. #include <log.h>
  13. #include <display.h>
  14. /***********************************************************************************************************************
  15. * Global variables
  16. **********************************************************************************************************************/
  17. SDL_Window *window = NULL;
  18. SDL_Renderer *renderer = NULL;
  19. uint32_t *frameBuffer = NULL;
  20. SDL_Texture *frameBufferTexture = NULL;
  21. uint32_t windowWidth = 800;
  22. uint32_t windowHeight = 600;
  23. /***********************************************************************************************************************
  24. * Functions
  25. **********************************************************************************************************************/
  26. #define MAX(_a, _b) ((_a) < (_b)) ? (_b) : (_a)
  27. #define MIN(_a, _b) ((_a) > (_b)) ? (_b) : (_a)
  28. bool initialiseWindow(bool fullScreen)
  29. {
  30. bool ret = false;
  31. int windowFlags = 0;
  32. if (SDL_Init(SDL_INIT_EVERYTHING))
  33. {
  34. log(TLOG_PANIC, NULL, "SDL Initialisation error!! error: %s", SDL_GetError());
  35. goto exit;
  36. }
  37. log(TLOG_DEBUG, NULL, "SDL properly initialised!");
  38. if (fullScreen)
  39. {
  40. log(TLOG_DEBUG, NULL, "Will go fullscreen! Fasten your seatbelts!");
  41. SDL_DisplayMode displayMode;
  42. SDL_GetCurrentDisplayMode(0, &displayMode);
  43. windowWidth = displayMode.w;
  44. windowHeight = displayMode.h;
  45. windowFlags = SDL_WINDOW_BORDERLESS;
  46. }
  47. window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight,
  48. windowFlags);
  49. if (window == NULL)
  50. {
  51. log(TLOG_PANIC, NULL, "SDL Window creation error: %s", SDL_GetError());
  52. goto exit;
  53. }
  54. log(TLOG_DEBUG, NULL, "SDL Window created!");
  55. renderer = SDL_CreateRenderer(window, -1, 0);
  56. if (renderer == NULL)
  57. {
  58. log(TLOG_PANIC, NULL, "SDL Renderer creation error: %s", SDL_GetError());
  59. goto exit;
  60. }
  61. log(TLOG_DEBUG, NULL, "SDL Renderer created!");
  62. if (fullScreen)
  63. {
  64. SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
  65. }
  66. ret = true;
  67. exit:
  68. return ret;
  69. }
  70. void destroyWindow()
  71. {
  72. if (frameBuffer != NULL)
  73. {
  74. free(frameBuffer);
  75. }
  76. SDL_DestroyRenderer(renderer);
  77. SDL_DestroyWindow(window);
  78. SDL_Quit();
  79. }
  80. void renderFrameBuffer()
  81. {
  82. SDL_UpdateTexture(frameBufferTexture, NULL, frameBuffer, (uint32_t)(windowWidth * sizeof(uint32_t)));
  83. SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
  84. }
  85. void drawPixel(uint32_t x, uint32_t y, uint32_t colour)
  86. {
  87. if ((x >= 0) && (x < windowWidth) && (y >= 0) && (y < windowHeight))
  88. {
  89. frameBuffer[x + (y * windowWidth)] = colour;
  90. }
  91. }
  92. void drawVLine(uint32_t x, uint32_t y0, uint32_t y1, uint32_t colour)
  93. {
  94. uint32_t i;
  95. if (y0 > y1) { i = y0; y1 = y0; y0 = i; }
  96. if ((x < 0) || (x > windowWidth))
  97. {
  98. return;
  99. }
  100. y0 = MAX(0, y0);
  101. y1 = MIN(y1, (windowHeight - 1));
  102. uint32_t *currentPos = &frameBuffer[x + (y0 * windowWidth)];
  103. for(i = y0; i < y1; i++, currentPos+=windowWidth)
  104. {
  105. *currentPos = colour;
  106. }
  107. }
  108. void drawHLine(uint32_t x0, uint32_t y, uint32_t x1, uint32_t colour)
  109. {
  110. uint32_t i;
  111. if (x0 > x1) { i = x0; x1 = x0; x0 = i; }
  112. if ((y < 0) || (y > windowHeight))
  113. {
  114. return;
  115. }
  116. x0 = MAX(0, x0);
  117. x1 = MIN(x1, (windowWidth - 1));
  118. uint32_t *currentPos = &frameBuffer[x0 + (y * windowWidth)];
  119. for(i = x0; i < x1; i++, currentPos++)
  120. {
  121. *currentPos = colour;
  122. }
  123. }
  124. void clearFrameBuffer(uint32_t colour)
  125. {
  126. uint32_t x, y;
  127. for (y = 0 ; y < windowHeight ; y++)
  128. {
  129. for (x = 0 ; x < windowWidth ; x++)
  130. {
  131. frameBuffer[x + (y * windowWidth)] = colour;
  132. }
  133. }
  134. }
  135. void drawGrid(int spacing, uint32_t colour)
  136. {
  137. uint32_t x, y;
  138. for (y = 0 ; y < windowHeight ; y++)
  139. {
  140. for (x = 0 ; x < windowWidth ; x++)
  141. {
  142. if (((x % spacing) == 0) || ((y % spacing) == 0))
  143. {
  144. frameBuffer[x + (y * windowWidth)] = colour;
  145. }
  146. }
  147. }
  148. }
  149. void drawRectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t colour)
  150. {
  151. uint32_t j;
  152. for (j = 0 ; j < h ; j++)
  153. {
  154. drawHLine(x, y + j, x + w, colour);
  155. }
  156. }
  157. /* FIXME: Some out of screen line may render weirdly. Will fix that later */
  158. void drawLine(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t colour)
  159. {
  160. int i;
  161. int32_t deltaX = (int32_t)x1 - (int32_t)x0;
  162. int32_t deltaY = (int32_t)y1 - (int32_t)y0;
  163. int32_t sideLength = ( abs(deltaX) >= abs(deltaY) ) ? abs(deltaX) : abs(deltaY);
  164. double incrementX = deltaX / (double)sideLength;
  165. double incrementY = deltaY / (double)sideLength;
  166. if (deltaX == 0)
  167. {
  168. drawVLine(x0, y0, y1, colour);
  169. }
  170. else if (deltaY == 0)
  171. {
  172. drawHLine(x0, y0, x1, colour);
  173. }
  174. else
  175. {
  176. double currentX = (int32_t)x0;
  177. double currentY = (int32_t)y0;
  178. for (i = 0 ; i < sideLength ; i++)
  179. {
  180. drawPixel(round(currentX), round(currentY), colour);
  181. currentX += incrementX;
  182. currentY += incrementY;
  183. }
  184. }
  185. }
  186. void drawTriangle(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t colour)
  187. {
  188. drawLine(x0, y0, x1, y1, colour);
  189. drawLine(x1, y1, x2, y2, colour);
  190. drawLine(x2, y2, x0, y0, colour);
  191. }