display.c 5.5 KB

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