application.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <imgui.h>
  8. namespace testbed {
  9. void application::update() {
  10. ImGui_ImplSDLRenderer3_NewFrame();
  11. ImGui_ImplSDL3_NewFrame();
  12. ImGui::NewFrame();
  13. // update...
  14. }
  15. void application::draw(const context &context) const {
  16. SDL_SetRenderDrawColor(context, 0u, 0u, 0u, SDL_ALPHA_OPAQUE);
  17. SDL_RenderClear(context);
  18. // draw...
  19. ImGui::Render();
  20. ImGuiIO &io = ImGui::GetIO();
  21. SDL_SetRenderScale(context, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
  22. ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), context);
  23. SDL_RenderPresent(context);
  24. }
  25. void application::input() {
  26. ImGuiIO &io = ImGui::GetIO();
  27. SDL_Event event{};
  28. while(SDL_PollEvent(&event)) {
  29. ImGui_ImplSDL3_ProcessEvent(&event);
  30. switch(event.type) {
  31. case SDL_EVENT_KEY_DOWN:
  32. switch(event.key.key) {
  33. case SDLK_ESCAPE:
  34. quit = true;
  35. break;
  36. }
  37. break;
  38. }
  39. }
  40. }
  41. application::application()
  42. : quit{} {
  43. SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO);
  44. }
  45. application::~application() {
  46. SDL_Quit();
  47. }
  48. int application::run() {
  49. context context{};
  50. quit = false;
  51. while(!quit) {
  52. update();
  53. draw(context);
  54. input();
  55. }
  56. return 0;
  57. }
  58. } // namespace testbed