1
0

application.cpp 2.9 KB

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