annotate pytouhou/ui/opengl/backend.pyx @ 633:38928d4d6709

package_data doesn’t work with no package specified, add the pytouhou package back.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 23 May 2015 11:54:20 +0200
parents a6af3ff86612
children 80687f258001
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
589
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
1 from pytouhou.lib import sdl
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 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
3 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
4
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 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
6 (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
7 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
8 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
9 glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup,
585
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 583
diff changeset
10 epoxy_gl_version, epoxy_is_desktop_gl, epoxy_has_gl_extension,
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
11 GL_PRIMITIVE_RESTART, glPrimitiveRestartIndex, glPixelStorei,
611
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 610
diff changeset
12 GL_PACK_INVERT_MESA, GL_QUADS, GL_TRIANGLE_STRIP, GL_TRIANGLES)
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13
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 GameRenderer = None
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 def init(options):
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
19 '''
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
20 Initialize the OpenGL module, and raise if something bad prevents it from
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
21 working.
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
22 '''
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
595
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
24 cdef str flavor
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
25
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
26 global profile, major, minor, double_buffer, is_legacy, GameRenderer
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
27
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
28 flavor = options['flavor']
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
29 assert flavor in ('core', 'es', 'compatibility', 'legacy')
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
30 profile = (sdl.GL_CONTEXT_PROFILE_CORE if flavor == 'core' else
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
31 sdl.GL_CONTEXT_PROFILE_ES if flavor == 'es' else
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
32 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
33
556
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
34 version = str(options['version'])
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
35 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
36 major = int(version[0])
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
37 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
38
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
39 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
40 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
41
595
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
42 is_legacy = flavor == 'legacy' or flavor == 'compatibility' and major < 2
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
43
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 #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
45
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 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
47
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 612
diff changeset
49 cdef bint discover_features() except True:
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
50 '''Discover which features are supported by our context.'''
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
51
610
1b31169dc344 Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 609
diff changeset
52 global use_debug_group, use_vao, use_primitive_restart, use_framebuffer_blit, use_pack_invert, use_scaled_rendering
593
974decb8df4f Only selects between GL_TRIANGLE_STRIP and GL_TRIANGLES once, in the backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 592
diff changeset
53 global primitive_mode
974decb8df4f Only selects between GL_TRIANGLE_STRIP and GL_TRIANGLES once, in the backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 592
diff changeset
54 global shader_header
595
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
55 global is_legacy
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
56
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
57 version = epoxy_gl_version()
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
58 is_desktop = epoxy_is_desktop_gl()
610
1b31169dc344 Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 609
diff changeset
59 is_legacy = is_legacy or (is_desktop and version < 20)
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
60
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
61 use_debug_group = (is_desktop and version >= 43) or epoxy_has_gl_extension('GL_KHR_debug')
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
62 use_vao = (is_desktop and version >= 30) or epoxy_has_gl_extension('GL_ARB_vertex_array_object')
585
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 583
diff changeset
63 use_primitive_restart = (is_desktop and version >= 31)
587
6c9d8a3d853f Use ARB_framebuffer_blit instead of a second rendering pass for scaled rendering, if supported, and remove framebuffer stuff from the Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
64 use_framebuffer_blit = (is_desktop and version >= 30)
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
65 use_pack_invert = epoxy_has_gl_extension('GL_MESA_pack_invert')
610
1b31169dc344 Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 609
diff changeset
66 use_scaled_rendering = not is_legacy #TODO: try to use the EXT framebuffer extension.
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
67
611
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 610
diff changeset
68 primitive_mode = (GL_QUADS if is_legacy else
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 610
diff changeset
69 GL_TRIANGLE_STRIP if use_primitive_restart else
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 610
diff changeset
70 GL_TRIANGLES)
593
974decb8df4f Only selects between GL_TRIANGLE_STRIP and GL_TRIANGLES once, in the backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 592
diff changeset
71
595
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
72 if not is_legacy:
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
73 if is_desktop:
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
74 # gl_FragColor isn’t supported anymore starting with GLSL 4.2.
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
75 if version >= 42:
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
76 version = 41
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
77 try:
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
78 glsl_version = {20: 110, 21: 120, 30: 130, 31: 140, 32: 150}[version]
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
79 except KeyError:
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
80 assert version >= 33
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
81 glsl_version = version * 10
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
82 shader_header = ('#version %d\n\n' % glsl_version).encode()
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
83 else:
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
84 # The attribute keyword isn’t supported past GLSL ES 3.0.
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
85 if version >= 30:
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
86 version = 20
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
87 glsl_version = {20: '100', 30: '300 es'}[version]
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
88 shader_header = ('#version %s\n\nprecision highp float;\n\n' % glsl_version).encode()
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
89
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
90
589
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
91 def create_window(title, x, y, width, height, swap_interval):
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
92 '''Create a window (using SDL) and an OpenGL context.'''
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
93
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
94 sdl.gl_set_attribute(sdl.GL_CONTEXT_PROFILE_MASK, profile)
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 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
96 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, minor)
612
73f134f84c7f Request a RGB888 context, since SDL2’s default of RGB332 sucks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 611
diff changeset
97 sdl.gl_set_attribute(sdl.GL_RED_SIZE, 8)
73f134f84c7f Request a RGB888 context, since SDL2’s default of RGB332 sucks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 611
diff changeset
98 sdl.gl_set_attribute(sdl.GL_GREEN_SIZE, 8)
73f134f84c7f Request a RGB888 context, since SDL2’s default of RGB332 sucks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 611
diff changeset
99 sdl.gl_set_attribute(sdl.GL_BLUE_SIZE, 8)
609
23b9418e4b2f Remove generic usage of GL_DRAW_FRAMEBUFFER which was introduced in GL 3.0 with framebuffer_blit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 595
diff changeset
100 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24 if is_legacy else 0)
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
101 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
102 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
103
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 flags = sdl.WINDOW_SHOWN | sdl.WINDOW_OPENGL
610
1b31169dc344 Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 609
diff changeset
105
1b31169dc344 Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 609
diff changeset
106 # Legacy contexts don’t support our required extensions for scaling.
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 if not is_legacy:
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 flags |= sdl.WINDOW_RESIZABLE
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 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
111 window.gl_create_context()
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112
583
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
113 discover_features()
47cf4e3d159d Use libepoxy to discover the actual GL version we are using, and available extensions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
114
610
1b31169dc344 Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 609
diff changeset
115 # If we can’t use scaling but have previously created a resizable window,
1b31169dc344 Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 609
diff changeset
116 # recreate it unresizable.
1b31169dc344 Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 609
diff changeset
117 if not use_scaled_rendering and flags & sdl.WINDOW_RESIZABLE:
595
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
118 flags &= ~sdl.WINDOW_RESIZABLE
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
119 window = Window(title, x, y, width, height, flags)
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
120 window.gl_create_context()
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
121
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
122 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
123 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "OpenGL initialisation")
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
124
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 # Initialize OpenGL
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 glEnable(GL_BLEND)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 if is_legacy:
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 glEnable(GL_TEXTURE_2D)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 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
130 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
131 glEnableClientState(GL_COLOR_ARRAY)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 glEnableClientState(GL_VERTEX_ARRAY)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
134
585
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 583
diff changeset
135 if use_primitive_restart:
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 583
diff changeset
136 glEnable(GL_PRIMITIVE_RESTART)
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 583
diff changeset
137 glPrimitiveRestartIndex(0xFFFF);
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 583
diff changeset
138
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
139 if use_pack_invert:
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
140 glPixelStorei(GL_PACK_INVERT_MESA, True)
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
141
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
142 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
143 glPopDebugGroup()
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144
589
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
145 if swap_interval is not None:
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
146 try:
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
147 sdl.gl_set_swap_interval(swap_interval)
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
148 except sdl.SDLError:
595
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
149 # The OpenGL context doesn’t support setting the swap interval,
b7b4a234bf70 Fix legacy OpenGL support, and detect the absence of non-legacy context with libepoxy.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 593
diff changeset
150 # we’ll probably fallback to SDL_Delay-based clocking.
589
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
151 pass
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
152
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 return window