moon-miner/ImguiWindow.py

62 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
import glfw
import OpenGL.GL as gl
import imgui as im
from imgui.integrations.glfw import GlfwRenderer
class ImguiWindow():
def impl_glfw_init(self, w, h):
window_name = "minimal Imgui/GLFW3 example"
if not glfw.init():
print("Could not initialize OpenGL context")
exit(1)
# OS X supports only forward-compatible core profiles from 3.2
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(
int(w), int(h), window_name, None, None
)
glfw.make_context_current(window)
if not window:
glfw.terminate()
print("Could not initialize Window")
exit(1)
return window
def __init__(self, draw_fn, window_name, width, height):
im.create_context()
self.window = self.impl_glfw_init(width, height)
impl = GlfwRenderer(self.window)
while not glfw.window_should_close(self.window):
glfw.poll_events()
impl.process_inputs()
im.new_frame()
draw_fn()
gl.glClearColor(1., 1., 1., 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
im.render()
impl.render(im.get_draw_data())
glfw.swap_buffers(self.window)
impl.shutdown()
glfw.terminate()