Mercurial > touhou
annotate pytouhou/ui/opengl/backend.pyx @ 554:653a9f087673
Make both double- and single-buffer available on the CLI, but default to SDL’s default.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 30 May 2014 04:06:09 +0200 |
parents | 8f51e34d911c |
children | 98380e4a0ee5 |
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 IF USE_GLEW: |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 from pytouhou.lib.opengl cimport glewInit |
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 GameRenderer = None |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 def init(options): |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 global flavor, version, major, minor, double_buffer, is_legacy, GameRenderer |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 flavor = options['flavor'] |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 assert flavor in ('core', 'es', 'compatibility', 'legacy') |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 version = options['version'] |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 major = int(version) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 minor = <int>(version * 10) % 10 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 |
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
|
26 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
|
27 double_buffer = maybe_double_buffer if maybe_double_buffer is not None else -1 |
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 |
553
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 is_legacy = flavor == 'legacy' |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 #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
|
32 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 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
|
34 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 def create_window(title, x, y, width, height): |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 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
|
44 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 #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
|
46 if not is_legacy: |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 flags |= sdl.WINDOW_RESIZABLE |
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 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
|
50 window.gl_create_context() |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 if USE_GLEW: |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 if glewInit() != 0: |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 raise Exception('GLEW init fail!') |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 # Initialize OpenGL |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 glEnable(GL_BLEND) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 if is_legacy: |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 glEnable(GL_TEXTURE_2D) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 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
|
61 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
|
62 glEnableClientState(GL_COLOR_ARRAY) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 glEnableClientState(GL_VERTEX_ARRAY) |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 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
|
65 |
8f51e34d911c
Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 return window |