display.c 6.8 KB

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