00001
00002
00003
00004
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007
00008 #include "GL/glut.h"
00009 #include "GL/ps2gl.h"
00010
00011 #include "ps2s/timer.h"
00012 #include "ps2s/gs.h"
00013
00014 #include "ps2gl/debug.h"
00015
00016 #include "pads.h"
00017
00018
00019
00020
00021
00022 typedef void (* tFunctionPtr_ii) (int, int);
00023 typedef void (* tFunctionPtr_ucii) (unsigned char, int, int);
00024 typedef void (* tFunctionPtr_iii) (int, int, int);
00025 typedef void (* tFunctionPtr) (void);
00026 typedef void (* tFunctionPtr_i) (int);
00027
00028
00029
00030
00031
00032 static void initGsMemory();
00033
00034
00035
00036
00037
00038 tFunctionPtr DisplayFunc = NULL;
00039 tFunctionPtr_ii ReshapeFunc = NULL;
00040 tFunctionPtr_ucii KeyboardFunc = NULL;
00041 tFunctionPtr_i VisibilityFunc = NULL;
00042 tFunctionPtr IdleFunc = NULL;
00043 tFunctionPtr_iii SpecialFunc = NULL;
00044
00045 CEETimer *Timer0;
00046 char default_module_path[] = "host0:/usr/local/sce/iop/modules";
00047 char *module_path = '\0';
00048
00049
00050
00051
00052
00084 void glutInit(int *argcp, char **argv)
00085 {
00086
00087
00088 if ( ! pglHasLibraryBeenInitted() ) {
00089 mWarn( "ps2gl library has not been initialized by the user; using default values." );
00090 int immBufferVertexSize = 64 * 1024;
00091 pglInit( immBufferVertexSize );
00092 }
00093
00094
00095
00096 if ( ! pglHasGsMemBeenInitted() ) {
00097 mWarn("GS memory has not been allocated by the user; using default values.");
00098 initGsMemory();
00099 }
00100
00101
00102
00103 Timer0 = new CEETimer(T0_MODE, T0_COUNT);
00104 mInitTimers( Timer0, CEETimer::BusClock_256th );
00105
00106
00107
00108 int i;
00109 for ( i = 0; i < *argcp; i++ ) {
00110 if ( strstr(argv[i], "iop_module_path=") == argv[i] ) {
00111 module_path = &argv[i][strlen("iop_module_path=")];
00112 break;
00113 }
00114 }
00115 if ( module_path == NULL ) {
00116 mWarn("No iop module path specified! Using %s.", default_module_path);
00117 module_path = default_module_path;
00118 }
00119 Pads::Init(module_path);
00120 }
00121
00125 void glutDisplayFunc(void (*func)(void))
00126 {
00127 DisplayFunc = func;
00128 }
00129
00135 void glutReshapeFunc(void (*func)(int width, int height))
00136 {
00137 ReshapeFunc = func;
00138 }
00139
00145 void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y))
00146 {
00147 KeyboardFunc = func;
00148 }
00149
00155 void glutVisibilityFunc(void (*func)(int state))
00156 {
00157 VisibilityFunc = func;
00158 }
00159
00164 void glutIdleFunc(void (*func)(void))
00165 {
00166 IdleFunc = func;
00167 }
00168
00174 void glutSpecialFunc(void (*func)(int key, int x, int y))
00175 {
00176 SpecialFunc = func;
00177 }
00178
00183 void glutMainLoop( void )
00184 {
00185 mErrorIf( DisplayFunc == NULL, "ps2glut: No display function!" );
00186
00187 if ( ReshapeFunc )
00188 ReshapeFunc( 640, 448 );
00189
00190 if ( VisibilityFunc )
00191 VisibilityFunc( GLUT_VISIBLE );
00192
00193 int frameCount = 0;
00194
00195 bool printTimes = false;
00196
00197 while(1) {
00198 mUpdateTimers();
00199
00200 mStartTimer("frame time");
00201
00202 Pads::Read();
00203
00204 if ( Pad0.WasPushed(Pads::kStart) )
00205 printTimes = true;
00206
00207 if ( SpecialFunc ) {
00208 if ( Pad0.WasPushed(Pads::kLeftUp) ) {
00209 SpecialFunc( GLUT_KEY_UP, 0, 0 );
00210 frameCount = 0;
00211 }
00212 if ( Pad0.WasPushed(Pads::kLeftDown) ) {
00213 SpecialFunc( GLUT_KEY_DOWN, 0, 0 );
00214 frameCount = 0;
00215 }
00216 if ( Pad0.WasPushed(Pads::kLeftRight) ) {
00217 SpecialFunc( GLUT_KEY_RIGHT, 0, 0 );
00218 frameCount = 0;
00219 }
00220 if ( Pad0.WasPushed(Pads::kLeftLeft) ) {
00221 SpecialFunc( GLUT_KEY_LEFT, 0, 0 );
00222 frameCount = 0;
00223 }
00224
00225 if ( Pad0.WasPushed(Pads::kL1 ) ) {
00226 SpecialFunc( GLUT_KEY_HOME, 0, 0 );
00227 frameCount = 0;
00228 }
00229 if ( Pad0.WasPushed(Pads::kL2 ) ) {
00230 SpecialFunc( GLUT_KEY_END, 0, 0 );
00231 frameCount = 0;
00232 }
00233
00234 if ( Pad0.WasPushed(Pads::kR1) ) {
00235 SpecialFunc( GLUT_KEY_PAGE_UP, 0, 0 );
00236 frameCount = 0;
00237 }
00238 if ( Pad0.WasPushed(Pads::kR2) ) {
00239 SpecialFunc( GLUT_KEY_PAGE_DOWN, 0, 0 );
00240 frameCount = 0;
00241 }
00242 }
00243
00244 if ( KeyboardFunc ) {
00245 if ( Pad0.IsDown(Pads::kRightUp) ) {
00246 KeyboardFunc( '8', 0, 0 );
00247 frameCount = 0;
00248 }
00249 if ( Pad0.IsDown(Pads::kRightDown) ) {
00250 KeyboardFunc( '2', 0, 0 );
00251 frameCount = 0;
00252 }
00253 if ( Pad0.IsDown(Pads::kRightLeft) ) {
00254 KeyboardFunc( '4', 0, 0 );
00255 frameCount = 0;
00256 }
00257 if ( Pad0.IsDown(Pads::kRightRight) ) {
00258 KeyboardFunc( '6', 0, 0 );
00259 frameCount = 0;
00260 }
00261 }
00262
00263 if ( frameCount > 40 && frameCount % 3 == 0 ) {
00264
00265 if ( SpecialFunc ) {
00266 if ( Pad0.IsDown(Pads::kLeftUp) ) {
00267 SpecialFunc( GLUT_KEY_UP, 0, 0 );
00268 }
00269 if ( Pad0.IsDown(Pads::kLeftDown) ) {
00270 SpecialFunc( GLUT_KEY_DOWN, 0, 0 );
00271 }
00272 if ( Pad0.IsDown(Pads::kLeftRight) ) {
00273 SpecialFunc( GLUT_KEY_RIGHT, 0, 0 );
00274 }
00275 if ( Pad0.IsDown(Pads::kLeftLeft) ) {
00276 SpecialFunc( GLUT_KEY_LEFT, 0, 0 );
00277 }
00278
00279 if ( Pad0.IsDown(Pads::kL1 ) ) {
00280 SpecialFunc( GLUT_KEY_HOME, 0, 0 );
00281 }
00282 if ( Pad0.IsDown(Pads::kL2 ) ) {
00283 SpecialFunc( GLUT_KEY_END, 0, 0 );
00284 }
00285
00286 if ( Pad0.IsDown(Pads::kR1) ) {
00287 SpecialFunc( GLUT_KEY_PAGE_UP, 0, 0 );
00288 }
00289 if ( Pad0.IsDown(Pads::kR2) ) {
00290 SpecialFunc( GLUT_KEY_PAGE_DOWN, 0, 0 );
00291 }
00292 }
00293
00294 if ( KeyboardFunc ) {
00295 if ( Pad0.IsDown(Pads::kRightUp) ) {
00296 KeyboardFunc( '8', 0, 0 );
00297 }
00298 if ( Pad0.IsDown(Pads::kRightDown) ) {
00299 KeyboardFunc( '2', 0, 0 );
00300 }
00301 if ( Pad0.IsDown(Pads::kRightLeft) ) {
00302 KeyboardFunc( '4', 0, 0 );
00303 }
00304 if ( Pad0.IsDown(Pads::kRightRight) ) {
00305 KeyboardFunc( '6', 0, 0 );
00306 }
00307 }
00308 }
00309
00310 if ( DisplayFunc ) {
00311 mStartTimer("DisplayFunc()");
00312 DisplayFunc();
00313 mStopTimer("DisplayFunc()");
00314 }
00315
00316 if ( IdleFunc ) {
00317 mStartTimer("IdleFunc()");
00318 IdleFunc();
00319 mStopTimer("IdleFunc()");
00320 }
00321
00322 mStartTimer("wait for vu1");
00323 pglWaitForVU1();
00324 mStopTimer("wait for vu1");
00325
00326 mStopTimer("frame time");
00327
00328 if ( printTimes ) {
00329 mDisplayTimers();
00330 printTimes = false;
00331 }
00332
00333 pglWaitForVSync();
00334 pglSwapBuffers();
00335
00336 frameCount++;
00337 }
00338 }
00339
00341
00342 void glutInitDisplayMode(unsigned int mode)
00343 {
00344 mNotImplemented( );
00345 }
00346
00347 void glutInitWindowPosition( int x, int y )
00348 {
00349 mNotImplemented( );
00350 }
00351
00352 void glutInitWindowSize( int x, int y )
00353 {
00354 mNotImplemented( );
00355 }
00356
00357 int glutCreateWindow(const char *title)
00358 {
00359 mNotImplemented( );
00360
00361 return 1;
00362 }
00363
00364 void glutPostRedisplay(void)
00365 {
00366
00367 }
00368
00369 void glutSwapBuffers( void )
00370 {
00371 }
00372
00373 int glutGet(GLenum type)
00374 {
00375 mNotImplemented( );
00376 return 0;
00377 }
00378
00379
00380
00381
00382
00383 static void
00384 initGsMemory()
00385 {
00386
00387 pgl_slot_handle_t frame_slot_0, frame_slot_1, depth_slot;
00388 frame_slot_0 = pglAddGsMemSlot( 0, 70, SCE_GS_PSMCT32 );
00389 frame_slot_1 = pglAddGsMemSlot( 70, 70, SCE_GS_PSMCT32 );
00390 depth_slot = pglAddGsMemSlot( 140, 70, SCE_GS_PSMZ32 );
00391
00392 pglLockGsMemSlot( frame_slot_0 );
00393 pglLockGsMemSlot( frame_slot_1 );
00394 pglLockGsMemSlot( depth_slot );
00395
00396
00397 pgl_area_handle_t frame_area_0, frame_area_1, depth_area;
00398 frame_area_0 = pglCreateGsMemArea( 640, 224, SCE_GS_PSMCT24 );
00399 frame_area_1 = pglCreateGsMemArea( 640, 224, SCE_GS_PSMCT24 );
00400 depth_area = pglCreateGsMemArea( 640, 224, SCE_GS_PSMZ32 );
00401
00402 pglBindGsMemAreaToSlot( frame_area_0, frame_slot_0 );
00403 pglBindGsMemAreaToSlot( frame_area_1, frame_slot_1 );
00404 pglBindGsMemAreaToSlot( depth_area, depth_slot );
00405
00406
00407 pglSetDrawBuffers( frame_area_0, frame_area_1, depth_area );
00408
00409 pglSetDisplayBuffers( frame_area_0, frame_area_1 );
00410
00411
00412
00413
00414 pglAddGsMemSlot( 210, 1, GS::kPsm32 );
00415 pglAddGsMemSlot( 211, 1, GS::kPsm32 );
00416 pglAddGsMemSlot( 212, 1, GS::kPsm32 );
00417 pglAddGsMemSlot( 213, 1, GS::kPsm32 );
00418
00419 pglAddGsMemSlot( 214, 2, GS::kPsm32 );
00420 pglAddGsMemSlot( 216, 2, GS::kPsm32 );
00421 pglAddGsMemSlot( 218, 2, GS::kPsm32 );
00422 pglAddGsMemSlot( 220, 2, GS::kPsm32 );
00423
00424 pglAddGsMemSlot( 222, 8, GS::kPsm32 );
00425 pglAddGsMemSlot( 230, 8, GS::kPsm32 );
00426
00427 pglAddGsMemSlot( 238, 32, GS::kPsm32 );
00428 pglAddGsMemSlot( 270, 32, GS::kPsm32 );
00429
00430 pglPrintGsMemAllocation();
00431 }