2021-11-28 19:59:05 +01:00
# include <psx.h>
# include <sound.h>
2021-06-23 19:25:12 +02:00
2021-07-10 12:08:52 +02:00
void setDCLightEnv ( MATRIX * curLevelCMat , MATRIX * curLevelLgtMat , SVECTOR * curLevelLgtAng ) {
memcpy ( dc_lvlcmatp , curLevelCMat , sizeof ( MATRIX ) ) ;
memcpy ( dc_lvllgtmatp , curLevelLgtMat , sizeof ( MATRIX ) ) ;
memcpy ( dc_lgtangp , curLevelLgtAng , sizeof ( SVECTOR ) ) ;
} ;
void setLightEnv ( DRAWENV draw [ 2 ] , CVECTOR * BGc , VECTOR * BKc ) {
2021-06-24 20:39:31 +02:00
// Set Draw area color
setRGB0 ( & draw [ 0 ] , BGc - > r , BGc - > g , BGc - > b ) ;
setRGB0 ( & draw [ 1 ] , BGc - > r , BGc - > g , BGc - > b ) ;
// Set Farcolor from here
2021-06-25 14:58:31 +02:00
SetFarColor ( BGc - > r , BGc - > g , BGc - > b ) ;
2021-06-24 20:39:31 +02:00
// Set Ambient color
SetBackColor ( BKc - > vx , BKc - > vy , BKc - > vz ) ;
// Set Light matrix
2021-07-10 12:08:52 +02:00
SetColorMatrix ( dc_lvlcmatp ) ;
2021-06-24 20:39:31 +02:00
} ;
2021-07-10 12:08:52 +02:00
void init ( DISPENV disp [ 2 ] , DRAWENV draw [ 2 ] , short db , CVECTOR * BGc , VECTOR * BKc , VECTOR * FC ) {
2021-06-23 19:25:12 +02:00
ResetCallback ( ) ;
// Init pad
//~ PadInit(0);
//~ InitPAD(controllers[0].pad, 34, controllers[1].pad, 34);
//~ StartPAD();
2021-08-10 18:44:19 +02:00
// Init SPU
SpuInit ( ) ;
2021-06-23 19:25:12 +02:00
// Reset the GPU
ResetGraph ( 0 ) ;
// Initialize and setup the GTE
InitGeom ( ) ;
SetGeomOffset ( CENTERX , CENTERY ) ; // x, y offset
2021-07-10 12:08:52 +02:00
SetGeomScreen ( FOV ) ; // Distance between eye and screen - Camera FOV
SetDispMask ( 1 ) ;
2021-06-23 19:25:12 +02:00
// Set the display and draw environments
SetDefDispEnv ( & disp [ 0 ] , 0 , 0 , SCREENXRES , SCREENYRES ) ;
SetDefDispEnv ( & disp [ 1 ] , 0 , SCREENYRES , SCREENXRES , SCREENYRES ) ;
SetDefDrawEnv ( & draw [ 0 ] , 0 , SCREENYRES , SCREENXRES , SCREENYRES ) ;
SetDefDrawEnv ( & draw [ 1 ] , 0 , 0 , SCREENXRES , SCREENYRES ) ;
// If PAL , add 8 pix vertical offset ((256 - 240) /2)
if ( VMODE ) {
SetVideoMode ( MODE_PAL ) ;
disp [ 0 ] . screen . y + = 8 ;
disp [ 1 ] . screen . y + = 8 ;
}
// Set Draw area color
2021-07-10 12:08:52 +02:00
setLightEnv ( draw , BGc , BKc ) ;
2021-06-23 19:25:12 +02:00
// Set Draw area clear flag
draw [ 0 ] . isbg = 1 ;
draw [ 1 ] . isbg = 1 ;
// Set the disp and draw env
PutDispEnv ( & disp [ db ] ) ;
PutDrawEnv ( & draw [ db ] ) ;
// Init font system
FntLoad ( FNT_VRAM_X , FNT_VRAM_Y ) ;
FntOpen ( FNT_SCR_X ,
FNT_SCR_Y ,
FNT_SCR_W ,
FNT_SCR_H ,
FNT_SCR_BG ,
FNT_SCR_MAX_CHAR
) ;
2021-07-03 11:57:06 +02:00
SetFarColor ( FC - > vx , FC - > vy , FC - > vz ) ;
2021-06-25 14:58:31 +02:00
// TODO : Move this to level files
2021-06-23 19:25:12 +02:00
SetFogNearFar ( FOG_NEAR , FOG_FAR , SCREENXRES ) ;
} ;
void ScrRst ( void ) {
RECT scr ;
VSync ( 0 ) ; // Wait for current drawing to finish
SetDispMask ( 0 ) ; // Set mask to not displayed
ResetGraph ( 1 ) ; // Cancel current drawing
2021-07-01 17:35:08 +02:00
// Clear FB
2021-06-23 19:25:12 +02:00
setRECT ( & scr , 0 , 0 , SCREENXRES , SCREENYRES ) ;
ClearImage ( & scr , CLEAR_COLOR_R , CLEAR_COLOR_G , CLEAR_COLOR_B ) ;
DrawSync ( 0 ) ;
} ;
void display ( DISPENV * disp , DRAWENV * draw , u_long * otdisc , char * primbuff , char * * nextprim , char * db ) {
// https://stackoverflow.com/questions/3526503/how-to-set-pointer-reference-through-a-function
DrawSync ( 0 ) ;
VSync ( VSYNC ) ; // Using VSync 2 insures constant framerate. 0 makes the fr polycount dependant.
ResetGraph ( 1 ) ;
PutDispEnv ( disp ) ;
PutDrawEnv ( draw ) ;
2021-07-10 12:08:52 +02:00
//~ SetDispMask(1);
2021-06-23 19:25:12 +02:00
// Main OT
DrawOTag ( otdisc + OT2LEN - 1 ) ;
* db = ! * db ;
* nextprim = primbuff ;
} ;
void LvlPtrSet ( LEVEL * curLevel , LEVEL * level ) {
curLevel - > BGc = level - > BGc ;
2021-06-24 20:39:31 +02:00
curLevel - > BKc = level - > BKc ;
2021-06-23 19:25:12 +02:00
curLevel - > cmat = level - > cmat ;
curLevel - > lgtmat = level - > lgtmat ;
curLevel - > meshes = level - > meshes ;
curLevel - > meshes_length = level - > meshes_length ;
curLevel - > actorPtr = level - > actorPtr ;
curLevel - > levelPtr = level - > levelPtr ;
curLevel - > propPtr = level - > propPtr ;
curLevel - > camPtr = level - > camPtr ;
curLevel - > camPath = level - > camPath ;
curLevel - > camAngles = level - > camAngles ;
curLevel - > curNode = level - > curNode ; // Blank
2021-08-16 16:20:36 +02:00
curLevel - > VAG = level - > VAG ;
curLevel - > XA = level - > XA ;
2021-08-26 19:55:00 +02:00
curLevel - > levelSounds = level - > levelSounds ;
2021-07-14 15:21:29 +02:00
//~ curLevel->actorPtr->body = level->actorPtr->body;
2021-06-23 19:25:12 +02:00
// Move these to drawPoly()
2021-06-29 16:30:44 +02:00
//~ curLevel->meshPlan = level->meshPlan;
2021-06-23 19:25:12 +02:00
//~ FntPrint("%x %x", curLevel->meshes, level->meshes);
} ;
int LoadLevelCD ( const char * const LevelName , u_long * LoadAddress ) {
int cdread = 0 , cdsync = 1 ;
cdread = CdReadFile ( ( char * ) ( LevelName ) , LoadAddress , 0 ) ;
cdsync = CdReadSync ( 0 , 0 ) ;
// return loaded size
return cdread ;
} ;
void SwitchLevel ( LEVEL * curLevel , LEVEL * loadLevel ) {
//~ ScrRst();
LvlPtrSet ( curLevel , loadLevel ) ;
2021-08-16 16:20:36 +02:00
// XA
getXAoffset ( curLevel ) ;
2021-06-23 19:25:12 +02:00
// Reload textures
for ( int k = 0 ; k < * curLevel - > meshes_length ; k + + ) {
2021-07-01 17:35:08 +02:00
// Check data exists
if ( curLevel - > meshes [ k ] - > tim_data ) {
LoadTexture ( curLevel - > meshes [ k ] - > tim_data , curLevel - > meshes [ k ] - > tim ) ;
}
2021-06-23 19:25:12 +02:00
}
// BG texture
if ( curLevel - > camPtr - > tim_data ) {
LoadTexture ( curLevel - > camPtr - > tim_data , curLevel - > camPtr - > BGtim ) ;
}
2021-07-10 12:08:52 +02:00
// TODO : per-level lgtang
SVECTOR lgtang = { 0 , 0 , 0 , 0 } ;
// Light environment
setDCLightEnv ( curLevel - > cmat , curLevel - > lgtmat , & lgtang ) ;
2021-07-14 15:21:29 +02:00
// Reset physics
// TODO : put in a function
copyVector ( & curLevel - > actorPtr - > body - > position , & loadLevel - > actorPtr - > body - > position ) ;
copyVector ( & curLevel - > actorPtr - > pos , & curLevel - > actorPtr - > body - > position ) ;
copyVector ( & curLevel - > propPtr - > body - > position , & loadLevel - > propPtr - > body - > position ) ;
copyVector ( & curLevel - > propPtr - > pos , & curLevel - > propPtr - > body - > position ) ;
applyVector ( & curLevel - > actorPtr - > body - > position , 0 , 100 , 0 , - = ) ;
applyVector ( & curLevel - > actorPtr - > body - > velocity , 0 , 0 , 0 , = ) ;
applyVector ( & curLevel - > actorPtr - > body - > gForce , 0 , 0 , 0 , = ) ;
applyVector ( & curLevel - > propPtr - > body - > position , 0 , 100 , 0 , - = ) ;
applyVector ( & curLevel - > propPtr - > body - > velocity , 0 , 0 , 0 , = ) ;
applyVector ( & curLevel - > propPtr - > body - > gForce , 0 , 0 , 0 , = ) ;
2021-08-16 16:20:36 +02:00
2021-06-23 19:25:12 +02:00
} ;
void LoadTexture ( u_long * tim , TIM_IMAGE * tparam ) { // This part is from Lameguy64's tutorial series : lameguy64.net/svn/pstutorials/chapter1/3-textures.html login/pw: annoyingmous
OpenTIM ( tim ) ; // Open the tim binary data, feed it the address of the data in memory
ReadTIM ( tparam ) ; // This read the header of the TIM data and sets the corresponding members of the TIM_IMAGE structure
LoadImage ( tparam - > prect , tparam - > paddr ) ; // Transfer the data from memory to VRAM at position prect.x, prect.y
DrawSync ( 0 ) ; // Wait for the drawing to end
if ( tparam - > mode & 0x8 ) { // check 4th bit // If 4th bit == 1, TIM has a CLUT
LoadImage ( tparam - > crect , tparam - > caddr ) ; // Load it to VRAM at position crect.x, crect.y
DrawSync ( 0 ) ; // Wait for drawing to end
}
} ;