display.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. int32_t windowWidth = 800;
  22. int32_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(int32_t x, int32_t y0, int32_t y1, uint32_t colour)
  93. {
  94. uint32_t i;
  95. if (y0 > y1) { i = y1; y1 = y0; y0 = i; }
  96. if ( ((x < 0) || (x > windowWidth)) ||
  97. ((y0 < 0) && (y1 < 0)) ||
  98. ((y0 > windowHeight) && (y1 > windowHeight)) )
  99. {
  100. return;
  101. }
  102. y0 = MAX(0, y0);
  103. y1 = MIN(y1, (windowHeight - 1));
  104. uint32_t *currentPos = &frameBuffer[x + (y0 * windowWidth)];
  105. for(i = y0; i < y1; i++, currentPos+=windowWidth)
  106. {
  107. *currentPos = colour;
  108. }
  109. }
  110. void drawHLine(int32_t x0, int32_t y, int32_t x1, uint32_t colour)
  111. {
  112. uint32_t i;
  113. if (x0 > x1) { i = x1; x1 = x0; x0 = i; }
  114. if ( ((y < 0) || (y > windowHeight)) ||
  115. ((x0 < 0) && (x1 < 0)) ||
  116. ((x0 > windowWidth) && (x1 > windowWidth)) )
  117. {
  118. return;
  119. }
  120. x0 = MAX(0, x0);
  121. x1 = MIN(x1, (windowWidth - 1));
  122. uint32_t *currentPos = &frameBuffer[x0 + (y * windowWidth)];
  123. for(i = x0; i < x1; i++, currentPos++)
  124. {
  125. *currentPos = colour;
  126. }
  127. }
  128. void clearFrameBuffer(int32_t colour)
  129. {
  130. uint32_t x, y;
  131. for (y = 0 ; y < windowHeight ; y++)
  132. {
  133. for (x = 0 ; x < windowWidth ; x++)
  134. {
  135. frameBuffer[x + (y * windowWidth)] = colour;
  136. }
  137. }
  138. }
  139. void drawGrid(int spacing, uint32_t colour)
  140. {
  141. int32_t x, y;
  142. for (y = 0 ; y < windowHeight ; y++)
  143. {
  144. for (x = 0 ; x < windowWidth ; x++)
  145. {
  146. if (((x % spacing) == 0) || ((y % spacing) == 0))
  147. {
  148. frameBuffer[x + (y * windowWidth)] = colour;
  149. }
  150. }
  151. }
  152. }
  153. void drawRectangle(int32_t x, int32_t y, int32_t w, uint32_t h, uint32_t colour)
  154. {
  155. uint32_t j;
  156. for (j = 0 ; j < h ; j++)
  157. {
  158. drawHLine(x, y + j, x + w, colour);
  159. }
  160. }
  161. /* FIXME: Some out of screen line may render weirdly. Will fix that later */
  162. void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t colour)
  163. {
  164. int i;
  165. int32_t deltaX = x1 - x0;
  166. int32_t deltaY = y1 - y0;
  167. int32_t sideLength = ( abs(deltaX) >= abs(deltaY) ) ? abs(deltaX) : abs(deltaY);
  168. double incrementX = deltaX / (double)sideLength;
  169. double incrementY = deltaY / (double)sideLength;
  170. if (deltaX == 0)
  171. {
  172. drawVLine(x0, y0, y1, colour);
  173. }
  174. else if (deltaY == 0)
  175. {
  176. drawHLine(x0, y0, x1, colour);
  177. }
  178. else
  179. {
  180. double currentX = x0;
  181. double currentY = y0;
  182. for (i = 0 ; i < sideLength ; i++)
  183. {
  184. drawPixel(round(currentX), round(currentY), colour);
  185. currentX += incrementX;
  186. currentY += incrementY;
  187. }
  188. }
  189. }
  190. void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
  191. {
  192. drawLine(x0, y0, x1, y1, colour);
  193. drawLine(x1, y1, x2, y2, colour);
  194. drawLine(x2, y2, x0, y0, colour);
  195. }