1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2024-11-22 18:28:52 -05:00

Added in basics of ImGui rendering

This commit is contained in:
Gordon McCann 2021-12-20 13:40:37 -05:00
parent f18fd5a4bf
commit bd672e25d1
7 changed files with 142 additions and 10 deletions

View File

@ -13,6 +13,9 @@ namespace Navigator {
m_window->SetEventCallback(BIND_EVENT_FUNCTION(Application::OnEvent));
PushLayer(new Layer());
m_imgui_layer = new ImGuiLayer();
PushOverlay(m_imgui_layer);
}
Application::~Application() {}
@ -20,6 +23,7 @@ namespace Navigator {
void Application::OnEvent(Event& event)
{
NAV_TRACE("Found event: {0}", event);
EventDispatcher dispatch(event);
dispatch.Dispatch<WindowCloseEvent>(BIND_EVENT_FUNCTION(Application::OnWindowCloseEvent));
for(auto iter = m_stack.end(); iter != m_stack.begin(); )
@ -53,10 +57,14 @@ namespace Navigator {
{
while(m_runFlag)
{
NAV_TRACE("Doing a run.");
for(auto layer : m_stack)
NAV_TRACE("Layer with name {0} found!", layer->GetName());
for(auto layer : m_stack)
layer->OnUpdate();
m_imgui_layer->Begin();
for(auto layer : m_stack)
layer->OnImGuiRender();
m_imgui_layer->End();
m_window->OnUpdate();
}
}

View File

@ -1,12 +1,13 @@
#ifndef APPLICATION_H
#define APPLICATION_H
#include "NavCore.h"
#include "Navigator/NavCore.h"
#include "Events/Event.h"
#include "Events/AppEvent.h"
#include "LayerStack.h"
#include "Layer.h"
#include "Window.h"
#include "Navigator/Events/AppEvent.h"
#include "Navigator/LayerStack.h"
#include "Navigator/Layer.h"
#include "Navigator/Window.h"
#include "Navigator/ImGui/ImGuiLayer.h"
namespace Navigator {
@ -24,13 +25,14 @@ namespace Navigator {
inline static Application& Get() { return *s_instance; }
inline void GetWindow() { return; }
inline Window& GetWindow() { return *m_window; }
private:
bool OnWindowCloseEvent(WindowCloseEvent& event);
LayerStack m_stack;
std::unique_ptr<Window> m_window;
ImGuiLayer* m_imgui_layer;
bool m_runFlag;
static Application* s_instance;

View File

@ -0,0 +1,4 @@
#include "backends/imgui_impl_opengl3.cpp"
#include "backends/imgui_impl_glfw.cpp"

View File

@ -0,0 +1,90 @@
#include "ImGuiLayer.h"
#include "Navigator/Application.h"
#include "Navigator/NavCore.h"
#include "imgui.h"
#include "backends/imgui_impl_opengl3.h"
#include "backends/imgui_impl_glfw.h"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
namespace Navigator {
ImGuiLayer::ImGuiLayer() :
Layer("ImGuiLayer")
{
}
ImGuiLayer::~ImGuiLayer()
{
}
void ImGuiLayer::OnAttach()
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
Application& app = Application::Get();
GLFWwindow* window = static_cast<GLFWwindow*>(app.GetWindow().GetNativeWindow());
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 410");
}
void ImGuiLayer::OnDetach()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void ImGuiLayer::Begin()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void ImGuiLayer::End()
{
Application& app = Application::Get();
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)app.GetWindow().GetWidth(), (float)app.GetWindow().GetHeight());
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
}
void ImGuiLayer::OnImGuiRender()
{
static bool show = true;
ImGui::ShowDemoWindow(&show);
}
}

View File

@ -0,0 +1,27 @@
#ifndef IMGUI_LAYER_H
#define IMGUI_LAYER_H
#include "Navigator/Layer.h"
namespace Navigator {
class ImGuiLayer : public Layer
{
public:
ImGuiLayer();
~ImGuiLayer();
virtual void OnAttach() override;
virtual void OnDetach() override;
virtual void OnImGuiRender() override;
void Begin();
void End();
private:
float m_time;
};
}
#endif

View File

@ -14,6 +14,7 @@ namespace Navigator {
virtual void OnAttach() {}
virtual void OnDetach() {}
virtual void OnImGuiRender() {}
virtual void OnUpdate() {}
virtual void OnEvent(Event& event) {}
inline const std::string& GetName() { return m_name; }

View File

@ -7,6 +7,6 @@
],
"settings":
{
"SublimeLinter.linters.g++.args": ["-c","-Wall","-fsyntax-only","-std=c++17","-include${folder}/Navigator/src/navpch.h","-INavigator/src/", "-INavigator/src/Navigator/","-INavigator/vendor/spdlog/include", "-INavigator/vendor/glfw/include/","-INavigator/vendor/glad/include"]
"SublimeLinter.linters.g++.args": ["-c","-Wall","-fsyntax-only","-std=c++17","-include${folder}/Navigator/src/navpch.h","-INavigator/src/", "-INavigator/src/Navigator/","-INavigator/vendor/spdlog/include", "-INavigator/vendor/glfw/include/","-INavigator/vendor/glad/include","-INavigator/vendor/imgui"]
}
}