Skip to content

Commit

Permalink
Merge pull request #311 from tayler6000/dev/version-2.0
Browse files Browse the repository at this point in the history
Cleaned up doc/examples
  • Loading branch information
KinoxKlark committed Nov 29, 2022
2 parents 4c08933 + a7dc7f2 commit 139b5eb
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 149 deletions.
29 changes: 14 additions & 15 deletions doc/examples/fonts.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import random
from glob import glob

import glfw
from glob import glob
from imgui.integrations.glfw import GlfwRenderer
import OpenGL.GL as gl
import glfw
import imgui
from imgui.integrations.glfw import GlfwRenderer
import os
import random
import sys


# use fonts bundled with imgui core project repository
FONTS_DIR = glob(
os.path.join(
os.path.dirname(__file__),
'..', '..', 'imgui-cpp', 'misc', 'fonts', "*.ttf"
os.path.dirname(__file__), "..", "..", "imgui-cpp", "misc", "fonts", "*.ttf"
)
)

Expand Down Expand Up @@ -41,14 +42,14 @@ def main():
io.fonts.clear()

# set global font scaling
io.font_global_scale = 1. / font_scaling_factor
io.font_global_scale = 1.0 / font_scaling_factor

# dictionary of font objects from our font directory
fonts = {
os.path.split(font_path)[-1]: io.fonts.add_font_from_file_ttf(
font_path,
FONT_SIZE_IN_PIXELS * font_scaling_factor,
io.fonts.get_glyph_ranges_latin()
io.fonts.get_glyph_ranges_latin(),
)
for font_path in FONTS_DIR
}
Expand Down Expand Up @@ -80,7 +81,7 @@ def main():
imgui.text("This window uses same custom font for all widgets")
imgui.end()

gl.glClearColor(1., 1., 1., 1)
gl.glClearColor(1.0, 1.0, 1.0, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

imgui.render()
Expand All @@ -97,7 +98,7 @@ def impl_glfw_init():

if not glfw.init():
print("Could not initialize OpenGL context")
exit(1)
sys.exit(1)

# OS X supports only forward-compatible core profiles from 3.2
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
Expand All @@ -107,15 +108,13 @@ def impl_glfw_init():
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
)
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)
sys.exit(1)

return window

Expand Down
76 changes: 40 additions & 36 deletions doc/examples/integrations_all_in_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import

import OpenGL.GL as gl
import imgui
import sys


backend = "pygame"
if "sdl2" in sys.argv:
backend = "sdl2"
Expand All @@ -30,45 +33,49 @@
from cocos.director import director
from imgui.integrations.cocos2d import ImguiLayer

import OpenGL.GL as gl
import imgui


def main_sdl2():
def 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)
sys.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, 8)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG)
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)
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)
sys.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)
sys.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)
sys.exit(1)
return window, gl_context

window, gl_context = pysdl2_init()
renderer = SDL2Renderer(window)
running = True
Expand All @@ -82,7 +89,7 @@ def pysdl2_init():
renderer.process_inputs()
imgui.new_frame()
on_frame()
gl.glClearColor(1., 1., 1., 1)
gl.glClearColor(1.0, 1.0, 1.0, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
imgui.render()
SDL_GL_SwapWindow(window)
Expand All @@ -96,33 +103,33 @@ def main_pygame():
pygame.init()
size = 800, 600
pygame.display.set_mode(size, pygame.DOUBLEBUF | pygame.OPENGL | pygame.RESIZABLE)

imgui.create_context()
impl = PygameRenderer()

io = imgui.get_io()
io.fonts.add_font_default()
io.display_size = size

show_custom_window = True

while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
sys.exit(0)
impl.process_event(event)
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
"Quit", "Cmd+Q", False, True
)

if clicked_quit:
exit(1)
sys.exit(0)

imgui.end_menu()
imgui.end_main_menu_bar()
Expand All @@ -133,9 +140,9 @@ def main_pygame():
is_expand, show_custom_window = imgui.begin("Custom window", True)
if is_expand:
imgui.text("Bar")
imgui.text_colored("Eggs", 0.2, 1., 0.)
imgui.text_colored("Eggs", 0.2, 1.0, 0.0)
imgui.end()

# note: cannot use screen.fill((1, 1, 1)) because pygame's screen
# does not support fill() on OpenGL sufraces
gl.glClearColor(1, 1, 1, 1)
Expand All @@ -151,30 +158,29 @@ def glfw_init():
window_name = "minimal ImGui/GLFW3 example"
if not glfw.init():
print("Could not initialize OpenGL context")
exit(1)
sys.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
)
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)
sys.exit(1)
return window

window = glfw_init()
impl = GlfwRenderer(window)
while not glfw.window_should_close(window):
glfw.poll_events()
impl.process_inputs()
imgui.new_frame()
on_frame()
gl.glClearColor(1., 1., 1., 1)
gl.glClearColor(1.0, 1.0, 1.0, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
imgui.render()
impl.render(imgui.get_draw_data())
Expand All @@ -195,7 +201,7 @@ def draw(self, *args, **kwargs):
self.process_inputs()
imgui.new_frame()
on_frame()
gl.glClearColor(1., 1., 1., 1)
gl.glClearColor(1.0, 1.0, 1.0, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
imgui.render()

Expand All @@ -209,17 +215,15 @@ def draw(self, *args, **kwargs):
def on_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
)
clicked_quit, selected_quit = imgui.menu_item("Quit", "Cmd+Q", False, True)
if clicked_quit:
exit(1)
sys.exit(0)
imgui.end_menu()
imgui.end_main_menu_bar()
imgui.show_test_window()
imgui.begin("Custom window", True)
imgui.text("Bar")
imgui.text_colored("Eggs", 0.2, 1., 0.)
imgui.text_colored("Eggs", 0.2, 1.0, 0.0)
imgui.end()


Expand Down
19 changes: 10 additions & 9 deletions doc/examples/integrations_cocos2d.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import absolute_import
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cocos
from __future__ import absolute_import
from cocos.director import director

from pyglet import gl

from imgui.integrations.cocos2d import ImguiLayer
from pyglet import gl
import cocos
import imgui
import sys


class HelloWorld(ImguiLayer):
Expand All @@ -25,11 +26,11 @@ def draw(self, *args, **kwargs):
if imgui.begin_menu("File", True):

clicked_quit, selected_quit = imgui.menu_item(
"Quit", 'Cmd+Q', False, True
"Quit", "Cmd+Q", False, True
)

if clicked_quit:
exit(1)
sys.exit()

imgui.end_menu()
imgui.end_main_menu_bar()
Expand All @@ -40,10 +41,10 @@ def draw(self, *args, **kwargs):
is_expand, self.show_custom_window = imgui.begin("Custom window", True)
if is_expand:
imgui.text("Bar")
imgui.text_colored("Eggs", 0.2, 1., 0.)
imgui.text_colored("Eggs", 0.2, 1.0, 0.0)
imgui.end()

gl.glClearColor(1., 1., 1., 1)
gl.glClearColor(1.0, 1.0, 1.0, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

imgui.render()
Expand Down

0 comments on commit 139b5eb

Please sign in to comment.