graphics.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * 2D Physic Engine
  3. * graphics.cpp: utilities to display stuff.
  4. * Based on pikuma.com Learn Game Physics Engine Programming course.
  5. * Copyright (c) 2022 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 07/06/2022.
  8. */
  9. #include <cstdarg>
  10. #include <SDL.h>
  11. #include <SDL_image.h>
  12. #include <SDL_ttf.h>
  13. #include <SDL2_gfxPrimitives.h>
  14. #include <imgui.h>
  15. #include <imgui_sdl.h>
  16. #include <imgui_impl_sdl.h>
  17. #include <logger.h>
  18. #include <graphics.h>
  19. SDL_Window* graphics::window = nullptr;
  20. SDL_Renderer* graphics::renderer = nullptr;
  21. int graphics::windowFlags = 0;
  22. int graphics::windowWidth = 0;
  23. int graphics::windowHeight = 0;
  24. TTF_Font *graphics::font = nullptr;
  25. bool graphics::openWindow(bool fullScreen)
  26. {
  27. if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
  28. {
  29. logger::critical("Error initializing SDL: %s", SDL_GetError());
  30. return false;
  31. }
  32. if (TTF_Init())
  33. {
  34. logger::critical("SDL_ttf Initialisation error: %s", SDL_GetError());
  35. return false;
  36. }
  37. SDL_DisplayMode display_mode;
  38. SDL_GetCurrentDisplayMode(0, &display_mode);
  39. if (fullScreen)
  40. {
  41. graphics::windowWidth = display_mode.w;
  42. graphics::windowHeight = display_mode.h;
  43. graphics::windowFlags = SDL_WINDOW_BORDERLESS;
  44. }
  45. else
  46. {
  47. graphics::windowWidth = 1024;
  48. graphics::windowHeight = 758;
  49. graphics::windowFlags = 0;
  50. }
  51. graphics::window = SDL_CreateWindow(nullptr, 0, 0,
  52. graphics::windowWidth, graphics::windowHeight,
  53. graphics::windowFlags);
  54. if (!graphics::window)
  55. {
  56. logger::critical("Error creating SDL window: %s", SDL_GetError());
  57. return false;
  58. }
  59. graphics::renderer = SDL_CreateRenderer(window, -1,
  60. SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  61. if (!graphics::renderer)
  62. {
  63. logger::critical("Error creating SDL renderer: %s", SDL_GetError());
  64. return false;
  65. }
  66. graphics::font = TTF_OpenFont("assets/pico8.ttf", 8);
  67. if (graphics::font == nullptr)
  68. {
  69. logger::critical("Cannot open the font for printing...: %s", SDL_GetError());
  70. return false;
  71. }
  72. if (fullScreen)
  73. {
  74. SDL_SetWindowFullscreen(graphics::window, SDL_WINDOW_FULLSCREEN);
  75. }
  76. ImGui::CreateContext();
  77. ImGuiSDL::Initialize(graphics::renderer, graphics::windowWidth, graphics::windowHeight);
  78. return true;
  79. }
  80. void graphics::closeWindow()
  81. {
  82. SDL_DestroyRenderer(graphics::renderer);
  83. SDL_DestroyWindow(graphics::window);
  84. SDL_Quit();
  85. }
  86. SDL_Texture *graphics::loadTexture(const char *filename)
  87. {
  88. SDL_Surface *surface = IMG_Load(filename);
  89. if (!surface)
  90. {
  91. logger::error("Error opening texture file '%s'!", filename);
  92. return nullptr;
  93. }
  94. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
  95. SDL_FreeSurface(surface);
  96. logger::info("Loaded texture %s", filename);
  97. return texture;
  98. }
  99. void graphics::renderFrame()
  100. {
  101. SDL_RenderPresent(graphics::renderer);
  102. }
  103. void graphics::clearScreen(uint32_t color)
  104. {
  105. SDL_SetRenderDrawColor(graphics::renderer, color >> 16, color >> 8, color, 255);
  106. SDL_RenderClear(graphics::renderer);
  107. }
  108. void graphics::draw::line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint32_t color)
  109. {
  110. lineColor(graphics::renderer, x0, y0, x1, y1, color);
  111. }
  112. void graphics::draw::circle(int16_t x, int16_t y, int16_t radius, double angle, uint32_t color)
  113. {
  114. circleColor(graphics::renderer, x, y, radius, color);
  115. lineColor(graphics::renderer,
  116. x, y,
  117. floor(x + cos(angle) * radius), floor(y + sin(angle) * radius),
  118. color);
  119. }
  120. void graphics::draw::fillCircle(int16_t x, int16_t y, int16_t radius, uint32_t color)
  121. {
  122. filledCircleColor(graphics::renderer, x, y, radius, color);
  123. }
  124. void graphics::draw::rect(int16_t x, int16_t y, int16_t width, int16_t height, uint32_t color)
  125. {
  126. lineColor(graphics::renderer,
  127. floor(x - width / 2.0), floor(y - height / 2.0),
  128. floor(x + width / 2.0), floor(y - height / 2.0), color);
  129. lineColor(graphics::renderer,
  130. floor(x + width / 2.0), floor(y - height / 2.0),
  131. floor(x + width / 2.0), floor(y + height / 2.0), color);
  132. lineColor(graphics::renderer,
  133. floor(x + width / 2.0), floor(y + height / 2.0),
  134. floor(x - width / 2.0), floor(y + height / 2.0), color);
  135. lineColor(graphics::renderer,
  136. floor(x - width / 2.0), floor(y + height / 2.0),
  137. floor(x - width / 2.0), floor(y - height / 2.0), color);
  138. }
  139. void graphics::draw::fillRect(int16_t x, int16_t y, int16_t width, int16_t height, uint32_t color)
  140. {
  141. boxColor(graphics::renderer,
  142. floor(x - width / 2.0), floor(y - height / 2.0),
  143. floor(x + width / 2.0), floor(y + height / 2.0), color);
  144. }
  145. void graphics::draw::polygon(int16_t x, int16_t y, const std::vector<vec2> &vertices, uint32_t color)
  146. {
  147. for (uint32_t i = 0; i < vertices.size(); i++)
  148. {
  149. uint32_t currIndex = i;
  150. uint32_t nextIndex = (i + 1) % vertices.size();
  151. lineColor(graphics::renderer,
  152. floor(vertices[currIndex].x), floor(vertices[currIndex].y),
  153. floor(vertices[nextIndex].x), floor(vertices[nextIndex].y),
  154. color);
  155. }
  156. filledCircleColor(graphics::renderer, x, y, 1, color);
  157. }
  158. void graphics::draw::fillPolygon(int16_t x, int16_t y, const std::vector<vec2> &vertices, uint32_t color)
  159. {
  160. std::vector<short> vx;
  161. std::vector<short> vy;
  162. for (const auto & vertice : vertices)
  163. {
  164. vx.push_back(static_cast<short>(vertice.x));
  165. }
  166. for (const auto & vertice : vertices)
  167. {
  168. vy.push_back(static_cast<short>(vertice.y));
  169. }
  170. filledPolygonColor(graphics::renderer, &vx[0], &vy[0], vertices.size(), color);
  171. filledCircleColor(graphics::renderer, x, y, 1, 0xFF000000);
  172. }
  173. void graphics::draw::texture(int16_t x, int16_t y, int16_t width, int16_t height,
  174. double rotation, SDL_Texture *texture)
  175. {
  176. SDL_Rect dstRect = {x - (width / 2), y - (height / 2), width, height};
  177. double rotationDeg = rotation * 57.2958;
  178. SDL_RenderCopyEx(graphics::renderer, texture, nullptr, &dstRect, rotationDeg,
  179. nullptr, SDL_FLIP_NONE);
  180. }
  181. void graphics::draw::text(int16_t x, int16_t y, uint32_t colour, const char *format, ...)
  182. {
  183. va_list va;
  184. uint8_t r = ((colour >> 16) & 0xFF);
  185. uint8_t g = ((colour >> 8) & 0xFF);
  186. uint8_t b = ((colour ) & 0xFF);
  187. SDL_Color c = { r, g, b , 255};
  188. char buffer[512];
  189. va_start(va, format);
  190. vsnprintf(buffer, 512, format, va);
  191. va_end(va);
  192. SDL_Surface *surface = TTF_RenderText_Blended(font, buffer, c);
  193. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
  194. int labelWidth, labelHeight;
  195. SDL_QueryTexture(texture, nullptr, nullptr, &labelWidth, &labelHeight);
  196. SDL_Rect destRect =
  197. {
  198. x, y, labelWidth, labelHeight
  199. };
  200. SDL_RenderCopyEx(renderer, texture, nullptr, &destRect,0,
  201. nullptr, SDL_FLIP_NONE);
  202. SDL_FreeSurface(surface);
  203. SDL_DestroyTexture(texture);
  204. }