Renderer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // Created by jovian on 18/07/17.
  3. //
  4. #include <iostream>
  5. #include <SDL2/SDL_image.h>
  6. #include "Renderer.h"
  7. #define RAD_TO_DEG 57.2957795130f
  8. Renderer::Renderer()
  9. : m_screenWidth(0), m_screenHeight(0), m_window(nullptr), m_renderer(nullptr), m_border(1) {}
  10. Renderer::~Renderer() {
  11. // Destroy textures
  12. while (!m_pictureTab.empty()) {
  13. if (m_pictureTab.back() != nullptr)
  14. SDL_DestroyTexture(m_pictureTab.back());
  15. m_pictureTab.pop_back();
  16. }
  17. // Destroy SDL renderer
  18. if (m_renderer != nullptr) {
  19. SDL_DestroyRenderer(m_renderer);
  20. m_renderer = nullptr;
  21. }
  22. // Destroy the beautiful window
  23. if (m_window != nullptr) {
  24. SDL_DestroyWindow(m_window);
  25. m_window = nullptr;
  26. }
  27. }
  28. bool Renderer::initialize(int nbPlayers) {
  29. // Announce
  30. std::cout << "Renderer::initialize() > ";
  31. // Already initialized
  32. if (m_window != nullptr) {
  33. std::cout << "Window already created." << std::endl << std::endl;
  34. return false;
  35. }
  36. // Default screen size
  37. m_screenWidth = 1200;
  38. m_screenHeight = 700;
  39. // Init video
  40. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  41. std::cout << "SDL video failed : " << SDL_GetError() << std::endl << std::endl;
  42. return false;
  43. }
  44. // Opening window
  45. m_window = SDL_CreateWindow("< TinyShooter >",
  46. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  47. m_screenWidth, m_screenHeight,
  48. SDL_WINDOW_SHOWN /*| SDL_WINDOW_FULLSCREEN_DESKTOP*/);
  49. if (m_window == nullptr) {
  50. std::cout << "Window creation failed : " << SDL_GetError() << std::endl << std::endl;
  51. SDL_Quit();
  52. return false;
  53. }
  54. // Hardware physical screen size
  55. SDL_GetWindowSize(m_window, &m_screenWidth, &m_screenHeight);
  56. // Create renderer
  57. m_renderer = SDL_CreateRenderer(m_window, -1,
  58. SDL_RENDERER_ACCELERATED |
  59. SDL_RENDERER_PRESENTVSYNC);
  60. if (m_renderer == nullptr) {
  61. SDL_DestroyWindow(m_window);
  62. std::cout << "SDL Renderer creation failed : " << SDL_GetError() << std::endl << std::endl;
  63. SDL_Quit();
  64. return false;
  65. }
  66. // Split-screen
  67. locateViews(nbPlayers);
  68. // End
  69. std::cout << "Done, no error detected." << std::endl;
  70. // Okay
  71. return loadEveryPicture();
  72. }
  73. void Renderer::clearWindow() {
  74. // Clean up buffer
  75. SDL_SetRenderDrawColor(m_renderer, 0x00, 0x00, 0x00, 0x00);
  76. SDL_RenderClear(m_renderer);
  77. }
  78. void Renderer::renderScene(std::vector<Visual *> &scope, const b2Vec2 &center, float zoom, int which) {
  79. // Rect
  80. SDL_Rect dst;
  81. SDL_Texture *tex;
  82. b2Vec2 rel;
  83. // View port (useful with split-screen)
  84. if (which < 0 || which > 3)
  85. return;
  86. SDL_RenderSetViewport(m_renderer, &m_viewPort[which]);
  87. SDL_SetRenderDrawColor(m_renderer, 0x01, 0x00, 0x11, 0xFF);
  88. dst.x = 0;
  89. dst.y = 0;
  90. dst.w = m_viewPort[which].w;
  91. dst.h = m_viewPort[which].h;
  92. SDL_RenderFillRect(m_renderer, &dst);
  93. // For each
  94. for (auto it(scope.begin()); it != scope.end(); it++) {
  95. // Skip if invalid texture
  96. if ((*it)->getImgId() > m_pictureTab.size() || m_pictureTab[(*it)->getImgId()] == nullptr)
  97. continue;
  98. tex = m_pictureTab[(*it)->getImgId()];
  99. // Rect set up
  100. rel = (*it)->getPos() - center;
  101. dst.x = (int) (rel.x * zoom) + m_viewPort[which].w / 2;
  102. dst.y = (int) (rel.y * zoom) + m_viewPort[which].h / 2;
  103. SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
  104. // Zoom correction
  105. dst.w = (int) (zoom * dst.w / DEFAULT_ZOOM);
  106. dst.h = (int) (zoom * dst.h / DEFAULT_ZOOM);
  107. // Center texture
  108. dst.x -= dst.w / 2;
  109. dst.y -= dst.h / 2;
  110. // SDL rendering
  111. SDL_RenderCopyEx(m_renderer, tex, NULL, &dst, (*it)->getAngle() * RAD_TO_DEG, NULL, SDL_FLIP_NONE);
  112. }
  113. }
  114. void Renderer::presentWindow() {
  115. // Activate display
  116. SDL_RenderPresent(m_renderer);
  117. }
  118. bool Renderer::loadPicture(std::string name) {
  119. SDL_Texture *texture = IMG_LoadTexture(m_renderer, name.c_str());
  120. if (texture == nullptr) {
  121. std::cout << "Renderer::loadPicture() > " << SDL_GetError() << std::endl << std::endl;
  122. return false;
  123. }
  124. m_pictureTab.push_back(texture);
  125. return true;
  126. }
  127. bool Renderer::loadEveryPicture() {
  128. bool okay(true);
  129. okay = okay && loadPicture("Pictures/NoPict.png"); // 0
  130. okay = okay && loadPicture("Pictures/Ally.png"); // 1
  131. okay = okay && loadPicture("Pictures/TinyWall1.png"); // 2
  132. okay = okay && loadPicture("Pictures/TinyWall2.png"); // 3
  133. okay = okay && loadPicture("Pictures/TinyWall3.png"); // 4
  134. okay = okay && loadPicture("Pictures/TinyWall4.png"); // 5
  135. okay = okay && loadPicture("Pictures/HighWall1.png"); // 6
  136. okay = okay && loadPicture("Pictures/HighWall2.png"); // 7
  137. okay = okay && loadPicture("Pictures/HighWall3.png"); // 8
  138. okay = okay && loadPicture("Pictures/HighWall4.png"); // 9
  139. okay = okay && loadPicture("Pictures/RedVisor.png"); // 10
  140. okay = okay && loadPicture("Pictures/RedArrow.png"); // 11
  141. okay = okay && loadPicture("Pictures/Bullet1.png"); // 12
  142. okay = okay && loadPicture("Pictures/Foe.png"); // 13
  143. okay = okay && loadPicture("Pictures/Bullet2.png"); // 14
  144. okay = okay && loadPicture("Pictures/LifeBar1.png"); // 15
  145. okay = okay && loadPicture("Pictures/LifeBar2.png"); // 16
  146. okay = okay && loadPicture("Pictures/LifeBar3.png"); // 17
  147. okay = okay && loadPicture("Pictures/LifeBar4.png"); // 18
  148. okay = okay && loadPicture("Pictures/LifeBar5.png"); // 19
  149. okay = okay && loadPicture("Pictures/LifeBar6.png"); // 20
  150. okay = okay && loadPicture("Pictures/WideWall1.png"); // 21
  151. okay = okay && loadPicture("Pictures/WideWall2.png"); // 22
  152. okay = okay && loadPicture("Pictures/WideWall3.png"); // 23
  153. okay = okay && loadPicture("Pictures/WideWall4.png"); // 24
  154. okay = okay && loadPicture("Pictures/BigWall1.png"); // 25
  155. okay = okay && loadPicture("Pictures/BigWall2.png"); // 26
  156. okay = okay && loadPicture("Pictures/BigWall3.png"); // 27
  157. okay = okay && loadPicture("Pictures/BigWall4.png"); // 28
  158. return okay;
  159. }
  160. SDL_Window *Renderer::getWindow() const {
  161. return m_window;
  162. }
  163. void Renderer::locateViews(int nbPlayers) {
  164. switch (nbPlayers) {
  165. default: // One player
  166. // First and single screen
  167. m_viewPort[0].x = 0;
  168. m_viewPort[0].y = 0;
  169. m_viewPort[0].w = m_screenWidth;
  170. m_viewPort[0].h = m_screenHeight;
  171. break;
  172. case 2: // Two players
  173. // First screen
  174. m_viewPort[0].x = 0;
  175. m_viewPort[0].y = 0;
  176. m_viewPort[0].w = m_screenWidth / 2 - m_border;
  177. m_viewPort[0].h = m_screenHeight;
  178. // Second screen
  179. m_viewPort[1].x = m_screenWidth / 2 + m_border;
  180. m_viewPort[1].y = 0;
  181. m_viewPort[1].w = m_screenWidth / 2;
  182. m_viewPort[1].h = m_screenHeight;
  183. break;
  184. case 3: // Three players
  185. // First screen
  186. m_viewPort[0].x = 0;
  187. m_viewPort[0].y = 0;
  188. m_viewPort[0].w = m_screenWidth / 2 - m_border;
  189. m_viewPort[0].h = m_screenHeight / 2;
  190. // Second screen
  191. m_viewPort[1].x = m_screenWidth / 2 + m_border;
  192. m_viewPort[1].y = 0;
  193. m_viewPort[1].w = m_screenWidth / 2;
  194. m_viewPort[1].h = m_screenHeight / 2;
  195. // Third screen
  196. m_viewPort[2].x = m_screenWidth / 4;
  197. m_viewPort[2].y = m_screenHeight / 2 + m_border;
  198. m_viewPort[2].w = m_screenWidth / 2;
  199. m_viewPort[2].h = m_screenHeight / 2;
  200. break;
  201. case 4: // Four players
  202. // First screen
  203. m_viewPort[0].x = 0;
  204. m_viewPort[0].y = 0;
  205. m_viewPort[0].w = m_screenWidth / 2 - m_border;
  206. m_viewPort[0].h = m_screenHeight / 2;
  207. // Second screen
  208. m_viewPort[1].x = m_screenWidth / 2 + m_border;
  209. m_viewPort[1].y = 0;
  210. m_viewPort[1].w = m_screenWidth / 2;
  211. m_viewPort[1].h = m_screenHeight / 2;
  212. // Third screen
  213. m_viewPort[2].x = 0;
  214. m_viewPort[2].y = m_screenHeight / 2;
  215. m_viewPort[2].w = m_screenWidth / 2 - m_border;
  216. m_viewPort[2].h = m_screenHeight / 2;
  217. // Fourth screen
  218. m_viewPort[3].x = m_screenWidth / 2 + m_border;
  219. m_viewPort[3].y = m_screenHeight / 2;
  220. m_viewPort[3].w = m_screenWidth / 2;
  221. m_viewPort[3].h = m_screenHeight / 2;
  222. break;
  223. }
  224. }
  225. b2Vec2 Renderer::computeDiago(float zoom, int which) {
  226. // Test limits
  227. if (which < 0 || which > 3)
  228. return b2Vec2_zero;
  229. // Compute with view-port dims and zoom
  230. b2Vec2 rep;
  231. rep.x = m_viewPort[which].w / 2;
  232. rep.x /= zoom;
  233. rep.y = m_viewPort[which].h / 2;
  234. rep.y /= zoom;
  235. return rep;
  236. }