dependencies/third-party/imgui/src/examples/example_emscripten/main.cpp
Go to the documentation of this file.
1 // dear imgui: standalone example application for Emscripten, using SDL2 + OpenGL3
2 // This is mostly the same code as the SDL2 + OpenGL3 example, simply with the modifications needed to run on Emscripten.
3 // It is possible to combine both code into a single source file that will compile properly on Desktop and using Emscripten.
4 // See https://github.com/ocornut/imgui/pull/2492 as an example on how to do just that.
5 //
6 // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
7 // (Emscripten is a C++-to-javascript compiler, used to publish executables for the web. See https://emscripten.org/)
8 
9 #include "imgui.h"
10 #include "imgui_impl_sdl.h"
11 #include "imgui_impl_opengl3.h"
12 #include <stdio.h>
13 #include <emscripten.h>
14 #include <SDL.h>
15 #include <SDL_opengles2.h>
16 
17 // Emscripten requires to have full control over the main loop. We're going to store our SDL book-keeping variables globally.
18 // Having a single function that acts as a loop prevents us to store state in the stack of said function. So we need some location for this.
19 SDL_Window* g_Window = NULL;
20 SDL_GLContext g_GLContext = NULL;
21 
22 // For clarity, our main loop code is declared at the end.
23 void main_loop(void*);
24 
25 int main(int, char**)
26 {
27  // Setup SDL
28  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
29  {
30  printf("Error: %s\n", SDL_GetError());
31  return -1;
32  }
33 
34  // For the browser using Emscripten, we are going to use WebGL1 with GL ES2. See the Makefile. for requirement details.
35  // It is very likely the generated file won't work in many browsers. Firefox is the only sure bet, but I have successfully
36  // run this code on Chrome for Android for example.
37  const char* glsl_version = "#version 100";
38  //const char* glsl_version = "#version 300 es";
39  SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
40  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
41  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
42  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
43 
44  // Create window with graphics context
45  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
46  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
47  SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
48  SDL_DisplayMode current;
49  SDL_GetCurrentDisplayMode(0, &current);
50  SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
51  g_Window = SDL_CreateWindow("Dear ImGui Emscripten example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
52  g_GLContext = SDL_GL_CreateContext(g_Window);
53  if (!g_GLContext)
54  {
55  fprintf(stderr, "Failed to initialize WebGL context!\n");
56  return 1;
57  }
58  SDL_GL_SetSwapInterval(1); // Enable vsync
59 
60  // Setup Dear ImGui context
63  ImGuiIO& io = ImGui::GetIO(); (void)io;
64  //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
65  //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
66 
67  // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
68  // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
69  io.IniFilename = NULL;
70 
71  // Setup Dear ImGui style
73  //ImGui::StyleColorsClassic();
74 
75  // Setup Platform/Renderer bindings
77  ImGui_ImplOpenGL3_Init(glsl_version);
78 
79  // Load Fonts
80  // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
81  // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
82  // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
83  // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
84  // - Read 'docs/FONTS.txt' for more instructions and details.
85  // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
86  // - Emscripten allows preloading a file or folder to be accessible at runtime. See Makefile for details.
87  //io.Fonts->AddFontDefault();
88 #ifndef IMGUI_DISABLE_FILE_FUNCTIONS
89  io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf", 16.0f);
90  //io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf", 15.0f);
91  //io.Fonts->AddFontFromFileTTF("fonts/DroidSans.ttf", 16.0f);
92  //io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf", 10.0f);
93  //ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
94  //IM_ASSERT(font != NULL);
95 #endif
96 
97  // This function call won't return, and will engage in an infinite loop, processing events from the browser, and dispatching them.
98  emscripten_set_main_loop_arg(main_loop, NULL, 0, true);
99 }
100 
101 void main_loop(void* arg)
102 {
103  ImGuiIO& io = ImGui::GetIO();
104  IM_UNUSED(arg); // We can pass this argument as the second parameter of emscripten_set_main_loop_arg(), but we don't use that.
105 
106  // Our state (make them static = more or less global) as a convenience to keep the example terse.
107  static bool show_demo_window = true;
108  static bool show_another_window = false;
109  static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
110 
111  // Poll and handle events (inputs, window resize, etc.)
112  // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
113  // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
114  // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
115  // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
117  while (SDL_PollEvent(&event))
118  {
120  // Capture events here, based on io.WantCaptureMouse and io.WantCaptureKeyboard
121  }
122 
123  // Start the Dear ImGui frame
126  ImGui::NewFrame();
127 
128  // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
129  if (show_demo_window)
131 
132  // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
133  {
134  static float f = 0.0f;
135  static int counter = 0;
136 
137  ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
138 
139  ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
140  ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
141  ImGui::Checkbox("Another Window", &show_another_window);
142 
143  ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
144  ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
145 
146  if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
147  counter++;
148  ImGui::SameLine();
149  ImGui::Text("counter = %d", counter);
150 
151  ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
152  ImGui::End();
153  }
154 
155  // 3. Show another simple window.
157  {
158  ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
159  ImGui::Text("Hello from another window!");
160  if (ImGui::Button("Close Me"))
161  show_another_window = false;
162  ImGui::End();
163  }
164 
165  // Rendering
166  ImGui::Render();
167  SDL_GL_MakeCurrent(g_Window, g_GLContext);
168  glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
172  SDL_GL_SwapWindow(g_Window);
173 }
ImGuiIO::Fonts
ImFontAtlas * Fonts
Definition: imgui.h:1435
SDL_Event
union SDL_Event SDL_Event
Definition: imgui_impl_sdl.h:20
imgui_impl_opengl3.h
NULL
NULL
Definition: test_security_zap.cpp:405
ImVec4::z
float z
Definition: imgui.h:223
imgui_impl_sdl.h
ImGui_ImplOpenGL3_RenderDrawData
void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData *draw_data)
Definition: imgui_impl_opengl3.cpp:285
ImFontAtlas::AddFontFromFileTTF
IMGUI_API ImFont * AddFontFromFileTTF(const char *filename, float size_pixels, const ImFontConfig *font_cfg=NULL, const ImWchar *glyph_ranges=NULL)
Definition: imgui_draw.cpp:1772
ImGui::GetDrawData
IMGUI_API ImDrawData * GetDrawData()
Definition: imgui.cpp:3293
ImGui::Button
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0))
Definition: imgui_widgets.cpp:678
ImGui_ImplSDL2_ProcessEvent
bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event *event)
Definition: imgui_impl_sdl.cpp:85
ImGui::Render
IMGUI_API void Render()
Definition: imgui.cpp:4222
ImGuiIO::IniFilename
const char * IniFilename
Definition: imgui.h:1425
imgui.h
ImGui::Text
IMGUI_API void Text(const char *fmt,...) IM_FMTARGS(1)
Definition: imgui_widgets.cpp:247
glViewport
#define glViewport
Definition: gl3w.h:672
ImGui::SameLine
IMGUI_API void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f)
Definition: imgui.cpp:6923
ImGui_ImplOpenGL3_Init
bool ImGui_ImplOpenGL3_Init(const char *glsl_version)
Definition: imgui_impl_opengl3.cpp:153
ImGui_ImplSDL2_NewFrame
void ImGui_ImplSDL2_NewFrame(SDL_Window *window)
Definition: imgui_impl_sdl.cpp:342
ImGui::GetIO
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:3286
IM_UNUSED
#define IM_UNUSED(_VAR)
Definition: imgui.h:89
glClear
#define glClear
Definition: gl3w.h:639
ImGui_ImplSDL2_InitForOpenGL
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window *window, void *sdl_gl_context)
Definition: imgui_impl_sdl.cpp:194
show_another_window
static bool show_another_window
Definition: dependencies/third-party/imgui/src/examples/example_glut_opengl2/main.cpp:23
ImGui::End
IMGUI_API void End()
Definition: imgui.cpp:6007
ImVec4::y
float y
Definition: imgui.h:223
main
int main(int, char **)
Definition: dependencies/third-party/imgui/src/examples/example_emscripten/main.cpp:25
main_loop
void main_loop(void *)
Definition: dependencies/third-party/imgui/src/examples/example_emscripten/main.cpp:101
event
struct _cl_event * event
Definition: glcorearb.h:4163
ImGui::ShowDemoWindow
IMGUI_API void ShowDemoWindow(bool *p_open=NULL)
Definition: imgui_demo.cpp:208
ImVec2::x
float x
Definition: imgui.h:210
ImGui::SliderFloat
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *format="%.3f", float power=1.0f)
Definition: imgui_widgets.cpp:2652
glClearColor
#define glClearColor
Definition: gl3w.h:640
clear_color
static ImVec4 clear_color
Definition: dependencies/third-party/imgui/src/examples/example_glut_opengl2/main.cpp:24
GL_COLOR_BUFFER_BIT
#define GL_COLOR_BUFFER_BIT
Definition: glcorearb.h:98
ImGui::Checkbox
IMGUI_API bool Checkbox(const char *label, bool *v)
Definition: imgui_widgets.cpp:1008
ImGui::CreateContext
IMGUI_API ImGuiContext * CreateContext(ImFontAtlas *shared_font_atlas=NULL)
Definition: imgui.cpp:3267
ImVec4::x
float x
Definition: imgui.h:223
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
ImGui::ColorEdit3
IMGUI_API bool ColorEdit3(const char *label, float col[3], ImGuiColorEditFlags flags=0)
Definition: imgui_widgets.cpp:4242
ImGui::NewFrame
IMGUI_API void NewFrame()
Definition: imgui.cpp:3691
ImGuiIO::DisplaySize
ImVec2 DisplaySize
Definition: imgui.h:1422
g_Window
SDL_Window * g_Window
Definition: dependencies/third-party/imgui/src/examples/example_emscripten/main.cpp:19
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:5397
ImVec4::w
float w
Definition: imgui.h:223
ImGuiIO
Definition: imgui.h:1414
ImVec4
Definition: imgui.h:221
g_GLContext
SDL_GLContext g_GLContext
Definition: dependencies/third-party/imgui/src/examples/example_emscripten/main.cpp:20
ImGui_ImplOpenGL3_NewFrame
void ImGui_ImplOpenGL3_NewFrame()
Definition: imgui_impl_opengl3.cpp:226
f
GLfloat f
Definition: glcorearb.h:3964
show_demo_window
static bool show_demo_window
Definition: dependencies/third-party/imgui/src/examples/example_glut_opengl2/main.cpp:22
ImGui::StyleColorsDark
IMGUI_API void StyleColorsDark(ImGuiStyle *dst=NULL)
Definition: imgui_draw.cpp:178
ImVec2::y
float y
Definition: imgui.h:210
IMGUI_CHECKVERSION
#define IMGUI_CHECKVERSION()
Definition: imgui.h:64


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55