Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: PazerOP/imgui_desktop
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6e027de076f86f7d59a5d7be5433c546d2e0040a
Choose a base ref
...
head repository: PazerOP/imgui_desktop
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b7e7bcc7bb88110b99560e1c564e22f7210413b7
Choose a head ref
  • 1 commit
  • 7 files changed
  • 1 contributor

Commits on Jul 11, 2020

  1. Copy the full SHA
    b7e7bcc View commit details
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.0)
project(imgui_desktop)

set(imgui_USE_OPENGL2 on)
set(imgui_USE_OPENGL3 on)
set(imgui_USE_GLBINDING on)
set(imgui_USE_SDL2 on)
add_subdirectory(submodules/imgui)
@@ -11,6 +12,7 @@ add_library(imgui_desktop
"imgui_desktop/include/imgui_desktop/ImGuiHelpers.h"
"imgui_desktop/src/GLContext.cpp"
"imgui_desktop/src/GLContext.h"
"imgui_desktop/include/imgui_desktop/GLContextVersion.h"
"imgui_desktop/src/ScopeGuards.cpp"
"imgui_desktop/include/imgui_desktop/ScopeGuards.h"
"imgui_desktop/src/Window.cpp"
25 changes: 25 additions & 0 deletions imgui_desktop/include/imgui_desktop/GLContextVersion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <ostream>

namespace ImGuiDesktop
{
struct GLContextVersion
{
GLContextVersion() = default;
explicit constexpr GLContextVersion(int major, int minor = 0) :
m_Major(major), m_Minor(minor)
{
}

int m_Major = -1;
int m_Minor = -1;
};
}

template<typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(
std::basic_ostream<CharT, Traits>& os, const ImGuiDesktop::GLContextVersion& version)
{
return os << version.m_Major << '.' << version.m_Minor;
}
4 changes: 4 additions & 0 deletions imgui_desktop/include/imgui_desktop/Window.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "GLContextVersion.h"

#include <cstdint>
#include <memory>

@@ -26,6 +28,8 @@ namespace ImGuiDesktop
void QueueUpdate();
void Update();

GLContextVersion GetGLContextVersion() const;

protected:
virtual void OnUpdate() {}
virtual void OnDraw() = 0;
45 changes: 40 additions & 5 deletions imgui_desktop/src/GLContext.cpp
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
#include <SDL.h>

#include <cassert>
#include <sstream>

using namespace ImGuiDesktop;

@@ -30,9 +31,43 @@ namespace
{
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
context = std::make_shared<GLContext>(std::shared_ptr<void>(SDL_GL_CreateContext(window), SDL_GLContextDeleter{}));

constexpr GLContextVersion VERSION_3(3, 2);
constexpr GLContextVersion VERSION_2(2, 0);

// Try OpenGL 3
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, VERSION_3.m_Major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, VERSION_3.m_Minor);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
context = std::make_shared<GLContext>(
std::shared_ptr<void>(SDL_GL_CreateContext(window), SDL_GLContextDeleter{}),
VERSION_3);
}

if (!context)
{
// Try OpenGL 2
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, VERSION_2.m_Major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, VERSION_2.m_Minor);
context = std::make_shared<GLContext>(
std::shared_ptr<void>(SDL_GL_CreateContext(window), SDL_GLContextDeleter{}),
VERSION_2);
}

if (!context)
{
// Neither worked, show an error and quit
std::stringstream ss;
ss << "Failed to initialize OpenGL " << VERSION_3 << " or OpenGL " << VERSION_2
<< ". Unfortunately, this means your computer is too old to run this software.";

SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OpenGL Initialization Failed",
ss.str().c_str(), window);

std::exit(1);
}

m_GLContext = context;
}
}
@@ -69,7 +104,7 @@ std::shared_ptr<GLContext> ImGuiDesktop::GetOrCreateGLContext(SDL_Window* window
return s_GLContextHolder.GetOrCreateGLContext(window);
}

GLContext::GLContext(const std::shared_ptr<void>& context) :
m_InnerContext(context)
GLContext::GLContext(const std::shared_ptr<void>& context, GLContextVersion version) :
m_InnerContext(context), m_GLVersion(version)
{
}
8 changes: 7 additions & 1 deletion imgui_desktop/src/GLContext.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "GLContextVersion.h"

#include <memory>
#include <mutex>

@@ -10,14 +12,18 @@ namespace ImGuiDesktop
class GLContext
{
public:
GLContext(const std::shared_ptr<void>& context);
GLContext(const std::shared_ptr<void>& context, GLContextVersion version);

GLContextVersion GetVersion() const { return m_GLVersion; }

private:
friend class GLContextScope;

std::shared_ptr<void> m_InnerContext;
std::recursive_mutex m_ActiveMutex;
int m_RecursionDepth = 0;

GLContextVersion m_GLVersion{};
};

std::shared_ptr<GLContext> GetOrCreateGLContext(SDL_Window* window);
32 changes: 28 additions & 4 deletions imgui_desktop/src/Window.cpp
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
#include <imgui.h>
#include <examples/imgui_impl_sdl.h>
#include <examples/imgui_impl_opengl2.h>
#include <examples/imgui_impl_opengl3.h>
#include <SDL.h>

#include <stdexcept>
@@ -55,8 +56,18 @@ Window::Window(uint32_t width, uint32_t height, const char* title)
m_ImGuiContext.reset(ImGui::CreateContext(&s_ImGuiFontAtlas));
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::GetIO().IniFilename = nullptr; // Don't save stuff... for now
if (!ImGui_ImplOpenGL2_Init())
throw std::runtime_error("Failed to initialize ImGui OpenGL3 impl");

if (GetGLContextVersion().m_Major >= 3)
{
if (!ImGui_ImplOpenGL3_Init())
throw std::runtime_error("Failed to initialize ImGui OpenGL3 impl");
}
else
{
if (!ImGui_ImplOpenGL2_Init())
throw std::runtime_error("Failed to initialize ImGui OpenGL2 impl");
}

if (!ImGui_ImplSDL2_InitForOpenGL(m_WindowImpl.get(), m_GLContext.get()))
throw std::runtime_error("Failed to initialize ImGui GLFW impl");

@@ -162,7 +173,11 @@ void Window::OnDrawInternal()
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

ImGui_ImplOpenGL2_NewFrame();
if (GetGLContextVersion().m_Major >= 3)
ImGui_ImplOpenGL3_NewFrame();
else
ImGui_ImplOpenGL2_NewFrame();

ImGui_ImplSDL2_NewFrame(m_WindowImpl.get());
ImGui::NewFrame();
{
@@ -194,7 +209,11 @@ void Window::OnDrawInternal()
}

ImGui::Render();
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());

if (GetGLContextVersion().m_Major >= 3)
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
else
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());

SDL_GL_SwapWindow(m_WindowImpl.get());
}
@@ -208,3 +227,8 @@ void Window::CustomDeleters::operator()(ImGuiContext* context) const
{
ImGui::DestroyContext(context);
}

GLContextVersion Window::GetGLContextVersion() const
{
return m_GLContext->GetVersion();
}
2 changes: 1 addition & 1 deletion submodules/imgui
Submodule imgui updated 1 files
+7 −1 CMakeLists.txt