11
11
#include < stdio.h>
12
12
#include < GLFW/glfw3.h>
13
13
14
+ #ifdef __EMSCRIPTEN__
15
+ #include < emscripten.h>
16
+ #endif
17
+
14
18
static void glfw_error_callback (int error, const char * description)
15
19
{
16
20
fprintf (stderr, " Error %d: %s\n " , error, description);
17
21
}
18
22
23
+ // Global variables nedded for step() method implementation
24
+ static GLFWwindow* window;
25
+ static bool show_demo_window;
26
+ static bool show_another_window;
27
+ static ImVec4 clear_color;
28
+
29
+ void step ();
30
+
19
31
int main (int , char **)
20
32
{
21
33
// Setup window
22
34
glfwSetErrorCallback (glfw_error_callback);
23
35
if (!glfwInit ())
24
36
return 1 ;
25
- GLFWwindow* window = glfwCreateWindow (1280 , 720 , " ImGui GLFW+OpenGL2 example" , NULL , NULL );
37
+ window = glfwCreateWindow (1280 , 720 , " ImGui GLFW+OpenGL2 example" , NULL , NULL );
26
38
glfwMakeContextCurrent (window);
39
+ #ifndef __EMSCRIPTEN__
27
40
glfwSwapInterval (1 ); // Enable vsync
41
+ #endif
28
42
29
43
// Setup ImGui binding
30
44
ImGui::CreateContext ();
@@ -51,68 +65,20 @@ int main(int, char**)
51
65
// ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
52
66
// IM_ASSERT(font != NULL);
53
67
54
- bool show_demo_window = true ;
55
- bool show_another_window = false ;
56
- ImVec4 clear_color = ImVec4 (0 .45f , 0 .55f , 0 .60f , 1 .00f );
68
+ // Default values
69
+ show_demo_window = true ;
70
+ show_another_window = false ;
71
+ clear_color = ImVec4 (0 .45f , 0 .55f , 0 .60f , 1 .00f );
57
72
58
73
// Main loop
74
+ #ifdef __EMSCRIPTEN__
75
+ emscripten_set_main_loop (step, 0 , 1 );
76
+ #else
59
77
while (!glfwWindowShouldClose (window))
60
78
{
61
- // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
62
- // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
63
- // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
64
- // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
65
- glfwPollEvents ();
66
- ImGui_ImplGlfwGL2_NewFrame ();
67
-
68
- // 1. Show a simple window.
69
- // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
70
- {
71
- static float f = 0 .0f ;
72
- static int counter = 0 ;
73
- ImGui::Text (" Hello, world!" ); // Display some text (you can use a format string too)
74
- ImGui::SliderFloat (" float" , &f, 0 .0f , 1 .0f ); // Edit 1 float using a slider from 0.0f to 1.0f
75
- ImGui::ColorEdit3 (" clear color" , (float *)&clear_color); // Edit 3 floats representing a color
76
-
77
- ImGui::Checkbox (" Demo Window" , &show_demo_window); // Edit bools storing our windows open/close state
78
- ImGui::Checkbox (" Another Window" , &show_another_window);
79
-
80
- if (ImGui::Button (" Button" )) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
81
- counter++;
82
- ImGui::SameLine ();
83
- ImGui::Text (" counter = %d" , counter);
84
-
85
- ImGui::Text (" Application average %.3f ms/frame (%.1f FPS)" , 1000 .0f / ImGui::GetIO ().Framerate , ImGui::GetIO ().Framerate );
86
- }
87
-
88
- // 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows.
89
- if (show_another_window)
90
- {
91
- ImGui::Begin (" Another Window" , &show_another_window);
92
- ImGui::Text (" Hello from another window!" );
93
- if (ImGui::Button (" Close Me" ))
94
- show_another_window = false ;
95
- ImGui::End ();
96
- }
97
-
98
- // 3. Show the ImGui demo window. Most of the sample code is in ImGui::ShowDemoWindow(). Read its code to learn more about Dear ImGui!
99
- if (show_demo_window)
100
- {
101
- ImGui::SetNextWindowPos (ImVec2 (650 , 20 ), ImGuiCond_FirstUseEver); // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
102
- ImGui::ShowDemoWindow (&show_demo_window);
103
- }
104
-
105
- // Rendering
106
- int display_w, display_h;
107
- glfwGetFramebufferSize (window, &display_w, &display_h);
108
- glViewport (0 , 0 , display_w, display_h);
109
- glClearColor (clear_color.x , clear_color.y , clear_color.z , clear_color.w );
110
- glClear (GL_COLOR_BUFFER_BIT);
111
- // glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound, but prefer using the GL3+ code.
112
- ImGui::Render ();
113
- ImGui_ImplGlfwGL2_RenderDrawData (ImGui::GetDrawData ());
114
- glfwSwapBuffers (window);
79
+ step ();
115
80
}
81
+ #endif
116
82
117
83
// Cleanup
118
84
ImGui_ImplGlfwGL2_Shutdown ();
@@ -121,3 +87,61 @@ int main(int, char**)
121
87
122
88
return 0 ;
123
89
}
90
+
91
+ void step ()
92
+ {
93
+ // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
94
+ // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
95
+ // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
96
+ // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
97
+ glfwPollEvents ();
98
+ ImGui_ImplGlfwGL2_NewFrame ();
99
+
100
+ // 1. Show a simple window.
101
+ // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
102
+ {
103
+ static float f = 0 .0f ;
104
+ static int counter = 0 ;
105
+ ImGui::Text (" Hello, world!" ); // Display some text (you can use a format string too)
106
+ ImGui::SliderFloat (" float" , &f, 0 .0f , 1 .0f ); // Edit 1 float using a slider from 0.0f to 1.0f
107
+ ImGui::ColorEdit3 (" clear color" , (float *)&clear_color); // Edit 3 floats representing a color
108
+
109
+ ImGui::Checkbox (" Demo Window" , &show_demo_window); // Edit bools storing our windows open/close state
110
+ ImGui::Checkbox (" Another Window" , &show_another_window);
111
+
112
+ if (ImGui::Button (" Button" )) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
113
+ counter++;
114
+ ImGui::SameLine ();
115
+ ImGui::Text (" counter = %d" , counter);
116
+
117
+ ImGui::Text (" Application average %.3f ms/frame (%.1f FPS)" , 1000 .0f / ImGui::GetIO ().Framerate , ImGui::GetIO ().Framerate );
118
+ }
119
+
120
+ // 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows.
121
+ if (show_another_window)
122
+ {
123
+ ImGui::Begin (" Another Window" , &show_another_window);
124
+ ImGui::Text (" Hello from another window!" );
125
+ if (ImGui::Button (" Close Me" ))
126
+ show_another_window = false ;
127
+ ImGui::End ();
128
+ }
129
+
130
+ // 3. Show the ImGui demo window. Most of the sample code is in ImGui::ShowDemoWindow(). Read its code to learn more about Dear ImGui!
131
+ if (show_demo_window)
132
+ {
133
+ ImGui::SetNextWindowPos (ImVec2 (650 , 20 ), ImGuiCond_FirstUseEver); // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
134
+ ImGui::ShowDemoWindow (&show_demo_window);
135
+ }
136
+
137
+ // Rendering
138
+ int display_w, display_h;
139
+ glfwGetFramebufferSize (window, &display_w, &display_h);
140
+ glViewport (0 , 0 , display_w, display_h);
141
+ glClearColor (clear_color.x , clear_color.y , clear_color.z , clear_color.w );
142
+ glClear (GL_COLOR_BUFFER_BIT);
143
+ // glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound, but prefer using the GL3+ code.
144
+ ImGui::Render ();
145
+ ImGui_ImplGlfwGL2_RenderDrawData (ImGui::GetDrawData ());
146
+ glfwSwapBuffers (window);
147
+ }
0 commit comments