| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | #include <cstdio> |
| 13 | #include <cstdlib> |
| 14 | #include <cstring> |
| 15 | |
| 16 | #include <SDL3/SDL.h> |
| 17 | |
| 18 | #include "imgui.h" |
| 19 | #include "imgui_impl_sdl3.h" |
| 20 | #include "imgui_impl_sdlgpu3.h" |
| 21 | |
| 22 | #include "editor.h" |
| 23 | #include "dejavu.h" |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | int main(int, char**) { |
| 31 | |
| 32 | if (!SDL_Init(SDL_INIT_VIDEO)) { |
| 33 | printf("Error: SDL_Init(): %s\n", SDL_GetError()); |
| 34 | return -1; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | SDL_Window* window = SDL_CreateWindow("TextEditor Example", 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); |
| 39 | |
| 40 | if (window == nullptr) { |
| 41 | printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | SDL_GPUDevice* gpu_device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB, true, nullptr); |
| 47 | |
| 48 | if (gpu_device == nullptr) { |
| 49 | printf("Error: SDL_CreateGPUDevice(): %s\n", SDL_GetError()); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | if (!SDL_ClaimWindowForGPUDevice(gpu_device, window)) { |
| 55 | printf("Error: SDL_ClaimWindowForGPUDevice(): %s\n", SDL_GetError()); |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | IMGUI_CHECKVERSION(); |
| 61 | ImGui::CreateContext(); |
| 62 | auto& io = ImGui::GetIO(); |
| 63 | io.IniFilename = nullptr; |
| 64 | ImGui::StyleColorsDark(); |
| 65 | |
| 66 | |
| 67 | ImGui_ImplSDL3_InitForSDLGPU(window); |
| 68 | ImGui_ImplSDLGPU3_InitInfo init_info = {}; |
| 69 | init_info.Device = gpu_device; |
| 70 | init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_device, window); |
| 71 | init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1; |
| 72 | ImGui_ImplSDLGPU3_Init(&init_info); |
| 73 | |
| 74 | |
| 75 | ImFontConfig config; |
| 76 | std::memcpy(config.Name, "DejaVu", 7); |
| 77 | config.FontDataOwnedByAtlas = false; |
| 78 | config.OversampleH = 1; |
| 79 | config.OversampleV = 1; |
| 80 | io.Fonts->Clear(); |
| 81 | io.Fonts->AddFontFromMemoryCompressedTTF((void*) &dejavu, dejavuSize, 15.0f, &config); |
| 82 | |
| 83 | |
| 84 | Editor editor; |
| 85 | SDL_Event event; |
| 86 | |
| 87 | while (!editor.isDone()) { |
| 88 | |
| 89 | while (SDL_PollEvent(&event)) { |
| 90 | ImGui_ImplSDL3_ProcessEvent(&event); |
| 91 | |
| 92 | if (event.type == SDL_EVENT_QUIT) { |
| 93 | editor.tryToQuit(); |
| 94 | |
| 95 | } else if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) { |
| 96 | editor.tryToQuit(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) { |
| 101 | SDL_Delay(10); |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | ImGui_ImplSDLGPU3_NewFrame(); |
| 107 | ImGui_ImplSDL3_NewFrame(); |
| 108 | ImGui::NewFrame(); |
| 109 | |
| 110 | |
| 111 | editor.render(); |
| 112 | |
| 113 | |
| 114 | ImGui::Render(); |
| 115 | ImDrawData* draw_data = ImGui::GetDrawData(); |
| 116 | const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f); |
| 117 | |
| 118 | |
| 119 | SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(gpu_device); |
| 120 | SDL_GPUTexture* swapchain_texture; |
| 121 | SDL_AcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr); |
| 122 | |
| 123 | if (swapchain_texture != nullptr && !is_minimized) { |
| 124 | |
| 125 | ImGui_ImplSDLGPU3_PrepareDrawData(draw_data, command_buffer); |
| 126 | |
| 127 | SDL_GPUColorTargetInfo target_info = {}; |
| 128 | target_info.texture = swapchain_texture; |
| 129 | target_info.load_op = SDL_GPU_LOADOP_CLEAR; |
| 130 | target_info.store_op = SDL_GPU_STOREOP_STORE; |
| 131 | target_info.mip_level = 0; |
| 132 | target_info.layer_or_depth_plane = 0; |
| 133 | target_info.cycle = false; |
| 134 | SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(command_buffer, &target_info, 1, nullptr); |
| 135 | |
| 136 | |
| 137 | ImGui_ImplSDLGPU3_RenderDrawData(draw_data, command_buffer, render_pass); |
| 138 | SDL_EndGPURenderPass(render_pass); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | SDL_SubmitGPUCommandBuffer(command_buffer); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | SDL_WaitForGPUIdle(gpu_device); |
| 147 | ImGui_ImplSDL3_Shutdown(); |
| 148 | ImGui_ImplSDLGPU3_Shutdown(); |
| 149 | ImGui::DestroyContext(); |
| 150 | |
| 151 | SDL_ReleaseWindowFromGPUDevice(gpu_device, window); |
| 152 | SDL_DestroyGPUDevice(gpu_device); |
| 153 | SDL_DestroyWindow(window); |
| 154 | SDL_Quit(); |
| 155 | return 0; |
| 156 | } |