Skip to content

Commit

Permalink
WIP adding examples/00-native-imgui/main.c
Browse files Browse the repository at this point in the history
  • Loading branch information
vinjn committed Apr 30, 2018
1 parent ca50e43 commit 9e43624
Show file tree
Hide file tree
Showing 4 changed files with 773 additions and 30 deletions.
147 changes: 147 additions & 0 deletions examples/00-native-imgui/main.c
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();
}
31 changes: 31 additions & 0 deletions include/imgui_impl_glfw_gl3.h
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);
81 changes: 51 additions & 30 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ solution "island"
optimize "On"
editandcontinue "Off"

project "imgui"
includedirs {
"3rdparty/cimgui/cimgui",
"3rdparty/cimgui/imgui",
}
files {
"3rdparty/cimgui/cimgui/*.h",
"3rdparty/cimgui/cimgui/*.cpp",
"3rdparty/cimgui/imgui/*",
}

project "glfw"
includedirs {
"3rdparty/glfw/include"
Expand Down Expand Up @@ -195,11 +206,17 @@ solution "island"

project "island"
includedirs {
"3rdparty/stb"
"include",
"3rdparty/stb",
"3rdparty/glfw/include",
"3rdparty/glew",
"3rdparty/cimgui/cimgui",
"3rdparty/cimgui/imgui",
}
files {
"3rdparty/stb/*",
"3rdparty/stb/stb/*",
"include/**",
"src/**",
}

Expand All @@ -219,35 +236,35 @@ solution "island"
}
end

project "soloud"
language "C++"
includedirs {
"3rdparty/soloud/include",
}
files {
"3rdparty/soloud/inlcude/*.h",
"3rdparty/soloud/src/core/*.cpp",
"3rdparty/soloud/src/audiosource/**",
"3rdparty/soloud/src/filter/*.cpp",
"3rdparty/soloud/src/c_api/*.cpp",
}
filter "system:windows"
defines {"WITH_WINMM"}
files {
"3rdparty/soloud/src/backend/winmm/*.cpp"
}
filter "system:linux"
defines {"WITH_OSS"}
files {
"3rdparty/soloud/src/backend/oss/*.cpp"
}
filter "system:macosx"
defines {
"WITH_COREAUDIO",
}
files {
"3rdparty/soloud/src/backend/coreaudio/*.cpp"
}
-- project "soloud"
-- language "C++"
-- includedirs {
-- "3rdparty/soloud/include",
-- }
-- files {
-- "3rdparty/soloud/inlcude/*.h",
-- "3rdparty/soloud/src/core/*.cpp",
-- "3rdparty/soloud/src/audiosource/**",
-- "3rdparty/soloud/src/filter/*.cpp",
-- "3rdparty/soloud/src/c_api/*.cpp",
-- }
-- filter "system:windows"
-- defines {"WITH_WINMM"}
-- files {
-- "3rdparty/soloud/src/backend/winmm/*.cpp"
-- }
-- filter "system:linux"
-- defines {"WITH_OSS"}
-- files {
-- "3rdparty/soloud/src/backend/oss/*.cpp"
-- }
-- filter "system:macosx"
-- defines {
-- "WITH_COREAUDIO",
-- }
-- files {
-- "3rdparty/soloud/src/backend/coreaudio/*.cpp"
-- }

function create_example_project( example_path )
leaf_name = string.sub(example_path, string.len("examples/") + 1);
Expand All @@ -264,6 +281,7 @@ solution "island"
"GLEW_NO_GLU",
"NANOVG_GL3_IMPLEMENTATION",
"RMT_USE_OPENGL",
"CIMGUI_DEFINE_ENUMS_AND_STRUCTS",
}

includedirs {
Expand All @@ -277,6 +295,7 @@ solution "island"
"3rdparty/stb",
"3rdparty/soloud/include",
"3rdparty/Remotery/lib",
"3rdparty/cimgui/cimgui",
}

-- TODO: automatically collect lib names
Expand All @@ -290,6 +309,7 @@ solution "island"
-- "soloud-d",
"Remotery-d",
"v7-d",
"imgui-d",
}

configuration "Release"
Expand All @@ -302,6 +322,7 @@ solution "island"
-- "soloud",
"Remotery",
"v7",
"imgui",
}

filter "system:windows"
Expand Down
Loading

0 comments on commit 9e43624

Please sign in to comment.