-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP adding examples/00-native-imgui/main.c
- Loading branch information
Showing
4 changed files
with
773 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#include "GL/glew.h" | ||
#include "GLFW/glfw3.h" | ||
#include "cimgui.h" | ||
#include "imgui_impl_glfw_gl3.h" | ||
#include <stdio.h> | ||
|
||
GLenum err; | ||
GLFWwindow* window; | ||
|
||
static void errorcb(int error, const char* desc) | ||
{ | ||
printf("GLFW error %d: %s\n", error, desc); | ||
} | ||
|
||
struct ImVec2 defaultVec2 = {0}; | ||
struct ImVec4 defaultImVec4 = {0}; | ||
ImGuiColorEditFlags defaultColorEditFlags = 0; | ||
ImGuiWindowFlags defaultWindowFlags = 0; | ||
|
||
int main() | ||
{ | ||
if (!glfwInit()) | ||
{ | ||
printf("Failed to init GLFW."); | ||
return -1; | ||
} | ||
|
||
glfwSetErrorCallback(errorcb); | ||
#ifndef _WIN32 // don't require this on win32, and works with more cards | ||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | ||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); | ||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | ||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | ||
#endif | ||
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); | ||
window = glfwCreateWindow(800, 600, "helloworld", NULL, NULL); | ||
if (!window) | ||
{ | ||
glfwTerminate(); | ||
return NULL; | ||
} | ||
glfwMakeContextCurrent(window); | ||
|
||
// glew | ||
glewExperimental = GL_TRUE; | ||
if (glewInit() != GLEW_OK) | ||
{ | ||
printf("Error: %s\n", glewGetErrorString(err)); | ||
return -1; | ||
} | ||
// GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here. | ||
glGetError(); | ||
|
||
// Setup ImGui binding | ||
igCreateContext(NULL); | ||
struct ImGuiIO* io = igGetIO(); (void)io; | ||
io->NavFlags |= ImGuiNavFlags_EnableKeyboard; // Enable Keyboard Controls | ||
io->NavFlags |= ImGuiNavFlags_EnableGamepad; // Enable Gamepad Controls | ||
ImGui_ImplGlfwGL3_Init(window, true, NULL); | ||
|
||
// Setup style | ||
igStyleColorsDark(NULL); | ||
//igStyleColorsClassic(); | ||
|
||
// Load Fonts | ||
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use igPushFont()/PopFont() to select them. | ||
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. | ||
// - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). | ||
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. | ||
// - Read 'misc/fonts/README.txt' for more instructions and details. | ||
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! | ||
ImFontAtlas_AddFontDefault(io->Fonts, NULL); | ||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); | ||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); | ||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); | ||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); | ||
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); | ||
//IM_ASSERT(font != NULL); | ||
|
||
bool show_demo_window = true; | ||
bool show_another_window = false; | ||
struct ImVec4 clear_color = { 0.45f, 0.55f, 0.60f, 1.00f }; | ||
|
||
// Main loop | ||
while (!glfwWindowShouldClose(window)) | ||
{ | ||
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. | ||
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. | ||
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. | ||
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. | ||
glfwPollEvents(); | ||
ImGui_ImplGlfwGL3_NewFrame(); | ||
|
||
// 1. Show a simple window. | ||
// Tip: if we don't call igBegin()/igEnd() the widgets automatically appears in a window called "Debug". | ||
{ | ||
static float f = 0.0f; | ||
static int counter = 0; | ||
igText("Hello, world!"); // Display some text (you can use a format string too) | ||
igSliderFloat("float", &f, 0.0f, 1.0f, "%.3f", 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f | ||
igColorEdit3("clear color", (float*)&clear_color, defaultColorEditFlags); // Edit 3 floats representing a color | ||
|
||
igCheckbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state | ||
igCheckbox("Another Window", &show_another_window); | ||
|
||
if (igButton("Button", defaultVec2)) // Buttons return true when clicked (NB: most widgets return true when edited/activated) | ||
counter++; | ||
igSameLine(0, -1.0); | ||
igText("counter = %d", counter); | ||
|
||
igText("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / igGetIO()->Framerate, igGetIO()->Framerate); | ||
} | ||
|
||
// 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows. | ||
if (show_another_window) | ||
{ | ||
igBegin("Another Window", &show_another_window, defaultWindowFlags); | ||
igText("Hello from another window!"); | ||
if (igButton("Close Me", defaultVec2)) | ||
show_another_window = false; | ||
igEnd(); | ||
} | ||
|
||
// 3. Show the ImGui demo window. Most of the sample code is in igShowDemoWindow(). Read its code to learn more about Dear ImGui! | ||
if (show_demo_window) | ||
{ | ||
struct ImVec2 pos = { 650, 20 }; | ||
igSetNextWindowPos(pos, ImGuiCond_FirstUseEver, defaultVec2); // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly! | ||
igShowDemoWindow(&show_demo_window); | ||
} | ||
|
||
// Rendering | ||
int display_w, display_h; | ||
glfwGetFramebufferSize(window, &display_w, &display_h); | ||
glViewport(0, 0, display_w, display_h); | ||
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); | ||
glClear(GL_COLOR_BUFFER_BIT); | ||
igRender(); | ||
ImGui_ImplGlfwGL3_RenderDrawData(igGetDrawData()); | ||
glfwSwapBuffers(window); | ||
} | ||
|
||
// Cleanup | ||
ImGui_ImplGlfwGL3_Shutdown(); | ||
igDestroyContext(NULL); | ||
glfwTerminate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// ImGui GLFW binding with OpenGL3 + shaders | ||
// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) | ||
// (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.) | ||
|
||
// Implemented features: | ||
// [X] User texture binding. Cast 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. | ||
// [X] Gamepad navigation mapping. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. | ||
|
||
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. | ||
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown(). | ||
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp. | ||
// https://github.com/ocornut/imgui | ||
|
||
struct GLFWwindow; | ||
|
||
CIMGUI_API int ImGui_ImplGlfwGL3_Init(struct GLFWwindow* window, int install_callbacks, const char* glsl_version); | ||
CIMGUI_API void ImGui_ImplGlfwGL3_Shutdown(); | ||
CIMGUI_API void ImGui_ImplGlfwGL3_NewFrame(); | ||
CIMGUI_API void ImGui_ImplGlfwGL3_RenderDrawData(struct ImDrawData* draw_data); | ||
|
||
// Use if you want to reset your rendering device without losing ImGui state. | ||
CIMGUI_API void ImGui_ImplGlfwGL3_InvalidateDeviceObjects(); | ||
CIMGUI_API int ImGui_ImplGlfwGL3_CreateDeviceObjects(); | ||
|
||
// GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization) | ||
// Provided here if you want to chain callbacks. | ||
// You can also handle inputs yourself and use those as a reference. | ||
CIMGUI_API void ImGui_ImplGlfw_MouseButtonCallback(struct GLFWwindow* window, int button, int action, int mods); | ||
CIMGUI_API void ImGui_ImplGlfw_ScrollCallback(struct GLFWwindow* window, double xoffset, double yoffset); | ||
CIMGUI_API void ImGui_ImplGlfw_KeyCallback(struct GLFWwindow* window, int key, int scancode, int action, int mods); | ||
CIMGUI_API void ImGui_ImplGlfw_CharCallback(struct GLFWwindow* window, unsigned int c); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.