app.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. }
  30. void application::input()
  31. {
  32. SDL_Event event;
  33. while(SDL_PollEvent(&event))
  34. {
  35. //ImGui_ImplSDL2_ProcessEvent(&event);
  36. //ImGuiIO &io = ImGui::GetIO();
  37. switch(event.type)
  38. {
  39. case SDL_QUIT:
  40. this->running = false;
  41. break;
  42. case SDL_KEYDOWN:
  43. if (event.key.keysym.sym == SDLK_ESCAPE)
  44. {
  45. this->running = false;
  46. }
  47. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = -50 * PIXELS_PER_METER;
  48. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 50 * PIXELS_PER_METER;
  49. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 50 * PIXELS_PER_METER;
  50. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = -50 * PIXELS_PER_METER;
  51. break;
  52. case SDL_KEYUP:
  53. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = 0;
  54. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 0;
  55. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 0;
  56. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = 0;
  57. break;
  58. case SDL_MOUSEBUTTONDOWN:
  59. if (event.button.button == SDL_BUTTON_LEFT)
  60. {
  61. int x, y;
  62. SDL_GetMouseState(&x, &y);
  63. particle *part = new particle(x, y, 1., 5);
  64. this->particles.push_back(part);
  65. }
  66. break;
  67. }
  68. }
  69. }
  70. void application::syncAndDeltatime()
  71. {
  72. waitTime.start();
  73. /* Let's make sure we are running at the expected framerate */
  74. uint32_t now = SDL_GetTicks();
  75. int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
  76. if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
  77. {
  78. SDL_Delay(timeToWait);
  79. }
  80. this->deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
  81. /* clamp maximum deltatime to avoid weird behaviours */
  82. if (this->deltaTime > 0.016)
  83. {
  84. this->deltaTime = 0.016;
  85. }
  86. this->millisecsPreviousFrame = SDL_GetTicks();
  87. waitTime.stop();
  88. }
  89. void application::update()
  90. {
  91. this->syncAndDeltatime();
  92. updateTime.start();
  93. for (auto part:this->particles)
  94. {
  95. part->addForce(this->pushForce);
  96. vec2 friction = force::generateFrictionForce(*part, 10 * PIXELS_PER_METER);
  97. part->addForce(friction);
  98. part->integrate(deltaTime);
  99. // check the particles position and keep the particle in the window.
  100. if ( ((part->position.y - part->radius) <= 0) ||
  101. ((part->position.y + part->radius) >= graphics::height()))
  102. {
  103. if ((part->position.y - part->radius) <= 0)
  104. {
  105. part->position.y = part->radius;
  106. }
  107. else
  108. {
  109. part->position.y = graphics::height() - part->radius;
  110. }
  111. part->velocity.y = -part->velocity.y;
  112. }
  113. if ( ((part->position.x - part->radius) <= 0) ||
  114. ((part->position.x + part->radius) >= graphics::width()))
  115. {
  116. if ((part->position.x - part->radius) <= 0)
  117. {
  118. part->position.x = part->radius;
  119. }
  120. else
  121. {
  122. part->position.x = graphics::width() - part->radius;
  123. }
  124. part->velocity.x = -part->velocity.x;
  125. }
  126. }
  127. updateTime.stop();
  128. }
  129. void application::render()
  130. {
  131. renderTime.start();
  132. graphics::clearScreen(0xFF056263);
  133. for (auto part:this->particles)
  134. {
  135. graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFFFFFFF);
  136. }
  137. graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
  138. graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
  139. graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
  140. graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
  141. graphics::renderFrame();
  142. renderTime.stop();
  143. }
  144. void application::destroy()
  145. {
  146. // Nothing for now.
  147. for (auto part:this->particles)
  148. {
  149. delete part;
  150. }
  151. graphics::closeWindow();
  152. }