changeset 513:5e3e0b09a531

Move the OpenGL backend to its own package.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 05 Dec 2013 02:16:31 +0100
parents b39ad30c6620
children 3d4410de78e1
files eosd pytouhou/ui/background.pxd pytouhou/ui/background.pyx pytouhou/ui/gamerenderer.pxd pytouhou/ui/gamerenderer.pyx pytouhou/ui/opengl/__init__.py pytouhou/ui/opengl/background.pxd pytouhou/ui/opengl/background.pyx pytouhou/ui/opengl/gamerenderer.pxd pytouhou/ui/opengl/gamerenderer.pyx pytouhou/ui/opengl/renderer.pxd pytouhou/ui/opengl/renderer.pyx pytouhou/ui/opengl/shader.pxd pytouhou/ui/opengl/shader.pyx pytouhou/ui/opengl/shaders/__init__.py pytouhou/ui/opengl/shaders/eosd.py pytouhou/ui/opengl/sprite.pxd pytouhou/ui/opengl/sprite.pyx pytouhou/ui/opengl/texture.pxd pytouhou/ui/opengl/texture.pyx pytouhou/ui/renderer.pxd pytouhou/ui/renderer.pyx pytouhou/ui/shader.pxd pytouhou/ui/shader.pyx pytouhou/ui/shaders/__init__.py pytouhou/ui/shaders/eosd.py pytouhou/ui/sprite.pxd pytouhou/ui/sprite.pyx pytouhou/ui/texture.pxd pytouhou/ui/texture.pyx pytouhou/ui/window.pyx setup.py
diffstat 18 files changed, 58 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/eosd
+++ b/eosd
@@ -58,7 +58,7 @@ import sys
 import logging
 
 if args.backend == 'opengl':
-    from pytouhou.ui.gamerenderer import GameRenderer
+    from pytouhou.ui.opengl.gamerenderer import GameRenderer
     opengl = True
 elif args.backend == 'sdl':
     from pytouhou.ui.sdl.gamerenderer import GameRenderer
new file mode 100644
rename from pytouhou/ui/background.pxd
rename to pytouhou/ui/opengl/background.pxd
rename from pytouhou/ui/background.pyx
rename to pytouhou/ui/opengl/background.pyx
rename from pytouhou/ui/gamerenderer.pxd
rename to pytouhou/ui/opengl/gamerenderer.pxd
rename from pytouhou/ui/gamerenderer.pyx
rename to pytouhou/ui/opengl/gamerenderer.pyx
rename from pytouhou/ui/renderer.pxd
rename to pytouhou/ui/opengl/renderer.pxd
rename from pytouhou/ui/renderer.pyx
rename to pytouhou/ui/opengl/renderer.pyx
rename from pytouhou/ui/shader.pxd
rename to pytouhou/ui/opengl/shader.pxd
rename from pytouhou/ui/shader.pyx
rename to pytouhou/ui/opengl/shader.pyx
rename from pytouhou/ui/shaders/__init__.py
rename to pytouhou/ui/opengl/shaders/__init__.py
rename from pytouhou/ui/shaders/eosd.py
rename to pytouhou/ui/opengl/shaders/eosd.py
--- a/pytouhou/ui/shaders/eosd.py
+++ b/pytouhou/ui/opengl/shaders/eosd.py
@@ -13,7 +13,7 @@
 ##
 
 
-from pytouhou.ui.shader import Shader
+from ..shader import Shader
 
 
 class GameShader(Shader):
rename from pytouhou/ui/sprite.pxd
rename to pytouhou/ui/opengl/sprite.pxd
rename from pytouhou/ui/sprite.pyx
rename to pytouhou/ui/opengl/sprite.pyx
--- a/pytouhou/ui/sprite.pyx
+++ b/pytouhou/ui/opengl/sprite.pyx
@@ -16,7 +16,7 @@
 from libc.math cimport M_PI as pi
 
 from pytouhou.utils.matrix cimport Matrix
-from pytouhou.ui.renderer cimport Texture #XXX
+from .renderer cimport Texture #XXX
 
 
 cpdef object get_sprite_rendering_data(Sprite sprite):
rename from pytouhou/ui/texture.pxd
rename to pytouhou/ui/opengl/texture.pxd
rename from pytouhou/ui/texture.pyx
rename to pytouhou/ui/opengl/texture.pyx
--- a/pytouhou/ui/window.pyx
+++ b/pytouhou/ui/window.pyx
@@ -12,13 +12,14 @@
 ## GNU General Public License for more details.
 ##
 
-from pytouhou.lib.opengl cimport \
-         (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
-          GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
-          GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY)
+IF USE_OPENGL:
+    from pytouhou.lib.opengl cimport \
+             (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
+              GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
+              GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY)
 
-IF USE_GLEW:
-    from pytouhou.lib.opengl cimport glewInit
+    IF USE_GLEW:
+        from pytouhou.lib.opengl cimport glewInit
 
 
 cdef class Clock:
@@ -94,7 +95,7 @@ cdef class Window:
 
         flags = sdl.WINDOW_SHOWN
 
-        if opengl:
+        if USE_OPENGL and opengl:
             sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2)
             sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, 1)
             sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, int(double_buffer))
@@ -111,7 +112,7 @@ cdef class Window:
                               640, 480, #XXX
                               flags)
 
-        if opengl:
+        if USE_OPENGL and opengl:
             self.win.gl_create_context()
 
             IF USE_GLEW:
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,7 @@ import os
 import sys
 from distutils.core import setup
 from distutils.extension import Extension
-from subprocess import check_output
+from subprocess import check_output, CalledProcessError
 
 # Cython is needed
 try:
@@ -17,15 +17,28 @@ except ImportError:
 
 COMMAND = 'pkg-config'
 SDL_LIBRARIES = ['sdl2', 'SDL2_image', 'SDL2_mixer', 'SDL2_ttf']
+GL_LIBRARIES = ['gl']
 
 packages = []
 extension_names = []
 extensions = []
 
 
+# Check for gl.pc, and don’t compile the OpenGL backend if it isn’t present.
+try:
+    check_output([COMMAND] + GL_LIBRARIES)
+except CalledProcessError:
+    use_opengl = False
+else:
+    use_opengl = True
+
+
 def get_arguments(arg, libraries):
     try:
         return check_output([COMMAND, arg] + libraries).split()
+    except CalledProcessError:
+        # The error has already been displayed, just exit.
+        sys.exit(1)
     except OSError:
         print('You don’t seem to have pkg-config installed. Please get a copy '
               'from http://pkg-config.freedesktop.org/ and install it.\n'
@@ -34,32 +47,50 @@ def get_arguments(arg, libraries):
         sys.exit(1)
 
 
+try:
+    sdl_args = {'extra_compile_args': get_arguments('--cflags', SDL_LIBRARIES),
+                'extra_link_args': get_arguments('--libs', SDL_LIBRARIES)}
+
+    if use_opengl:
+        opengl_args = {'extra_compile_args': get_arguments('--cflags', GL_LIBRARIES + SDL_LIBRARIES),
+                       'extra_link_args': get_arguments('--libs', GL_LIBRARIES + SDL_LIBRARIES)}
+except CalledProcessError:
+    # The error has already been displayed, just exit.
+    sys.exit(1)
+except OSError:
+    # pkg-config is needed too.
+    print('You don’t seem to have pkg-config installed. Please get a copy '
+          'from http://pkg-config.freedesktop.org/ and install it.\n'
+          'If you prefer to use it from somewhere else, just modify the '
+          'setup.py script.')
+    sys.exit(1)
+
+
 for directory, _, files in os.walk('pytouhou'):
     package = directory.replace(os.path.sep, '.')
     packages.append(package)
-    if package not in ('pytouhou.game', 'pytouhou.lib', 'pytouhou.ui', 'pytouhou.utils', 'pytouhou.ui.sdl'):
+    if package not in ('pytouhou.game', 'pytouhou.lib', 'pytouhou.utils') and not package.startswith('pytouhou.ui'):
         continue
-    if package == 'pytouhou.ui':
-        compile_args = get_arguments('--cflags', ['gl'] + SDL_LIBRARIES)
-        link_args = get_arguments('--libs', ['gl'] + SDL_LIBRARIES)
-    elif package == 'pytouhou.ui.sdl':
-        compile_args = get_arguments('--cflags', SDL_LIBRARIES)
-        link_args = get_arguments('--libs', SDL_LIBRARIES)
+    if package == 'pytouhou.ui' or package == 'pytouhou.ui.sdl':
+        package_args = sdl_args
+    elif package == 'pytouhou.ui.opengl':
+        package_args = opengl_args
     else:
-        compile_args = None
-        link_args = None
+        package_args = {}
     for filename in files:
         if (filename.endswith('.pyx') or filename.endswith('.py') and
                 not filename == '__init__.py'):
             extension_name = '%s.%s' % (package, os.path.splitext(filename)[0])
             extension_names.append(extension_name)
             if extension_name == 'pytouhou.lib.sdl':
-                compile_args = get_arguments('--cflags', SDL_LIBRARIES)
-                link_args = get_arguments('--libs', SDL_LIBRARIES)
+                compile_args = sdl_args
+            elif extension_name == 'pytouhou.ui.window' and use_opengl:
+                compile_args = opengl_args
+            else:
+                compile_args = package_args
             extensions.append(Extension(extension_name,
                                         [os.path.join(directory, filename)],
-                                        extra_compile_args=compile_args,
-                                        extra_link_args=link_args))
+                                        **compile_args))
 
 
 # TODO: find a less-intrusive, cleaner way to do this...
@@ -87,6 +118,7 @@ setup(name='PyTouhou',
                             compile_time_env={'MAX_TEXTURES': 1024,
                                               'MAX_ELEMENTS': 640 * 4 * 3,
                                               'MAX_CHANNELS': 26,
+                                              'USE_OPENGL': use_opengl,
                                               'USE_GLEW': is_windows}),
       scripts=['eosd', 'anmviewer'],
       **extra)