comparison sgoex.c @ 0:435ac1cdb64e

create task dandy directry.
author koba <koba@cr.ie.u-ryukyu.ac.jp>
date Sat, 11 Dec 2010 21:25:28 +0900
parents
children 00c3272a801f
comparison
equal deleted inserted replaced
-1:000000000000 0:435ac1cdb64e
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 DefSpriteEx(int number, short middlex, short middley)
61 {
62 sptable[number].mx = middlex;
63 sptable[number].my = middley;
64 }
65
66
67 void SDL_GL_Enter2DMode()
68 {
69 // SDL_Surface *sc = SDL_GetVideoSurface();
70
71 /* Note, there may be other things you need to change,
72 depending on how you have your OpenGL state set up.
73 */
74 glPushAttrib(GL_ENABLE_BIT);
75 glDisable(GL_DEPTH_TEST);
76 glDisable(GL_CULL_FACE);
77 glEnable(GL_TEXTURE_2D);
78
79 /* This allows alpha blending of 2D textures with the scene */
80 glEnable(GL_BLEND);
81 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
82
83 glViewport(0, 0, screen->w, screen->h);
84
85 glMatrixMode(GL_PROJECTION);
86 glPushMatrix();
87 glLoadIdentity();
88
89 glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
90
91 glMatrixMode(GL_MODELVIEW);
92 glPushMatrix();
93 glLoadIdentity();
94
95 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
96 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
97 }
98
99
100 void SDL_GL_Leave2DMode()
101 {
102 glMatrixMode(GL_MODELVIEW);
103 glPopMatrix();
104
105 glMatrixMode(GL_PROJECTION);
106 glPopMatrix();
107
108 glPopAttrib();
109 }
110
111
112 static int power_of_two(int input)
113 {
114 int value = 1;
115
116 while ( value < input ) {
117 value <<= 1;
118 }
119 return value;
120 }
121
122
123 void DefSprite(int number, const char *name, float w, float h, int color, OBJECT *obj)
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;
145 printf("texMaxX = %f, w = %d, tex_w = %d\n",
146 texMaxX[number], sptable[number].w, sptable[number].tex_w);
147 sptable[number].texture = surfaces->texture;
148 }
149
150 void PutSprite(int zorder, short x, short y, int number)
151 {
152 glBindTexture(GL_TEXTURE_2D, (GLuint)sptable[number].texture);
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
164 void PutSpriteEx(int number, int x, int y, float scalex, float scaley, float angle)
165 {
166 SpriteTable *m = &sptable[number];
167 x -= m->w;
168 y -= m->h;
169
170 SDL_GL_Enter2DMode();
171 glEnable(GL_TEXTURE_2D);
172 glEnable(GL_BLEND);
173 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
174 glBindTexture(GL_TEXTURE_2D, (GLuint)sptable[number].texture);
175 glTranslatef(x + m->w/2, y + m->h/2, 0.0);
176 glRotatef(angle, 0.0, 0.0, 1.0);
177 glScalef(scalex, scaley, 1.0);
178 glBegin(GL_TRIANGLE_STRIP);
179 {
180 glTexCoord2f(texMinX[number], texMinY[number]); glVertex2i(-m->w/2, -m->h/2);
181 glTexCoord2f(texMaxX[number], texMinY[number]); glVertex2i( m->w/2, -m->h/2);
182 glTexCoord2f(texMinX[number], texMaxY[number]); glVertex2i(-m->w/2, m->h/2);
183 glTexCoord2f(texMaxX[number], texMaxY[number]); glVertex2i( m->w/2, m->h/2);
184 }
185 glEnd();
186 glDisable(GL_TEXTURE_2D);
187 glDisable(GL_BLEND);
188 SDL_GL_Leave2DMode();
189 // SDL_GL_SwapBuffers();
190 }
191
192
193
194 struct SGO_PAD pad[2];
195
196 #ifdef ENABLE_TRACEMODE
197 extern int runmode;
198 #endif //ENABLE_TRACEMODE
199
200 /* コントローラ状態の読み込み */
201 void Pad(SDL_Joystick *joy)
202 {
203 Sint16 axis;
204
205 SDL_JoystickUpdate();
206
207 if(SDL_JoystickGetButton(joy,PS2_CROSS)==SDL_PRESSED)
208 pad[0].k0++;
209 else
210 pad[0].k0=0;
211
212 if(SDL_JoystickGetButton(joy,PS2_CIRCLE)==SDL_PRESSED)
213 pad[0].k1++;
214 else
215 pad[0].k1=0;
216
217 if(SDL_JoystickGetButton(joy,PS2_SQUARE)==SDL_PRESSED)
218 pad[0].k3++;
219 else
220 pad[0].k3=0;
221
222 if(SDL_JoystickGetButton(joy,PS2_TRIANGLE)==SDL_PRESSED)
223 pad[0].k4++;
224 else
225 pad[0].k4=0;
226
227 if(SDL_JoystickGetButton(joy,PS2_L1)==SDL_PRESSED)
228 pad[0].l1++;
229 else
230 pad[0].l1=0;
231
232 if(SDL_JoystickGetButton(joy,PS2_R1)==SDL_PRESSED)
233 pad[0].r1++;
234 else
235 pad[0].r1=0;
236
237 if(SDL_JoystickGetButton(joy,PS2_L2)==SDL_PRESSED)
238 pad[0].l2++;
239 else
240 pad[0].l2=0;
241
242 if(SDL_JoystickGetButton(joy,PS2_R2)==SDL_PRESSED)
243 pad[0].r2++;
244 else
245 pad[0].r2=0;
246
247 if(SDL_JoystickGetButton(joy,PS2_START)==SDL_PRESSED)
248 pad[0].st++;
249 else
250 pad[0].st=0;
251
252 if(SDL_JoystickGetButton(joy,PS2_SELECT)==SDL_PRESSED)
253 pad[0].se++;
254 else
255 pad[0].se=0;
256
257 if(SDL_JoystickGetButton(joy,PS2_L3)==SDL_PRESSED)
258 pad[0].l3++;
259 else
260 pad[0].l3=0;
261
262 if(SDL_JoystickGetButton(joy,PS2_R3)==SDL_PRESSED)
263 pad[0].r3++;
264 else
265 pad[0].r3=0;
266 //x
267 axis=SDL_JoystickGetAxis(joy,0);
268 if(axis>=3200){
269 pad[0].left=0;
270 pad[0].right++;
271 }
272 else if(axis<=-3200){
273 pad[0].right=0;
274 pad[0].left++;
275 }
276 else {
277 pad[0].right=0;
278 pad[0].left=0;
279 }
280 //y
281 axis=SDL_JoystickGetAxis(joy,1);
282 if(axis>=3200){
283 pad[0].up=0;
284 pad[0].down++;
285 }
286 else if(axis<=-3200){
287 pad[0].down=0;
288 pad[0].up++;
289 }
290 else {
291 pad[0].down=0;
292 pad[0].up=0;
293 }
294
295 if ((pad[0].l1 != 0) && (pad[0].r1 != 0) &&
296 (pad[0].l2 != 0) && (pad[0].r2 != 0) &&
297 (pad[0].st != 0) && (pad[0].se != 0)) {
298 pad[0].quit = 1;
299 } else {
300 pad[0].quit = 0;
301 }
302
303 }
304
305
306 void keybord()
307 {
308 SDL_PumpEvents();
309 Uint8 *keys = SDL_GetKeyState(NULL);
310
311 if (keys[SDLK_UP]) {
312 pad[0].up++;
313 } else {
314 pad[0].up = 0;
315 }
316 if (keys[SDLK_DOWN]) {
317 pad[0].down++;
318 } else {
319 pad[0].down = 0;
320 }
321
322 if (keys[SDLK_RIGHT]) {
323 pad[0].right++;
324 } else {
325 pad[0].right = 0;
326 }
327
328 if (keys[SDLK_LEFT]) {
329 pad[0].left++;
330 } else {
331 pad[0].left = 0;
332 }
333
334 if (keys[SDLK_a]) {
335 pad[0].k0++;
336 } else {
337 pad[0].k0 = 0;
338 }
339
340 if (keys[SDLK_z]) {
341 pad[0].k1++;
342 } else {
343 pad[0].k1 = 0;
344 }
345
346 if (keys[SDLK_s]) {
347 pad[0].k3++;
348 } else {
349 pad[0].k3 = 0;
350 }
351
352 if (keys[SDLK_x]) {
353 pad[0].k4++;
354 } else {
355 pad[0].k4 = 0;
356 }
357
358 if (keys[SDLK_r]) {
359 pad[0].r2++;
360 } else {
361 pad[0].r2 = 0;
362 }
363
364 if (keys[SDLK_e]) {
365 pad[0].r1++;
366 } else {
367 pad[0].r1 = 0;
368 }
369
370 if (keys[SDLK_w]) {
371 pad[0].l1++;
372 } else {
373 pad[0].l1 = 0;
374 }
375
376 if (keys[SDLK_q]) {
377 pad[0].l2++;
378 } else {
379 pad[0].l2 = 0;
380 }
381
382 // START ボタンは Return が似合う気がする
383 //if(keys[SDLK_1])
384 if (keys[SDLK_RETURN]) {
385 pad[0].st++;
386 } else {
387 pad[0].st = 0;
388 }
389
390 if (keys[SDLK_2]) {
391 pad[0].se++;
392 } else {
393 pad[0].se = 0;
394 }
395
396 if (keys[SDLK_ESCAPE]) {
397 SDL_Quit();
398 exit(1);
399 //pad[0].st = 1;
400 //pad[0].se = 1;
401 }
402
403 if (keys[SDLK_0]) {
404 pad[0].quit = 1;
405 } else {
406 pad[0].quit = 0;
407 }
408 }
409