Last active
May 8, 2023 16:28
-
-
Save frakw/49b97c6c3d51490e3c56b21a3ff30ca0 to your computer and use it in GitHub Desktop.
example code for test imgui using glad and glfw
This file contains hidden or 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
#include "imgui/imgui.h" | |
#include "imgui/imgui_impl_glfw.h" | |
#include "imgui/imgui_impl_opengl3.h" | |
#include <glad/glad.h> | |
#include <GLFW/glfw3.h> | |
#include <iostream> | |
#include <string> | |
int main(void) | |
{ | |
GLFWwindow* window; | |
/* Initialize the library */ | |
if (!glfwInit()) | |
return -1; | |
/* Create a windowed mode window and its OpenGL context */ | |
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); | |
if (!window) | |
{ | |
glfwTerminate(); | |
return -1; | |
} | |
/* Make the window's context current */ | |
glfwMakeContextCurrent(window); | |
if (gladLoadGL() == 0) | |
{ | |
return -1; | |
} | |
// Setup Dear ImGui context | |
IMGUI_CHECKVERSION(); | |
ImGui::CreateContext(); | |
ImGuiIO& io = ImGui::GetIO(); (void)io; | |
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls | |
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls | |
// Setup Dear ImGui style | |
ImGui::StyleColorsDark(); | |
//ImGui::StyleColorsLight(); | |
// Setup Platform/Renderer backends | |
ImGui_ImplGlfw_InitForOpenGL(window, true); | |
ImGui_ImplOpenGL3_Init("#version 130"); | |
bool show_demo_window = true; | |
bool show_another_window = false; | |
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); | |
/* Loop until the user closes the window */ | |
while (!glfwWindowShouldClose(window)) | |
{ | |
/* Render here */ | |
glClear(GL_COLOR_BUFFER_BIT); | |
/* Poll for and process events */ | |
glfwPollEvents(); | |
// Start the Dear ImGui frame | |
ImGui_ImplOpenGL3_NewFrame(); | |
ImGui_ImplGlfw_NewFrame(); | |
ImGui::NewFrame(); | |
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). | |
if (show_demo_window) | |
ImGui::ShowDemoWindow(&show_demo_window); | |
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. | |
{ | |
static float f = 0.0f; | |
static int counter = 0; | |
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. | |
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) | |
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state | |
ImGui::Checkbox("Another Window", &show_another_window); | |
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f | |
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color | |
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) | |
counter++; | |
ImGui::SameLine(); | |
ImGui::Text("counter = %d", counter); | |
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); | |
ImGui::End(); | |
} | |
// 3. Show another simple window. | |
if (show_another_window) | |
{ | |
ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) | |
ImGui::Text("Hello from another window!"); | |
if (ImGui::Button("Close Me")) | |
show_another_window = false; | |
ImGui::End(); | |
} | |
// 4. MineSweeper board | |
{ | |
ImGui::Begin("MineSweeper Board"); | |
for (int i = 0; i < 9; i++) | |
{ | |
for (int j = 0; j < 9; j++) | |
{ | |
std::string buttonName = std::to_string(i) + "," + std::to_string(j); | |
if (ImGui::Button(buttonName.c_str())) | |
{ | |
std::cout << "LeftClick at row:" << i << " col:" << j << std::endl; | |
} | |
if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(1)) | |
{ | |
std::cout << "RightClick at row:" << i << " col:" << j << std::endl; | |
} | |
ImGui::SameLine(); | |
} | |
ImGui::NewLine(); | |
} | |
ImGui::End(); | |
} | |
// Rendering | |
ImGui::Render(); | |
int display_w, display_h; | |
glfwGetFramebufferSize(window, &display_w, &display_h); | |
glViewport(0, 0, display_w, display_h); | |
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); | |
glClear(GL_COLOR_BUFFER_BIT); | |
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | |
glfwSwapBuffers(window); | |
} | |
// Cleanup | |
ImGui_ImplOpenGL3_Shutdown(); | |
ImGui_ImplGlfw_Shutdown(); | |
ImGui::DestroyContext(); | |
glfwDestroyWindow(window); | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment