Import WX
Import pyglet
From pyglet. GL import *
Import sys
If SYS. Platform = 'win32 ':
From pyglet. Window. Win32 import _ USER32
From pyglet. GL import WGL
Elif SYS. Platform. startswith ('linux '):
From pyglet. image. codecs. gdkpixbuf2 import gdk
From pyglet. GL import Glx
Class abstractcanvas (pyglet. event. eventdispatcher ):
Def _ init _ (self, context, config ):
# Create context (same as pyglet. Window. Window. _ init __)
If not config:
Platform = pyglet. Window. get_platform ()
Display = platform. get_default_display ()
Screen = display. get_screens () [0]
For template_config in [
Pyglet. gl. config (double_buffer = true, depth_size = 24 ),
Pyglet. gl. config (double_buffer = true, depth_size = 16)]:
Try:
Config = screen. get_best_config (template_config)
Break
Failed t pyglet. Window. nosuchconfigexception:
Pass
If not config:
Raise pyglet. Window. nosuchconfigexception (
'No standard config is available .')
If not config. is_complete ():
Config = screen. get_best_config (config)
If not context:
Context = config. create_context (pyglet. gl. current_context)
Self. _ display = display
Self. _ screen = screen
Self. _ Config = config
Self. _ context = Context
Def on_resize (self, width, height ):
Self. switch_to ()
Glviewport (0, 0, width, height)
Glmatrixmode (gl_projection)
Glloadidentity ()
Glortho (0, width, 0, height,-1, 1)
Glmatrixmode (gl_modelview)
Def switch_to (Self ):
Self. _ switch_to_impl ()
Self. _ context. set_current ()
Pyglet. gl. gl_info.set_active_context ()
Pyglet. gl. glu_info.set_active_context ()
Def _ switch_to_impl (Self ):
Raise notimplementederror ('abstract ')
Def flip (Self ):
Raise notimplementederror ('abstract ')
Abstractcanvas. register_event_type ('on _ draw ')
Abstractcanvas. register_event_type ('on _ resize ')
Class abstractwxcanvas (wx. Panel, abstractcanvas ):
Def _ init _ (self, parent, id =-1, Config = none, context = none ):
Wx. Window. _ init _ (self, parent, ID, style = wx. full_repaint_on_resize)
Abstractcanvas. _ init _ (self, config, context)
# Self. setbackgroundstyle (wx. bg_style_custom)
Self. BIND (wx. evt_paint, self. _ onpaint)
Self. BIND (wx. evt_size, self. _ onsize)
Self. BIND (wx. evt_erase_background, self. _ onerasebackground)
Def _ onpaint (self, event ):
# Wx handler for evt_paint
Wx. paintdc (Self)
Self. dispatch_event ('on _ draw ')
Self. Flip ()
Def _ onerasebackground (self, event ):
Pass
Def _ onsize (self, event ):
# Wx handler for evt_size
Width, height = self. getclientsize ()
Self. dispatch_event ('on _ resize ', width, height)
Class win32wxcanvas (abstractwxcanvas ):
Def _ init _ (self, parent, id =-1, Config = none, context = none ):
Abstractwxcanvas. _ init _ (self, parent, ID, config, context)
Self. _ hwnd = self. gethandle ()
Self. _ Dc = _ user32.getdc (self. _ hwnd)
Self. _ context. _ set_window (Self)
Self. _ wgl_context = self. _ context. _ Context
Self. switch_to ()
Def _ switch_to_impl (Self ):
WGL. wglmakecurrent (self. _ DC, self. _ wgl_context)
Def flip (Self ):
WGL. wglswaplayerbuffers (self. _ DC, WGL. wgl_swap_main_plane)
Class gtkwxcanvas (abstractwxcanvas ):
_ Window = none
Def _ init _ (self, parent, id =-1, Config = none, context = none ):
Super (gtkwxcanvas, self). _ init _ (parent, ID, config, context)
Self. _ glx_context = self. _ context. _ Context
Self. _ x_display = self. _ config. _ display
Self. _ x_screen_id = self. _ screen. _ x_screen_id
# Glx 1.3 doesn' t work here (badmatch error)
Self. _ glx_1_3 = false # self. _ display.info. have_version (1, 3)
Def _ onpaint (self, event ):
If not self. _ window:
Self. _ window = self. gethandle ()
# Can also get the gdk window... (not used yet)
Gdk_window = gdk. gdk_window_lookup (self. _ window)
If self. _ glx_1_3:
Self. _ glx_window = Glx. glxcreatewindow (self. _ x_display,
Self. _ config. _ fbconfig, self. _ window, none)
Self. switch_to ()
Super (gtkwxcanvas, self). _ onpaint (Event)
Def _ switch_to_impl (Self ):
If not self. _ window:
Return
If self. _ glx_1_3:
Glx. glxmakecontextcurrent (self. _ x_display,
Self. _ glx_window, self. _ glx_window, self. _ glx_context)
Else:
Glx. glxmakecurrent (self. _ x_display, self. _ window, self. _ glx_context)
Def flip (Self ):
If not self. _ window:
Return
If self. _ glx_1_3:
Glx. glxswapbuffers (self. _ x_display, self. _ glx_window)
Else:
Glx. glxswapbuffers (self. _ x_display, self. _ window)
If SYS. Platform = 'win32 ':
Wxcanvas = win32wxcanvas
Elif SYS. Platform. startswith ('linux '):
Wxcanvas = gtkwxcanvas
Else:
Assert false
Class testcanvas (wxcanvas ):
Label = pyglet. Text. Label ('hello, world', font_size = 48,
Anchor_x = 'center', anchor_y = 'center ')
Batch = none;
Def createpics (Self ):
Foo_image = pyglet.image.load('foo.png ')
Self. Batch = pyglet. Graphics. Batch ()
Self. Sprite = pyglet. Sprite. Sprite (foo_image, batch = self. Batch)
Self. Sprite. x = 0
Self. Sprite. Y = 0
# Self. sprite2 = pyglet. Sprite. Sprite (foo_image, batch = self. Batch)
# Self. sprite2.x = self. spritepos [0] + 100
# Self. sprite2.y = self. spritepos [1] + 200
Def on_draw (Self ):
Width, height = self. getclientsize ()
Glclear (gl_color_buffer_bit)
Self. Label. Text = 'opengl % s' % pyglet. gl. gl_info.get_version ()
Self. Label. x = width // 2
Self. Label. Y = height // 2
Self. Label. Draw ()
If self. Batch:
Self. batch. Draw ()
Class testframe (wx. Frame ):
Def _ init _ (self, parent, title ):
Wx. Frame. _ init _ (self, parent,-1, title, size = (640,480 ))
Canvas = testcanvas (Self)
Canvas. createpics ()
Class testapp (wx. app ):
Def oninit (Self ):
Frame = testframe (none, 'test wxpython + pyglet ')
# Self. settopwindow (FRAME)
Frame. Show (true)
Return true
If _ name _ = '_ main __':
Testapp (redirect = false). mainloop ()
--------------------------------------------------------
The value of this Code is long.
For the combination of pyopengl and wxpython, The wxpython official solution has been provided: use OpenGL controls. But pyopengl and pyglet are different, pyglet is the basic library of cocos2d-python (cocos2d-python is the originator of the entire cocos2d family ). So after understanding the director of pyglet and cocos2d, we should be able to integrate wxpython and cocos2d. I already have some ideas. I plan to take some time to look at them.