#/usr/bin/env python

"""
Pete Shinners
May 20, 2002

This shows how to load images from a zipfile. The script itself
will take the name of a zip file and display all the image inside
the zip.

You will want to change to code yourself to suit your game,
but this script shows you how to find images inside a zip file
and then load a specific image from the zip.

This example originally was included as an example with earlier
versions of pygame.
"""



import zipfile, sys, glob, pygame
from cStringIO import StringIO
from pygame.locals import *



def showimage(imgsurface, name):
    "show a loaded image onto the screen"
    size = imgsurface.get_size()
    pygame.display.set_caption(name+'  '+`size`)
    screen = pygame.display.set_mode(size)
    screen.blit(imgsurface, (0,0))
    pygame.display.flip()

    #loop through events until done with this pic
    while 1:
        e = pygame.event.wait()
        if e.type == QUIT:
            return 0
        if e.type in (KEYDOWN, MOUSEBUTTONDOWN):
            break
    return 1


def zipslideshow(zipfilename):    
    "loop through all the images in the zipfile"
    zip = zipfile.ZipFile(zipfilename)
    for file in zip.namelist():
        #get data from zipfile
        data = zip.read(file)

        #create stringio for data (a file-like object)
        data_io = StringIO(data)

        #load from a stringio object (ignore erroe exceptions)
        try: surf = pygame.image.load(data_io, file)
        except: continue

        #show image on the screen
        if not showimage(surf, file):
            return




def main():
    "run the program, handle arguments"
    pygame.init()
    zipfiles = sys.argv[1:]
    if not zipfiles:
        zipfiles = glob.glob('*.zip') #find any zipfile in this directory
    if not zipfiles:
        raise SystemExit('No ZIP files given, or in current directory')
    zipslideshow(zipfiles[0])


#run the script if not being imported
if __name__ == '__main__': main()

