forked from BalazsJako/ImGuiColorTextEdit
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (124 loc) · 4.21 KB
/
main.cpp
File metadata and controls
156 lines (124 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// TextEditor - A syntax highlighting text editor for ImGui
// Copyright (c) 2024-2026 Johan A. Goossens. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
//
// Include files
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <SDL3/SDL.h>
#include "imgui.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlgpu3.h"
#include "editor.h"
#include "dejavu.h"
//
// main
//
int main(int, char**) {
// setup SDL
if (!SDL_Init(SDL_INIT_VIDEO)) {
printf("Error: SDL_Init(): %s\n", SDL_GetError());
return -1;
}
// create SDL window graphics context
SDL_Window* window = SDL_CreateWindow("TextEditor Example", 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY);
if (window == nullptr) {
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return -1;
}
// create GPU device
SDL_GPUDevice* gpu_device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB, true, nullptr);
if (gpu_device == nullptr) {
printf("Error: SDL_CreateGPUDevice(): %s\n", SDL_GetError());
return -1;
}
// claim window for GPU device
if (!SDL_ClaimWindowForGPUDevice(gpu_device, window)) {
printf("Error: SDL_ClaimWindowForGPUDevice(): %s\n", SDL_GetError());
return -1;
}
// setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
auto& io = ImGui::GetIO();
io.IniFilename = nullptr;
ImGui::StyleColorsDark();
// setup platform/renderer backends
ImGui_ImplSDL3_InitForSDLGPU(window);
ImGui_ImplSDLGPU3_InitInfo init_info = {};
init_info.Device = gpu_device;
init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_device, window);
init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1;
ImGui_ImplSDLGPU3_Init(&init_info);
// setup our font
ImFontConfig config;
std::memcpy(config.Name, "DejaVu", 7);
config.FontDataOwnedByAtlas = false;
config.OversampleH = 1;
config.OversampleV = 1;
io.Fonts->Clear();
io.Fonts->AddFontFromMemoryCompressedTTF((void*) &dejavu, dejavuSize, 15.0f, &config);
// main loop
Editor editor;
SDL_Event event;
while (!editor.isDone()) {
// poll and handle events (inputs, window resize, etc.)
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_QUIT) {
editor.tryToQuit();
} else if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) {
editor.tryToQuit();
}
}
if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) {
SDL_Delay(10);
continue;
}
// start the Dear ImGui frame
ImGui_ImplSDLGPU3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
// render editor
editor.render();
// render to the screen
ImGui::Render();
ImDrawData* draw_data = ImGui::GetDrawData();
const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f);
// acquire a GPU command buffer and swapchain texture
SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(gpu_device);
SDL_GPUTexture* swapchain_texture;
SDL_AcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr);
if (swapchain_texture != nullptr && !is_minimized) {
// setup and start a render pass
ImGui_ImplSDLGPU3_PrepareDrawData(draw_data, command_buffer);
SDL_GPUColorTargetInfo target_info = {};
target_info.texture = swapchain_texture;
target_info.load_op = SDL_GPU_LOADOP_CLEAR;
target_info.store_op = SDL_GPU_STOREOP_STORE;
target_info.mip_level = 0;
target_info.layer_or_depth_plane = 0;
target_info.cycle = false;
SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(command_buffer, &target_info, 1, nullptr);
// render ImGui
ImGui_ImplSDLGPU3_RenderDrawData(draw_data, command_buffer, render_pass);
SDL_EndGPURenderPass(render_pass);
}
// submit the command buffer
SDL_SubmitGPUCommandBuffer(command_buffer);
}
// cleanup
SDL_WaitForGPUIdle(gpu_device);
ImGui_ImplSDL3_Shutdown();
ImGui_ImplSDLGPU3_Shutdown();
ImGui::DestroyContext();
SDL_ReleaseWindowFromGPUDevice(gpu_device, window);
SDL_DestroyGPUDevice(gpu_device);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}