Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ps2glut.cpp

Go to the documentation of this file.
00001 /*        Copyright (C) 2000,2001,2002  Sony Computer Entertainment America
00002           
00003           This file is subject to the terms and conditions of the GNU Lesser
00004           General Public License Version 2.1. See the file "COPYING" in the
00005           main directory of this archive for more details.                             */
00006 
00007 #include <stdlib.h>
00008 #include <stdio.h>
00009 
00010 #include "libgraph.h"
00011 #include "libdev.h"
00012 #include "sifdev.h"
00013 
00014 #include "GL/glut.h"
00015 #include "GL/ps2gl.h"
00016 
00017 #include "ps2s/timer.h"
00018 #include "ps2s/gs.h"
00019 #include "ps2s/drawenv.h"
00020 #include "ps2s/displayenv.h"
00021 #include "ps2s/core.h"
00022 
00023 #include "ps2gl/glcontext.h"
00024 #include "ps2gl/displaycontext.h"
00025 #include "ps2gl/drawcontext.h"
00026 #include "ps2gl/debug.h"
00027 
00028 #include "pads.h"
00029 
00030 /********************************************
00031  * some function pointer types
00032  */
00033 
00034 typedef void (* tFunctionPtr_ii)        (int, int);
00035 typedef void (* tFunctionPtr_ucii)      (unsigned char, int, int);
00036 typedef void (* tFunctionPtr_iii)       (int, int, int);
00037 typedef void (* tFunctionPtr)           (void);
00038 typedef void (* tFunctionPtr_i)         (int);
00039 
00040 /********************************************
00041  * function prototypes
00042  */
00043 
00044 static void parse_cl( int *argcp, char **argv );
00045 static void normal_main_loop();
00046 static void initGsMemory();
00047 static void do_keys();
00048 
00049 /********************************************
00050  * local data
00051  */
00052 
00053 tFunctionPtr DisplayFunc = NULL;
00054 tFunctionPtr_ii ReshapeFunc = NULL;
00055 tFunctionPtr_ucii KeyboardFunc = NULL;
00056 tFunctionPtr_i VisibilityFunc = NULL;
00057 tFunctionPtr IdleFunc = NULL;
00058 tFunctionPtr_iii SpecialFunc = NULL;
00059 
00060 static CEETimer *Timer0;
00061 static char default_module_path[] = "host0:/usr/local/sce/iop/modules";
00062 static char *module_path = '\0';
00063 
00064 static bool printTimes = false;
00065 
00066 /********************************************
00067  * macros
00068  */
00069 
00070 /********************************************
00071  * glut implementation (loosely speaking..)
00072  */
00073 
00106 void glutInit(int *argcp, char **argv)
00107 {
00108    parse_cl( argcp, argv );
00109 
00110    // get the iop module path and initialize the pads
00111    // this is before ps2gl initialization because the sifInitRPC in
00112    // Pads::Init needs to happen before the dtv init
00113 
00114    if ( module_path == NULL ) {
00115       mWarn("No iop module path specified!  Using %s.", default_module_path);
00116       module_path = default_module_path;
00117    }
00118    Pads::Init(module_path);
00119 
00120    // does the ps2gl library need to be initialized?
00121 
00122    if ( ! pglHasLibraryBeenInitted() ) {
00123       // reset the machine
00124       sceDevVif0Reset();
00125       sceDevVu0Reset();
00126       sceDmaReset(1);    
00127       sceGsResetPath();
00128 
00129       sceGsResetGraph(0, SCE_GS_INTERLACE, SCE_GS_NTSC, SCE_GS_FRAME);
00130 
00131       mWarn( "ps2gl library has not been initialized by the user; using default values." );
00132       int immBufferVertexSize = 64 * 1024;
00133       pglInit( immBufferVertexSize, 1000 );
00134    }
00135 
00136    // does gs memory need to be initialized?
00137 
00138    if ( ! pglHasGsMemBeenInitted() ) {
00139       mWarn("GS memory has not been allocated by the user; using default values.");
00140       initGsMemory();
00141    }
00142 }
00143 
00147 void glutDisplayFunc(void (*func)(void))
00148 {
00149    DisplayFunc = func;
00150 }
00151 
00157 void glutReshapeFunc(void (*func)(int width, int height))
00158 {
00159    ReshapeFunc = func;
00160 }
00161 
00167 void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y))
00168 {
00169    KeyboardFunc = func;
00170 }
00171 
00177 void glutVisibilityFunc(void (*func)(int state))
00178 {
00179    VisibilityFunc = func;
00180 }
00181 
00186 void glutIdleFunc(void (*func)(void))
00187 {
00188    IdleFunc = func;
00189 }
00190 
00196 void glutSpecialFunc(void (*func)(int key, int x, int y))
00197 {
00198    SpecialFunc = func;
00199 }
00200 
00205 void glutMainLoop( void )
00206 {
00207    mErrorIf( DisplayFunc == NULL, "ps2glut:  No display function!" );
00208 
00209    if ( VisibilityFunc )
00210       VisibilityFunc( GLUT_VISIBLE );
00211 
00212    normal_main_loop();
00213 }
00214  // glut_api
00216 
00217 void glutInitDisplayMode(unsigned int mode)
00218 {
00219    mNotImplemented( );
00220 }
00221 
00222 void glutInitWindowPosition( int x, int y )
00223 {
00224    mNotImplemented( );
00225 }
00226 
00227 void glutInitWindowSize( int x, int y )
00228 {
00229    mNotImplemented( );
00230 }
00231 
00232 int glutCreateWindow(const char *title)
00233 {
00234    mNotImplemented( );
00235 
00236    return 1;
00237 }
00238 
00239 void glutPostRedisplay(void)
00240 {
00241    // mNotImplemented( );
00242 }
00243 
00244 void glutSwapBuffers( void )
00245 {
00246 }
00247 
00248 int glutGet(GLenum type)
00249 {
00250    mNotImplemented( );
00251    return 0;
00252 }
00253 
00254 void*
00255 pglutAllocDmaMem( unsigned int num_bytes )
00256 {
00257    // don't really need memalign, but it's clearer..
00258    return memalign( 16, num_bytes );
00259 }
00260 
00261 void
00262 pglutFreeDmaMem( void *mem )
00263 {
00264    free( mem );
00265 }
00266 
00267 /********************************************
00268  * local function definitions
00269  */
00270 
00271 void normal_main_loop()
00272 {
00273    bool firstTime = true;
00274 
00275    // init the timing system
00276 
00277    Timer0 = new CEETimer( CEETimer::Timer0 );
00278    mInitTimers( Timer0, CEETimer::BusClock_256th );
00279 
00280    if ( ReshapeFunc )
00281       ReshapeFunc( 640, 448 );
00282 
00283    while(1) {
00284       mUpdateTimers();
00285 
00286       mStartTimer("frame time");
00287 
00288       Pads::Read();
00289 
00290       if ( Pad0.WasPushed(Pads::kStart) )
00291          printTimes = true;
00292 
00293       do_keys();
00294 
00295       if ( DisplayFunc ) {
00296          mStartTimer("DisplayFunc()");
00297          pglBeginGeometry();
00298          DisplayFunc();
00299          pglEndGeometry();
00300          mStopTimer("DisplayFunc()");
00301       }
00302 
00303       if ( IdleFunc ) {
00304          mStartTimer("IdleFunc()");
00305          IdleFunc();
00306          mStopTimer("IdleFunc()");
00307       }
00308 
00309       mStartTimer("wait for vu1");
00310       if ( ! firstTime )
00311          pglFinishRenderingGeometry( PGL_DONT_FORCE_IMMEDIATE_STOP );
00312       else
00313          firstTime = false;
00314       mStopTimer("wait for vu1");
00315 
00316       mStopTimer("frame time");
00317 
00318       if ( printTimes ) {
00319          mDisplayTimers();
00320          printTimes = false;
00321 
00322          // pglPrintGsMemAllocation();
00323       }
00324 
00325       pglWaitForVSync();
00326       pglSwapBuffers();
00327       pglRenderGeometry();
00328    }
00329 }
00330 
00331 void
00332 parse_cl( int *argcp, char **argv )
00333 {
00334    for ( int i = 0; i < *argcp; i++ ) {
00335       bool found = false;
00336 
00337       if ( strstr(argv[i], "iop_module_path=") == argv[i] ) {
00338          module_path = &argv[i][strlen("iop_module_path=")];
00339          found = true;
00340       }
00341 
00342       if ( found ) argv[i] = NULL;
00343    }
00344 
00345    // fix up argv
00346 
00347    int numArgs = *argcp;
00348    for ( int i = 0; i < numArgs; i++ ) {
00349       if ( argv[i] == NULL ) {
00350          // move everything after left one
00351          for ( int j = i; j < numArgs - 1; j++ )
00352             argv[j] = argv[j+1];
00353          *argcp -= 1;
00354       }
00355    }
00356 }
00357 
00358 void
00359 do_keys()
00360 {
00361    static int frameCount = 0;
00362 
00363    if ( SpecialFunc ) {
00364       if ( Pad0.WasPushed(Pads::kLeftUp) ) {
00365          SpecialFunc( GLUT_KEY_UP, 0, 0 );
00366          frameCount = 0;
00367       }
00368       if ( Pad0.WasPushed(Pads::kLeftDown) ) {
00369          SpecialFunc( GLUT_KEY_DOWN, 0, 0 );
00370          frameCount = 0;
00371       }
00372       if ( Pad0.WasPushed(Pads::kLeftRight) ) {
00373          SpecialFunc( GLUT_KEY_RIGHT, 0, 0 );
00374          frameCount = 0;
00375       }
00376       if ( Pad0.WasPushed(Pads::kLeftLeft) ) {
00377          SpecialFunc( GLUT_KEY_LEFT, 0, 0 );
00378          frameCount = 0;
00379       }
00380 
00381       if ( Pad0.WasPushed(Pads::kL1 ) ) {
00382          SpecialFunc( GLUT_KEY_HOME, 0, 0 );
00383          frameCount = 0;
00384       }
00385       if ( Pad0.WasPushed(Pads::kL2 ) ) {
00386          SpecialFunc( GLUT_KEY_END, 0, 0 );
00387          frameCount = 0;
00388       }
00389 
00390       if ( Pad0.WasPushed(Pads::kR1) ) {
00391          SpecialFunc( GLUT_KEY_PAGE_UP, 0, 0 );
00392          frameCount = 0;
00393       }
00394       if ( Pad0.WasPushed(Pads::kR2) ) {
00395          SpecialFunc( GLUT_KEY_PAGE_DOWN, 0, 0 );
00396          frameCount = 0;
00397       }
00398    }
00399 
00400    if ( KeyboardFunc ) {
00401       if ( Pad0.IsDown(Pads::kRightUp) ) {
00402          KeyboardFunc( '8', 0, 0 );
00403          frameCount = 0;
00404       }
00405       if ( Pad0.IsDown(Pads::kRightDown) ) {
00406          KeyboardFunc( '2', 0, 0 );
00407          frameCount = 0;
00408       }
00409       if ( Pad0.IsDown(Pads::kRightLeft) ) {
00410          KeyboardFunc( '4', 0, 0 );
00411          frameCount = 0;
00412       }
00413       if ( Pad0.IsDown(Pads::kRightRight) ) {
00414          KeyboardFunc( '6', 0, 0 );
00415          frameCount = 0;
00416       }
00417    }
00418 
00419    if ( frameCount > 40 && frameCount % 3 == 0 ) {
00420 
00421       if ( SpecialFunc ) {
00422          if ( Pad0.IsDown(Pads::kLeftUp) ) {
00423             SpecialFunc( GLUT_KEY_UP, 0, 0 );
00424          }
00425          if ( Pad0.IsDown(Pads::kLeftDown) ) {
00426             SpecialFunc( GLUT_KEY_DOWN, 0, 0 );
00427          }
00428          if ( Pad0.IsDown(Pads::kLeftRight) ) {
00429             SpecialFunc( GLUT_KEY_RIGHT, 0, 0 );
00430          }
00431          if ( Pad0.IsDown(Pads::kLeftLeft) ) {
00432             SpecialFunc( GLUT_KEY_LEFT, 0, 0 );
00433          }
00434 
00435          if ( Pad0.IsDown(Pads::kL1 ) ) {
00436             SpecialFunc( GLUT_KEY_HOME, 0, 0 );
00437          }
00438          if ( Pad0.IsDown(Pads::kL2 ) ) {
00439             SpecialFunc( GLUT_KEY_END, 0, 0 );
00440          }
00441 
00442          if ( Pad0.IsDown(Pads::kR1) ) {
00443             SpecialFunc( GLUT_KEY_PAGE_UP, 0, 0 );
00444          }
00445          if ( Pad0.IsDown(Pads::kR2) ) {
00446             SpecialFunc( GLUT_KEY_PAGE_DOWN, 0, 0 );
00447          }
00448       }
00449 
00450       if ( KeyboardFunc ) {
00451          if ( Pad0.IsDown(Pads::kRightUp) ) {
00452             KeyboardFunc( '8', 0, 0 );
00453          }
00454          if ( Pad0.IsDown(Pads::kRightDown) ) {
00455             KeyboardFunc( '2', 0, 0 );
00456          }
00457          if ( Pad0.IsDown(Pads::kRightLeft) ) {
00458             KeyboardFunc( '4', 0, 0 );
00459          }
00460          if ( Pad0.IsDown(Pads::kRightRight) ) {
00461             KeyboardFunc( '6', 0, 0 );
00462          }
00463       }
00464    }
00465 
00466    frameCount++;
00467 }
00468 
00469 static void
00470 initGsMemory()
00471 {
00472    // frame and depth buffer
00473    pgl_slot_handle_t frame_slot_0, frame_slot_1, depth_slot;
00474    frame_slot_0 = pglAddGsMemSlot( 0, 70, GS::kPsm32 );
00475    frame_slot_1 = pglAddGsMemSlot( 70, 70, GS::kPsm32 );
00476    depth_slot = pglAddGsMemSlot( 140, 70, GS::kPsmz24 );
00477    // lock these slots so that they aren't allocated by the memory manager
00478    pglLockGsMemSlot( frame_slot_0 );
00479    pglLockGsMemSlot( frame_slot_1 );
00480    pglLockGsMemSlot( depth_slot );
00481 
00482    // create gs memory area objects to use for frame and depth buffers
00483    pgl_area_handle_t frame_area_0, frame_area_1, depth_area;
00484    frame_area_0 = pglCreateGsMemArea( 640, 224, GS::kPsm24 );
00485    frame_area_1 = pglCreateGsMemArea( 640, 224, GS::kPsm24 );
00486    depth_area = pglCreateGsMemArea( 640, 224, GS::kPsmz24 );
00487    // bind the areas to the slots we created above
00488    pglBindGsMemAreaToSlot( frame_area_0, frame_slot_0 );
00489    pglBindGsMemAreaToSlot( frame_area_1, frame_slot_1 );
00490    pglBindGsMemAreaToSlot( depth_area, depth_slot );
00491 
00492    // draw to the new areas...
00493    pglSetDrawBuffers( PGL_INTERLACED, frame_area_0, frame_area_1, depth_area );
00494    // ...and display from them
00495    pglSetDisplayBuffers( PGL_INTERLACED, frame_area_0, frame_area_1 );
00496 
00497    // 32 bit
00498 
00499    // a slot for fonts (probably)
00500    pglAddGsMemSlot( 210, 2, GS::kPsm8 );
00501 
00502    // 64x32
00503    pglAddGsMemSlot( 212, 1, GS::kPsm32 );
00504    pglAddGsMemSlot( 213, 1, GS::kPsm32 );
00505    // 64x64
00506    pglAddGsMemSlot( 214, 2, GS::kPsm32 );
00507    pglAddGsMemSlot( 216, 2, GS::kPsm32 );
00508    pglAddGsMemSlot( 218, 2, GS::kPsm32 );
00509    pglAddGsMemSlot( 220, 2, GS::kPsm32 );
00510    // 128x128
00511    pglAddGsMemSlot( 222, 8, GS::kPsm32 );
00512    pglAddGsMemSlot( 230, 8, GS::kPsm32 );
00513    // 256x256
00514    pglAddGsMemSlot( 238, 32, GS::kPsm32 );
00515    pglAddGsMemSlot( 270, 32, GS::kPsm32 );
00516    // 512x256
00517    pglAddGsMemSlot( 302, 64, GS::kPsm32 );
00518    pglAddGsMemSlot( 366, 64, GS::kPsm32 );
00519 
00520    pglPrintGsMemAllocation();
00521 }
00522 

ps2gl version cvs