display.c 8.1 KB

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