application.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/command_system.h>
  15. #include <system/hud_system.h>
  16. #include <system/imgui_system.h>
  17. #include <system/input_system.h>
  18. #include <system/movement_system.h>
  19. #include <system/rendering_system.h>
  20. namespace testbed {
  21. void application::update(entt::registry &registry) {
  22. ImGui_ImplSDLRenderer3_NewFrame();
  23. ImGui_ImplSDL3_NewFrame();
  24. ImGui::NewFrame();
  25. command_system(registry);
  26. movement_system(registry);
  27. }
  28. void application::draw(entt::registry &registry, const context &context) const {
  29. SDL_SetRenderDrawColor(context, 0u, 0u, 0u, SDL_ALPHA_OPAQUE);
  30. SDL_RenderClear(context);
  31. rendering_system(registry, context);
  32. hud_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. registry.emplace<input_listener_component>(entt);
  58. registry.emplace<position_component>(entt, SDL_FPoint{400.f, 400.f});
  59. registry.emplace<rect_component>(entt, SDL_FRect{0.f, 0.f, 20.f, 20.f});
  60. registry.emplace<renderable_component>(entt);
  61. }
  62. int application::run() {
  63. entt::registry registry{};
  64. context context{};
  65. meta_setup();
  66. static_setup_for_dev_purposes(registry);
  67. quit = false;
  68. while(!quit) {
  69. update(registry);
  70. draw(registry, context);
  71. input(registry);
  72. }
  73. return 0;
  74. }
  75. } // namespace testbed