add imgui
This commit is contained in:
+51
-1
@@ -1,6 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_sdl3.h>
|
||||
#include <imgui_impl_sdlgpu3.h>
|
||||
|
||||
#include "defer.h"
|
||||
#include "log.h"
|
||||
@@ -781,8 +784,34 @@ int main(int argc, char **argv) {
|
||||
V2 quad_pos = { 0.101f, 0.101f };
|
||||
V2 quad_size = { 0.1f, 0.1f };
|
||||
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||
// io.ConfigFlags |= ImGuiConfigFlags_IsSRGB;
|
||||
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
ImGui_ImplSDL3_InitForSDLGPU(window);
|
||||
ImGui_ImplSDLGPU3_InitInfo imgui_init_info = {
|
||||
.Device = device,
|
||||
.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(device, window),
|
||||
.MSAASamples = SDL_GPU_SAMPLECOUNT_1,
|
||||
};
|
||||
ImGui_ImplSDLGPU3_Init(&imgui_init_info);
|
||||
|
||||
bool show_demo_window = true;
|
||||
|
||||
// MSG Message;
|
||||
while (Running) {
|
||||
ImGui_ImplSDLGPU3_NewFrame();
|
||||
ImGui_ImplSDL3_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
if (show_demo_window)
|
||||
ImGui::ShowDemoWindow(&show_demo_window);
|
||||
|
||||
SDL_GPUCommandBuffer *command_buffer = SDL_AcquireGPUCommandBuffer(device);
|
||||
if (!command_buffer) {
|
||||
log_error("Failed to acquire gpu command buffer (%s). Exiting.", SDL_GetError());
|
||||
@@ -799,6 +828,8 @@ int main(int argc, char **argv) {
|
||||
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
ImGui_ImplSDL3_ProcessEvent(&event);
|
||||
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_QUIT: {
|
||||
Running = false;
|
||||
@@ -810,6 +841,9 @@ int main(int argc, char **argv) {
|
||||
} break;
|
||||
|
||||
case SDL_EVENT_KEY_DOWN: {
|
||||
if (io.WantCaptureKeyboard)
|
||||
continue;
|
||||
|
||||
if (event.key.key == SDLK_UP || event.key.key == SDLK_W) {
|
||||
player.pos_y = clamp(0, player.pos_y - 1, map_height - 1);
|
||||
}
|
||||
@@ -836,6 +870,9 @@ int main(int argc, char **argv) {
|
||||
} break;
|
||||
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
|
||||
if (io.WantCaptureMouse)
|
||||
continue;
|
||||
|
||||
float mouse_x = event.button.x;
|
||||
float mouse_y = event.button.y;
|
||||
|
||||
@@ -964,8 +1001,21 @@ int main(int argc, char **argv) {
|
||||
SDL_BindGPUFragmentSamplers(render_pass, 0, texture_bindings, SDL_arraysize(texture_bindings));
|
||||
SDL_DrawGPUIndexedPrimitives(render_pass, 6, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
SDL_EndGPURenderPass(render_pass);
|
||||
|
||||
ImGui::Render();
|
||||
ImDrawData *draw_data = ImGui::GetDrawData();
|
||||
Imgui_ImplSDLGPU3_PrepareDrawData(draw_data, command_buffer);
|
||||
|
||||
SDL_GPUColorTargetInfo imgui_color_target_info = {
|
||||
.texture = swapchain_texture,
|
||||
.load_op = SDL_GPU_LOADOP_LOAD,
|
||||
.store_op = SDL_GPU_STOREOP_STORE,
|
||||
};
|
||||
|
||||
SDL_GPURenderPass *imgui_render_pass = SDL_BeginGPURenderPass(command_buffer, &imgui_color_target_info, 1, NULL);
|
||||
ImGui_ImplSDLGPU3_RenderDrawData(draw_data, command_buffer, imgui_render_pass);
|
||||
SDL_EndGPURenderPass(imgui_render_pass);
|
||||
|
||||
if (!SDL_SubmitGPUCommandBuffer(command_buffer)) {
|
||||
log_error("Failed to submit gpu command buffer (%s). Exiting.", SDL_GetError());
|
||||
|
||||
Reference in New Issue
Block a user