-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupDearIMGUI
More file actions
87 lines (60 loc) · 2.18 KB
/
Copy pathsetupDearIMGUI
File metadata and controls
87 lines (60 loc) · 2.18 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
# Using Dear ImGui to Show FPS Overlay in OpenGL
This guide outlines the steps to integrate Dear ImGui into your OpenGL project and display the FPS in the top right corner.
---
## 1. Add Dear ImGui to Your Project
- Download ImGui from [https://github.com/ocornut/imgui](https://github.com/ocornut/imgui).
- Add the following files to your build:
- `imgui.cpp`, `imgui_draw.cpp`, `imgui_tables.cpp`, `imgui_widgets.cpp`, `imgui_demo.cpp`
- Backends: `imgui_impl_glfw.cpp`, `imgui_impl_opengl3.cpp`
- Include the headers in your project.
---
## 2. Initialize ImGui
In your initialization code (after creating your OpenGL context and window):
```cpp
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
```
---
## 3. Start a New ImGui Frame in Your Render Loop
At the beginning of each frame:
```cpp
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
```
---
## 4. Show the FPS Overlay
Add this code in your render loop to display the FPS in the top right corner:
```cpp
ImGui::SetNextWindowPos(ImVec2(W_width - 110, 10), ImGuiCond_Always);
ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background
ImGui::Begin("FPS", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav);
ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate);
ImGui::End();
```
---
## 5. Render ImGui
After your normal OpenGL rendering:
```cpp
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
```
---
## 6. Cleanup ImGui on Exit
Before exiting your application:
```cpp
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
```
---
## Notes
- Make sure to link against ImGui and its backends in your build system.
- You may need to adjust the window position (`W_width - 110`) depending on your window size variable.
- For more details, see the [ImGui examples](https://github.com/ocornut/imgui/tree/master/examples).