application.cpp 2.7 KB

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