1
0

application.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <cstdint>
  2. #include <SDL3/SDL.h>
  3. #include <SDL3/SDL_stdinc.h>
  4. #include <application/application.h>
  5. #include <application/context.h>
  6. #include <backends/imgui_impl_sdl3.h>
  7. #include <backends/imgui_impl_sdlrenderer3.h>
  8. #include <component/input_listener_component.h>
  9. #include <component/position_component.h>
  10. #include <component/rect_component.h>
  11. #include <component/renderable_component.h>
  12. #include <component/velocity_component.h>
  13. #include <entt/entity/registry.hpp>
  14. #include <imgui.h>
  15. #include <meta/meta.h>
  16. #include <system/command_system.h>
  17. #include <system/hud_system.h>
  18. #include <system/imgui_system.h>
  19. #include <system/input_system.h>
  20. #include <system/movement_system.h>
  21. #include <system/rendering_system.h>
  22. namespace testbed {
  23. void application::update(entt::registry &registry, const context &ctx, const double delta) {
  24. ImGui_ImplSDLRenderer3_NewFrame();
  25. ImGui_ImplSDL3_NewFrame();
  26. ImGui::NewFrame();
  27. command_system(registry);
  28. movement_system(registry, ctx, delta);
  29. }
  30. void application::draw(entt::registry &registry, const context &context) const {
  31. SDL_SetRenderDrawColor(context, 0u, 0u, 0u, SDL_ALPHA_OPAQUE);
  32. SDL_RenderClear(context);
  33. rendering_system(registry, context);
  34. hud_system(registry, context);
  35. imgui_system(registry);
  36. ImGui::Render();
  37. ImGuiIO &io = ImGui::GetIO();
  38. SDL_SetRenderScale(context, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
  39. ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), context);
  40. SDL_RenderPresent(context);
  41. }
  42. void application::input(entt::registry &registry) {
  43. ImGuiIO &io = ImGui::GetIO();
  44. SDL_Event event{};
  45. while(SDL_PollEvent(&event)) {
  46. ImGui_ImplSDL3_ProcessEvent(&event);
  47. input_system(registry, event, quit);
  48. }
  49. }
  50. application::application()
  51. : quit{} {
  52. SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO);
  53. }
  54. application::~application() {
  55. SDL_Quit();
  56. }
  57. static void static_setup_for_dev_purposes(entt::registry &registry) {
  58. const auto entt = registry.create();
  59. // makes the entity a vessel for input
  60. registry.emplace<input_listener_component>(entt);
  61. // makes the entity able to move around
  62. registry.emplace<position_component>(entt, SDL_FPoint{400.f, 400.f});
  63. registry.emplace<velocity_component>(entt);
  64. // makes the entity visible on the screen
  65. registry.emplace<rect_component>(entt, SDL_FRect{0.f, 0.f, 20.f, 20.f});
  66. registry.emplace<renderable_component>(entt);
  67. }
  68. int application::run() {
  69. entt::registry registry{};
  70. context context{};
  71. meta_setup();
  72. static_setup_for_dev_purposes(registry);
  73. Uint64 last{SDL_GetPerformanceCounter()};
  74. Uint64 current{};
  75. double delta{};
  76. quit = false;
  77. while(!quit) {
  78. current = SDL_GetPerformanceCounter();
  79. delta = (current - last) / static_cast<double>(SDL_GetPerformanceFrequency());
  80. last = current;
  81. update(registry, context, delta);
  82. draw(registry, context);
  83. input(registry);
  84. }
  85. return 0;
  86. }
  87. } // namespace testbed