app.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. this->millisecsPreviousFrame = SDL_GetTicks();
  57. this->part->velocity = vec2(100 * deltaTime, 30 * deltaTime);
  58. this->part->position += (this->part->velocity);
  59. }
  60. void application::render()
  61. {
  62. graphics::clearScreen(0xFF056263);
  63. graphics::draw::fillCircle(this->part->position.x, this->part->position.y, 4, 0xFFFFFFFF);
  64. graphics::renderFrame();
  65. }
  66. void application::destroy()
  67. {
  68. // Nothing for now.
  69. graphics::closeWindow();
  70. delete this->part;
  71. }