4.2 KB、156行 生 Blame 履歴
1// TextEditor - A syntax highlighting text editor for ImGui
2// Copyright (c) 2024-2026 Johan A. Goossens. All rights reserved.
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7
8//
9// Include files
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// main
28//
29
30int main(int, char**) {
31 // setup SDL
32 if (!SDL_Init(SDL_INIT_VIDEO)) {
33 printf("Error: SDL_Init(): %s\n", SDL_GetError());
34 return -1;
35 }
36
37 // create SDL window graphics context
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 // create GPU device
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 // claim window for GPU device
54 if (!SDL_ClaimWindowForGPUDevice(gpu_device, window)) {
55 printf("Error: SDL_ClaimWindowForGPUDevice(): %s\n", SDL_GetError());
56 return -1;
57 }
58
59 // setup Dear ImGui context
60 IMGUI_CHECKVERSION();
61 ImGui::CreateContext();
62 auto& io = ImGui::GetIO();
63 io.IniFilename = nullptr;
64 ImGui::StyleColorsDark();
65
66 // setup platform/renderer backends
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 // setup our font
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 // main loop
84 Editor editor;
85 SDL_Event event;
86
87 while (!editor.isDone()) {
88 // poll and handle events (inputs, window resize, etc.)
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 // start the Dear ImGui frame
106 ImGui_ImplSDLGPU3_NewFrame();
107 ImGui_ImplSDL3_NewFrame();
108 ImGui::NewFrame();
109
110 // render editor
111 editor.render();
112
113 // render to the screen
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 // acquire a GPU command buffer and swapchain texture
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 // setup and start a render pass
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 // render ImGui
137 ImGui_ImplSDLGPU3_RenderDrawData(draw_data, command_buffer, render_pass);
138 SDL_EndGPURenderPass(render_pass);
139 }
140
141 // submit the command buffer
142 SDL_SubmitGPUCommandBuffer(command_buffer);
143 }
144
145 // cleanup
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}