-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
127 lines (112 loc) · 3.77 KB
/
main.c
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
//------------------------------------------------------------------------------
// cimgui-sapp.c
//
// Demonstrates Dear ImGui UI rendering in C via
// sokol_gfx.h + sokol_imgui.h + cimgui.h
//------------------------------------------------------------------------------
#define SOKOL_GLCORE
#define SOKOL_NO_ENTRY
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_log.h"
#include "sokol_glue.h"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui.h"
#define SOKOL_IMGUI_IMPL
#include "util/sokol_imgui.h"
#include <cosmo.h>
#include <nvapi.h>
#include "win32_tweaks.h"
typedef struct {
uint64_t last_time;
bool show_test_window;
bool show_another_window;
sg_pass_action pass_action;
} state_t;
static state_t state;
void init(void) {
// setup sokol-gfx, sokol-time and sokol-imgui
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
});
// 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){
.logger.func = slog_func,
});
/* initialize application state */
state = (state_t) {
.show_test_window = true,
.pass_action = {
.colors[0] = {
.load_action = SG_LOADACTION_CLEAR,
.clear_value = { 0.7f, 0.5f, 0.0f, 1.0f }
}
}
};
}
void frame(void) {
const int width = sapp_width();
const int height = sapp_height();
simgui_new_frame(&(simgui_frame_desc_t){
.width = width,
.height = height,
.delta_time = sapp_frame_duration(),
.dpi_scale = sapp_dpi_scale()
});
/*// 1. Show a simple window*/
/*// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"*/
static float f = 0.0f;
igText("Hello, world!");
igSliderFloat("float", &f, 0.0f, 1.0f, "%.3f", ImGuiSliderFlags_None);
igColorEdit3("clear color", (float*)&state.pass_action.colors[0].clear_value, 0);
if (igButton("Test Window", (ImVec2) { 0.0f, 0.0f})) state.show_test_window ^= 1;
if (igButton("Another Window", (ImVec2) { 0.0f, 0.0f })) state.show_another_window ^= 1;
igText("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / igGetIO()->Framerate, igGetIO()->Framerate);
/*// 2. Show another simple window, this time using an explicit Begin/End pair*/
if (state.show_another_window) {
igSetNextWindowSize((ImVec2){200,100}, ImGuiCond_FirstUseEver);
igBegin("Another Window", &state.show_another_window, 0);
igText("Hello");
igEnd();
}
/*// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowDemoWindow()*/
if (state.show_test_window) {
igSetNextWindowPos((ImVec2){460,20}, ImGuiCond_FirstUseEver, (ImVec2){0,0});
igShowDemoWindow(0);
}
// the sokol_gfx draw pass
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
simgui_render();
sg_end_pass();
sg_commit();
}
void cleanup(void) {
simgui_shutdown();
sg_shutdown();
}
void input(const sapp_event* event) {
simgui_handle_event(event);
}
int main(int argc, char* argv[]) {
ShowCrashReports();
if (IsWindows()) {
win32_tweaks_hide_console();
nvapi_disable_threaded_optimization("cosmo-sokol");
}
sapp_desc app = {
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = input,
.width = 1024,
.height = 768,
.window_title = "cimgui (sokol-app)",
.ios_keyboard_resizes_canvas = false,
.icon.sokol_default = true,
.enable_clipboard = true,
.logger.func = slog_func,
};
sapp_run(&app);
}