comparison pytouhou/ui/opengl/backend.pyx @ 635:80687f258001

Make sdl.Window inherit from gui.Window, so we can swap implementations.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 14 Apr 2016 21:18:03 +0100
parents a6af3ff86612
children 4fa0a8e7d941
comparison
equal deleted inserted replaced
634:5270c34b4c00 635:80687f258001
1 from pytouhou.lib import sdl 1 cimport pytouhou.lib.gui as gui
2 from pytouhou.lib cimport sdl 2 from pytouhou.lib.gui import Error as GUIError
3 from pytouhou.lib.sdl cimport Window 3 from .backend_sdl import create_sdl_window
4 4
5 from pytouhou.lib.opengl cimport \ 5 from pytouhou.lib.opengl cimport \
6 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND, 6 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
7 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, 7 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
8 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY, 8 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
25 25
26 global profile, major, minor, double_buffer, is_legacy, GameRenderer 26 global profile, major, minor, double_buffer, is_legacy, GameRenderer
27 27
28 flavor = options['flavor'] 28 flavor = options['flavor']
29 assert flavor in ('core', 'es', 'compatibility', 'legacy') 29 assert flavor in ('core', 'es', 'compatibility', 'legacy')
30 profile = (sdl.GL_CONTEXT_PROFILE_CORE if flavor == 'core' else 30 profile = flavor
31 sdl.GL_CONTEXT_PROFILE_ES if flavor == 'es' else
32 sdl.GL_CONTEXT_PROFILE_COMPATIBILITY)
33
34 version = str(options['version']) 31 version = str(options['version'])
35 assert len(version) == 3 and version[1] == '.' 32 assert len(version) == 3 and version[1] == '.'
36 major = int(version[0]) 33 major = int(version[0])
37 minor = int(version[2]) 34 minor = int(version[2])
38 35
89 86
90 87
91 def create_window(title, x, y, width, height, swap_interval): 88 def create_window(title, x, y, width, height, swap_interval):
92 '''Create a window (using SDL) and an OpenGL context.''' 89 '''Create a window (using SDL) and an OpenGL context.'''
93 90
94 sdl.gl_set_attribute(sdl.GL_CONTEXT_PROFILE_MASK, profile) 91 cdef gui.Window window
95 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, major) 92 window = create_sdl_window(title, x, y, width, height)
96 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, minor) 93 window.create_gl_context()
97 sdl.gl_set_attribute(sdl.GL_RED_SIZE, 8)
98 sdl.gl_set_attribute(sdl.GL_GREEN_SIZE, 8)
99 sdl.gl_set_attribute(sdl.GL_BLUE_SIZE, 8)
100 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24 if is_legacy else 0)
101 if double_buffer >= 0:
102 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, double_buffer)
103
104 flags = sdl.WINDOW_SHOWN | sdl.WINDOW_OPENGL
105
106 # Legacy contexts don’t support our required extensions for scaling.
107 if not is_legacy:
108 flags |= sdl.WINDOW_RESIZABLE
109
110 window = Window(title, x, y, width, height, flags)
111 window.gl_create_context()
112
113 discover_features() 94 discover_features()
114
115 # If we can’t use scaling but have previously created a resizable window,
116 # recreate it unresizable.
117 if not use_scaled_rendering and flags & sdl.WINDOW_RESIZABLE:
118 flags &= ~sdl.WINDOW_RESIZABLE
119 window = Window(title, x, y, width, height, flags)
120 window.gl_create_context()
121 95
122 if use_debug_group: 96 if use_debug_group:
123 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "OpenGL initialisation") 97 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "OpenGL initialisation")
124 98
125 # Initialize OpenGL 99 # Initialize OpenGL
142 if use_debug_group: 116 if use_debug_group:
143 glPopDebugGroup() 117 glPopDebugGroup()
144 118
145 if swap_interval is not None: 119 if swap_interval is not None:
146 try: 120 try:
147 sdl.gl_set_swap_interval(swap_interval) 121 window.set_swap_interval(swap_interval)
148 except sdl.SDLError: 122 except GUIError:
149 # The OpenGL context doesn’t support setting the swap interval, 123 # The OpenGL context doesn’t support setting the swap interval,
150 # we’ll probably fallback to SDL_Delay-based clocking. 124 # we’ll probably fallback to SDL_Delay-based clocking.
151 pass 125 pass
152 126
153 return window 127 return window