application.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "application.hh"
  2. #include <stdexcept>
  3. #include <SDL2/SDL_image.h>
  4. #include <iostream>
  5. #include <string>
  6. Application::Application()
  7. {
  8. auto res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
  9. if(res != 0)
  10. throw std::runtime_error(SDL_GetError());
  11. if(!IMG_Init(IMG_INIT_PNG))
  12. throw std::runtime_error(SDL_GetError());
  13. if(TTF_Init() == -1)
  14. throw std::runtime_error(SDL_GetError());
  15. window = SDL_CreateWindow("Tetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Constants::BLOCK_SIZE * (Constants::WIDTH+6), Constants::BLOCK_SIZE * Constants::HEIGHT, SDL_WINDOW_SHOWN);
  16. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
  17. auto surface = IMG_Load("assets/blocks.png");
  18. if(!surface)
  19. throw std::runtime_error(SDL_GetError());
  20. blocksTexture = SDL_CreateTextureFromSurface(renderer, surface);
  21. SDL_FreeSurface(surface);
  22. tetromino = Tetromino(&well);
  23. uiBlockPos.w = Constants::BLOCK_SIZE;
  24. uiBlockPos.h = Constants::BLOCK_SIZE;
  25. font = TTF_OpenFont("assets/font.ttf",26);
  26. SDL_Surface* tmpSurface = TTF_RenderText_Solid(font, "SCORE", fontColor);
  27. scoreLabelTexture = SDL_CreateTextureFromSurface(renderer, tmpSurface);
  28. scoreLabelRect.w = tmpSurface->w;
  29. scoreLabelRect.h = tmpSurface->h;
  30. SDL_FreeSurface(tmpSurface);
  31. SDL_Surface* tmpSurface2 = TTF_RenderText_Solid(font, "0", fontColor);
  32. scoreTexture = SDL_CreateTextureFromSurface(renderer, tmpSurface2);
  33. scoreRect.w = tmpSurface2->w;
  34. scoreRect.h = tmpSurface2->h;
  35. SDL_FreeSurface(tmpSurface2);
  36. controller = SDL_JoystickOpen(0);
  37. }
  38. void Application::handleEvents()
  39. {
  40. while(SDL_PollEvent(&event) != 0)
  41. {
  42. switch(event.type)
  43. {
  44. case SDL_QUIT:
  45. quit = true;
  46. break;
  47. case SDL_KEYDOWN:
  48. switch(event.key.keysym.sym)
  49. {
  50. case SDLK_d:
  51. tetromino.move(1);
  52. break;
  53. case SDLK_a:
  54. tetromino.move(0);
  55. break;
  56. case SDLK_f:
  57. tetromino.rotate();
  58. break;
  59. case SDLK_s:
  60. sPressed = true;
  61. break;
  62. }
  63. break;
  64. case SDL_KEYUP:
  65. switch(event.key.keysym.sym)
  66. {
  67. case SDLK_s:
  68. sPressed = false;
  69. break;
  70. }
  71. break;
  72. case SDL_JOYBUTTONDOWN:
  73. switch(event.jbutton.button)
  74. {
  75. case 16:
  76. tetromino.move(1);
  77. break;
  78. case 15:
  79. tetromino.move(0);
  80. break;
  81. case 1:
  82. tetromino.rotate();
  83. break;
  84. case 14:
  85. sPressed = true;
  86. break;
  87. }
  88. break;
  89. case SDL_JOYBUTTONUP:
  90. switch(event.jbutton.button)
  91. {
  92. case 14:
  93. sPressed = false;
  94. break;
  95. }
  96. break;
  97. }
  98. }
  99. }
  100. void Application::render()
  101. {
  102. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  103. SDL_RenderClear(renderer);
  104. uiBlockPos.x = 0;
  105. for(auto i=0; i<Constants::HEIGHT; i++)
  106. {
  107. uiBlockPos.y = i*Constants::BLOCK_SIZE;
  108. SDL_RenderCopy(renderer, blocksTexture, &uiBlockTex, &uiBlockPos);
  109. }
  110. uiBlockPos.x = (Constants::WIDTH+1)*Constants::BLOCK_SIZE;
  111. for(auto i=0; i<Constants::HEIGHT; i++)
  112. {
  113. uiBlockPos.y = i*Constants::BLOCK_SIZE;
  114. SDL_RenderCopy(renderer, blocksTexture, &uiBlockTex, &uiBlockPos);
  115. }
  116. well.draw(renderer, blocksTexture);
  117. tetromino.draw(renderer, blocksTexture);
  118. SDL_RenderCopy(renderer, scoreLabelTexture, NULL, &scoreLabelRect);
  119. SDL_RenderCopy(renderer, scoreTexture, NULL, &scoreRect);
  120. SDL_RenderPresent(renderer);
  121. }
  122. void Application::update()
  123. {
  124. tetromino.update();
  125. if(score != tetromino.score)
  126. {
  127. score = tetromino.score;
  128. SDL_Surface* tmpSurface = TTF_RenderText_Solid(font, std::to_string(score).c_str(), fontColor);
  129. SDL_DestroyTexture(scoreTexture);
  130. scoreTexture = SDL_CreateTextureFromSurface(renderer, tmpSurface);
  131. scoreRect.w = tmpSurface->w;
  132. scoreRect.h = tmpSurface->h;
  133. SDL_FreeSurface(tmpSurface);
  134. }
  135. }
  136. int Application::run()
  137. {
  138. auto frameTime = 0;
  139. auto prevTicks = SDL_GetTicks();
  140. while(!quit)
  141. {
  142. handleEvents();
  143. if(sPressed)
  144. speed = 100;
  145. else
  146. speed = 250;
  147. if(SDL_GetTicks()-prevTicks >= speed)
  148. {
  149. update();
  150. prevTicks = SDL_GetTicks();
  151. }
  152. render();
  153. frameTime = (SDL_GetTicks()/1000.f - frameTime);
  154. if(frameTime < 1000.f/60.f)
  155. SDL_Delay((1000.f/60.f)-frameTime);
  156. }
  157. return 1;
  158. }