示例#1
0
    def test_gl_get_set_attribute(self):
        self.assertRaises(sdl.SDLError, video.gl_get_attribute,
                          video.SDL_GL_DEPTH_SIZE)
        self.assertRaises(sdl.SDLError, video.gl_set_attribute,
                          1455, 24)

        self.assertTrue(video.gl_load_library())

        window = video.create_window("OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_OPENGL)
        ctx = video.gl_create_context(window)

        depth = video.gl_get_attribute(video.SDL_GL_DEPTH_SIZE)

        video.gl_delete_context(ctx)
        video.destroy_window(window)

        newdepth = 24
        if depth == 8:
            newdepth = 16
        elif depth == 16:
            newdepth = 24
        elif depth == 24:
            newdepth = 16
        video.gl_set_attribute(video.SDL_GL_DEPTH_SIZE, newdepth)

        window = video.create_window("OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_OPENGL)
        ctx = video.gl_create_context(window)

        val = video.gl_get_attribute(video.SDL_GL_DEPTH_SIZE)
        self.assertNotEqual(depth, val)
        self.assertEqual(val, newdepth)

        video.gl_delete_context(ctx)
        video.destroy_window(window)
        video.gl_unload_library()
示例#2
0
文件: gl.py 项目: 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