graphics.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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::arrow(int16_t x, int16_t y, int16_t w, int16_t h, uint32_t color)
  113. {
  114. line(x, y, x + w, y + h, color);
  115. fillCircle(x + w, y + h, 4, color);
  116. }
  117. void graphics::draw::circle(int16_t x, int16_t y, int16_t radius, double angle, uint32_t color)
  118. {
  119. circleColor(graphics::renderer, x, y, radius, color);
  120. lineColor(graphics::renderer,
  121. x, y,
  122. floor(x + cos(angle) * radius), floor(y + sin(angle) * radius),
  123. color);
  124. }
  125. void graphics::draw::fillCircle(int16_t x, int16_t y, int16_t radius, uint32_t color)
  126. {
  127. filledCircleColor(graphics::renderer, x, y, radius, color);
  128. }
  129. void graphics::draw::rect(int16_t x, int16_t y, int16_t width, int16_t height, uint32_t color)
  130. {
  131. lineColor(graphics::renderer,
  132. floor(x - width / 2.0), floor(y - height / 2.0),
  133. floor(x + width / 2.0), floor(y - height / 2.0), color);
  134. lineColor(graphics::renderer,
  135. floor(x + width / 2.0), floor(y - height / 2.0),
  136. floor(x + width / 2.0), floor(y + height / 2.0), color);
  137. lineColor(graphics::renderer,
  138. floor(x + width / 2.0), floor(y + height / 2.0),
  139. floor(x - width / 2.0), floor(y + height / 2.0), color);
  140. lineColor(graphics::renderer,
  141. floor(x - width / 2.0), floor(y + height / 2.0),
  142. floor(x - width / 2.0), floor(y - height / 2.0), color);
  143. }
  144. void graphics::draw::fillRect(int16_t x, int16_t y, int16_t width, int16_t height, uint32_t color)
  145. {
  146. boxColor(graphics::renderer,
  147. floor(x - width / 2.0), floor(y - height / 2.0),
  148. floor(x + width / 2.0), floor(y + height / 2.0), color);
  149. }
  150. void graphics::draw::polygon(int16_t x, int16_t y, const std::vector<vec2> &vertices, uint32_t color)
  151. {
  152. for (uint32_t i = 0; i < vertices.size(); i++)
  153. {
  154. uint32_t currIndex = i;
  155. uint32_t nextIndex = (i + 1) % vertices.size();
  156. lineColor(graphics::renderer,
  157. floor(vertices[currIndex].x), floor(vertices[currIndex].y),
  158. floor(vertices[nextIndex].x), floor(vertices[nextIndex].y),
  159. color);
  160. }
  161. filledCircleColor(graphics::renderer, x, y, 1, color);
  162. }
  163. void graphics::draw::fillPolygon(int16_t x, int16_t y, const std::vector<vec2> &vertices, uint32_t color)
  164. {
  165. std::vector<short> vx;
  166. std::vector<short> vy;
  167. for (const auto & vertice : vertices)
  168. {
  169. vx.push_back(static_cast<short>(vertice.x));
  170. }
  171. for (const auto & vertice : vertices)
  172. {
  173. vy.push_back(static_cast<short>(vertice.y));
  174. }
  175. filledPolygonColor(graphics::renderer, &vx[0], &vy[0], vertices.size(), color);
  176. filledCircleColor(graphics::renderer, x, y, 1, 0xFF000000);
  177. }
  178. void graphics::draw::texture(int16_t x, int16_t y, int16_t width, int16_t height,
  179. double rotation, SDL_Texture *texture)
  180. {
  181. SDL_Rect dstRect = {x - (width / 2), y - (height / 2), width, height};
  182. double rotationDeg = rotation * 57.2958;
  183. SDL_RenderCopyEx(graphics::renderer, texture, nullptr, &dstRect, rotationDeg,
  184. nullptr, SDL_FLIP_NONE);
  185. }
  186. void graphics::draw::text(int16_t x, int16_t y, uint32_t colour, const char *format, ...)
  187. {
  188. va_list va;
  189. uint8_t r = ((colour >> 16) & 0xFF);
  190. uint8_t g = ((colour >> 8) & 0xFF);
  191. uint8_t b = ((colour ) & 0xFF);
  192. SDL_Color c = { r, g, b , 255};
  193. char buffer[512];
  194. va_start(va, format);
  195. vsnprintf(buffer, 512, format, va);
  196. va_end(va);
  197. SDL_Surface *surface = TTF_RenderText_Blended(font, buffer, c);
  198. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
  199. int labelWidth, labelHeight;
  200. SDL_QueryTexture(texture, nullptr, nullptr, &labelWidth, &labelHeight);
  201. SDL_Rect destRect =
  202. {
  203. x, y, labelWidth, labelHeight
  204. };
  205. SDL_RenderCopyEx(renderer, texture, nullptr, &destRect,0,
  206. nullptr, SDL_FLIP_NONE);
  207. SDL_FreeSurface(surface);
  208. SDL_DestroyTexture(texture);
  209. }