Skip to content

Commit

Permalink
imgui: Store 'imgui.ini' contents in clap settings
Browse files Browse the repository at this point in the history
Instead of letting imgui store its ini files all over the place, store
its settings in our own settings file. This also has the added benefit
of actually working in the browser. Load imgui settings when the global
settings are loaded. Right now, it's just a straight up ini file's
contents stuffed into a string instead of individual settings encoded in
a json tree.

Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Oct 31, 2024
1 parent 3cd10d5 commit 5deae4e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/ui-debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ static inline void ui_show_debug_once(const char *debug_name)
ui_show_debug(str_basename(debug_name));
}

struct settings;
void imgui_init(struct clap_context *ctx, void *data, int width, int height);
void imgui_set_settings(struct settings *rs);
void imgui_done(void);
void imgui_render_begin(int width, int height);
void imgui_render(void);
Expand Down
20 changes: 20 additions & 0 deletions core/ui-imgui.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
#include "common.h"
#include "settings.h"
#include "ui-debug.h"

#ifdef __EMSCRIPTEN__
Expand All @@ -11,9 +12,21 @@

#include "imgui_impl_opengl3.h"

static struct settings *settings;
static struct ImGuiContext *ctx;
static struct ImGuiIO *io;

void imgui_set_settings(struct settings *rs)
{
settings = rs;

const char *ini = settings_get_str(settings, "imgui_config");
if (!ini)
return;

igLoadIniSettingsFromMemory(ini, strlen(ini));
}

void imgui_render_begin(int width, int height)
{
io->DisplaySize.x = width;
Expand All @@ -35,13 +48,20 @@ void imgui_render(void)
{
igRender();
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData());

if (io->WantSaveIniSettings && settings) {
settings_set_string(settings, "imgui_config", igSaveIniSettingsToMemory(NULL));
io->WantSaveIniSettings = false;
}
}

void imgui_init(struct clap_context *clap_ctx, void *data, int width, int height)
{
ctx = igCreateContext(NULL);
io = igGetIO();

io->IniFilename = NULL;
io->LogFilename = NULL;
io->DisplaySize.x = width;
io->DisplaySize.y = height;

Expand Down
2 changes: 2 additions & 0 deletions demo/ldjam56/onehandclap.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ static void settings_onload(struct settings *rs, void *data)
float gain = settings_get_num(rs, "music_volume");

sound_set_gain(intro_sound, gain);

imgui_set_settings(rs);
}

static int handle_input(struct message *m, void *data)
Expand Down

0 comments on commit 5deae4e

Please sign in to comment.