-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhello-imgui.cpp
158 lines (131 loc) · 3.56 KB
/
hello-imgui.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "imgui.h"
#include "imgui_impl.h"
#include <cstdio>
#include <cstdlib>
#include <exception>
#define LOG(fmt, ...) fprintf(stdout, fmt, ##__VA_ARGS__); fflush(stdout);
// -----------------------------------------------------------------------------
void renderGui()
{
static bool showDemoWindow = false;
ImGui::Begin("Window");
{
static float f = 0.0f;
static int counter = 0;
ImGui::Text("Hello, ImGui!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
if (ImGui::Button("Button"))
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::Checkbox("ShowDemoWindow", &showDemoWindow);
}
ImGui::End();
if (showDemoWindow)
ImGui::ShowDemoWindow(&showDemoWindow);
ImGui::Render();
}
// -----------------------------------------------------------------------------
void
keyboardCallback(
GLFWwindow* window,
int key, int scancode, int action, int modsls
) {
ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureKeyboard)
return;
if (action == GLFW_PRESS) {
switch (key) {
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
default: break;
}
}
}
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureMouse)
return;
}
void mouseMotionCallback(GLFWwindow* window, double x, double y)
{
static double x0 = 0, y0 = 0;
double dx = x - x0,
dy = y - y0;
ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureMouse)
return;
x0 = x;
y0 = y;
}
void mouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureMouse)
return;
}
// -----------------------------------------------------------------------------
int main(int argc, char **argv)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create the Window
LOG("Loading {Window-Main}\n");
GLFWwindow* window = glfwCreateWindow(1024, 768, "Hello Imgui", NULL, NULL);
if (window == NULL) {
LOG("=> Failure <=\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, &keyboardCallback);
glfwSetCursorPosCallback(window, &mouseMotionCallback);
glfwSetMouseButtonCallback(window, &mouseButtonCallback);
glfwSetScrollCallback(window, &mouseScrollCallback);
// Load OpenGL functions
LOG("Loading {OpenGL}\n");
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
LOG("gladLoadGLLoader failed\n");
return -1;
}
LOG("-- Begin -- Demo\n");
try {
ImGui::CreateContext();
ImGui_ImplGlfwGL3_Init(window, false);
ImGui::StyleColorsDark();
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplGlfwGL3_NewFrame();
renderGui();
ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
ImGui_ImplGlfwGL3_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
} catch (std::exception& e) {
LOG("%s", e.what());
ImGui_ImplGlfwGL3_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
LOG("(!) Demo Killed (!)\n");
return EXIT_FAILURE;
} catch (...) {
ImGui_ImplGlfwGL3_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
LOG("(!) Demo Killed (!)\n");
return EXIT_FAILURE;
}
LOG("-- End -- Demo\n");
return 0;
}