-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathexample.cpp
More file actions
191 lines (160 loc) · 5.9 KB
/
example.cpp
File metadata and controls
191 lines (160 loc) · 5.9 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
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
#include <SDL2/SDL.h>
#include <imgui.h>
#include <backends/imgui_impl_sdl.h>
#include <backends/imgui_impl_opengl3.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/glew.h>
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#pragma comment(lib, "opengl32.lib")
#include "ImFileDialog.h"
// SDL defines main
#undef main
int main(int argc, char* argv[])
{
bool run = true;
srand(time(NULL));
// init sdl2
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) {
printf("Failed to initialize SDL2\n");
return 0;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
Uint32 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
// create the window
int wndWidth = 1200, wndHeight = 800;
SDL_Window* wnd = SDL_CreateWindow("File Dialog", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, wndWidth, wndHeight, windowFlags);
// create the GL context
SDL_GLContext glContext = SDL_GL_CreateContext(wnd);
SDL_GL_MakeCurrent(wnd, glContext);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
// init glew
glewExperimental = true;
if (glewInit() != GLEW_OK) {
printf("Failed to initialize GLEW\n");
return 0;
}
// imgui
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsLight();
ImGui_ImplSDL2_InitForOpenGL(wnd, glContext);
ImGui_ImplOpenGL3_Init();
// ImFileDialog requires you to set the CreateTexture and DeleteTexture
ifd::FileDialog::Instance().CreateTexture = [](uint8_t* data, int w, int h, char fmt) -> void* {
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, (fmt == 0) ? GL_BGRA : GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return (void*)tex;
};
ifd::FileDialog::Instance().DeleteTexture = [](void* tex) {
GLuint texID = (GLuint)((uintptr_t)tex);
glDeleteTextures(1, &texID);
};
int record_type = 0; // 0 = images, 1 = video
SDL_Event event;
while (run) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
run = false;
else if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_MOVED || event.window.event == SDL_WINDOWEVENT_MAXIMIZED || event.window.event == SDL_WINDOWEVENT_RESIZED) {
Uint32 wndFlags = SDL_GetWindowFlags(wnd);
SDL_GetWindowSize(wnd, &wndWidth, &wndHeight);
}
}
// let ImGui handle the events
ImGui_ImplSDL2_ProcessEvent(&event);
}
if (!run) break;
// Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(wnd);
ImGui::NewFrame();
// define a callback to extend ImFileDialog with radio button
// options to choose between capturing still images and videos
auto cb = [&record_type](bool get_size)
{
static bool sameline = true;
if (get_size)
{
auto &st = ImGui::GetStyle();
return sameline ? 0.f : (ImGui::GetFontSize() + st.FramePadding.y + st.ItemSpacing.y * 2.f);
}
ImGui::Checkbox("Same line", &sameline);
ImGui::SameLine();
ImGui::RadioButton("Images", &record_type, 0);
ImGui::SameLine();
ImGui::RadioButton("Video", &record_type, 1);
if (sameline)
ImGui::SameLine(); // same line as OK/Cancel buttons
return 0.f;
};
// Simple window
ImGui::Begin("Control Panel");
if (ImGui::Button("Open file"))
ifd::FileDialog::Instance().Open("ShaderOpenDialog", "Open a shader", "Image file (*.png;*.jpg;*.jpeg;*.bmp;*.tga){.png,.jpg,.jpeg,.bmp,.tga},.*", true);
if (ImGui::Button("Open directory"))
ifd::FileDialog::Instance().Open("DirectoryOpenDialog", "Open a directory", "", false, "", cb);
if (ImGui::Button("Save file"))
ifd::FileDialog::Instance().Save("ShaderSaveDialog", "Save a shader", "*.sprj {.sprj}");
ImGui::End();
// file dialogs
if (ifd::FileDialog::Instance().IsDone("ShaderOpenDialog")) {
if (ifd::FileDialog::Instance().HasResult()) {
const std::vector<std::filesystem::path>& res = ifd::FileDialog::Instance().GetResults();
for (const auto& r : res) // ShaderOpenDialog supports multiselection
printf("OPEN[%s]\n", r.u8string().c_str());
}
ifd::FileDialog::Instance().Close();
}
if (ifd::FileDialog::Instance().IsDone("DirectoryOpenDialog")) {
if (ifd::FileDialog::Instance().HasResult()) {
std::string res = ifd::FileDialog::Instance().GetResult().u8string();
printf("DIRECTORY[%s] (for %s)\n", res.c_str(), record_type ? "video" : "images");
}
ifd::FileDialog::Instance().Close();
}
if (ifd::FileDialog::Instance().IsDone("ShaderSaveDialog")) {
if (ifd::FileDialog::Instance().HasResult()) {
std::string res = ifd::FileDialog::Instance().GetResult().u8string();
printf("SAVE[%s]\n", res.c_str());
}
ifd::FileDialog::Instance().Close();
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glViewport(0, 0, wndWidth, wndHeight);
// render Dear ImGui
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(wnd);
}
// Dear ImGui cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
// SDL2 cleanup
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(wnd);
SDL_Quit();
return 0;
}