Mercurial > hg > Members > koba > t_dandy
annotate sgoex.c @ 42:59b070dd0553
add demo file.
author | koba <koba@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 12 Jan 2011 00:18:16 +0900 |
parents | cbe5bb9068c3 |
children | 56ef94618a0e |
rev | line source |
---|---|
0 | 1 /* |
2 スプライト管理関数 | |
3 */ | |
4 | |
5 //#include<libps.h> | |
6 #include <stdio.h> | |
7 #include <SDL.h> | |
8 #include "SDL_image.h" | |
9 #include "SDL_opengl.h" | |
10 #include "texture.h" | |
11 #include "object.h" | |
12 #include "tree_controll.h" | |
13 #include "sgoex.h" | |
14 #include "trace.h" | |
15 #include "syokika.h" | |
16 #include "LoadSprite.h" | |
17 | |
18 | |
19 #define OT_LENGTH 1 /* オーダリングテーブル */ | |
20 #define MAXOBJ 320 /*スプライト表示上限 */ | |
21 | |
22 #define TRUE 1 | |
23 #define FALSE 0 | |
24 | |
25 | |
26 //static u_short tpage; | |
27 //static SDL_Rect rect; | |
28 | |
29 //static int nActiveBuff; /*ばふぁ */ | |
30 // int i; /* Multi-purpose */ | |
31 //static int pageno; | |
32 | |
33 //static int padd; | |
34 | |
35 static const int sgo_tpx[12] = | |
36 { 0, 64, 128, 192, 256, 320, 0, 64, 128, 192, 256, 320 }; | |
37 static const int sgo_tpy[12] = { 0, 0, 0, 0, 0, 0, 256, 256, 256, 256, 256, 256 }; | |
38 | |
39 /* sgo.h 独自の変数形 */ | |
40 SpriteTable sptable[DEFOBJ]; | |
41 // static SpriteView spview[MAXOBJ]; | |
42 | |
43 #define IMAGE_ADJUSTMENT (0) | |
44 | |
45 /** | |
46 * 一つの画像をpageno(page number)で区切るtexture_page_offsetで | |
47 * pagenoに対応した領域のx,yを用意しておく。PlayStationではpageno | |
48 * で区切る必要があったのだろうが、PS2では全く意味はなさない。 | |
49 */ | |
50 static const struct texture_page_offset { | |
51 int x; | |
52 int y; | |
53 } texpage_offset[] = { | |
54 {0, 0}, {128 + IMAGE_ADJUSTMENT, 0}, {256 + IMAGE_ADJUSTMENT, 0}, {384 + IMAGE_ADJUSTMENT, 0}}; | |
55 | |
56 /*------------------------------------------------------------- | |
57 関数プロトタイプ | |
58 ---------------------------------------------------------------*/ | |
59 | |
60 void SDL_GL_Enter2DMode() | |
61 { | |
62 // SDL_Surface *sc = SDL_GetVideoSurface(); | |
63 | |
64 /* Note, there may be other things you need to change, | |
65 depending on how you have your OpenGL state set up. | |
66 */ | |
67 glPushAttrib(GL_ENABLE_BIT); | |
68 glDisable(GL_DEPTH_TEST); | |
69 glDisable(GL_CULL_FACE); | |
70 glEnable(GL_TEXTURE_2D); | |
71 | |
72 /* This allows alpha blending of 2D textures with the scene */ | |
73 glEnable(GL_BLEND); | |
74 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
75 | |
76 glViewport(0, 0, screen->w, screen->h); | |
77 | |
78 glMatrixMode(GL_PROJECTION); | |
79 glPushMatrix(); | |
80 glLoadIdentity(); | |
81 | |
82 glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); | |
83 | |
84 glMatrixMode(GL_MODELVIEW); | |
85 glPushMatrix(); | |
86 glLoadIdentity(); | |
87 | |
88 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | |
89 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | |
90 } | |
91 | |
92 | |
93 void SDL_GL_Leave2DMode() | |
94 { | |
95 glMatrixMode(GL_MODELVIEW); | |
96 glPopMatrix(); | |
97 | |
98 glMatrixMode(GL_PROJECTION); | |
99 glPopMatrix(); | |
100 | |
101 glPopAttrib(); | |
102 } | |
103 | |
104 | |
105 static int power_of_two(int input) | |
106 { | |
107 int value = 1; | |
108 | |
109 while ( value < input ) { | |
110 value <<= 1; | |
111 } | |
112 return value; | |
113 } | |
114 | |
19 | 115 void |
116 DefSpriteEx(int number, short middlex, short middley) | |
117 { | |
118 sptable[number].mx = middlex; | |
119 sptable[number].my = middley; | |
120 } | |
0 | 121 |
19 | 122 void |
123 DefSprite(int number, const char *name, float w, float h, int color, OBJECT *obj) | |
0 | 124 { |
125 SURFACE *surfaces; | |
126 surfaces = search_node(obj, name); | |
127 if(surfaces == NULL) | |
128 { | |
129 fprintf(stderr, "can't get node\n"); | |
130 printf("%s", name); | |
131 SDL_Quit(); | |
132 exit(1); | |
133 } | |
134 sptable[number].w = w; | |
135 sptable[number].h = h; | |
136 sptable[number].color = (color & 32); | |
137 sptable[number].mx = w / 2; | |
138 sptable[number].my = h / 2; | |
139 sptable[number].tex_w = power_of_two(sptable[number].w); | |
140 sptable[number].tex_h = power_of_two(sptable[number].h); | |
141 texMinX[number] = 0.0f; | |
142 texMinY[number] = 0.0f; | |
143 texMaxX[number] = (GLfloat)sptable[number].w / sptable[number].tex_w; | |
144 texMaxY[number] = (GLfloat)sptable[number].h / sptable[number].tex_h; | |
19 | 145 // printf("texMaxX = %f, w = %d, tex_w = %d\n", texMaxX[number], sptable[number].w, sptable[number].tex_w); |
0 | 146 sptable[number].texture = surfaces->texture; |
147 } | |
148 | |
19 | 149 void |
150 PutSprite(int zorder, short x, short y, int number) | |
0 | 151 { |
19 | 152 glBindTexture(GL_TEXTURE_2D, (GLuint&)sptable[number].texture); |
0 | 153 glEnable(GL_BLEND); |
154 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
155 glBegin(GL_QUADS); | |
156 glTexCoord2f(texMinX[number],texMinY[number]); glVertex2i((GLuint)x, (GLuint)y ); | |
157 glTexCoord2f(texMinX[number],texMaxY[number]); glVertex2i((GLuint)x, (GLuint)(y+sptable[number].tex_h) ); | |
158 glTexCoord2f(texMaxX[number],texMaxY[number]); glVertex2i((GLuint)(x+sptable[number].tex_w), (GLuint)(y+sptable[number].tex_h)); | |
159 glTexCoord2f(texMaxX[number],texMinY[number]); glVertex2i((GLuint)(x+sptable[number].tex_w), (GLuint)y); | |
160 glEnd(); | |
161 glDisable(GL_BLEND); | |
162 } | |
163 | |
19 | 164 void |
165 PutSpriteEx(int number, int x, int y, float scalex, float scaley, float angle) | |
0 | 166 { |
167 SpriteTable *m = &sptable[number]; | |
168 x -= m->w; | |
169 y -= m->h; | |
170 | |
171 SDL_GL_Enter2DMode(); | |
172 glEnable(GL_TEXTURE_2D); | |
173 glEnable(GL_BLEND); | |
174 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
19 | 175 glBindTexture(GL_TEXTURE_2D, (GLuint&)sptable[number].texture); |
0 | 176 glTranslatef(x + m->w/2, y + m->h/2, 0.0); |
177 glRotatef(angle, 0.0, 0.0, 1.0); | |
178 glScalef(scalex, scaley, 1.0); | |
179 glBegin(GL_TRIANGLE_STRIP); | |
180 { | |
181 glTexCoord2f(texMinX[number], texMinY[number]); glVertex2i(-m->w/2, -m->h/2); | |
182 glTexCoord2f(texMaxX[number], texMinY[number]); glVertex2i( m->w/2, -m->h/2); | |
183 glTexCoord2f(texMinX[number], texMaxY[number]); glVertex2i(-m->w/2, m->h/2); | |
184 glTexCoord2f(texMaxX[number], texMaxY[number]); glVertex2i( m->w/2, m->h/2); | |
185 } | |
186 glEnd(); | |
187 glDisable(GL_TEXTURE_2D); | |
188 glDisable(GL_BLEND); | |
189 SDL_GL_Leave2DMode(); | |
190 // SDL_GL_SwapBuffers(); | |
191 } | |
192 | |
193 struct SGO_PAD pad[2]; | |
194 | |
195 | |
196 /* コントローラ状態の読み込み */ | |
197 void Pad(SDL_Joystick *joy) | |
198 { | |
199 Sint16 axis; | |
200 | |
201 SDL_JoystickUpdate(); | |
202 | |
203 if(SDL_JoystickGetButton(joy,PS2_CROSS)==SDL_PRESSED) | |
204 pad[0].k0++; | |
205 else | |
206 pad[0].k0=0; | |
207 | |
208 if(SDL_JoystickGetButton(joy,PS2_CIRCLE)==SDL_PRESSED) | |
209 pad[0].k1++; | |
210 else | |
211 pad[0].k1=0; | |
212 | |
213 if(SDL_JoystickGetButton(joy,PS2_SQUARE)==SDL_PRESSED) | |
214 pad[0].k3++; | |
215 else | |
216 pad[0].k3=0; | |
217 | |
218 if(SDL_JoystickGetButton(joy,PS2_TRIANGLE)==SDL_PRESSED) | |
219 pad[0].k4++; | |
220 else | |
221 pad[0].k4=0; | |
222 | |
223 if(SDL_JoystickGetButton(joy,PS2_L1)==SDL_PRESSED) | |
224 pad[0].l1++; | |
225 else | |
226 pad[0].l1=0; | |
227 | |
228 if(SDL_JoystickGetButton(joy,PS2_R1)==SDL_PRESSED) | |
229 pad[0].r1++; | |
230 else | |
231 pad[0].r1=0; | |
232 | |
233 if(SDL_JoystickGetButton(joy,PS2_L2)==SDL_PRESSED) | |
234 pad[0].l2++; | |
235 else | |
236 pad[0].l2=0; | |
237 | |
238 if(SDL_JoystickGetButton(joy,PS2_R2)==SDL_PRESSED) | |
239 pad[0].r2++; | |
240 else | |
241 pad[0].r2=0; | |
242 | |
243 if(SDL_JoystickGetButton(joy,PS2_START)==SDL_PRESSED) | |
244 pad[0].st++; | |
245 else | |
246 pad[0].st=0; | |
247 | |
248 if(SDL_JoystickGetButton(joy,PS2_SELECT)==SDL_PRESSED) | |
249 pad[0].se++; | |
250 else | |
251 pad[0].se=0; | |
252 | |
253 if(SDL_JoystickGetButton(joy,PS2_L3)==SDL_PRESSED) | |
254 pad[0].l3++; | |
255 else | |
256 pad[0].l3=0; | |
257 | |
258 if(SDL_JoystickGetButton(joy,PS2_R3)==SDL_PRESSED) | |
259 pad[0].r3++; | |
260 else | |
261 pad[0].r3=0; | |
262 //x | |
263 axis=SDL_JoystickGetAxis(joy,0); | |
264 if(axis>=3200){ | |
265 pad[0].left=0; | |
266 pad[0].right++; | |
267 } | |
268 else if(axis<=-3200){ | |
269 pad[0].right=0; | |
270 pad[0].left++; | |
271 } | |
272 else { | |
273 pad[0].right=0; | |
274 pad[0].left=0; | |
275 } | |
276 //y | |
277 axis=SDL_JoystickGetAxis(joy,1); | |
278 if(axis>=3200){ | |
279 pad[0].up=0; | |
280 pad[0].down++; | |
281 } | |
282 else if(axis<=-3200){ | |
283 pad[0].down=0; | |
284 pad[0].up++; | |
285 } | |
286 else { | |
287 pad[0].down=0; | |
288 pad[0].up=0; | |
289 } | |
290 | |
291 if ((pad[0].l1 != 0) && (pad[0].r1 != 0) && | |
292 (pad[0].l2 != 0) && (pad[0].r2 != 0) && | |
293 (pad[0].st != 0) && (pad[0].se != 0)) { | |
294 pad[0].quit = 1; | |
295 } else { | |
296 pad[0].quit = 0; | |
297 } | |
298 | |
299 } | |
300 | |
301 | |
302 void keybord() | |
303 { | |
304 SDL_PumpEvents(); | |
305 Uint8 *keys = SDL_GetKeyState(NULL); | |
306 | |
307 if (keys[SDLK_UP]) { | |
308 pad[0].up++; | |
309 } else { | |
310 pad[0].up = 0; | |
311 } | |
312 if (keys[SDLK_DOWN]) { | |
313 pad[0].down++; | |
314 } else { | |
315 pad[0].down = 0; | |
316 } | |
317 | |
318 if (keys[SDLK_RIGHT]) { | |
319 pad[0].right++; | |
320 } else { | |
321 pad[0].right = 0; | |
322 } | |
323 | |
324 if (keys[SDLK_LEFT]) { | |
325 pad[0].left++; | |
326 } else { | |
327 pad[0].left = 0; | |
328 } | |
329 | |
330 if (keys[SDLK_a]) { | |
331 pad[0].k0++; | |
332 } else { | |
333 pad[0].k0 = 0; | |
334 } | |
335 | |
336 if (keys[SDLK_z]) { | |
337 pad[0].k1++; | |
338 } else { | |
339 pad[0].k1 = 0; | |
340 } | |
341 | |
342 if (keys[SDLK_s]) { | |
343 pad[0].k3++; | |
344 } else { | |
345 pad[0].k3 = 0; | |
346 } | |
347 | |
348 if (keys[SDLK_x]) { | |
349 pad[0].k4++; | |
350 } else { | |
351 pad[0].k4 = 0; | |
352 } | |
353 | |
354 if (keys[SDLK_r]) { | |
355 pad[0].r2++; | |
356 } else { | |
357 pad[0].r2 = 0; | |
358 } | |
359 | |
360 if (keys[SDLK_e]) { | |
361 pad[0].r1++; | |
362 } else { | |
363 pad[0].r1 = 0; | |
364 } | |
365 | |
366 if (keys[SDLK_w]) { | |
367 pad[0].l1++; | |
368 } else { | |
369 pad[0].l1 = 0; | |
370 } | |
371 | |
372 if (keys[SDLK_q]) { | |
373 pad[0].l2++; | |
374 } else { | |
375 pad[0].l2 = 0; | |
376 } | |
377 | |
378 // START ボタンは Return が似合う気がする | |
379 //if(keys[SDLK_1]) | |
380 if (keys[SDLK_RETURN]) { | |
381 pad[0].st++; | |
382 } else { | |
383 pad[0].st = 0; | |
384 } | |
385 | |
386 if (keys[SDLK_2]) { | |
387 pad[0].se++; | |
388 } else { | |
389 pad[0].se = 0; | |
390 } | |
391 | |
392 if (keys[SDLK_ESCAPE]) { | |
39
5484b8606e8e
add trace mode to tree_dandy2(GL).
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
393 pad_file_close(); |
5484b8606e8e
add trace mode to tree_dandy2(GL).
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
394 |
5484b8606e8e
add trace mode to tree_dandy2(GL).
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
395 SDL_Quit(); |
5484b8606e8e
add trace mode to tree_dandy2(GL).
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
396 exit(1); |
5484b8606e8e
add trace mode to tree_dandy2(GL).
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
397 //pad[0].st = 1; |
5484b8606e8e
add trace mode to tree_dandy2(GL).
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
398 //pad[0].se = 1; |
0 | 399 } |
400 | |
401 if (keys[SDLK_0]) { | |
402 pad[0].quit = 1; | |
403 } else { | |
404 pad[0].quit = 0; | |
405 } | |
406 } |