-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (85 loc) · 2.98 KB
/
main.cpp
File metadata and controls
105 lines (85 loc) · 2.98 KB
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
#include <iostream>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <GLFW/glfw3.h>
#include <sstream>
char txt[256] = "Hello, World!";
double last_x = 0;
double last_y = 0;
void test_mouse() {
// get mouse relative position even if window is not focus and print it to console
double x, y;
glfwGetCursorPos(glfwGetCurrentContext(), &x, &y);
std::stringstream ss;
ss << "Mouse Position: " << x - last_x << ", " << y - last_y;
if ((x-last_x != 0 || y-last_y != 0) && (x-last_x < 10000 || y-last_y < 10000)) {
std::cout << ss.str() << std::endl;
}
last_x = x;
last_y = y;
}
void imgui_render() {
ImGui::Begin("Hello, world!", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground);
ImGui::SetWindowPos(ImVec2(0, 0));
// Button that changes the color of the text
static bool change_color = false;
if (change_color) {
ImGui::TextColored(ImVec4(0, 1, 0, 1), "%s", txt);
} else {
ImGui::TextColored(ImVec4(1, 0, 1, 1), "%s", txt);
}
if (ImGui::Button("Change Color")) {
change_color = !change_color;
}
if (ImGui::Button("Open Popup to Change Text")) {
ImGui::OpenPopup("change-text");
}
// Popup to change the text
if (ImGui::BeginPopupModal("change-text", NULL, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) {
ImGui::InputText("Text", txt, ImGuiInputTextFlags_AutoSelectAll);
if (ImGui::Button("Close")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
ImGui::End();
}
int main() {
std::cout << "Hello, World!" << std::endl;
// simple glfw window with imgui
glfwInit();
GLFWwindow* window = glfwCreateWindow(1280, 720, "Hello World", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
while (!glfwWindowShouldClose(window))
{
test_mouse();
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
imgui_render();
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwMakeContextCurrent(window);
glfwSwapBuffers(window);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}