Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1664 | pj | 1 | |
2 | TASK MovePlayerDown(void* arg) |
||
3 | { |
||
4 | |||
5 | int bLevelInitialised = 0; |
||
6 | int iNextLevel,iNextLevelY; |
||
7 | |||
8 | while(1) |
||
9 | { |
||
10 | |||
11 | mutex_lock(&player_mutex); |
||
12 | |||
13 | if( !bLevelInitialised) |
||
14 | { |
||
15 | iNextLevel = player.iLevel + 1; |
||
16 | iNextLevelY = (iNextLevel * 120) + 120 - player_image_height; |
||
17 | bDown = 1; |
||
18 | bLevelInitialised = 1; |
||
19 | } |
||
20 | |||
21 | int oldX,oldY; |
||
22 | oldX = player.x; |
||
23 | oldY = player.y; |
||
24 | player.iLeaf = -1; |
||
25 | player.y += 5; |
||
26 | if (player.y == iNextLevelY) |
||
27 | { |
||
28 | mutex_unlock(&player_mutex); |
||
29 | mutex_lock(&grx_mutex); |
||
30 | mutex_lock(&player_mutex); |
||
31 | FAB_image_put_within(image_bg,0,0,oldX,oldY, |
||
32 | oldX+player_image_width, |
||
33 | oldY+player_image_height); |
||
34 | FAB_image_put(image_player,player.x,player.y + |
||
35 | IMAGE_OFFSET); |
||
36 | mutex_unlock(&player_mutex); |
||
37 | mutex_unlock(&grx_mutex); |
||
38 | |||
39 | mutex_lock(&player_mutex); |
||
40 | |||
41 | int iLeaf = CheckIfonAnyLeaf(player.iLevel); |
||
42 | |||
43 | if(-1 != iLeaf) |
||
44 | { |
||
45 | ++player.iLevel; |
||
46 | |||
47 | if((iScoreDown) && player.iLevel > iScoreLevel) |
||
48 | { |
||
49 | ++iScoreLevel; |
||
50 | iScore = iScore + 10; |
||
51 | if (iScoreLevel == MAX_DOWN_LEVELS) |
||
52 | { |
||
53 | iScoreDown = 0; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | player.iLeaf = iLeaf; |
||
58 | |||
59 | } |
||
60 | else |
||
61 | { |
||
62 | mutex_unlock(&player_mutex); |
||
63 | sys_end(); |
||
64 | } |
||
65 | bDown = 0; |
||
66 | bLevelInitialised = 0; |
||
67 | mutex_unlock(&player_mutex); |
||
68 | task_disable(g_DownPlayer); |
||
69 | task_endcycle(); |
||
70 | continue; |
||
71 | |||
72 | } |
||
73 | |||
74 | mutex_unlock(&player_mutex); |
||
75 | mutex_lock(&grx_mutex); |
||
76 | mutex_lock(&player_mutex); |
||
77 | |||
78 | FAB_image_put_within(image_bg,0,0,oldX,oldY,oldX+player_image_width, |
||
79 | oldY+player_image_height); |
||
80 | FAB_image_put(image_player,player.x,player.y); |
||
81 | |||
82 | mutex_unlock(&player_mutex); |
||
83 | mutex_unlock(&grx_mutex); |
||
84 | |||
85 | task_endcycle(); |
||
86 | } |
||
87 | |||
88 | return; |
||
89 | } |
||
90 | |||
91 | TASK MovePlayerUp(void* arg) |
||
92 | { |
||
93 | |||
94 | int bLevelInitialised = 0; |
||
95 | int iNextLevel,iNextLevelY; |
||
96 | while(1) |
||
97 | { |
||
98 | |||
99 | mutex_lock(&player_mutex); |
||
100 | |||
101 | if (! bLevelInitialised) |
||
102 | { |
||
103 | iNextLevel = player.iLevel - 1; |
||
104 | iNextLevelY = (iNextLevel * 120) + 120 - player_image_height; |
||
105 | bUp = 1; |
||
106 | bLevelInitialised = 1; |
||
107 | } |
||
108 | |||
109 | int oldX,oldY; |
||
110 | oldX = player.x; |
||
111 | oldY = player.y; |
||
112 | player.iLeaf = -1; |
||
113 | |||
114 | player.y -= 5; |
||
115 | if (player.y == iNextLevelY) |
||
116 | { |
||
117 | |||
118 | int iTempOffset; |
||
119 | if (iNextLevel != 0) |
||
120 | { |
||
121 | iTempOffset = IMAGE_OFFSET; |
||
122 | } |
||
123 | else |
||
124 | { |
||
125 | iTempOffset = 0; |
||
126 | } |
||
127 | mutex_unlock(&player_mutex); |
||
128 | mutex_lock(&grx_mutex); |
||
129 | mutex_lock(&player_mutex); |
||
130 | FAB_image_put_within(image_bg,0,0,oldX,oldY, |
||
131 | oldX+player_image_width, |
||
132 | oldY+player_image_height); |
||
133 | FAB_image_put(image_player,player.x,player.y + |
||
134 | iTempOffset ); |
||
135 | mutex_unlock(&player_mutex); |
||
136 | mutex_unlock(&grx_mutex); |
||
137 | |||
138 | mutex_lock(&player_mutex); |
||
139 | if (iNextLevel != 0) |
||
140 | { |
||
141 | int iLeaf = CheckIfonAnyLeaf(iNextLevel -1); |
||
142 | |||
143 | if(-1 != iLeaf) |
||
144 | { |
||
145 | --player.iLevel; |
||
146 | if((!iScoreDown) && player.iLevel < iScoreLevel) |
||
147 | { |
||
148 | --iScoreLevel; |
||
149 | iScore = iScore + 10; |
||
150 | |||
151 | } |
||
152 | |||
153 | player.iLeaf = iLeaf; |
||
154 | } |
||
155 | else |
||
156 | { |
||
157 | mutex_unlock(&player_mutex); |
||
158 | sys_end(); |
||
159 | } |
||
160 | } |
||
161 | else |
||
162 | { |
||
163 | --player.iLevel; |
||
164 | if((!iScoreDown) && player.iLevel < iScoreLevel) |
||
165 | { |
||
166 | --iScoreLevel; |
||
167 | iScore = iScore + 10; |
||
168 | |||
169 | iScoreDown = 1; |
||
170 | |||
171 | } |
||
172 | } |
||
173 | bUp = 0; |
||
174 | bLevelInitialised = 0; |
||
175 | mutex_unlock(&player_mutex); |
||
176 | task_disable(g_UpPlayer); |
||
177 | task_endcycle(); |
||
178 | continue; |
||
179 | } |
||
180 | |||
181 | mutex_unlock(&player_mutex); |
||
182 | |||
183 | mutex_lock(&grx_mutex); |
||
184 | |||
185 | mutex_lock(&player_mutex); |
||
186 | |||
187 | FAB_image_put_within(image_bg,0,0,oldX,oldY, oldX + player_image_width, |
||
188 | oldY+player_image_height); |
||
189 | FAB_image_put(image_player,player.x,player.y); |
||
190 | mutex_unlock(&player_mutex); |
||
191 | |||
192 | mutex_unlock(&grx_mutex); |
||
193 | task_endcycle(); |
||
194 | } |
||
195 | |||
196 | return; |
||
197 | } |
||
198 | void moveright(KEY_EVT* k ) |
||
199 | { |
||
200 | |||
201 | mutex_lock(&player_mutex); |
||
202 | |||
203 | if (player.x < 700 ) |
||
204 | { |
||
205 | int oldX,oldY; |
||
206 | |||
207 | oldX = player.x; |
||
208 | oldY = player.y; |
||
209 | |||
210 | player.x += 10; |
||
211 | if ( player.iLeaf != -1) |
||
212 | { |
||
213 | mutex_lock(&leaves_mutex[player.iLeaf]); |
||
214 | int iFallDown = player.x > leaves[player.iLeaf].x0 + leaf_image_width ; |
||
215 | mutex_unlock(&leaves_mutex[player.iLeaf]); |
||
216 | if (iFallDown) |
||
217 | { |
||
218 | mutex_unlock(&player_mutex); |
||
219 | KEY_EVT k; |
||
220 | movedown(k); |
||
221 | return; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | //Release the player mutex before taking graphics mutex |
||
226 | mutex_unlock(&player_mutex); |
||
227 | |||
228 | mutex_lock(&grx_mutex); |
||
229 | |||
230 | //Now take the player mutex |
||
231 | mutex_lock(&player_mutex); |
||
232 | FAB_image_put_within(image_bg,0,0,oldX,oldY,oldX + player_image_width,oldY + player_image_height); |
||
233 | FAB_image_put(image_player,player.x,player.y); |
||
234 | |||
235 | //Release player mutex and graphics mutex |
||
236 | mutex_unlock(&player_mutex); |
||
237 | mutex_unlock(&grx_mutex); |
||
238 | } |
||
239 | else |
||
240 | { |
||
241 | mutex_unlock(&player_mutex); |
||
242 | } |
||
243 | |||
244 | } |
||
245 | |||
246 | void moveleft(KEY_EVT* k) |
||
247 | { |
||
248 | |||
249 | mutex_lock(&player_mutex); |
||
250 | if(player.x > 0) |
||
251 | { |
||
252 | int oldX,oldY; |
||
253 | |||
254 | oldX = player.x; |
||
255 | oldY = player.y; |
||
256 | |||
257 | player.x -= 10; |
||
258 | if ( player.iLeaf != -1) |
||
259 | { |
||
260 | mutex_lock(&leaves_mutex[player.iLeaf]); |
||
261 | int iFallDown = player.x + player_image_width < leaves[player.iLeaf].x0; |
||
262 | mutex_unlock(&leaves_mutex[player.iLeaf]); |
||
263 | if (iFallDown ) |
||
264 | { |
||
265 | mutex_unlock(&player_mutex); |
||
266 | KEY_EVT k; |
||
267 | movedown(k); |
||
268 | return; |
||
269 | } |
||
270 | } |
||
271 | mutex_unlock(&player_mutex); |
||
272 | |||
273 | mutex_lock(&grx_mutex); |
||
274 | mutex_lock(&player_mutex); |
||
275 | FAB_image_put_within(image_bg,0,0, |
||
276 | oldX,oldY,oldX+player_image_width, |
||
277 | oldY+player_image_height); |
||
278 | FAB_image_put(image_player,player.x,player.y); |
||
279 | mutex_unlock(&player_mutex); |
||
280 | mutex_unlock(&grx_mutex); |
||
281 | } |
||
282 | else |
||
283 | { |
||
284 | mutex_unlock(&player_mutex); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | void CreatePlayerMoveTask() |
||
289 | { |
||
290 | SOFT_TASK_MODEL m; |
||
291 | soft_task_default_model(m); |
||
292 | soft_task_def_level(m,0); |
||
293 | soft_task_def_periodic(m); |
||
294 | soft_task_def_save_arrivals(m); |
||
295 | soft_task_def_period(m,25000); |
||
296 | soft_task_def_wcet(m,500); |
||
297 | soft_task_def_met(m,100); |
||
298 | |||
299 | g_UpPlayer = task_create("UpPlayer", MovePlayerUp, &m, NULL); |
||
300 | g_DownPlayer = task_create("DownPlayer", MovePlayerDown,&m, NULL); |
||
301 | |||
302 | } |