display.c 5.5 KB

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