1. Duangle
  2. Untitled project
  3. pysdl-cffi

Source

pysdl-cffi / sdl / internal.py

Leonard Ritter 16274db 
Leonard Ritter f9e58ba 

Leonard Ritter 16274db 


Leonard Ritter f9e58ba 


Leonard Ritter 8d3f870 
Leonard Ritter f9e58ba 









Leonard Ritter 8d3f870 
Leonard Ritter f9e58ba 
Leonard Ritter abb03b4 
Leonard Ritter 16274db 
Leonard Ritter 99d0d72 

Leonard Ritter 16274db 
Leonard Ritter 8d3f870 

Leonard Ritter 99d0d72 
Leonard Ritter 16274db 
Leonard Ritter 14c8075 



Leonard Ritter 99d0d72 
Leonard Ritter 14c8075 
Leonard Ritter abb03b4 
Leonard Ritter 14c8075 
Leonard Ritter 16274db 
Leonard Ritter 14c8075 
Leonard Ritter 99d0d72 
Leonard Ritter f9e58ba 
Leonard Ritter 14c8075 










Leonard Ritter f9e58ba 











Leonard Ritter 8d3f870 

Leonard Ritter f9e58ba 



Leonard Ritter 8d3f870 
Leonard Ritter f9e58ba 
Leonard Ritter 8d3f870 
Leonard Ritter f9e58ba 











from __future__ import (print_function, division, absolute_import)

from cffi import FFI
import traceback
import sys
import os

__all__ = [
    '_ffi',
    'load_lib',
    'lookup',
    'check_error',
    'guard',
    'SDLError',
]

class SDLError(Exception):
    pass

_ffi = FFI()
_LIB = None

IS_DARWIN = sys.platform == 'darwin'
IS_WIN32 = sys.platform == 'win32'
IS_LINUX = sys.platform == 'linux2'
_THISDIR = os.path.abspath(os.path.dirname(__file__))  

def load_lib(cdefs):
    global _LIB
    libname = 'SDL2'
    if IS_WIN32:
        local_dls = [
            os.path.join(_THISDIR, 'SDL2.dll'),
            os.path.join(os.getcwd(), 'SDL2.dll'), 
        ]
    elif IS_LINUX:
        local_dls = [os.path.join(_THISDIR, 'libSDL2.so')]
    elif IS_DARWIN:
        local_dls = [os.path.join(_THISDIR, 'libSDL2.dylib')]
    else:
        local_dls = []
        
    _ffi.cdef(cdefs)
    
    for dlpath in local_dls:
        try:
            _LIB = _ffi.dlopen(dlpath)
            print("Loaded",dlpath)
            return _LIB
        except OSError:
            pass
        except:
            traceback.print_exc()
    print("ERROR: unable to find SDL2 library")

def wrapstr(result):
    if result:
        return _ffi.string(result)
    return None

def wrap_retstr(func):
    def wrapper(*argv):
        return wrapstr(func(*argv))
    return wrapper

def lookup(name):
    if hasattr(_LIB, name):
        return getattr(_LIB, name)
    print("SDL warning: function",name,"missing.")
    return None 
    
def check_error():
    error = _ffi.string(_LIB.SDL_GetError())
    if error:
        _LIB.SDL_ClearError()
        raise SDLError, error

def guard(func):
    if not func:
        return None    
    name = repr(func)
    def newfunc(*args):
        result = func(*args)
        check_error()
        return result
    newfunc.func_name = name
    newfunc.__doc__ = func.__doc__
    return newfunc