# HG changeset patch # User Emmanuel Gil Peyrot # Date 1386206191 -3600 # Node ID 5e3e0b09a531ced9f466897cb5b3c812d7284c83 # Parent b39ad30c6620768ab6ae029381e44396483ea181 Move the OpenGL backend to its own package. diff --git a/eosd b/eosd --- 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 diff --git a/pytouhou/ui/opengl/__init__.py b/pytouhou/ui/opengl/__init__.py new file mode 100644 diff --git a/pytouhou/ui/background.pxd b/pytouhou/ui/opengl/background.pxd rename from pytouhou/ui/background.pxd rename to pytouhou/ui/opengl/background.pxd diff --git a/pytouhou/ui/background.pyx b/pytouhou/ui/opengl/background.pyx rename from pytouhou/ui/background.pyx rename to pytouhou/ui/opengl/background.pyx diff --git a/pytouhou/ui/gamerenderer.pxd b/pytouhou/ui/opengl/gamerenderer.pxd rename from pytouhou/ui/gamerenderer.pxd rename to pytouhou/ui/opengl/gamerenderer.pxd diff --git a/pytouhou/ui/gamerenderer.pyx b/pytouhou/ui/opengl/gamerenderer.pyx rename from pytouhou/ui/gamerenderer.pyx rename to pytouhou/ui/opengl/gamerenderer.pyx diff --git a/pytouhou/ui/renderer.pxd b/pytouhou/ui/opengl/renderer.pxd rename from pytouhou/ui/renderer.pxd rename to pytouhou/ui/opengl/renderer.pxd diff --git a/pytouhou/ui/renderer.pyx b/pytouhou/ui/opengl/renderer.pyx rename from pytouhou/ui/renderer.pyx rename to pytouhou/ui/opengl/renderer.pyx diff --git a/pytouhou/ui/shader.pxd b/pytouhou/ui/opengl/shader.pxd rename from pytouhou/ui/shader.pxd rename to pytouhou/ui/opengl/shader.pxd diff --git a/pytouhou/ui/shader.pyx b/pytouhou/ui/opengl/shader.pyx rename from pytouhou/ui/shader.pyx rename to pytouhou/ui/opengl/shader.pyx diff --git a/pytouhou/ui/shaders/__init__.py b/pytouhou/ui/opengl/shaders/__init__.py rename from pytouhou/ui/shaders/__init__.py rename to pytouhou/ui/opengl/shaders/__init__.py diff --git a/pytouhou/ui/shaders/eosd.py b/pytouhou/ui/opengl/shaders/eosd.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): diff --git a/pytouhou/ui/sprite.pxd b/pytouhou/ui/opengl/sprite.pxd rename from pytouhou/ui/sprite.pxd rename to pytouhou/ui/opengl/sprite.pxd diff --git a/pytouhou/ui/sprite.pyx b/pytouhou/ui/opengl/sprite.pyx 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): diff --git a/pytouhou/ui/texture.pxd b/pytouhou/ui/opengl/texture.pxd rename from pytouhou/ui/texture.pxd rename to pytouhou/ui/opengl/texture.pxd diff --git a/pytouhou/ui/texture.pyx b/pytouhou/ui/opengl/texture.pyx rename from pytouhou/ui/texture.pyx rename to pytouhou/ui/opengl/texture.pyx diff --git a/pytouhou/ui/window.pyx b/pytouhou/ui/window.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: diff --git a/setup.py b/setup.py --- 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)