display.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <SDL_ttf.h>
  13. #include <log.h>
  14. #include <display.h>
  15. /***********************************************************************************************************************
  16. * Global variables
  17. **********************************************************************************************************************/
  18. SDL_Window *window = NULL;
  19. SDL_Renderer *renderer = NULL;
  20. colour_t *frameBuffer = NULL;
  21. double *zBuffer = NULL;
  22. SDL_Texture *frameBufferTexture = NULL;
  23. int32_t windowWidth = 1024;
  24. int32_t windowHeight = 768;
  25. static TTF_Font *font = NULL;
  26. /***********************************************************************************************************************
  27. * Functions
  28. **********************************************************************************************************************/
  29. #define MAX(_a, _b) ((_a) < (_b)) ? (_b) : (_a)
  30. #define MIN(_a, _b) ((_a) > (_b)) ? (_b) : (_a)
  31. bool initialiseWindow(bool fullScreen)
  32. {
  33. bool ret = false;
  34. int windowFlags = 0;
  35. if (SDL_Init(SDL_INIT_EVERYTHING))
  36. {
  37. Log(TLOG_PANIC, NULL, "SDL Initialisation error!! error: %s", SDL_GetError());
  38. goto exit;
  39. }
  40. Log(TLOG_DEBUG, NULL, "SDL properly initialised!");
  41. ret = TTF_Init();
  42. if (ret)
  43. {
  44. Log(TLOG_PANIC, NULL, "SDL_ttf Initialisation error!! error: %s", SDL_GetError());
  45. goto exit;
  46. }
  47. if (fullScreen)
  48. {
  49. Log(TLOG_DEBUG, NULL, "Will go fullscreen! Fasten your seatbelts!");
  50. SDL_DisplayMode displayMode;
  51. SDL_GetCurrentDisplayMode(0, &displayMode);
  52. windowWidth = displayMode.w;
  53. windowHeight = displayMode.h;
  54. windowFlags = SDL_WINDOW_BORDERLESS;
  55. }
  56. window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight,
  57. windowFlags);
  58. if (window == NULL)
  59. {
  60. Log(TLOG_PANIC, NULL, "SDL Window creation error: %s", SDL_GetError());
  61. goto exit;
  62. }
  63. Log(TLOG_DEBUG, NULL, "SDL Window created!");
  64. renderer = SDL_CreateRenderer(window, -1, 0);
  65. if (renderer == NULL)
  66. {
  67. Log(TLOG_PANIC, NULL, "SDL Renderer creation error: %s", SDL_GetError());
  68. goto exit;
  69. }
  70. Log(TLOG_DEBUG, NULL, "SDL Renderer created!");
  71. font = TTF_OpenFont("assets/pico8.ttf", 8);
  72. if (font == NULL)
  73. {
  74. Log(TLOG_WARNING, NULL, "Cannot open the font for printing...: %s", SDL_GetError());
  75. goto exit;
  76. }
  77. if (fullScreen)
  78. {
  79. SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
  80. }
  81. ret = true;
  82. exit:
  83. return ret;
  84. }
  85. void destroyWindow()
  86. {
  87. if (font != NULL)
  88. {
  89. TTF_CloseFont(font);
  90. }
  91. if (frameBuffer != NULL)
  92. {
  93. free(frameBuffer);
  94. }
  95. if (zBuffer != NULL)
  96. {
  97. free(zBuffer);
  98. }
  99. SDL_DestroyRenderer(renderer);
  100. SDL_DestroyWindow(window);
  101. SDL_Quit();
  102. }
  103. void renderFrameBuffer()
  104. {
  105. SDL_UpdateTexture(frameBufferTexture, NULL, frameBuffer, (uint32_t)(windowWidth * sizeof(uint32_t)));
  106. SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
  107. }
  108. void drawText(int32_t x, int32_t y, colour_t colour, const char *format, ...)
  109. {
  110. va_list va;
  111. uint8_t r = ((colour >> 16) & 0xFF);
  112. uint8_t g = ((colour >> 8) & 0xFF);
  113. uint8_t b = ((colour ) & 0xFF);
  114. SDL_Color c = { r, g, b , 255};
  115. char buffer[512];
  116. va_start(va, format);
  117. vsnprintf(buffer, 512, format, va);
  118. va_end(va);
  119. SDL_Surface *surface = TTF_RenderText_Blended(font, buffer, c);
  120. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
  121. int labelWidth, labelHeight;
  122. SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
  123. SDL_Rect destRect =
  124. {
  125. x, y, labelWidth, labelHeight
  126. };
  127. SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
  128. SDL_FreeSurface(surface);
  129. SDL_DestroyTexture(texture);
  130. }
  131. void drawPixel(int32_t x, int32_t y, colour_t colour)
  132. {
  133. if ((x >= 0) && (x < windowWidth) && (y >= 0) && (y < windowHeight))
  134. {
  135. frameBuffer[x + (y * windowWidth)] = colour;
  136. }
  137. }
  138. void drawVLine(int32_t x, int32_t y0, int32_t y1, colour_t colour)
  139. {
  140. int32_t i;
  141. if (y0 > y1) {
  142. intSwap(&y0, &y1); }
  143. if ( ((x < 0) || (x > windowWidth)) ||
  144. ((y0 < 0) && (y1 < 0)) ||
  145. ((y0 > windowHeight) && (y1 > windowHeight)) )
  146. {
  147. return;
  148. }
  149. y0 = MAX(0, y0);
  150. y1 = MIN(y1, (windowHeight - 1));
  151. uint32_t *currentPos = &frameBuffer[x + (y0 * windowWidth)];
  152. for(i = y0; i <= y1; i++, currentPos+=windowWidth)
  153. {
  154. *currentPos = colour;
  155. }
  156. }
  157. void drawHLine(int32_t x0, int32_t y, int32_t x1, colour_t colour)
  158. {
  159. int32_t i;
  160. if (x0 > x1) {
  161. intSwap(&x0, &x1); }
  162. if ( ((y < 0) || (y > windowHeight)) ||
  163. ((x0 < 0) && (x1 < 0)) ||
  164. ((x0 > windowWidth) && (x1 > windowWidth)) )
  165. {
  166. return;
  167. }
  168. x0 = MAX(0, x0);
  169. x1 = MIN(x1, (windowWidth - 1));
  170. uint32_t *currentPos = &frameBuffer[x0 + (y * windowWidth)];
  171. for(i = x0; i <= x1; i++, currentPos++)
  172. {
  173. *currentPos = colour;
  174. }
  175. }
  176. void clearFrameBuffer(colour_t colour)
  177. {
  178. int32_t x, y;
  179. for (y = 0 ; y < windowHeight ; y++)
  180. {
  181. for (x = 0 ; x < windowWidth ; x++)
  182. {
  183. frameBuffer[x + (y * windowWidth)] = colour;
  184. }
  185. }
  186. }
  187. void clearZBuffer()
  188. {
  189. int32_t x, y;
  190. if (doZBuffer)
  191. {
  192. for (y = 0 ; y < windowHeight ; y++)
  193. {
  194. for (x = 0 ; x < windowWidth ; x++)
  195. {
  196. zBuffer[x + (y * windowWidth)] = 1.0;
  197. }
  198. }
  199. }
  200. }
  201. void drawGrid(int spacing, colour_t colour)
  202. {
  203. int32_t x, y;
  204. for (y = 0 ; y < windowHeight ; y++)
  205. {
  206. for (x = 0 ; x < windowWidth ; x++)
  207. {
  208. if (((x % spacing) == 0) || ((y % spacing) == 0))
  209. {
  210. frameBuffer[x + (y * windowWidth)] = colour;
  211. }
  212. }
  213. }
  214. }
  215. void drawRectangle(int32_t x, int32_t y, int32_t w, int32_t h, colour_t colour)
  216. {
  217. int32_t j;
  218. for (j = 0 ; j < h ; j++)
  219. {
  220. drawHLine(x, y + j, x + w, colour);
  221. }
  222. }
  223. /* FIXME: Some out of screen line may render weirdly. Will fix that later */
  224. void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour)
  225. {
  226. int i;
  227. int32_t deltaX = x1 - x0;
  228. int32_t deltaY = y1 - y0;
  229. int32_t sideLength = ( abs(deltaX) >= abs(deltaY) ) ? abs(deltaX) : abs(deltaY);
  230. double incrementX = deltaX / (double)sideLength;
  231. double incrementY = deltaY / (double)sideLength;
  232. if (deltaX == 0)
  233. {
  234. drawVLine(x0, y0, y1, colour);
  235. }
  236. else if (deltaY == 0)
  237. {
  238. drawHLine(x0, y0, x1, colour);
  239. }
  240. else
  241. {
  242. double currentX = x0;
  243. double currentY = y0;
  244. for (i = 0 ; i < sideLength ; i++)
  245. {
  246. drawPixel(round(currentX), round(currentY), colour);
  247. currentX += incrementX;
  248. currentY += incrementY;
  249. }
  250. }
  251. }