#/usr/bin/env python
"""Load images from Zip Example

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.

Many game projects require a very large number of graphics files -
sprite images, backgrounds, textures and other such resources. Some
developers simply ship their games with the graphics files as-is,
while others prefer to group them together in compressed archives.
While it won't keep your distribution size down (unless you're
using an uncompressed format like .bmp), python's zipfile module
makes it easy to ship your graphics resources in one easy-to
handle, familiar archive format.

The zipshow script will display each stored file in a zip
archive, one at a time - hit a key or click the mouse to page
through them, or hit ESC to quit. Place it in a directory with
an archive, or feed it the name of a zip file, and watch it do
its thing.
"""
__author__ = "Pete Shinners"
__copyright__ = "Copyright (c) 2001 Pete Shinners"
__license__ = "Public Domain"
__version__ = "1.0"


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()

