WIP : Add imgui based client to test that ui library

This commit is contained in:
Kienan Stewart 2019-09-03 10:20:25 -04:00
parent d9bc7a5a49
commit e7ed798f83
No known key found for this signature in database
GPG Key ID: 075A846E78FE47EA
3 changed files with 147 additions and 2 deletions

1
3rdparty/imgui vendored Submodule

@ -0,0 +1 @@
Subproject commit c077dd4872f435dd959feb024e5a9adb2c7df20c

View File

@ -8,10 +8,13 @@ find_package(PkgConfig REQUIRED)
# OpenGL
find_package(OpenGL REQUIRED)
#find_package(GLEW REQUIRED)
find_package(GLUT REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
# I can't get glew to initial windows. Always fails
# with error 4 (Unknown) on Debian sid. - Kienan 2019.08.03
#find_package(GLEW REQUIRED)
# Glad
set(GLAD_API "gl=3.3" CACHE STRING " " FORCE)
add_subdirectory("${PROJECT_SOURCE_DIR}/3rdparty/glad")
@ -19,13 +22,43 @@ add_subdirectory("${PROJECT_SOURCE_DIR}/3rdparty/glad")
# Nuklear
set(NUKLEAR_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/3rdparty/nuklear")
#Client
# Imgui
add_definitions(-DIMGUI_IMPL_LOADER_GLAD)
set(imgui_dir "${PROJECT_SOURCE_DIR}/3rdparty/imgui")
add_library(imgui STATIC
${imgui_dir}/imgui.h
${imgui_dir}/imstb_rectpack.h ${imgui_dir}/imstb_textedit.h ${imgui_dir}/imstb_truetype.h ${imgui_dir}/imgui_internal.h
${imgui_dir}/imgui.cpp ${imgui_dir}/imgui_draw.cpp ${imgui_dir}/imgui_widgets.cpp)
target_compile_definitions(imgui PUBLIC -DIMGUI_IMPL_OPENGL_LOADER_GLAD)
target_include_directories(imgui PUBLIC ${imgui_dir})
# Imgui glfw-gl3
set(igo_dir "${imgui_dir}/examples")
add_library(imgui-glfw-gl3 STATIC
${igo_dir}/imgui_impl_glfw.h ${igo_dir}/imgui_impl_opengl3.h
${igo_dir}/imgui_impl_glfw.cpp ${igo_dir}/imgui_impl_opengl3.cpp
)
target_compile_definitions(imgui-glfw-gl3 PUBLIC -DIMGUI_IMPL_OPENGL_LOADER_GLAD)
target_include_directories(imgui-glfw-gl3 PUBLIC ${igo_dir})
target_link_libraries(imgui-glfw-gl3 PUBLIC
${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} glad imgui
)
# Client (Nuklear, default)
#target_include_directories(client "${PROJECT_SOURCE_DIR}/src/client")
add_executable(client ${PROJECT_SOURCE_DIR}/src/client/client.cpp)
target_include_directories(client PUBLIC ${GLFW_INCLUDE_DIRS} ${GLUT_INCLUDE_DIR} ${OPENGL_INCLUDE_DIRS} ${NUKLEAR_INCLUDE_DIR} ${GLAD_INCLUDE_DIRS})
target_link_libraries(client ${GLFW_LIBRARIES} ${GLUT_LIBRARY}
${OPENGL_LIBRARIES} ${GLAD_LIBRARIES} ${CMAKE_DL_LIBS})
# Client (Imgui)
add_executable(client_imgui ${PROJECT_SOURCE_DIR}/src/client/client_imgui.cpp)
target_compile_definitions(client_imgui PUBLIC -DIMGUI_IMPL_OPENGL_LOADER_GLAD)
target_include_directories(client_imgui PUBLIC ${GLFW_INCLUDE_DIRS} ${GLUT_INCLUDE_DIR} ${OPENGL_INCLUDE_DIRS} ${GLAD_INCLUDE_DIRS} ${imgui_dir} ${igo_dir})
target_link_libraries(client_imgui ${GLFW_LIBRARIES} ${GLUT_LIBRARY}
${OPENGL_LIBRARIES} ${GLAD_LIBRARIES} ${CMAKE_DL_LIBS} imgui imgui-glfw-gl3)
# Server
#target_include_directories(server "${PROJECT_SOURCE_DIR}/src/server")
add_executable(server ${PROJECT_SOURCE_DIR}/src/server/server.cpp)

111
src/client/client_imgui.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error %d: %s\n", error, description);
}
int main(int argc, char** argv)
{
GLFWwindow* window;
/* Initialize the library */
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
fprintf(stdout, "[GFLW] Error: failed to init!\n");
return -1;
}
/* Create a windowed mode window and its OpenGL context */
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
window = glfwCreateWindow(1024, 768, "Client", NULL, NULL);
glfwSetWindowAttrib(window, GLFW_DECORATED, GLFW_TRUE);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Vsync
if (!gladLoadGL()) {
fprintf(stderr, "[GLAD] Error loading\n");
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::StyleColorsClassic();
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130");
int height = 0, width = 0;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
// Process input.
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
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::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 / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
// @TODO Game tick
// 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.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
return 0;
}