app.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. void application::parseParameters(int argc, char *argv[])
  16. {
  17. // Nothing to do for now
  18. }
  19. void application::setup()
  20. {
  21. this->running = graphics::openWindow();
  22. this->millisecsPreviousFrame = SDL_GetTicks();
  23. this->particles.push_back(new particle(50, 100, 1.0, 4));
  24. //this->particles.push_back(new particle(200, 100, 3.0, 12));
  25. }
  26. void application::input()
  27. {
  28. SDL_Event event;
  29. while(SDL_PollEvent(&event))
  30. {
  31. //ImGui_ImplSDL2_ProcessEvent(&event);
  32. //ImGuiIO &io = ImGui::GetIO();
  33. switch(event.type)
  34. {
  35. case SDL_QUIT:
  36. this->running = false;
  37. break;
  38. case SDL_KEYDOWN:
  39. if (event.key.keysym.sym == SDLK_ESCAPE)
  40. {
  41. this->running = false;
  42. }
  43. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = -50 * PIXELS_PER_METER;
  44. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 50 * PIXELS_PER_METER;
  45. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 50 * PIXELS_PER_METER;
  46. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = -50 * PIXELS_PER_METER;
  47. break;
  48. case SDL_KEYUP:
  49. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = 0;
  50. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 0;
  51. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 0;
  52. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = 0;
  53. break;
  54. }
  55. }
  56. }
  57. void application::syncAndDeltatime()
  58. {
  59. waitTime.start();
  60. /* Let's make sure we are running at the expected framerate */
  61. uint32_t now = SDL_GetTicks();
  62. int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
  63. if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
  64. {
  65. SDL_Delay(timeToWait);
  66. }
  67. this->deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
  68. /* clamp maximum deltatime to avoid weird behaviours */
  69. if (this->deltaTime > 0.016)
  70. {
  71. this->deltaTime = 0.016;
  72. }
  73. this->millisecsPreviousFrame = SDL_GetTicks();
  74. waitTime.stop();
  75. }
  76. void application::update()
  77. {
  78. vec2 wind = vec2(0.2 * PIXELS_PER_METER, 0);
  79. vec2 weight = vec2(0, 9.81 * PIXELS_PER_METER);
  80. this->syncAndDeltatime();
  81. updateTime.start();
  82. for (auto part:this->particles)
  83. {
  84. part->addForce(wind);
  85. part->addForce(this->pushForce);
  86. part->addForce(weight * part->mass);
  87. part->integrate(deltaTime);
  88. // check the particles position and keep the particle in the window.
  89. if ( ((part->position.y - part->radius) <= 0) ||
  90. ((part->position.y + part->radius) >= graphics::height()))
  91. {
  92. part->velocity.y = -part->velocity.y;
  93. }
  94. if ( ((part->position.x - part->radius) <= 0) ||
  95. ((part->position.x + part->radius) >= graphics::width()))
  96. {
  97. part->velocity.x = -part->velocity.x;
  98. }
  99. }
  100. updateTime.stop();
  101. }
  102. void application::render()
  103. {
  104. renderTime.start();
  105. graphics::clearScreen(0xFF056263);
  106. for (auto part:this->particles)
  107. {
  108. graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFFFFFFF);
  109. }
  110. graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
  111. graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
  112. graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
  113. graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
  114. graphics::renderFrame();
  115. renderTime.stop();
  116. }
  117. void application::destroy()
  118. {
  119. // Nothing for now.
  120. for (auto part:this->particles)
  121. {
  122. delete part;
  123. }
  124. graphics::closeWindow();
  125. }