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