Mercurial > touhou
annotate pytouhou/ui/opengl/backend.pyx @ 561:bafe6361c0af
Use logging for errors in the pytouhou script.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 05 Jun 2014 18:47:31 +0200 |
parents | 1be60813f7cb |
children | b8df946d394d |
rev | line source |
---|---|
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 from pytouhou.lib cimport sdl |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 from pytouhou.lib.sdl cimport Window |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 from pytouhou.lib.opengl cimport \ |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND, |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 GameRenderer = None |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 def init(options): |
558
94725968dabb
Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
557
diff
changeset
|
14 global flavor, version, major, minor, double_buffer, is_legacy, use_vao, shader_header, GameRenderer |
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 |
556
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
16 flavor_name = options['flavor'] |
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
17 assert flavor_name in ('core', 'es', 'compatibility', 'legacy') |
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
18 flavor = (sdl.GL_CONTEXT_PROFILE_CORE if flavor_name == 'core' else |
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
19 sdl.GL_CONTEXT_PROFILE_ES if flavor_name == 'es' else |
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
20 sdl.GL_CONTEXT_PROFILE_COMPATIBILITY) |
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 |
556
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
22 version = str(options['version']) |
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
23 assert len(version) == 3 and version[1] == '.' |
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
24 major = int(version[0]) |
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
25 minor = int(version[2]) |
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 |
554
653a9f087673
Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
553
diff
changeset
|
27 maybe_double_buffer = options['double-buffer'] |
653a9f087673
Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
553
diff
changeset
|
28 double_buffer = maybe_double_buffer if maybe_double_buffer is not None else -1 |
558
94725968dabb
Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
557
diff
changeset
|
29 use_vao = (major == 3 and minor >= 1) or major > 3 |
554
653a9f087673
Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
553
diff
changeset
|
30 |
556
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
31 is_legacy = flavor_name == 'legacy' |
559
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
32 is_gles = flavor_name == 'es' |
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 |
559
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
34 if not is_gles: |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
35 try: |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
36 glsl_version = {'2.0': 110, '2.1': 120, '3.0': 130, '3.1': 140, '3.2': 150}[version] |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
37 except KeyError: |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
38 assert (major == 3 and minor == 3) or major > 3 |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
39 glsl_version = 100 * major + 10 * minor |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
40 shader_header = '#version %d\n' % glsl_version |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
41 else: |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
42 glsl_version = {'2.0': 100, '3.0': 300}[version] |
1be60813f7cb
Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
558
diff
changeset
|
43 shader_header = '#version %d\n\nprecision highp float;\n' % glsl_version |
557
0f2af7552462
Don’t hardcode GLSL version in our shaders, instead make them dependent on GL version.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
556
diff
changeset
|
44 |
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 #TODO: check for framebuffer/renderbuffer support. |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 from pytouhou.ui.opengl.gamerenderer import GameRenderer |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 def create_window(title, x, y, width, height): |
556
c34b23e29d16
Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
555
diff
changeset
|
51 sdl.gl_set_attribute(sdl.GL_CONTEXT_PROFILE_MASK, flavor) |
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, major) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, minor) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24) |
554
653a9f087673
Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
553
diff
changeset
|
55 if double_buffer >= 0: |
653a9f087673
Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
553
diff
changeset
|
56 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, double_buffer) |
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 flags = sdl.WINDOW_SHOWN | sdl.WINDOW_OPENGL |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 #TODO: legacy can support one of the framebuffer extensions. |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 if not is_legacy: |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 flags |= sdl.WINDOW_RESIZABLE |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 window = Window(title, x, y, width, height, flags) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
65 window.gl_create_context() |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 # Initialize OpenGL |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 glEnable(GL_BLEND) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 if is_legacy: |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 glEnable(GL_TEXTURE_2D) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 glHint(GL_FOG_HINT, GL_NICEST) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 glEnableClientState(GL_COLOR_ARRAY) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 glEnableClientState(GL_VERTEX_ARRAY) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 glEnableClientState(GL_TEXTURE_COORD_ARRAY) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 return window |