imgui_impl_glut.cpp
Go to the documentation of this file.
1 // dear imgui: Platform Binding for GLUT/FreeGLUT
2 // This needs to be used along with a Renderer (e.g. OpenGL2)
3 
4 // !!! GLUT/FreeGLUT IS OBSOLETE SOFTWARE. Using GLUT is not recommended unless you really miss the 90's. !!!
5 // !!! If someone or something is teaching you GLUT in 2020, you are being abused. Please show some resistance. !!!
6 // !!! Nowadays, prefer using GLFW or SDL instead!
7 
8 // Issues:
9 // [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
10 // [ ] Platform: Missing mouse cursor shape/visibility support.
11 // [ ] Platform: Missing clipboard support (not supported by Glut).
12 // [ ] Platform: Missing gamepad support.
13 
14 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
15 // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
16 // https://github.com/ocornut/imgui
17 
18 // CHANGELOG
19 // (minor and older changes stripped away, please see git history for details)
20 // 2019-04-03: Misc: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h.
21 // 2019-03-25: Misc: Made io.DeltaTime always above zero.
22 // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
23 // 2018-03-22: Added GLUT Platform binding.
24 
25 #include "imgui.h"
26 #include "imgui_impl_glut.h"
27 #ifdef __APPLE__
28  #include <GLUT/glut.h>
29 #else
30  #include <GL/freeglut.h>
31 #endif
32 
33 #ifdef _MSC_VER
34 #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
35 #endif
36 
37 static int g_Time = 0; // Current time, in milliseconds
38 
40 {
41  ImGuiIO& io = ImGui::GetIO();
42 
43 #ifdef FREEGLUT
44  io.BackendPlatformName ="imgui_impl_glut (freeglut)";
45 #else
46  io.BackendPlatformName ="imgui_impl_glut";
47 #endif
48 
49  g_Time = 0;
50 
51  // Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
52  io.KeyMap[ImGuiKey_Tab] = '\t'; // == 9 == CTRL+I
53  io.KeyMap[ImGuiKey_LeftArrow] = 256 + GLUT_KEY_LEFT;
54  io.KeyMap[ImGuiKey_RightArrow] = 256 + GLUT_KEY_RIGHT;
55  io.KeyMap[ImGuiKey_UpArrow] = 256 + GLUT_KEY_UP;
56  io.KeyMap[ImGuiKey_DownArrow] = 256 + GLUT_KEY_DOWN;
57  io.KeyMap[ImGuiKey_PageUp] = 256 + GLUT_KEY_PAGE_UP;
58  io.KeyMap[ImGuiKey_PageDown] = 256 + GLUT_KEY_PAGE_DOWN;
59  io.KeyMap[ImGuiKey_Home] = 256 + GLUT_KEY_HOME;
60  io.KeyMap[ImGuiKey_End] = 256 + GLUT_KEY_END;
61  io.KeyMap[ImGuiKey_Insert] = 256 + GLUT_KEY_INSERT;
62  io.KeyMap[ImGuiKey_Delete] = 127;
63  io.KeyMap[ImGuiKey_Backspace] = 8; // == CTRL+H
64  io.KeyMap[ImGuiKey_Space] = ' ';
65  io.KeyMap[ImGuiKey_Enter] = 13; // == CTRL+M
66  io.KeyMap[ImGuiKey_Escape] = 27;
67  io.KeyMap[ImGuiKey_KeyPadEnter] = 13; // == CTRL+M
68  io.KeyMap[ImGuiKey_A] = 'A';
69  io.KeyMap[ImGuiKey_C] = 'C';
70  io.KeyMap[ImGuiKey_V] = 'V';
71  io.KeyMap[ImGuiKey_X] = 'X';
72  io.KeyMap[ImGuiKey_Y] = 'Y';
73  io.KeyMap[ImGuiKey_Z] = 'Z';
74 
75  return true;
76 }
77 
79 {
80  glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc);
81  glutMotionFunc(ImGui_ImplGLUT_MotionFunc);
82  glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc);
83  glutMouseFunc(ImGui_ImplGLUT_MouseFunc);
84 #ifdef __FREEGLUT_EXT_H__
85  glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc);
86 #endif
87  glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc);
88  glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc);
89  glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc);
90  glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc);
91 }
92 
94 {
95 }
96 
98 {
99  // Setup time step
100  ImGuiIO& io = ImGui::GetIO();
101  int current_time = glutGet(GLUT_ELAPSED_TIME);
102  int delta_time_ms = (current_time - g_Time);
103  if (delta_time_ms <= 0)
104  delta_time_ms = 1;
105  io.DeltaTime = delta_time_ms / 1000.0f;
106  g_Time = current_time;
107 
108  // Start the frame
109  ImGui::NewFrame();
110 }
111 
113 {
114  ImGuiIO& io = ImGui::GetIO();
115  int mods = glutGetModifiers();
116  io.KeyCtrl = (mods & GLUT_ACTIVE_CTRL) != 0;
117  io.KeyShift = (mods & GLUT_ACTIVE_SHIFT) != 0;
118  io.KeyAlt = (mods & GLUT_ACTIVE_ALT) != 0;
119 }
120 
121 void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
122 {
123  // Send character to imgui
124  //printf("char_down_func %d '%c'\n", c, c);
125  ImGuiIO& io = ImGui::GetIO();
126  if (c >= 32)
127  io.AddInputCharacter((unsigned int)c);
128 
129  // Store letters in KeysDown[] array as both uppercase and lowercase + Handle GLUT translating CTRL+A..CTRL+Z as 1..26.
130  // This is a hacky mess but GLUT is unable to distinguish e.g. a TAB key from CTRL+I so this is probably the best we can do here.
131  if (c >= 1 && c <= 26)
132  io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = true;
133  else if (c >= 'a' && c <= 'z')
134  io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = true;
135  else if (c >= 'A' && c <= 'Z')
136  io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = true;
137  else
138  io.KeysDown[c] = true;
140  (void)x; (void)y; // Unused
141 }
142 
143 void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
144 {
145  //printf("char_up_func %d '%c'\n", c, c);
146  ImGuiIO& io = ImGui::GetIO();
147  if (c >= 1 && c <= 26)
148  io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = false;
149  else if (c >= 'a' && c <= 'z')
150  io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = false;
151  else if (c >= 'A' && c <= 'Z')
152  io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = false;
153  else
154  io.KeysDown[c] = false;
156  (void)x; (void)y; // Unused
157 }
158 
159 void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
160 {
161  //printf("key_down_func %d\n", key);
162  ImGuiIO& io = ImGui::GetIO();
163  if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
164  io.KeysDown[key + 256] = true;
166  (void)x; (void)y; // Unused
167 }
168 
169 void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
170 {
171  //printf("key_up_func %d\n", key);
172  ImGuiIO& io = ImGui::GetIO();
173  if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
174  io.KeysDown[key + 256] = false;
176  (void)x; (void)y; // Unused
177 }
178 
179 void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
180 {
181  ImGuiIO& io = ImGui::GetIO();
182  io.MousePos = ImVec2((float)x, (float)y);
183  int button = -1;
184  if (glut_button == GLUT_LEFT_BUTTON) button = 0;
185  if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
186  if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
187  if (button != -1 && state == GLUT_DOWN)
188  io.MouseDown[button] = true;
189  if (button != -1 && state == GLUT_UP)
190  io.MouseDown[button] = false;
191 }
192 
193 #ifdef __FREEGLUT_EXT_H__
194 void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
195 {
196  ImGuiIO& io = ImGui::GetIO();
197  io.MousePos = ImVec2((float)x, (float)y);
198  if (dir > 0)
199  io.MouseWheel += 1.0;
200  else if (dir < 0)
201  io.MouseWheel -= 1.0;
202  (void)button; // Unused
203 }
204 #endif
205 
207 {
208  ImGuiIO& io = ImGui::GetIO();
209  io.DisplaySize = ImVec2((float)w, (float)h);
210 }
211 
213 {
214  ImGuiIO& io = ImGui::GetIO();
215  io.MousePos = ImVec2((float)x, (float)y);
216 }
ImGuiIO::KeyMap
int KeyMap[ImGuiKey_COUNT]
Definition: imgui.h:1430
ImGuiKey_Z
@ ImGuiKey_Z
Definition: imgui.h:1008
ImGuiIO::KeyCtrl
bool KeyCtrl
Definition: imgui.h:1489
ImGui_ImplGLUT_ReshapeFunc
void ImGui_ImplGLUT_ReshapeFunc(int w, int h)
Definition: imgui_impl_glut.cpp:206
ImGuiKey_PageUp
@ ImGuiKey_PageUp
Definition: imgui.h:992
imgui_impl_glut.h
ImGui_ImplGLUT_MouseFunc
void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
Definition: imgui_impl_glut.cpp:179
ImGuiIO::KeyShift
bool KeyShift
Definition: imgui.h:1490
ImGuiKey_Enter
@ ImGuiKey_Enter
Definition: imgui.h:1000
ImGuiKey_Delete
@ ImGuiKey_Delete
Definition: imgui.h:997
ImGuiKey_X
@ ImGuiKey_X
Definition: imgui.h:1006
ImGuiKey_RightArrow
@ ImGuiKey_RightArrow
Definition: imgui.h:989
imgui.h
ImGuiKey_Space
@ ImGuiKey_Space
Definition: imgui.h:999
ImGui_ImplGLUT_SpecialFunc
void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
Definition: imgui_impl_glut.cpp:159
y
GLint y
Definition: glcorearb.h:2768
ImVec2
Definition: imgui.h:208
ImGuiKey_End
@ ImGuiKey_End
Definition: imgui.h:995
ImGuiIO::KeyAlt
bool KeyAlt
Definition: imgui.h:1491
ImGui::GetIO
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:3286
x
GLint GLenum GLint x
Definition: glcorearb.h:2834
ImGui_ImplGLUT_InstallFuncs
void ImGui_ImplGLUT_InstallFuncs()
Definition: imgui_impl_glut.cpp:78
ImGuiIO::AddInputCharacter
IMGUI_API void AddInputCharacter(unsigned int c)
Definition: imgui.cpp:1130
ImGuiKey_A
@ ImGuiKey_A
Definition: imgui.h:1003
ImGuiKey_Home
@ ImGuiKey_Home
Definition: imgui.h:994
ImGuiIO::MouseDown
bool MouseDown[5]
Definition: imgui.h:1486
ImGui_ImplGLUT_NewFrame
void ImGui_ImplGLUT_NewFrame()
Definition: imgui_impl_glut.cpp:97
ImGui_ImplGLUT_MouseWheelFunc
IMGUI_IMPL_API void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
ImGui_ImplGLUT_Shutdown
void ImGui_ImplGLUT_Shutdown()
Definition: imgui_impl_glut.cpp:93
ImGui_ImplGLUT_KeyboardUpFunc
void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
Definition: imgui_impl_glut.cpp:143
ImGui_ImplGLUT_MotionFunc
void ImGui_ImplGLUT_MotionFunc(int x, int y)
Definition: imgui_impl_glut.cpp:212
ImGuiKey_Y
@ ImGuiKey_Y
Definition: imgui.h:1007
ImGuiKey_DownArrow
@ ImGuiKey_DownArrow
Definition: imgui.h:991
ImGui_ImplGLUT_Init
bool ImGui_ImplGLUT_Init()
Definition: imgui_impl_glut.cpp:39
ImGuiKey_KeyPadEnter
@ ImGuiKey_KeyPadEnter
Definition: imgui.h:1002
g_Time
static int g_Time
Definition: imgui_impl_glut.cpp:37
ImGuiIO::MouseWheel
float MouseWheel
Definition: imgui.h:1487
ImGuiKey_LeftArrow
@ ImGuiKey_LeftArrow
Definition: imgui.h:988
IM_ARRAYSIZE
#define IM_ARRAYSIZE(_ARR)
Definition: imgui.h:88
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
ImGuiKey_PageDown
@ ImGuiKey_PageDown
Definition: imgui.h:993
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
ImGuiKey_C
@ ImGuiKey_C
Definition: imgui.h:1004
ImGuiKey_Escape
@ ImGuiKey_Escape
Definition: imgui.h:1001
ImGuiIO::BackendPlatformName
const char * BackendPlatformName
Definition: imgui.h:1455
ImGuiIO::MousePos
ImVec2 MousePos
Definition: imgui.h:1485
ImGui::NewFrame
IMGUI_API void NewFrame()
Definition: imgui.cpp:3691
ImGuiIO::DisplaySize
ImVec2 DisplaySize
Definition: imgui.h:1422
ImGui_ImplGLUT_SpecialUpFunc
void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
Definition: imgui_impl_glut.cpp:169
ImGui_ImplGLUT_UpdateKeyboardMods
static void ImGui_ImplGLUT_UpdateKeyboardMods()
Definition: imgui_impl_glut.cpp:112
ImGuiIO
Definition: imgui.h:1414
ImGuiKey_Backspace
@ ImGuiKey_Backspace
Definition: imgui.h:998
ImGuiIO::KeysDown
bool KeysDown[512]
Definition: imgui.h:1493
ImGuiKey_UpArrow
@ ImGuiKey_UpArrow
Definition: imgui.h:990
ImGuiKey_Insert
@ ImGuiKey_Insert
Definition: imgui.h:996
w
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:3126
ImGuiIO::DeltaTime
float DeltaTime
Definition: imgui.h:1423
ImGui_ImplGLUT_KeyboardFunc
void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
Definition: imgui_impl_glut.cpp:121
ImGuiKey_Tab
@ ImGuiKey_Tab
Definition: imgui.h:987
h
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:4147
ImGuiKey_V
@ ImGuiKey_V
Definition: imgui.h:1005


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