tagged release 0.6.4
[parrot.git] / examples / sdl / tetris / blockdata.pir
bloba717741ebda75eff0d296b88661e814fe39a6ed0
1 =head1 TITLE
3 blockdata.pir - a tetris block data class
5 =head1 SYNOPSIS
7     # create a new random block
8     data = _BlockData::new( -1 )
10     # rotate the data clockwise
11     _BlockData::rotate( data, 1 )
13 =cut
15 .namespace ["Tetris::BlockData"]
17 .sub __onload :load
18     $P0 = get_class "Tetris::BlockData"
19     unless null $P0 goto END
20     newclass $P0, "Tetris::BlockData"
21     addattribute $P0, "data"
22 END:
23 .end
25 =head1 METHODS
27 =over 4
29 =item blockdata."rotate"( dir )
31 Changes the the C<block data> in a way to make
32 the block look rotated.
34 =over 4
36 =item parameter C<dir>
38 +1 = rotate clockwise
40 -1 = rotate counterclockwise
42 =back
44 This method returns the old data representation.
46 =cut
48 .sub rotateData :method
49     .param int val
50     .local int size
51     .local int x
52     .local int y
53     .local int i
54     .local int j
55     .local int v
56     .local pmc olddata
58     getattribute olddata, self, 'data'
59     olddata = clone olddata
61     size = self."size"()
62     set y, size
63     set i, size
64     mul i, size
65     dec i
66 yLOOP:
67     set x, 0
68     dec y
69     if y < 0 goto END
70 xLOOP:
72     if val != +1 goto WAY2
73     set j, x
74     mul j, size
75     add j, y
76     branch DONE
77 WAY2:
78     set j, size
79     sub j, x
80     dec j
81     mul j, size
82     set v, size
83     sub v, y
84     dec v
85     add j, v
86 DONE:
88     v = olddata[i]
89     self[j] = v
91     dec i
92     inc x
93     if x >= size goto yLOOP
94     branch xLOOP
95 END:
96     .return (olddata)
97 .end
99 =item rows = blockdata."vfree"()
101 Checks how many free rows exist at the top edge
102 of block represented by this block data.
104 Returns the number of free rows.
106 =cut
108 .sub vfree :method
109     .local int size
110     .local int free
111     .local int i
112     .local int temp
113     .local int size2
115     i = 0
116     size = self."size"()
117     if size == 0 goto END
118     set i, 0
119     set size2, size
120     mul size2, size
121 VFREE_LOOP:
122     if i > size2 goto VFREE_END
123     temp = self[i]
124     if temp goto VFREE_END
125     inc i
126     branch VFREE_LOOP
127 VFREE_END:
128     div i, size
129 END:
130     .return (i)
131 .end
133 =item columns = blockdata."hfree"()
135 Checks how many free columns exist at the left
136 an right edges.
137 Positive return values means that this many free
138 colums were counted. If the returned value is negative,
139 the absolute value is the number of free colums found at
140 the right edge.
142 Returns the number of free columns.
144 =cut
146 .sub hfree :method
147     .local int size
148     .local int free
149     .local int i
150     .local int offset
151     .local int free
152     .local int temp
154     size = self."size"()
155     free = 0
156 HFREE_LOOPfree:
157     inc free
158     set i, 0
159     set offset, free
160     dec offset
161 HFREE_LOOPcheck:
162     temp = self[offset]
163     if temp goto HFREE_ERROR
165     inc i
166     add offset, size
167     if i < size goto HFREE_LOOPcheck
168     if free < size goto HFREE_LOOPfree
170 HFREE_ERROR:
171     dec free
172     if free goto HFREE_END
174     free = 0
175 HFREE_LOOPfree2:
176     dec free
177     set i, size
178     mul i, -1
179     if free < i goto HFREE_ERROR2
180     set i, 0
181     if offset < 0 goto HFREE_ERROR2
182 HFREE_LOOPcheck2:
183     set temp, size
184     add temp, free
185     set offset, i
186     mul offset, size
187     add offset, temp
189     temp = self[offset]
191     if temp goto HFREE_ERROR2
193     inc i
194     if i < size goto HFREE_LOOPcheck2
195     if free < size goto HFREE_LOOPfree2
196 HFREE_ERROR2:
197     inc free
198 HFREE_END:
199     .return (free)
200 .end
202 =item size = blockdata."size"()
204 Returns the size of the block represented by this
205 block data. The square of the size is the number of
206 items in the blockdata array.
208 =cut
210 .sub size :method
211     getattribute $P0, self, 'data'
212     $I0 = 0
213     if_null $P0, END
214     $I0 = $P0
215     $N0 = $I0
216     sqrt $N0, $I0
217     $I0 = $N0
218 END:
219     .return ($I0)
220 .end
222 .sub __set_pmc :method
223     .param pmc data
225     setattribute self, 'data', data
226 .end
228 .sub __get_integer_keyed :method
229     .param pmc key
230     .local int index
232     index = key
234     getattribute $P0, self, 'data'
235     if_null $P0, ERR
236     $I0 = $P0
237     if index >= $I0 goto ERR
238     $I0 = $P0[index]
239     .return ($I0)
241 ERR:
242     print "index out of bounds ("
243     print index
244     print ">="
245     print $I0
246     print ")!\n"
247     $P0 = new 'Exception'
248     $P0["_message"] = "out of bounds!"
249     throw $P0
250 .end
252 .sub __set_integer_keyed :method
253     .param pmc key
254     .param int val
255     .local int index
257     index = key
259     getattribute $P0, self, 'data'
260     if_null $P0, ERR
261     $I0 = $P0
262     if index >= $I0 goto ERR
263     $P0[index] = val
264     .return ($I0)
266 ERR:
267     print "index out of bounds ("
268     print index
269     print ">="
270     print $I0
271     print ")!\n"
272     $P0 = new 'Exception'
273     $P0["_message"] = "out of bounds!"
274     throw $P0
275 .end
277 =back
279 =head1 AUTHOR
281 Jens Rieks E<lt>parrot at jensbeimsurfen dot deE<gt> is the author
282 and maintainer.
283 Please send patches and suggestions to the Perl 6 Internals mailing list.
285 =head1 COPYRIGHT
287 Copyright (C) 2004-2008, The Perl Foundation.
289 =cut
291 # Local Variables:
292 #   mode: pir
293 #   fill-column: 100
294 # End:
295 # vim: expandtab shiftwidth=4 ft=pir: