Skip to content

Instantly share code, notes, and snippets.

@incrediblejr
Created December 14, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incrediblejr/0c4f78436c99cdb0b4d7b43daee98a2a to your computer and use it in GitHub Desktop.
Save incrediblejr/0c4f78436c99cdb0b4d7b43daee98a2a to your computer and use it in GitHub Desktop.
sokol_test.c
#define SOKOL_D3D11
#define SOKOL_IMPL
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_time.h"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4201) /* warning C4201: nonstandard extension used : nameless struct/union */
#endif
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui/cimgui.h"
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#define SOKOL_IMGUI_IMPL
#include "util/sokol_imgui.h"
static uint64_t last_time = 0;
static sg_context main_sg_ctx;
static const void *sapp_d3d11_get_render_target_view_userdata(void)
{
sapp_window window = { (uint32_t)(uintptr_t)sg_active_context_userdata() };
return sapp_d3d11_get_render_target_view_window(window);
}
static const void *sapp_d3d11_get_depth_stencil_view_userdata(void)
{
sapp_window window = { (uint32_t)(uintptr_t)sg_active_context_userdata() };
return sapp_d3d11_get_depth_stencil_view_window(window);
}
void init(void)
{
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4204) /* warning C4204: nonstandard extension used : non-constant aggregate initializer */
#endif
main_sg_ctx = sg_setup(&(sg_desc) {
.mtl_device = sapp_metal_get_device(),
.mtl_renderpass_descriptor_cb = sapp_metal_get_renderpass_descriptor,
.mtl_drawable_cb = sapp_metal_get_drawable,
.d3d11_device = sapp_d3d11_get_device(),
.d3d11_device_context = sapp_d3d11_get_device_context(),
.d3d11_render_target_view_cb = sapp_d3d11_get_render_target_view_userdata, /* NB: not 'default' */
.d3d11_depth_stencil_view_cb = sapp_d3d11_get_depth_stencil_view_userdata, /* NB: not 'default' */
.context_userdata = (void*)(uintptr_t)sapp_main_window().id,
.gl_force_gles2 = sapp_gles2()
});
#ifdef _MSC_VER
#pragma warning(pop)
#endif
stm_setup();
// use sokol-imgui with all default-options (we're not doing
// multi-sampled rendering or using non-default pixel formats)
simgui_setup(&(simgui_desc_t) { 0 });
}
void frame(void)
{
static sapp_window other_windows[15];
static sg_context other_windows_contexts[15];
static int nvalid_other_windows;
int width = sapp_window_width(sapp_main_window());
int height = sapp_window_height(sapp_main_window());
const double delta_time = stm_sec(stm_laptime(&last_time));
simgui_new_frame(width, height, delta_time);
igText("sokol app multi window proof-of-concept");
static float clear_color[4] = { 0.7f, 0.5f, 0.0f, 1.0f };
igColorEdit3("clear color", clear_color, 0);
for (int i = 0; i < nvalid_other_windows; ++i) {
// clean up windows that was destroyed by 'clicking X' / window close
// how should sapp really handle this ?
if (!sapp_valid_window(other_windows[i])) {
sg_discard_context(other_windows_contexts[i]);
--nvalid_other_windows;
other_windows[i] = other_windows[nvalid_other_windows];
other_windows_contexts[i] = other_windows_contexts[nvalid_other_windows];
--i;
}
}
if (nvalid_other_windows != 15) {
if (igButton("create window", (ImVec2) { 0.0f, 0.0f })) {
sapp_window w = sapp_create_window(&(sapp_window_desc) { .window_title = "sokol: not main window" });
sg_context c = sg_setup_context(&(sg_context_desc) {
.d3d11_render_target_view_cb = sapp_d3d11_get_render_target_view_userdata,
.d3d11_depth_stencil_view_cb = sapp_d3d11_get_depth_stencil_view_userdata,
.context_userdata = (void*)(uintptr_t)w.id,
});
other_windows[nvalid_other_windows] = w;
other_windows_contexts[nvalid_other_windows] = c;
++nvalid_other_windows;
}
}
if (nvalid_other_windows) {
if (igButton("destroy window", (ImVec2) { 0.0f, 0.0f })) {
int idx = (int)(sapp_frame_count() % nvalid_other_windows);
sapp_destroy_window(other_windows[idx]);
sg_discard_context(other_windows_contexts[idx]);
--nvalid_other_windows;
other_windows[idx] = other_windows[nvalid_other_windows];
other_windows_contexts[idx] = other_windows_contexts[nvalid_other_windows];
}
}
sg_pass_action pass_action = {
.colors[0] = { .action = SG_ACTION_CLEAR }
};
for (int c=0; c!=4; ++c)
pass_action.colors[0].val[c] = clear_color[c];
sg_activate_context(main_sg_ctx);
sg_begin_default_pass(&pass_action, width, height);
simgui_render();
sg_end_pass();
sg_commit();
static float cc[4][4] = {
{ 1.0f, 0.0f, 0.0f, 1.0f },
{ 0.0f, 1.0f, 0.0f, 1.0f },
{ 0.0f, 0.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 0.0f, 1.0f }
};
for (int i = 0; i < nvalid_other_windows; ++i) {
width = sapp_window_width(other_windows[i]);
height = sapp_window_height(other_windows[i]);
sg_activate_context(other_windows_contexts[i]);
for (int c = 0; c != 4; ++c)
pass_action.colors[0].val[c] = cc[i%4][c];
sg_begin_default_pass(&pass_action, width, height);
sg_end_pass();
sg_commit();
}
}
void cleanup(void)
{
simgui_shutdown();
sg_shutdown();
}
void input(const sapp_event* event)
{
int i = 0;
simgui_handle_event(event);
if (event->type == SAPP_EVENTTYPE_QUIT_REQUESTED)
i++;
}
sapp_desc sokol_main(int argc, char* argv[])
{
(void)argc;
(void)argv;
return (sapp_desc)
{
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = input,
.width = 1024,
.height = 768,
.gl_force_gles2 = true,
.high_dpi = true,
.window_title = "sokol: multi window main",
.ios_keyboard_resizes_canvas = false,
.num_windows = 16
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment