graphics.cpp 6.8 KB

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