-
Notifications
You must be signed in to change notification settings - Fork 36
/
main_opengl.cpp
193 lines (161 loc) · 6.79 KB
/
main_opengl.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include <imgui/imgui.h>
#include <common/glfw/imgui_impl_glfw.h>
#include <opengl/imgui_impl_opengl3.h>
#include <stdio.h>
#include <ctools/FileHelper.h>
#include <MainFrame.h>
#include <Res/CustomFont.cpp>
#include <Res/Roboto_Medium.cpp>
#include <common/freetype/imgui_freetype.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define SHOW_CONSOLE
// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers.
// To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma.
// Your own project should not be affected, as you are likely to link with a newer binary of GLFW that is adequate for your version of Visual Studio.
#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
#pragma comment(lib, "legacy_stdio_definitions")
#endif
static void glfw_window_drop_callback(GLFWwindow* window, int count, const char** paths)
{
UNUSED(window);
MainFrame::Instance()->JustDropFiles(count, paths);
}
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
static void glfw_window_close_callback(GLFWwindow* window)
{
glfwSetWindowShouldClose(window, GL_FALSE); // block app closing
MainFrame::Instance()->IWantToCloseTheApp();
}
int main(int, char** argv)
{
FileHelper::Instance()->SetAppPath(std::string(argv[0]));
#ifdef _DEBUG
FileHelper::Instance()->SetCurDirectory(PROJECT_PATH);
#else
FileHelper::Instance()->SetCurDirectory(FileHelper::Instance()->GetAppPath());
#endif
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
// Decide GL+GLSL versions
#if APPLE
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
// Create window with graphics context
GLFWwindow* mainWindow = glfwCreateWindow(1280, 720, "ImGuiFontStudio", nullptr, nullptr);
if (mainWindow == 0)
return 1;
glfwMakeContextCurrent(mainWindow);
glfwSwapInterval(1); // Enable vsync
glfwSetWindowCloseCallback(mainWindow, glfw_window_close_callback);
glfwSetDropCallback(mainWindow, glfw_window_drop_callback);
if (gladLoadGL() == 0)
{
fprintf(stderr, "Failed to initialize OpenGL loader!\n");
return 1;
}
#ifdef MSVC
#if defined(_DEBUG) && defined(SHOW_CONSOLE)
ShowWindow(GetConsoleWindow(), SW_SHOW); // show
#else
ShowWindow(GetConsoleWindow(), SW_HIDE); // hide
#endif
#endif
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Viewport
io.FontAllowUserScaling = true; // activate zoom feature with ctrl + mousewheel
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(mainWindow, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// load memory font file
ImGui::GetIO().Fonts->AddFontFromMemoryCompressedBase85TTF(FONT_ICON_BUFFER_NAME_RM, 15.0f);
static const ImWchar icons_ranges[] = { ICON_MIN_IGFS, ICON_MAX_IGFS, 0 };
ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true;
ImGui::GetIO().Fonts->AddFontFromMemoryCompressedBase85TTF(FONT_ICON_BUFFER_NAME_IGFS, 15.0f, &icons_config, icons_ranges);
ImGuiFreeType::FT_Error freetypeError;
uint32_t m_FreeTypeFlag = ImGuiFreeType::FreeType_Default;
if (!ImGuiFreeType::BuildFontAtlas(ImGui::GetIO().Fonts, m_FreeTypeFlag, &freetypeError))
{
printf("Faila to load font, reason : %s \n", ImGuiFreeType::GetErrorMessage(freetypeError));
return 1;
}
MainFrame::Instance(mainWindow)->Init();
// Main loop
int display_w, display_h;
ImVec2 pos, size;
while (!glfwWindowShouldClose(mainWindow))
{
// maintain active, prevent user change via imgui dialog
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
glfwGetFramebufferSize(mainWindow, &display_w, &display_h);
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGuiViewport* viewport = ImGui::GetMainViewport();
if (viewport)
{
pos = viewport->WorkPos;
size = viewport->WorkSize;
}
}
else
{
pos = ImVec2(0, 0);
size = ImVec2((float)display_w, (float)display_h);
}
MainFrame::Instance()->Display(pos, size);
ImGui::Render();
glViewport(0, 0, display_w, display_h);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Update and Render additional Platform Windows
// (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
// For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
glfwSwapBuffers(mainWindow);
}
MainFrame::Instance()->Unit();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 0;
}