app.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. }
  63. }
  64. }
  65. void application::syncAndDeltatime()
  66. {
  67. waitTime.start();
  68. /* Let's make sure we are running at the expected framerate */
  69. uint32_t now = SDL_GetTicks();
  70. int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
  71. if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
  72. {
  73. SDL_Delay(timeToWait);
  74. }
  75. this->deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
  76. /* clamp maximum deltatime to avoid weird behaviours */
  77. if (this->deltaTime > 0.016)
  78. {
  79. this->deltaTime = 0.016;
  80. }
  81. this->millisecsPreviousFrame = SDL_GetTicks();
  82. waitTime.stop();
  83. }
  84. void application::update()
  85. {
  86. vec2 wind = vec2(0.2 * PIXELS_PER_METER, 0);
  87. vec2 weight = vec2(0, 9.81 * PIXELS_PER_METER);
  88. this->syncAndDeltatime();
  89. updateTime.start();
  90. for (auto part:this->particles)
  91. {
  92. //part->addForce(wind);
  93. part->addForce(this->pushForce);
  94. part->addForce(weight * part->mass);
  95. // Apply drag if in liquid.
  96. if (part->position.y >= this->liquid.y)
  97. {
  98. vec2 drag = force::generateDragForce(*part, 0.01);
  99. part->addForce(drag);
  100. }
  101. part->integrate(deltaTime);
  102. // check the particles position and keep the particle in the window.
  103. if ( ((part->position.y - part->radius) <= 0) ||
  104. ((part->position.y + part->radius) >= graphics::height()))
  105. {
  106. part->velocity.y = -part->velocity.y;
  107. }
  108. if ( ((part->position.x - part->radius) <= 0) ||
  109. ((part->position.x + part->radius) >= graphics::width()))
  110. {
  111. part->velocity.x = -part->velocity.x;
  112. }
  113. }
  114. updateTime.stop();
  115. }
  116. void application::render()
  117. {
  118. renderTime.start();
  119. graphics::clearScreen(0xFF056263);
  120. for (auto part:this->particles)
  121. {
  122. graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFFFFFFF);
  123. }
  124. graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
  125. graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
  126. graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
  127. graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
  128. graphics::renderFrame();
  129. renderTime.stop();
  130. }
  131. void application::destroy()
  132. {
  133. // Nothing for now.
  134. for (auto part:this->particles)
  135. {
  136. delete part;
  137. }
  138. graphics::closeWindow();
  139. }