Working Imgui layer

main
anulax1225 ago%!(EXTRA string=12 months)
parent 5959834aca
commit b61777f5d7
  1. 0
      examples/sandbox.cpp
  2. 12
      src/bakara/core/application.cpp
  3. 6
      src/bakara/core/application.h
  4. 1
      src/bakara/core/layer.h
  5. 62
      src/bakara/imgui/imgui_layer.cpp
  6. 9
      src/bakara/imgui/imgui_layer.h
  7. 7
      src/platforms/glfw/glfw_window.cpp

@ -7,7 +7,9 @@ namespace Bk {
{
BK_CORE_MSG_ASSERT(p_instance == nullptr, "Application already exists, can not create two application.")
Application::p_instance = this;
h_window = std::shared_ptr<Window>(Window::create_window());
h_window = std::unique_ptr<Window>(Window::create_window());
imgui_layer = new ImguiLayer();
push_overlay(imgui_layer);
h_window->set_event_callback(BK_BIND_EVENT_FN(on_event));
p_running = true;
}
@ -48,6 +50,14 @@ namespace Bk {
{
while (p_running)
{
for (Layer* layer : p_layer_stack)
layer->on_update();
imgui_layer->begin();
for (Layer* layer : p_layer_stack)
layer->imgui_render();
imgui_layer->end();
h_window->on_update();
}
}

@ -8,6 +8,7 @@ This file contains the main app abstraction.
#include <bakara/core/window.h>
#include <bakara/events/events.h>
#include <bakara/core/layer_stack.h>
#include <bakara/imgui/imgui_layer.h>
namespace Bk {
/*! \class Bk::Application
@ -67,12 +68,13 @@ namespace Bk {
*/
void run();
std::shared_ptr<Window> get_window() { return std::shared_ptr<Window>(h_window); }
Window& get_window() { return *h_window; }
static Application& get() { return *p_instance; }
protected:
std::shared_ptr<Window> h_window; //!< Pointer to the main window
std::unique_ptr<Window> h_window; //!< Pointer to the main window
ImguiLayer* imgui_layer;
/*! \fn Bk::Application::close
Stops the application and the update loop without creating an event.

@ -13,6 +13,7 @@ namespace Bk {
virtual void on_detach() {}
virtual void on_event(Event& e) {}
virtual void on_update() {}
virtual void imgui_render() {}
const std::string to_string() const { return name; }
protected:
std::string name;

@ -1,32 +1,80 @@
#include "imgui_layer.h"
#include "bakarapch.h"
#include "bakara/core/application.h"
#include <imgui.h>
#include <imgui_internal.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h>
#include <GLFW/glfw3.h>
#include <glad/glad.h>
namespace Bk {
void ImguiLayer::on_attach()
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = NULL;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
//io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();
Application& app = Application::get();
GLFWwindow* window = static_cast<GLFWwindow*>(app.get_window()->get_native_window());
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
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.get_window().get_native_window());
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 410");
ImGui_ImplOpenGL3_Init("#version 420");
}
void ImguiLayer::on_detach()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void ImguiLayer::on_event(Bk::Event& e)
void ImguiLayer::begin()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void ImguiLayer::on_update()
void ImguiLayer::end()
{
ImGuiIO& io = ImGui::GetIO();
Application& app = Application::get();
io.DisplaySize = ImVec2((float)app.get_window().get_width(), (float)app.get_window().get_height());
// Rendering
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);
}
}
}

@ -1,10 +1,7 @@
#pragma once
#include <imgui.h>
#include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_glfw.h>
#include <bakara/core/application.h>
#include <bakara/core/layer.h>
#include "bakara/core/layer.h"
namespace Bk {
class ImguiLayer : public Layer
@ -15,7 +12,7 @@ namespace Bk {
void on_attach() override;
void on_detach() override;
void on_event(Bk::Event& e) override;
void on_update() override;
void begin();
void end();
};
}

@ -1,4 +1,5 @@
#include <glad/glad.h>
#include <string.h>
#include "glfw_window.h"
namespace Bk {
@ -44,7 +45,11 @@ namespace Bk {
int success = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
BK_CORE_MSG_ASSERT(success, "Couldn't load glad!")
BK_CORE_INFO("Opengl Raw Version : {0}",GL_VERSION);
GLint majVers = 0, minVers = 0;
glGetIntegerv(GL_MAJOR_VERSION, &majVers);
glGetIntegerv(GL_MINOR_VERSION, &minVers);
BK_CORE_INFO("Opengl Version : {0}.{1}", majVers, minVers);
glfwSetWindowUserPointer(p_window, &p_data);
set_vsync(true);

Loading…
Cancel
Save