app.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * 2D Physic Engine
  3. * app.cpp: basic application handling.
  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 <app.h>
  10. #include <graphics.h>
  11. #include <imgui.h>
  12. #include <imgui_sdl.h>
  13. #include <imgui_impl_sdl.h>
  14. #include <physics/constants.h>
  15. #include <physics/particle.h>
  16. #include <physics/vec2.h>
  17. #include <physics/force.h>
  18. #include <SDL.h>
  19. void application::parseParameters(int argc, char *argv[])
  20. {
  21. // Nothing to do for now
  22. }
  23. void application::setup()
  24. {
  25. this->running = graphics::openWindow();
  26. this->millisecsPreviousFrame = SDL_GetTicks();
  27. this->particles.push_back(new particle(50, 100, 1.0, 4));
  28. this->particles.push_back(new particle(200, 100, 3.0, 12));
  29. this->liquid.x = 0;
  30. this->liquid.y = graphics::height() / 2;
  31. this->liquid.w = graphics::width();
  32. this->liquid.h = graphics::height() / 2;
  33. }
  34. void application::input()
  35. {
  36. SDL_Event event;
  37. while(SDL_PollEvent(&event))
  38. {
  39. //ImGui_ImplSDL2_ProcessEvent(&event);
  40. //ImGuiIO &io = ImGui::GetIO();
  41. switch(event.type)
  42. {
  43. case SDL_QUIT:
  44. this->running = false;
  45. break;
  46. case SDL_KEYDOWN:
  47. if (event.key.keysym.sym == SDLK_ESCAPE)
  48. {
  49. this->running = false;
  50. }
  51. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = -50 * PIXELS_PER_METER;
  52. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 50 * PIXELS_PER_METER;
  53. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 50 * PIXELS_PER_METER;
  54. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = -50 * PIXELS_PER_METER;
  55. break;
  56. case SDL_KEYUP:
  57. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = 0;
  58. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 0;
  59. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 0;
  60. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = 0;
  61. break;
  62. case SDL_MOUSEBUTTONDOWN:
  63. if (event.button.button == SDL_BUTTON_LEFT)
  64. {
  65. int x, y;
  66. SDL_GetMouseState(&x, &y);
  67. particle *part = new particle(x, y, 1., 5);
  68. this->particles.push_back(part);
  69. }
  70. break;
  71. }
  72. }
  73. }
  74. void application::syncAndDeltatime()
  75. {
  76. waitTime.start();
  77. /* Let's make sure we are running at the expected framerate */
  78. uint32_t now = SDL_GetTicks();
  79. int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
  80. if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
  81. {
  82. SDL_Delay(timeToWait);
  83. }
  84. this->deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
  85. /* clamp maximum deltatime to avoid weird behaviours */
  86. if (this->deltaTime > 0.016)
  87. {
  88. this->deltaTime = 0.016;
  89. }
  90. this->millisecsPreviousFrame = SDL_GetTicks();
  91. waitTime.stop();
  92. }
  93. void application::update()
  94. {
  95. vec2 wind = vec2(0.2 * PIXELS_PER_METER, 0);
  96. vec2 weight = vec2(0, 9.81 * PIXELS_PER_METER);
  97. this->syncAndDeltatime();
  98. updateTime.start();
  99. for (auto part:this->particles)
  100. {
  101. //part->addForce(wind);
  102. part->addForce(this->pushForce);
  103. part->addForce(weight * part->mass);
  104. // Apply drag if in liquid.
  105. if (part->position.y >= this->liquid.y)
  106. {
  107. vec2 drag = force::generateDragForce(*part, 0.01);
  108. part->addForce(drag);
  109. }
  110. part->integrate(deltaTime);
  111. // check the particles position and keep the particle in the window.
  112. if ( ((part->position.y - part->radius) <= 0) ||
  113. ((part->position.y + part->radius) >= graphics::height()))
  114. {
  115. if ((part->position.y - part->radius) <= 0)
  116. {
  117. part->position.y = part->radius;
  118. }
  119. else
  120. {
  121. part->position.y = graphics::height() - part->radius;
  122. }
  123. part->velocity.y = -part->velocity.y;
  124. }
  125. if ( ((part->position.x - part->radius) <= 0) ||
  126. ((part->position.x + part->radius) >= graphics::width()))
  127. {
  128. if ((part->position.x - part->radius) <= 0)
  129. {
  130. part->position.x = part->radius;
  131. }
  132. else
  133. {
  134. part->position.x = graphics::width() - part->radius;
  135. }
  136. part->velocity.x = -part->velocity.x;
  137. }
  138. }
  139. updateTime.stop();
  140. }
  141. void application::render()
  142. {
  143. renderTime.start();
  144. graphics::clearScreen(0xFF056263);
  145. // Draw the liquid
  146. graphics::draw::fillRect(this->liquid.x + this->liquid.w / 2, this->liquid.y + this->liquid.h / 2,
  147. this->liquid.w, this->liquid.h, 0xFF633713);
  148. for (auto part:this->particles)
  149. {
  150. graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFFFFFFF);
  151. }
  152. graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
  153. graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
  154. graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
  155. graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
  156. graphics::renderFrame();
  157. renderTime.stop();
  158. }
  159. void application::destroy()
  160. {
  161. // Nothing for now.
  162. for (auto part:this->particles)
  163. {
  164. delete part;
  165. }
  166. graphics::closeWindow();
  167. }