Skip to content

Commit

Permalink
impl: refactor impl classes (refs #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
swistakm committed Apr 7, 2017
1 parent 861cdb7 commit a4fec87
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 136 deletions.
81 changes: 44 additions & 37 deletions doc/examples/glfw3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,24 @@


def main():
width, height = 1280, 720
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(width), int(height), window_name, None, None
)
glfw.make_context_current(window)

if not window:
glfw.terminate()
print("Could not initialize Window")
exit(1)

imgui_ctx = GlfwImpl(window)
imgui_ctx.enable()
window = impl_glfw_init()
impl = GlfwImpl(window)

opened = True

style = imgui.GuiStyle()

while not glfw.window_should_close(window):
glfw.poll_events()
imgui_ctx.new_frame()
impl.process_inputs()

imgui.new_frame()

if imgui.begin_main_menu_bar():
if imgui.begin_menu("File", True):
clicked_quit, selected_quit = imgui.menu_item("Quit", 'Cmd+Q', False, True)
if clicked_quit:
exit(1)
imgui.end_menu()
imgui.end_main_menu_bar()

imgui.show_user_guide()
imgui.show_test_window()
Expand All @@ -55,20 +38,44 @@ def main():
with imgui.styled(imgui.STYLE_ALPHA, 1):
imgui.show_metrics_window()

imgui.show_style_editor(style)

# note: this is redundant
width, height = glfw.get_framebuffer_size(window)
gl.glViewport(0, 0, int(width/2), int(height))

gl.glClearColor(114/255., 144/255., 154/255., 1)
gl.glClearColor(114 / 255., 144 / 255., 154 / 255., 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

imgui.render()
glfw.swap_buffers(window)

impl.shutdown()
imgui.shutdown()
glfw.terminate()


def impl_glfw_init():
width, height = 1280, 720
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(width), int(height), window_name, None, None
)
glfw.make_context_current(window)

if not window:
glfw.terminate()
print("Could not initialize Window")
exit(1)

return window

if __name__ == "__main__":
main()
97 changes: 49 additions & 48 deletions doc/examples/pysdl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,10 @@


def main():
width, height = 1280, 720
window_name = "minimal ImGui/SDL2 example"

if SDL_Init(SDL_INIT_EVERYTHING) < 0:
print("Error: SDL could not initialize! SDL Error: " + SDL_GetError())
return False

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8)
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 16)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)

SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, b"1")
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, b"1")

window = SDL_CreateWindow(window_name.encode('utf-8'),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height,
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE)

if window is None:
print("Error: Window could not be created! SDL Error: " + SDL_GetError())
return False

gl_context = SDL_GL_CreateContext(window)
if gl_context is None:
print("Error: Cannot create OpenGL Context! SDL Error: " + SDL_GetError())
return False

SDL_GL_MakeCurrent(window, gl_context)
if SDL_GL_SetSwapInterval(1) < 0:
print("Warning: Unable to set VSync! SDL Error: " + SDL_GetError())

imgui_ctx = SDL2Impl(window)
imgui_ctx.enable()
window, gl_context = impl_pysdl2_init()
impl = SDL2Impl(window)

opened = True
style = imgui.GuiStyle()

running = True
event = SDL_Event()
Expand All @@ -60,9 +20,10 @@ def main():
if event.type == SDL_QUIT:
running = False
break
imgui_ctx.process_event(event)
impl.process_event(event)
impl.process_inputs()

imgui_ctx.new_frame()
imgui.new_frame()

if imgui.begin_main_menu_bar():
if imgui.begin_menu("File", True):
Expand All @@ -84,21 +45,61 @@ def main():
with imgui.styled(imgui.STYLE_ALPHA, 1):
imgui.show_metrics_window()

imgui.show_style_editor(style)

gl.glViewport(0, 0, int(width / 2), int(height))
gl.glClearColor(114 / 255., 144 / 255., 154 / 255., 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

imgui.render()

SDL_GL_SwapWindow(window)

imgui_ctx.shutdown()
impl.shutdown()
SDL_GL_DeleteContext(gl_context)
SDL_DestroyWindow(window)
SDL_Quit()


def impl_pysdl2_init():
width, height = 1280, 720
window_name = "minimal ImGui/SDL2 example"

if SDL_Init(SDL_INIT_EVERYTHING) < 0:
print("Error: SDL could not initialize! SDL Error: " + SDL_GetError())
exit(1)

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8)
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 16)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)

SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, b"1")
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, b"1")

window = SDL_CreateWindow(window_name.encode('utf-8'),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height,
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE)

if window is None:
print("Error: Window could not be created! SDL Error: " + SDL_GetError())
exit(1)

gl_context = SDL_GL_CreateContext(window)
if gl_context is None:
print("Error: Cannot create OpenGL Context! SDL Error: " + SDL_GetError())
exit(1)

SDL_GL_MakeCurrent(window, gl_context)
if SDL_GL_SetSwapInterval(1) < 0:
print("Warning: Unable to set VSync! SDL Error: " + SDL_GetError())
exit(1)

return window, gl_context

if __name__ == "__main__":
main()
8 changes: 4 additions & 4 deletions doc/source/gen_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def render_snippet(
print("Could not initialize Window")
exit(1)

imgui_ctx = GlfwImpl(window)
imgui_ctx.enable()
impl = GlfwImpl(window)
glfw.poll_events()

# render target for framebuffer
Expand Down Expand Up @@ -119,13 +118,14 @@ def render_snippet(

# note: Mouse click MUST be simulated before new_frame call!
if click:
imgui_ctx.io.mouse_draw_cursor = True
impl.io.mouse_draw_cursor = True
simulate_click(click[0], click[1], m_state)
else:
# just make sure mouse state is clear
_clear_mouse()

imgui_ctx.new_frame()
impl.process_inputs()
imgui.new_frame()

with imgui.styled(imgui.STYLE_ALPHA, 1):
imgui.core.set_next_window_size(0, 0)
Expand Down
25 changes: 8 additions & 17 deletions imgui/impl/_glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@


class GlfwImpl(OpenGLBaseImpl):
def __init__(self, window):
def __init__(self, window, attach_callbacks=True):
super(GlfwImpl, self).__init__()
self.window = window

def enable(self):
# setup input callbacks
# todo: add some option to have additional callbacks
glfw.set_key_callback(self.window, self.keyboard_callback)
glfw.set_cursor_pos_callback(self.window, self.mouse_callback)
glfw.set_window_size_callback(self.window, self.resize_callback)
glfw.set_char_callback(self.window, self.char_callback)
glfw.set_scroll_callback(self.window, self.scroll_callback)
if attach_callbacks:
glfw.set_key_callback(self.window, self.keyboard_callback)
glfw.set_cursor_pos_callback(self.window, self.mouse_callback)
glfw.set_window_size_callback(self.window, self.resize_callback)
glfw.set_char_callback(self.window, self.char_callback)
glfw.set_scroll_callback(self.window, self.scroll_callback)

self.io.display_size = glfw.get_framebuffer_size(self.window)

self._map_keys()
self._gui_time = None

super(GlfwImpl, self).enable()

def _map_keys(self):
key_map = self.io.key_map

Expand Down Expand Up @@ -95,7 +91,7 @@ def mouse_callback(self, *args, **kwargs):
def scroll_callback(self, window, x_offset, y_offset):
self.io.mouse_wheel = y_offset

def new_frame(self):
def process_inputs(self):
# todo: consider moving to init
io = imgui.get_io()

Expand Down Expand Up @@ -124,8 +120,3 @@ def new_frame(self):
self.io.delta_time = 1. / 60.

self._gui_time = current_time

imgui.new_frame()

def render(self, draw_data):
super(GlfwImpl, self).render(draw_data)

0 comments on commit a4fec87

Please sign in to comment.