Skip to content

Commit

Permalink
dyntex3d-sapp: add sgimgui debug ui
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Aug 29, 2024
1 parent bce40b8 commit fb25c8f
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions sapp/dyntex3d-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "cimgui/cimgui.h"
#define SOKOL_IMGUI_IMPL
#include "sokol_imgui.h"
#define SOKOL_GFX_IMGUI_IMPL
#include "sokol_gfx_imgui.h"
#include <assert.h> // assert()
#include <string.h> // memset()

Expand All @@ -30,6 +32,8 @@ static struct {
sg_bindings bind;
int width_height;
bool immutable;
bool img_dirty;
sgimgui_t sgimgui;
} state = {
.width_height = 16,
.immutable = false,
Expand All @@ -48,6 +52,7 @@ static void init(void) {
.logger.func = slog_func,
});
simgui_setup(&(simgui_desc_t){ .logger.func = slog_func });
sgimgui_init(&state.sgimgui, &(sgimgui_desc_t){0});

state.pass_action = (sg_pass_action){
.colors[0] = { .load_action = SG_LOADACTION_CLEAR, .clear_value = { 0.0f, 0.0f, 0.0f, 1.0f } },
Expand Down Expand Up @@ -79,12 +84,14 @@ static void init(void) {
}

static void frame(void) {
draw_ui();
if (state.img_dirty) {
recreate_image();
state.img_dirty = false;
}
if (!state.immutable) {
update_pixels(sapp_frame_count());
sg_update_image(state.img, &(sg_image_data){ .subimage[0][0] = pixels_as_range() });
}

sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
Expand All @@ -93,7 +100,7 @@ static void frame(void) {
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 6, 1);
}
simgui_render();
draw_ui();
sg_end_pass();
sg_commit();
}
Expand All @@ -103,6 +110,8 @@ static void input(const sapp_event* ev) {
}

static void cleanup(void) {
sgimgui_discard(&state.sgimgui);
simgui_shutdown();
sg_shutdown();
}

Expand All @@ -113,18 +122,24 @@ static void draw_ui(void) {
.delta_time = sapp_frame_duration(),
.dpi_scale = sapp_dpi_scale(),
});
igSetNextWindowPos((ImVec2){20, 20}, ImGuiCond_Once, (ImVec2){0,0});
if (igBeginMainMenuBar()) {
sgimgui_draw_menu(&state.sgimgui, "sokol-gfx");
igEndMainMenuBar();
}
igSetNextWindowPos((ImVec2){20, 40}, ImGuiCond_Once, (ImVec2){0,0});
igSetNextWindowSize((ImVec2){220, 150}, ImGuiCond_Once);
igSetNextWindowBgAlpha(0.35f);
if (igBegin("Controls", 0, ImGuiWindowFlags_NoDecoration|ImGuiWindowFlags_AlwaysAutoResize)) {
if (igSliderInt("Size", &state.width_height, MIN_WIDTH_HEIGHT, MAX_WIDTH_HEIGHT, "%d", ImGuiSliderFlags_Logarithmic)) {
recreate_image();
state.img_dirty = true;
}
if (igCheckbox("Immutable", &state.immutable)) {
recreate_image();
state.img_dirty = true;
}
}
igEnd();
sgimgui_draw(&state.sgimgui);
simgui_render();
}

static sg_range pixels_as_range(void) {
Expand Down

0 comments on commit fb25c8f

Please sign in to comment.