1
0

application.cpp 2.4 KB

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