Commits

Anonymous committed 3c8eb8d

pygame.image opengl support

Comments (0)

Files changed (3)

pygame/display.py

         if not title:
             SDL_WM_SetCaption('pygame window', 'pygame')
 
-        SDL_PumpEvents()
+    SDL_PumpEvents()
 
-        if _display_surface:
-            _display_surface._surf = surf
-        else:
-            _display_surface = pygame.surface.Surface(surf=surf)
+    if _display_surface:
+        _display_surface._surf = surf
+    else:
+        _display_surface = pygame.surface.Surface(surf=surf)
 
     if sys.platform != 'darwin' and False: # XXX
         if not _icon_was_set:
             namehint = file.name
         namehint = os.path.splitext(namehint)[1]
         rw = SDL_RWFromObject(file)
-        # XXX Should this really be freesrc when we didn't open it?
-        surf = IMG_LoadTyped_RW(rw, 1, namehint)
+        # XXX Differ from pygame: don't freesrc when we didn't allocate it
+        surf = IMG_LoadTyped_RW(rw, 0, namehint)
     return pygame.surface.Surface(surf=surf)
 
 def load_basic(file, namehint=''):
         surf = SDL_LoadBMP(file)
     else:
         rw = SDL_RWFromObject(file)
-        # XXX Should this really be freesrc when we didn't open it?
-        surf = SDL_LoadBMP_RW(rw, 1)
+        # XXX Differ from pygame: don't freesrc when we didn't allocate it
+        surf = SDL_LoadBMP_RW(rw, 0)
     return pygame.surface.Surface(surf=surf)
 
 def load(file, namehint=''):
 
     '''
     if surface._surf.flags & SDL_OPENGL:
-        raise NotImplementedError, 'TODO: OpenGL surfaces'
+        surf = _get_opengl_surface(surface._surf)
     else:
         surface._prep()
+        surf = surface._surf
 
     if hasattr(file, 'write'):
         # TODO TGA not BMP save
         rw = SDL_RWFromObject(file)
-        # XXX Should this really be freesrc when we didn't open it?
-        SDL_SaveBMP_RW(surface._surf, rw, 1)  
+        # XXX Differ from pygame: don't freesrc when we didn't allocate it
+        SDL_SaveBMP_RW(surf, rw, 0)  
     else:
         fileext = os.path.splitext(file)[1].lower()
         if fileext == '.bmp':
-            SDL_SaveBMP(surface._surf, file)
+            SDL_SaveBMP(surf, file)
         elif fileext in ('.jpg', '.jpeg'):
             raise pygame.base.error, 'No support for jpg compiled in.'
         elif fileext == '.png':
             raise NotImplementedError, 'TODO: TGA support'
     
     if surface._surf.flags & SDL_OPENGL:
-        pass # TODO
+        SDL_FreeSurface(surf)
     else:
         surface._unprep()
 
     '''
     surf = surface._surf
     if surf.flags & SDL_OPENGL:
-        raise NotImplementedError, 'TODO: OpenGL support.'
+        surf = _get_opengl_surface(surf)
 
     rows = []
     pitch = surf.pitch
                                       chr(c[0]) \
                                       for c in pixels[y*pitch:y*pitch + w] ]))
 
-    if surf.flags & SDL_OPENGL:
-        pass # TODO
+    if surface._surf.flags & SDL_OPENGL:
+        SDL_FreeSurface(surf)
 
     return ''.join(rows)
 
     The size and format image must compute the exact same size as the passed
     string buffer. Otherwise an exception will be raised. 
 
-    See the pygame.image.frombuffer() method for a potentially faster way to
-    transfer images into Pygame.
-
     :Parameters:
         `string` : str
             String containing image data.
     This method takes the same arguments as pygame.image.fromstring(), but is
     unable to vertically flip the source data.
 
-    This will run much faster than pygame.image.fromstring, since no pixel data
-    must be allocated and copied.
+    :note: In pygame-ctypes, this function is identical to `fromstring`.
 
     :Parameters:
         `string` : str
     :rtype: `Surface`
     '''
     return fromstring(string, size, format)
+
+def _get_opengl_surface(surf):
+    import OpenGL.GL
+    data = OpenGL.GL.glReadPixels(0, 0, surf.w, surf.h, 
+                                  OpenGL.GL.GL_RGB, OpenGL.GL.GL_UNSIGNED_BYTE)
+    if SDL_BYTEORDER == SDL_LIL_ENDIAN:
+        Rmask = 0x000000ff
+        Gmask = 0x0000ff00
+        Bmask = 0x00ff0000
+    else:
+        Rmask = 0x00ff0000
+        Gmask = 0x0000ff00
+        Bmask = 0x000000ff
+    # Flip vertically
+    pitch = surf.w * 3
+    data = ''.join([data[y*pitch:y*pitch+pitch] \
+                    for y in range(surf.h - 1, -1, -1)])
+    newsurf = SDL_CreateRGBSurfaceFrom(data, surf.w, surf.h, 24, pitch,
+                                       Rmask, Gmask, Bmask, 0)
+    return newsurf
+

test_pygame/image_opengl.py

+#!/usr/bin/env python
+
+'''Test saving OpenGL surface.
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id$'
+
+import os
+import sys
+
+import pygame
+from pygame.locals import *
+from OpenGL.GL import *
+from OpenGL.GLU import *
+from OpenGL.GLUT import *
+
+def wait():
+    while True:
+        event = pygame.event.wait()
+        if event.type == KEYDOWN:
+            break
+        elif event.type == QUIT:
+            sys.exit(0)
+
+if __name__ == '__main__':
+    pygame.init()
+    pygame.display.init()
+
+    width, height = 800, 600
+    screen = pygame.display.set_mode((width, height), OPENGL)
+    pygame.display.set_caption('OpenGL view')
+
+    glViewport(0, 0, width, height)
+    gluPerspective(60, width/float(height), 1, 100)
+    glEnable(GL_LIGHTING)
+    glEnable(GL_LIGHT0)
+    glEnable(GL_COLOR_MATERIAL)
+    glEnable(GL_DEPTH_TEST)
+    glTranslate(0, 0, -5)
+    glLight(GL_LIGHT0, GL_POSITION, (0, 0, 0, 1))
+    glTranslate(-2, 2, -3)
+    glColor(1, 1, 1)
+    glutSolidTeapot(1)
+    glTranslate(4, 0, 0)
+    glColor(1, 0, 0)
+    glutSolidTeapot(1)
+    glTranslate(0, -4, 0)
+    glColor(0, 1, 0)
+    glutSolidTeapot(1)
+    glTranslate(-4, 0, 0)
+    glColor(0, 0, 1)
+    glutSolidTeapot(1)
+
+    pygame.display.flip()
+    wait()
+
+    tmp_file = os.tmpfile()
+    pygame.image.save(screen, tmp_file)
+    tmp_file.seek(0)
+
+    image = pygame.image.load(tmp_file, 'tmp.bmp')
+    screen = pygame.display.set_mode((width, height))
+    pygame.display.set_caption('Saved view')
+
+    screen.blit(image, (0, 0))
+    pygame.display.flip()
+    wait()