application.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <entt/entity/registry.hpp>
  8. #include <imgui.h>
  9. #include <system/imgui_system.h>
  10. #include <system/rendering_system.h>
  11. namespace testbed {
  12. void application::update(entt::registry &registry) {
  13. ImGui_ImplSDLRenderer3_NewFrame();
  14. ImGui_ImplSDL3_NewFrame();
  15. ImGui::NewFrame();
  16. // update...
  17. static_cast<void>(registry);
  18. }
  19. void application::draw(entt::registry &registry, const context &context) const {
  20. SDL_SetRenderDrawColor(context, 0u, 0u, 0u, SDL_ALPHA_OPAQUE);
  21. SDL_RenderClear(context);
  22. rendering_system(registry, context);
  23. imgui_system(registry);
  24. ImGui::Render();
  25. ImGuiIO &io = ImGui::GetIO();
  26. SDL_SetRenderScale(context, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
  27. ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), context);
  28. SDL_RenderPresent(context);
  29. }
  30. void application::input() {
  31. ImGuiIO &io = ImGui::GetIO();
  32. SDL_Event event{};
  33. while(SDL_PollEvent(&event)) {
  34. ImGui_ImplSDL3_ProcessEvent(&event);
  35. switch(event.type) {
  36. case SDL_EVENT_KEY_DOWN:
  37. switch(event.key.key) {
  38. case SDLK_ESCAPE:
  39. quit = true;
  40. break;
  41. }
  42. break;
  43. }
  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<double>(entt, 1.2);
  56. registry.emplace<int>(entt, 3);
  57. }
  58. int application::run() {
  59. entt::registry registry{};
  60. context context{};
  61. static_setup_for_dev_purposes(registry);
  62. quit = false;
  63. while(!quit) {
  64. update(registry);
  65. draw(registry, context);
  66. input();
  67. }
  68. return 0;
  69. }
  70. } // namespace testbed