main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <iostream>
  2. #include <SDL.h>
  3. #include <cmath>
  4. #include "texturer.h"
  5. #include "Input.h"
  6. using namespace std;
  7. int main() {
  8. // Start
  9. cout << "SDL2 app is loading..." << endl;
  10. // Useful data
  11. const int SCREEN_WIDTH = 1200;
  12. const int SCREEN_HEIGHT = 700;
  13. // Init video
  14. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  15. logSDLError(cout, "SDL_Init(SDL_INIT_VIDEO)");
  16. return 1;
  17. }
  18. // Opening window
  19. SDL_Window *win = SDL_CreateWindow("Hello SDL!",
  20. SDL_WINDOWPOS_UNDEFINED,
  21. SDL_WINDOWPOS_UNDEFINED,
  22. SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  23. if (win == nullptr) {
  24. logSDLError(cout, "SDL_CreateWindow");
  25. SDL_Quit();
  26. return 1;
  27. }
  28. // Create a renderer
  29. SDL_Renderer *ren = SDL_CreateRenderer(win, -1,
  30. SDL_RENDERER_ACCELERATED |
  31. SDL_RENDERER_PRESENTVSYNC);
  32. if (ren == nullptr) {
  33. SDL_DestroyWindow(win);
  34. logSDLError(cout, "SDL_CreateRenderer");
  35. SDL_Quit();
  36. return 1;
  37. }
  38. // Loading
  39. const string file0("cruiser_on.png");
  40. const string file1("cruiser.png");
  41. const string file2("cruiser_ftl.png");
  42. const string file3("base.jpg");
  43. const string file4("pierres.bmp");
  44. SDL_Texture *cruiser_on(loadTexture(file0, ren));
  45. SDL_Texture *cruiser_off(loadTexture(file1, ren));
  46. SDL_Texture *cruiser_ftl(loadTexture(file2, ren));
  47. SDL_Texture *background1(loadTexture(file3, ren));
  48. SDL_Texture *background2(loadTexture(file4, ren));
  49. if (cruiser_on == nullptr || cruiser_off == nullptr || cruiser_ftl == nullptr ||
  50. background1 == nullptr || background2 == nullptr) {
  51. SDL_DestroyTexture(cruiser_on);
  52. SDL_DestroyTexture(cruiser_off);
  53. SDL_DestroyTexture(cruiser_ftl);
  54. SDL_DestroyTexture(background1);
  55. SDL_DestroyTexture(background2);
  56. SDL_DestroyRenderer(ren);
  57. SDL_DestroyWindow(win);
  58. SDL_Quit();
  59. return 1;
  60. }
  61. //Set standard alpha blending
  62. SDL_SetTextureBlendMode(background1, SDL_BLENDMODE_BLEND);
  63. SDL_SetTextureBlendMode(background2, SDL_BLENDMODE_BLEND);
  64. // Events
  65. Input myInput;
  66. // Time
  67. Uint32 prevTime(0);
  68. Uint32 waitTime(1000 / 60);
  69. // Drawing
  70. SDL_Texture *cruiser(cruiser_off);
  71. SDL_Texture *background(background1);
  72. int backW, backH;
  73. SDL_QueryTexture(background, NULL, NULL, &backW, &backH);
  74. double angle(0.0);
  75. float bx(0), by(0);
  76. float speed(0.0f);
  77. Uint8 ftlLevel(0);
  78. bool ftlTriggered(false);
  79. while (!myInput.isFinished()) {
  80. // Update events
  81. myInput.updateEvents();
  82. // Movement
  83. if (myInput.getMouseKey(1)) {
  84. cruiser = cruiser_on;
  85. speed += 0.05f;
  86. if (ftlLevel > 0)
  87. ftlLevel--;
  88. } else if (myInput.getMouseKey(3)) {
  89. if (ftlLevel < 255) {
  90. ftlLevel++;
  91. } else if (!ftlTriggered) {
  92. ftlTriggered = true;
  93. if (background == background1)
  94. background = background2;
  95. else
  96. background = background1;
  97. SDL_QueryTexture(background, NULL, NULL, &backW, &backH);
  98. }
  99. cruiser = cruiser_ftl;
  100. } else {
  101. cruiser = cruiser_off;
  102. speed *= 0.99f;
  103. if (ftlLevel > 0)
  104. ftlLevel--;
  105. else
  106. ftlTriggered = false;
  107. }
  108. by += speed * sin(-angle * M_PI_2 / 90.0);
  109. bx -= speed * cos(-angle * M_PI_2 / 90.0);
  110. if (by < -backH)
  111. by += backH;
  112. if (by > 0)
  113. by -= backH;
  114. if (bx < -backW)
  115. bx += backW;
  116. if (bx > 0)
  117. bx -= backW;
  118. // Reset renderer
  119. SDL_RenderClear(ren);
  120. // Background
  121. SDL_SetTextureAlphaMod(background, (Uint8) 255 - ftlLevel);
  122. for (int x((int) bx); x < SCREEN_WIDTH; x += backW) {
  123. for (int y((int) by); y < SCREEN_HEIGHT; y += backH) {
  124. renderTexture(background, ren, x, y);
  125. }
  126. }
  127. // Foreground
  128. SDL_SetTextureColorMod(cruiser, (Uint8) 255 - ftlLevel, (Uint8) 255 - ftlLevel, 255);
  129. angle = myInput.getX() * 360.0 / SCREEN_WIDTH + 90.0;
  130. renderTexture(cruiser, ren, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, angle, true);
  131. // Render
  132. SDL_RenderPresent(ren);
  133. // Pause
  134. if (SDL_GetTicks() < prevTime + waitTime)
  135. SDL_Delay(waitTime + prevTime - SDL_GetTicks());
  136. prevTime = SDL_GetTicks();
  137. }
  138. // End
  139. SDL_DestroyTexture(background1);
  140. SDL_DestroyTexture(background2);
  141. SDL_DestroyTexture(cruiser_on);
  142. SDL_DestroyTexture(cruiser_off);
  143. SDL_DestroyTexture(cruiser_ftl);
  144. SDL_DestroyRenderer(ren);
  145. SDL_DestroyWindow(win);
  146. SDL_Quit();
  147. return 0;
  148. }