app.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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->part = new particle(50, 100, 1.0);
  24. }
  25. void application::input()
  26. {
  27. SDL_Event event;
  28. while(SDL_PollEvent(&event))
  29. {
  30. //ImGui_ImplSDL2_ProcessEvent(&event);
  31. //ImGuiIO &io = ImGui::GetIO();
  32. switch(event.type)
  33. {
  34. case SDL_QUIT:
  35. this->running = false;
  36. break;
  37. case SDL_KEYDOWN:
  38. if (event.key.keysym.sym == SDLK_ESCAPE)
  39. {
  40. this->running = false;
  41. }
  42. break;
  43. }
  44. }
  45. }
  46. void application::update()
  47. {
  48. /* Let's make sure we are running at the expected framerate */
  49. uint32_t now = SDL_GetTicks();
  50. int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
  51. if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
  52. {
  53. SDL_Delay(timeToWait);
  54. }
  55. double deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
  56. /* clamp maximum deltatime to avoid weird behaviours */
  57. if (deltaTime > 0.016)
  58. {
  59. deltaTime = 0.016;
  60. }
  61. this->millisecsPreviousFrame = SDL_GetTicks();
  62. this->part->acceleration = vec2(2.0 * PIXELS_PER_METER, 9.8 * PIXELS_PER_METER);
  63. // Integrate the change
  64. this->part->velocity += (this->part->acceleration * deltaTime);
  65. this->part->position += (this->part->velocity * deltaTime);
  66. // check the particles position and keep the particule in the window.
  67. if ( ((this->part->position.y - this->part->radius) <= 0) ||
  68. ((this->part->position.y + this->part->radius) >= graphics::height()))
  69. {
  70. this->part->velocity.y = -this->part->velocity.y;
  71. }
  72. if ( ((this->part->position.x - this->part->radius) <= 0) ||
  73. ((this->part->position.x + this->part->radius) >= graphics::width()))
  74. {
  75. this->part->velocity.x = -this->part->velocity.x;
  76. }
  77. }
  78. void application::render()
  79. {
  80. graphics::clearScreen(0xFF056263);
  81. graphics::draw::fillCircle(this->part->position.x, this->part->position.y, 4, 0xFFFFFFFF);
  82. graphics::renderFrame();
  83. }
  84. void application::destroy()
  85. {
  86. // Nothing for now.
  87. graphics::closeWindow();
  88. delete this->part;
  89. }