application.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <entt/entity/registry.hpp>
  12. #include <imgui.h>
  13. #include <system/hud_system.h>
  14. #include <system/imgui_system.h>
  15. #include <system/input_system.h>
  16. #include <system/rendering_system.h>
  17. namespace testbed {
  18. void application::update(entt::registry &registry) {
  19. ImGui_ImplSDLRenderer3_NewFrame();
  20. ImGui_ImplSDL3_NewFrame();
  21. ImGui::NewFrame();
  22. // update...
  23. static_cast<void>(registry);
  24. }
  25. void application::draw(entt::registry &registry, const context &context) const {
  26. SDL_SetRenderDrawColor(context, 0u, 0u, 0u, SDL_ALPHA_OPAQUE);
  27. SDL_RenderClear(context);
  28. rendering_system(registry, context);
  29. hud_system(registry, context);
  30. imgui_system(registry);
  31. ImGui::Render();
  32. ImGuiIO &io = ImGui::GetIO();
  33. SDL_SetRenderScale(context, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
  34. ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), context);
  35. SDL_RenderPresent(context);
  36. }
  37. void application::input(entt::registry &registry) {
  38. ImGuiIO &io = ImGui::GetIO();
  39. SDL_Event event{};
  40. while(SDL_PollEvent(&event)) {
  41. ImGui_ImplSDL3_ProcessEvent(&event);
  42. input_system(registry, event, quit);
  43. }
  44. }
  45. application::application()
  46. : quit{} {
  47. SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO);
  48. }
  49. application::~application() {
  50. SDL_Quit();
  51. }
  52. static void static_setup_for_dev_purposes(entt::registry &registry) {
  53. const auto entt = registry.create();
  54. registry.emplace<double>(entt, 1.2);
  55. registry.emplace<int>(entt, 3);
  56. registry.emplace<input_listener_component>(entt);
  57. registry.emplace<position_component>(entt, SDL_FPoint{400.f, 400.f});
  58. registry.emplace<rect_component>(entt, SDL_FRect{0.f, 0.f, 20.f, 20.f});
  59. registry.emplace<renderable_component>(entt);
  60. }
  61. int application::run() {
  62. entt::registry registry{};
  63. context context{};
  64. static_setup_for_dev_purposes(registry);
  65. quit = false;
  66. while(!quit) {
  67. update(registry);
  68. draw(registry, context);
  69. input(registry);
  70. }
  71. return 0;
  72. }
  73. } // namespace testbed