-2

I am facing the problem that compiler throws no error, glfw Works fine. However, ImGui's demo window is not being displayed.

Here's the code, In ImGuiLayer.cpp,

#include <avpch.h>
#include "ImGuiLayer.h"

#include <Platforms/OpenGL/ImGuiOpenGLRenderer.h>
#include <Platforms/OpenGL/imgui_impl_glfw.h>

#include <Core/Application.h>

#include <Glfw/glfw3.h>


namespace Aveer {

    ImGuiLayer::ImGuiLayer() : Layer("ImGuiLayer") {}

    ImGuiLayer::~ImGuiLayer() 
    {}

    void ImGuiLayer::OnAttach()
    {
        IMGUI_CHECKVERSION();
        ImGui::CreateContext();
        ImGuiIO& io = ImGui::GetIO(); (void)io;
        
        io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;         // Enable Docking
        io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
        io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;


        // Setup Dear ImGui style
        ImGui::StyleColorsDark();


        Application& app = Application::Get();
        
        GLFWwindow* window = static_cast<GLFWwindow*>(app.GetWindow().GetNativeWindow());
        io.DisplaySize = ImVec2(app.GetWindow().GetWidth(), app.GetWindow().GetHeight());
        
        // Setup Platform/Renderer backends 
        ImGui_ImplGlfw_InitForOpenGL(window, true);
        ImGui_ImplOpenGL3_Init("#version 330");

    }

    void ImGuiLayer::OnDetach()
    {
        ImGui_ImplOpenGL3_Shutdown();
        ImGui_ImplGlfw_Shutdown();
        ImGui::DestroyContext();
    }

    void ImGuiLayer::OnUpdate()
    {
        AV_CORE_INFO("ImGui Layer");
        Application& app = Application::Get();
        ImGuiIO& io = ImGui::GetIO();

        float time = (float)glfwGetTime();
        io.DeltaTime = m_Time > 0.0f ? (time - m_Time) : (1.0f / 60.0f);
        m_Time = time;

        // Start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        static bool show = true;
        ImGui::ShowDemoWindow(&show);

        // 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);      // Edit bools storing our window open/close state
            ImGui::Checkbox("Another Window", &show);

            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 / io.Framerate, io.Framerate);
            ImGui::End();
        }

        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

    }

    void ImGuiLayer::OnEvent(Event& e)
    {
    }
    
}

In ImGuiLayer.h,

#pragma once

#include "Layer.h"


#include <imgui.h>
#include <imgui_internal.h>

#include <Events/AppEvent.h>
#include <Events/KeyEvent.h>
#include <Events/MouseEvent.h>

namespace Aveer {
    class ImGuiLayer : public Layer {
    public:
        ImGuiLayer();
        ~ImGuiLayer();

        void OnAttach();
        void OnDetach();
        void OnUpdate();
        void OnEvent(Event& e);
    private:

        float m_Time = 0.0f;
    };
}

In Application.h,

#pragma once

#include "Base.h"
#include "Window.h"
#include <Events/Event.h>
#include <Events/AppEvent.h>

#include <Layers/Layer.h>
#include <Layers/LayerStack.h>
#include <Layers/ImGuiLayer.h>

#include "Timestep.h"


namespace Aveer
{
    class Application
    {
    public:
        Application();
        virtual ~Application();

        void OnEvent(Event& e);
        void PushLayer(Layer* layer);
        void PushOverlay(Layer* layer);
        void Run();
        void Close();

        static Application& Get() { return *s_Instance; }
        //void SubmitToMainThread(const std::function<void()>& function);

        Window& GetWindow() { return *m_Window; }

    private:
        bool OnWindowClose(WindowCloseEvent& e);
        bool OnWindowResize(WindowResizeEvent& e);

    private:
        std::unique_ptr<Window> m_Window;
        bool m_Running = true;
        bool m_Minimized = false;
        LayerStack m_LayerStack;
        float m_LastFrameTime = 0.0f;

    private:
        static Application* s_Instance;
    };

    Application* CreateApp();
}

In Application.cpp,

#include <avpch.h>
#include "Application.h"

#include <Utils/Utils.h>
#include <iostream>

#include <glad/glad.h>

namespace Aveer
{
    Application* Application::s_Instance = nullptr;

    Application::Application()
    {
        m_Window = std::unique_ptr<Window>(Window::Create());
        m_Window->SetEventCallback(AV_BIND_EVENT_FN(Application::OnEvent));

        s_Instance = this;
    }

    Application::~Application()
    {
       
    }

    void Application::PushLayer(Layer* layer)
    {
        m_LayerStack.PushLayer(layer);
        layer->OnAttach();
    }

    void Application::PushOverlay(Layer* overlay)
    {
        m_LayerStack.PushOverlay(overlay);
        overlay->OnAttach();
    }

    void Application::Close()
    {
        m_Running = false;
    }

    void Application::OnEvent(Event& e)
    {
        EventDispatcher dispatcher(e);
        dispatcher.Dispatch<WindowCloseEvent>(AV_BIND_EVENT_FN(OnWindowClose));
        AV_CORE_INFO("{0}", e);
    }

    void Application::Run()
    {
        while (m_Running)
        {
            glClear(GL_COLOR_BUFFER_BIT);
            m_Window->OnUpdate();

            for (auto it = m_LayerStack.begin(); it != m_LayerStack.end(); ++it)
            {
                (*it)->OnUpdate();
            }
        }
    }

    bool Application::OnWindowResize(WindowResizeEvent& e)
    {

        if (e.GetWidth() == 0 || e.GetHeight() == 0)
        {
            m_Minimized = true;
            return false;
        }

        m_Minimized = false;

        return false;
    }

    bool Application::OnWindowClose(WindowCloseEvent& e)
    {
        m_Running = false;
        return m_Running;
    }
}

In WindowsWindow.h,

#pragma once

#include <Core/Window.h>
#include <glad/glad.h>

#include <Glfw/glfw3.h>

namespace Aveer {
    
    class WindowsWindow : public Window {
    public:
        WindowsWindow(const WindowProps& props);
        virtual ~WindowsWindow();

        void OnUpdate() override;

        inline unsigned int GetWidth() const override { return m_Data.Width; }
        inline unsigned int GetHeight() const override { return m_Data.Height; }

        // Window attributes
        void SetEventCallback(const EventCallbackFn& callback) override { m_Data.EventCallback = callback; };
        void SetVSync(bool enabled) override;
        bool IsVSync() const override;

        virtual void* GetNativeWindow() const { return m_Window; }
    private:
        virtual void Init(const WindowProps& props);
        virtual void Shutdown();
    private:
        GLFWwindow* m_Window;
        //Scope<GraphicsContext> m_Context;

        struct WindowData
        {
            std::string Title;
            unsigned int Width, Height;
            bool VSync;

            EventCallbackFn EventCallback;
        };

        WindowData m_Data;
    };
}

In WindowsWindow.h,

#pragma once

#include <Core/Window.h>
#include <glad/glad.h>

#include <Glfw/glfw3.h>

namespace Aveer {
    
    class WindowsWindow : public Window {
    public:
        WindowsWindow(const WindowProps& props);
        virtual ~WindowsWindow();

        void OnUpdate() override;

        inline unsigned int GetWidth() const override { return m_Data.Width; }
        inline unsigned int GetHeight() const override { return m_Data.Height; }

        // Window attributes
        void SetEventCallback(const EventCallbackFn& callback) override { m_Data.EventCallback = callback; };
        void SetVSync(bool enabled) override;
        bool IsVSync() const override;

        virtual void* GetNativeWindow() const { return m_Window; }
    private:
        virtual void Init(const WindowProps& props);
        virtual void Shutdown();
    private:
        GLFWwindow* m_Window;
        //Scope<GraphicsContext> m_Context;

        struct WindowData
        {
            std::string Title;
            unsigned int Width, Height;
            bool VSync;

            EventCallbackFn EventCallback;
        };

        WindowData m_Data;
    };
}

Here ImGuiOpenGLRenderer is same as imgui_impl_opengl3. ImGui's not only demo window, but custom is not working.

What's the cause of the problem?

genpfault
  • 51,148
  • 11
  • 85
  • 139

0 Answers0