Skip to content

Commit

Permalink
support imgui
Browse files Browse the repository at this point in the history
Former-commit-id: da1cf26
  • Loading branch information
Xayah-Hina committed Jun 1, 2022
1 parent 590d0f3 commit d97f6a4
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ add_subdirectory(HinaGUI)
#START("LOADING SAMPLES")
add_subdirectory(samples)
#END("SAMPLES DONE")

add_definitions(-DHINAGUI_SCENE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/scenes/")
1 change: 1 addition & 0 deletions HinaGUI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(ImGUI_LIB libimgui)
file(GLOB_RECURSE HINA_GUI_SRC *.h *.cpp)
add_library(HinaGUI ${HINA_GUI_SRC})
target_include_directories(HinaGUI PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(HinaGUI PRIVATE ${ImGUI_LIB})
31 changes: 28 additions & 3 deletions HinaGUI/HinaViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@

using namespace HinaGUI;

void HinaViewer::init(const std::string &json_file)
{
DescInitable::init(json_file);

auto name = desc_->as<HinaViewerDesc>()->name;
auto width = desc_->as<HinaViewerDesc>()->width;
auto height = desc_->as<HinaViewerDesc>()->height;
auto pos_x = desc_->as<HinaViewerDesc>()->pos_x;
auto pos_y = desc_->as<HinaViewerDesc>()->pos_y;

window_ = new Window();
window_->init(name, width, height, pos_x, pos_y);
}

void HinaViewer::resize(int width, int height)
{
desc_->as<HinaViewerDesc>()->width = width;
desc_->as<HinaViewerDesc>()->height = height;
}

void HinaViewer::parse(const nlohmann::json &json)
{
DescInitable::parse(json);
auto res = new HinaViewerDesc();

res->name = json["name"];
res->pos_x = json["pos_x"];
res->pos_y = json["pos_y"];
res->width = json["width"];
Expand All @@ -15,8 +36,12 @@ void HinaViewer::parse(const nlohmann::json &json)
desc_ = res;
}

void HinaViewer::resize(int width, int height)
void HinaViewer::launch()
{
desc_->as<HinaViewerDesc>()->width = width;
desc_->as<HinaViewerDesc>()->height = height;
window_->render();
}

void HinaViewer::kill()
{
window_->kill();
}
11 changes: 10 additions & 1 deletion HinaGUI/HinaViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define HINAGUI_HINAVIEWER_H

#include "base/DescInitable.h"
#include "core/Window.h"

namespace HinaGUI
{
Expand All @@ -10,14 +11,22 @@ namespace HinaGUI
public:
struct HinaViewerDesc : public Desc
{
int pos_x, pos_y;
std::string name;
int width, height;
int pos_x, pos_y;
};

public:
void init(const std::string &json_file) override;
void launch();
void kill();
void resize(int width, int height);

protected:
void parse(const nlohmann::json &json) override;

protected:
Window *window_ = nullptr;
};
}

Expand Down
1 change: 1 addition & 0 deletions HinaGUI/base/DescInitable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void HinaGUI::Base::DescInitable::init(const std::string &json_file)
json_file_ = json_file;
nlohmann::json json = nlohmann::json::parse(std::ifstream(json_file));
parse(json);
inited = true;
}

void HinaGUI::Base::DescInitable::parse(const nlohmann::json &json)
Expand Down
3 changes: 2 additions & 1 deletion HinaGUI/base/DescInitable.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ namespace HinaGUI::Base
};

public:
void init(const std::string &json_file);
virtual void init(const std::string &json_file);

protected:
virtual void parse(const nlohmann::json &json);
Desc *desc_ = nullptr; // DISABLE arbitrary altering from outside
std::string json_file_;
bool inited = false;
};
}

Expand Down
6 changes: 6 additions & 0 deletions HinaGUI/base/defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef HINAGUI_DEFINES_H
#define HINAGUI_DEFINES_H

#define HINA_INLINE

#endif //HINAGUI_DEFINES_H
103 changes: 103 additions & 0 deletions HinaGUI/core/Window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "Window.h"

#include <utility>

void Window::init(const std::string &window_name, int width, int height, int pos_x, int pos_y)
{
init_opengl(window_name, width, height, pos_x, pos_y);
init_imgui();
}

void Window::kill()
{
kill_imgui();
kill_opengl();
window_ = nullptr;
}

void Window::render()
{
while (!glfwWindowShouldClose(window_))
{
if (glfwGetKey(window_, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window_, true);

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

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();
}

ImGui::Render();

glClearColor(1.f, 1.f, 1.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);

ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

glfwSwapBuffers(window_);
glfwPollEvents();
}
}

void Window::init_opengl(const std::string &window_name, int width, int height, int pos_x, int pos_y)
{
glfwInit();
glfwWindowHint(GLFW_SAMPLES, 8); // MSAA samples
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

window_ = glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr);
glfwSetWindowPos(window_, pos_x, pos_y);
glfwMakeContextCurrent(window_);

gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
}

void Window::init_imgui()
{
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void) io;

// Setup Dear ImGui style
ImGui::StyleColorsLight();
ImGui_ImplGlfw_InitForOpenGL(window_, true);
ImGui_ImplOpenGL3_Init("#version 330 core");

// TODO: Load Fonts
}

void Window::kill_opengl()
{
glfwDestroyWindow(window_);
glfwTerminate();
}

void Window::kill_imgui()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
33 changes: 33 additions & 0 deletions HinaGUI/core/Window.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef HINAGUI_WINDOW_H
#define HINAGUI_WINDOW_H

#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "implot.h"

#include "../base/defines.h"

#include <string>

// default OpenGL backend window, TODO: support multi-backend
class Window
{
public:
void init(const std::string &window_name, int width, int height, int pos_x = 0, int pos_y = 0);
void render();
void kill();

protected:
void init_opengl(const std::string &window_name, int width, int height, int pos_x = 0, int pos_y = 0);
void init_imgui();
void kill_opengl();
void kill_imgui();

protected:
GLFWwindow *window_ = nullptr;
};

#endif //HINAGUI_WINDOW_H
9 changes: 5 additions & 4 deletions samples/sample3/1.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"pos_x": 1,
"pos_y": 2,
"width": 3,
"height": 4
"name": "Hina Window",
"width": 1024,
"height": 768,
"pos_x": 0,
"pos_y": 0
}
4 changes: 3 additions & 1 deletion samples/sample4/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
int main()
{
HinaGUI::HinaViewer viewer;
viewer.init("C:/Users/Administrator/Desktop/BFU_Graphics/HinaGUI/samples/sample3/1.json");
viewer.init("/Users/xayah/Desktop/BFU-Graphics/HinaGUI/samples/sample3/1.json");
viewer.launch();
viewer.kill();
return 0;
}

0 comments on commit d97f6a4

Please sign in to comment.