display.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. bool initialiseWindow(bool fullScreen)
  27. {
  28. bool ret = false;
  29. int windowFlags = 0;
  30. if (SDL_Init(SDL_INIT_EVERYTHING))
  31. {
  32. log(TLOG_PANIC, NULL, "SDL Initialisation error!! error: %s", SDL_GetError());
  33. goto exit;
  34. }
  35. log(TLOG_DEBUG, NULL, "SDL properly initialised!");
  36. if (fullScreen)
  37. {
  38. log(TLOG_DEBUG, NULL, "Will go fullscreen! Fasten your seatbelts!");
  39. SDL_DisplayMode displayMode;
  40. SDL_GetCurrentDisplayMode(0, &displayMode);
  41. windowWidth = displayMode.w;
  42. windowHeight = displayMode.h;
  43. windowFlags = SDL_WINDOW_BORDERLESS;
  44. }
  45. window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight,
  46. windowFlags);
  47. if (window == NULL)
  48. {
  49. log(TLOG_PANIC, NULL, "SDL Window creation error: %s", SDL_GetError());
  50. goto exit;
  51. }
  52. log(TLOG_DEBUG, NULL, "SDL Window created!");
  53. renderer = SDL_CreateRenderer(window, -1, 0);
  54. if (renderer == NULL)
  55. {
  56. log(TLOG_PANIC, NULL, "SDL Renderer creation error: %s", SDL_GetError());
  57. goto exit;
  58. }
  59. log(TLOG_DEBUG, NULL, "SDL Renderer created!");
  60. if (fullScreen)
  61. {
  62. SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
  63. }
  64. ret = true;
  65. exit:
  66. return ret;
  67. }
  68. void destroy_window()
  69. {
  70. if (frameBuffer != NULL)
  71. {
  72. free(frameBuffer);
  73. }
  74. SDL_DestroyRenderer(renderer);
  75. SDL_DestroyWindow(window);
  76. SDL_Quit();
  77. }
  78. void renderFrameBuffer()
  79. {
  80. SDL_UpdateTexture(frameBufferTexture, NULL, frameBuffer, (uint32_t)(windowWidth * sizeof(uint32_t)));
  81. SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
  82. }
  83. void drawPixel(uint32_t x, uint32_t y, uint32_t colour)
  84. {
  85. if ((x >= 0) && (x < windowWidth) && (y >= 0) && (y < windowHeight))
  86. {
  87. frameBuffer[x + (y * windowWidth)] = colour;
  88. }
  89. }
  90. void clearFrameBuffer(uint32_t colour)
  91. {
  92. uint32_t x, y;
  93. for (y = 0 ; y < windowHeight ; y++)
  94. {
  95. for (x = 0 ; x < windowWidth ; x++)
  96. {
  97. frameBuffer[x + (y * windowWidth)] = colour;
  98. }
  99. }
  100. }
  101. void drawGrid(int spacing, uint32_t colour)
  102. {
  103. uint32_t x, y;
  104. for (y = 0 ; y < windowHeight ; y++)
  105. {
  106. for (x = 0 ; x < windowWidth ; x++)
  107. {
  108. if (((x % spacing) == 0) || ((y % spacing) == 0))
  109. {
  110. frameBuffer[x + (y * windowWidth)] = colour;
  111. }
  112. }
  113. }
  114. }
  115. void drawRectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t colour)
  116. {
  117. uint32_t i, j;
  118. for (j = 0 ; j < h ; j++)
  119. {
  120. for (i = 0 ; i < w ; i++)
  121. {
  122. drawPixel(x + i, y + j, colour);
  123. }
  124. }
  125. }