Exemplo n.º 1
0
 def setUp(self):
     if sys.version.startswith("3.1"):
         self.assertIsInstance = lambda x, t: self.assertTrue(isinstance(x, t))
     sdl.init(0)
     sdlimage.init()
Exemplo n.º 2
0
Arquivo: gl.py Projeto: lxnt/fgtestbed
def sdl_init(size=(1280, 800), title = "DFFG testbed", icon = None, gldebug=False, fwdcore=False):
    log = logging.getLogger('fgt.sdl_init')
    sdlhints.set_hint(SDL_HINT_RENDER_DRIVER, 'software') # do not need no renderer
    sdlhints.set_hint(SDL_HINT_FRAMEBUFFER_ACCELERATION, '0') # do not need no window surface
    sdl.init(sdl.SDL_INIT_VIDEO | sdl.SDL_INIT_NOPARACHUTE)
    cflags = 0
    cmask = 0
    fwdcore = False # mesa doesn't support it :(
    if fwdcore:
        cflags |= SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG
        cmask |= SDL_GL_CONTEXT_PROFILE_CORE
    else:
        cmask |= SDL_GL_CONTEXT_PROFILE_COMPATIBILITY
    if gldebug:
        cflags |= SDL_GL_CONTEXT_DEBUG_FLAG
    
    gl_attrs = (
        (SDL_GL_RED_SIZE, "SDL_GL_RED_SIZE", 8),
        (SDL_GL_GREEN_SIZE, "SDL_GL_GREEN_SIZE", 8),
        (SDL_GL_BLUE_SIZE, "SDL_GL_BLUE_SIZE", 8),
        (SDL_GL_ALPHA_SIZE, "SDL_GL_ALPHA_SIZE", 8),
        (SDL_GL_DEPTH_SIZE, "SDL_GL_DEPTH_SIZE", 0),
        (SDL_GL_STENCIL_SIZE, "SDL_GL_STENCIL_SIZE", 0),
        (SDL_GL_DOUBLEBUFFER, "SDL_GL_DOUBLEBUFFER", 1),
        (SDL_GL_CONTEXT_MAJOR_VERSION, "SDL_GL_CONTEXT_MAJOR_VERSION", 3),
        (SDL_GL_CONTEXT_MINOR_VERSION, "SDL_GL_CONTEXT_MINOR_VERSION", 0),
        (SDL_GL_CONTEXT_PROFILE_MASK, "SDL_GL_CONTEXT_PROFILE_MASK", cmask),
        (SDL_GL_CONTEXT_FLAGS, "SDL_GL_CONTEXT_FLAGS", cflags),
    )

    for attr, name, val in gl_attrs:
        log.debug("requesting {} [{}] = {}".format(name, attr, val))
        sdlvideo.gl_set_attribute(attr, val)

    window = sdlvideo.create_window(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 
        size[0], size[1], SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE)
    if icon:
        sdlvideo.set_window_icon(window, icon)
    context = sdlvideo.gl_create_context(window)
    gldumplog("just after context", logger=log) # this catches PyOpenGL's try: glGetString() except: glGetStringiv() unsanity
    for attr, name, val in gl_attrs:
        got = sdlvideo.gl_get_attribute(attr)
        log.debug("{} requested {} got {}".format(name, val, got))
    
    try:
        log.info("glGet: vers = {}.{} flags={}  " .format(
            glGetInteger(GL_MAJOR_VERSION),
            glGetInteger(GL_MINOR_VERSION),
            glGetInteger(GL_CONTEXT_FLAGS)))
    except KeyError:
        pass

    if not fwdcore:
        glEnable(GL_POINT_SPRITE)

    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glDisable(GL_DEPTH_TEST)
    
    sdlttf.init()
    sdlimage.init()
    return window, context
Exemplo n.º 3
0
Arquivo: gl.py Projeto: lxnt/fgtestbed
def sdl_offscreen_init():
    """ init sdl core and the SDL_image lib (raw.py standalone run)"""
    sdl.init(0)
    sdlimage.init()
Exemplo n.º 4
0
def load_image(fname, enforce=None):
    """Creates a SDL_Surface from an image file.

    If assurface is True, a SDL_Surface will be returned instead of a
    Sprite or SoftSprite object. If renderer is set to a SDL_Renderer, a
    Sprite will be returned.

    This function makes use of the Python Imaging Library, if it is available
    on the target execution environment. The function will try to load the
    file via pygame2.sdlimage first. If the file could not be loaded, it will
    try to load it via PIL.

    You can force the function to use only one of them, by passing the enforce
    as either "PIL" or "SDL".

    Note: This will call pygame2.sdlimage.init() implicitly with the
    default arguments, if the module is available.
    """
    if enforce is not None and enforce not in ("PIL", "SDL"):
        raise ValueError("enforce must be either 'PIL' or 'SDL', if set")

    if enforce == "PIL" and not _HASPIL:
        raise UnsupportedError("PIL loading")
    if enforce == "SDL" and not _HASSDLIMAGE:
        raise UnsupportedError("SDL loading")

    surface = None
    if enforce != "PIL" and _HASSDLIMAGE:
        try:
            sdlimage.init()
            surface = sdlimage.load(fname)
        except:
            # An error occured - if we do not try PIL, break out now
            if not _HASPIL or enforce == "SDL":
                raise

    if enforce != "SDL" and _HASPIL and surface is None:
        image = Image.open(fname)
        mode = image.mode
        width, height = image.size
        rmask = gmask = bmask = amask = 0
        if mode in ("1", "L", "P"):
            # 1 = B/W, 1 bit per byte
            # "L" = greyscale, 8-bit
            # "P" = palette-based, 8-bit
            pitch = width
            depth = 8
        elif mode == "RGB":
            # 3x8-bit, 24bpp
            if SDL_BYTEORDER == SDL_LIL_ENDIAN:
                rmask = 0x0000FF
                gmask = 0x00FF00
                bmask = 0xFF0000
            else:
                rmask = 0xFF0000
                gmask = 0x00FF00
                bmask = 0x0000FF
            depth = 24
            pitch = width * 3
        elif mode in ("RGBA", "RGBX"):
            # RGBX: 4x8-bit, no alpha
            # RGBA: 4x8-bit, alpha
            if SDL_BYTEORDER == SDL_LIL_ENDIAN:
                rmask = 0x000000FF
                gmask = 0x0000FF00
                bmask = 0x00FF0000
                if mode == "RGBA":
                    amask = 0xFF000000
            else:
                rmask = 0xFF000000
                gmask = 0x00FF0000
                bmask = 0x0000FF00
                if mode == "RGBA":
                    amask = 0x000000FF
            depth = 32
            pitch = width * 4
        else:
            # We do not support CMYK or YCbCr for now
            raise TypeError("unsupported image format")

        pxbuf = image.tostring()
        surface = sdlsurface.create_rgb_surface_from(pxbuf, width, height,
                                                     depth, pitch, rmask,
                                                     gmask, bmask, amask)

        if mode == "P":
            # Create a SDL_Palette for the SDL_Surface
            def _chunk(seq, size):
                for x in range(0, len(seq), size):
                    yield seq[x:x + size]

            rgbcolors = image.getpalette()
            sdlpalette = sdlpixels.alloc_palette(len(rgbcolors) // 3)
            SDL_Color = sdlpixels.SDL_Color
            for idx, (r, g, b) in enumerate(_chunk(rgbcolors, 3)):
                sdlpalette.colors[idx] = SDL_Color(r, g, b)

            try:
                sdlsurface.set_surface_palette(surface, sdlpalette)
            except:
                sdlsurface.free_surface(surface)
                raise
            finally:
                # This will decrease the refcount on the palette, so it gets
                # freed properly on releasing the SDL_Surface.
                sdlpixels.free_palette(sdlpalette)
    return surface