ARGH
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / pgu / ani.py
1 """animation loading and manipulating functions.
2
3 Please note that this file is alpha, and is subject to modification in
4 future versions of pgu!
5 """
6
7 print ('pgu.ani - This module is alpha, and is subject to change.')
8
9 import math
10 import pygame
11
12 def _ani_load(tv,name,parts,frames,shape):
13     l = len(frames)
14     n = parts.pop()
15     if len(parts):
16         s = l/n
17         for i in xrange(0,n):
18             _ani_load(tv,name + ".%d"%i,parts[:],frames[s*i:s*(i+1)],shape)
19         return
20     
21     for i in xrange(0,n):
22         tv.images[name+".%d"%i] = frames[i],shape
23
24 def ani_load(tv,name,img,size,shape,parts):
25     """Load an animation from an image
26
27     Arguments:    
28         tv -- vid to load into
29         name -- prefix name to give the images
30         image -- image to load anis from
31         size -- w,h size of image
32         shape -- shape of image (usually a subset of 0,0,w,h) used for collision detection
33         parts -- list of parts to divide the animation into 
34             for example parts = [4,5] would yield 4 animations 5 frames long, 20 total
35             for example parts = [a,b,c] would yield ... images['name.a.b.c'] ..., a*b*c total
36     
37     """
38     parts = parts[:]
39     parts.reverse()
40     w,h = size
41     frames = []
42     for y in xrange(0,img.get_height(),h):
43         for x in xrange(0,img.get_width(),w):
44             frames.append(img.subsurface(x,y,w,h))
45     _ani_load(tv,name,parts,frames,shape)
46     
47     
48 def image_rotate(tv,name,img,shape,angles,diff=0):
49     """Rotate an image and put it into tv.images
50     
51     Arguments:
52         tv -- vid to load into
53         name -- prefix name to give the images
54         image -- image to load anis from
55         shape -- shape fimage (usually a subset of 0,0,w,h) used for collision detection
56         angles -- a list of angles to render in degrees
57         diff -- a number to add to the angles, to correct for source image not actually being at 0 degrees
58
59     """
60     w1,h1 = img.get_width(),img.get_height()
61     shape = pygame.Rect(shape)
62     ps = shape.topleft,shape.topright,shape.bottomleft,shape.bottomright
63     for a in angles:
64         img2 = pygame.transform.rotate(img,a+diff)
65         w2,h2 = img2.get_width(),img2.get_height()
66         minx,miny,maxx,maxy = 1024,1024,0,0
67         for x,y in ps:
68             x,y = x-w1/2,y-h1/2
69             a2 = math.radians(a+diff)
70             #NOTE: the + and - are switched from the normal formula because of
71             #the weird way that pygame does the angle...
72             x2 = x*math.cos(a2) + y*math.sin(a2) 
73             y2 = y*math.cos(a2) - x*math.sin(a2)
74             x2,y2 = x2+w2/2,y2+h2/2
75             minx = min(minx,x2)
76             miny = min(miny,y2)
77             maxx = max(maxx,x2)
78             maxy = max(maxy,y2)
79         r = pygame.Rect(minx,miny,maxx-minx,maxy-miny)
80         #((ww-w)/2,(hh-h)/2,w,h)
81         tv.images["%s.%d"%(name,a)] = img2,r
82         
83

UCC git Repository :: git.ucc.asn.au