2021-04-10 14:22:48 +02:00
# include "psx.h"
void init ( DISPENV disp [ 2 ] , DRAWENV draw [ 2 ] , short db , MATRIX * cmat , CVECTOR * BGc , VECTOR * BKc ) {
ResetCallback ( ) ;
2021-04-18 23:41:28 +02:00
// Init pad
2021-04-10 14:22:48 +02:00
2021-04-18 23:41:28 +02:00
//~ PadInit(0);
2021-04-15 18:18:39 +02:00
2021-04-18 23:41:28 +02:00
//~ InitPAD(controllers[0].pad, 34, controllers[1].pad, 34);
//~ StartPAD();
// Reset the GPU
2021-04-20 19:11:18 +02:00
ResetGraph ( 0 ) ;
2021-04-18 23:41:28 +02:00
2021-04-10 14:22:48 +02:00
// Initialize and setup the GTE
InitGeom ( ) ;
SetGeomOffset ( CENTERX , CENTERY ) ; // x, y offset
SetGeomScreen ( FOV ) ; // Distance between eye and screen - Camera FOV
// 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 ) ;
2021-04-20 19:11:18 +02:00
// If PAL , add 8 pix vertical offset ((256 - 240) /2)
2021-04-10 14:22:48 +02:00
if ( VMODE ) {
SetVideoMode ( MODE_PAL ) ;
disp [ 0 ] . screen . y + = 8 ;
disp [ 1 ] . screen . y + = 8 ;
}
// Set Draw area color
setRGB0 ( & draw [ 0 ] , BGc - > r , BGc - > g , BGc - > b ) ;
setRGB0 ( & draw [ 1 ] , BGc - > r , BGc - > g , BGc - > b ) ;
// 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
2021-04-20 19:11:18 +02:00
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-04-10 14:22:48 +02:00
// Lighting setup
2021-04-20 19:11:18 +02:00
SetColorMatrix ( cmat ) ;
2021-04-10 14:22:48 +02:00
2021-04-20 19:11:18 +02:00
SetBackColor ( BKc - > vx , BKc - > vy , BKc - > vz ) ;
2021-04-10 14:22:48 +02:00
2021-04-20 19:11:18 +02:00
SetFarColor ( BGc - > r , BGc - > g , BGc - > b ) ;
2021-04-10 14:22:48 +02:00
2021-04-20 19:11:18 +02:00
SetFogNearFar ( FOG_NEAR , FOG_FAR , SCREENXRES ) ;
2021-04-10 14:22:48 +02:00
} ;
2021-04-16 20:22:03 +02:00
void ScrRst ( void ) {
RECT scr ;
2021-04-20 19:11:18 +02:00
VSync ( 0 ) ; // Wait for current drawing to finish
2021-04-16 20:22:03 +02:00
2021-04-20 19:11:18 +02:00
SetDispMask ( 0 ) ; // Set mask to not displayed
2021-04-16 20:22:03 +02:00
2021-04-20 19:11:18 +02:00
ResetGraph ( 1 ) ; // Cancel current drawing
2021-04-16 20:22:03 +02:00
2021-04-20 19:11:18 +02:00
setRECT ( & scr , 0 , 0 , SCREENXRES , SCREENYRES ) ;
2021-04-16 20:22:03 +02:00
2021-04-20 19:11:18 +02:00
ClearImage ( & scr , CLEAR_COLOR_R , CLEAR_COLOR_G , CLEAR_COLOR_B ) ;
2021-04-16 20:22:03 +02:00
2021-04-20 19:11:18 +02:00
DrawSync ( 0 ) ;
2021-04-16 20:22:03 +02:00
} ;
2021-04-10 14:22:48 +02:00
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
2021-04-18 23:41:28 +02:00
DrawSync ( 0 ) ;
2021-04-10 14:22:48 +02:00
2021-04-20 19:11:18 +02:00
VSync ( VSYNC ) ; // Using VSync 2 insures constant framerate. 0 makes the fr polycount dependant.
2021-04-10 14:22:48 +02:00
ResetGraph ( 1 ) ;
PutDispEnv ( disp ) ;
PutDrawEnv ( draw ) ;
SetDispMask ( 1 ) ;
// Main OT
DrawOTag ( otdisc + OT2LEN - 1 ) ;
* db = ! * db ;
* nextprim = primbuff ;
} ;
2021-04-16 20:22:03 +02:00
void LvlPtrSet ( LEVEL * curLevel , LEVEL * level ) {
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
// Move these to drawPoly()
curLevel - > meshPlan = level - > meshPlan ;
//~ FntPrint("%x %x", curLevel->meshes, level->meshes);
2021-04-20 16:45:11 +02:00
2021-04-16 20:22:03 +02:00
} ;
2021-04-26 12:08:34 +02:00
int LoadLevelCD ( const char * const LevelName , u_long * LoadAddress ) {
2021-04-15 18:18:39 +02:00
int cdread = 0 , cdsync = 1 ;
cdread = CdReadFile ( ( char * ) ( LevelName ) , LoadAddress , 0 ) ;
cdsync = CdReadSync ( 0 , 0 ) ;
2021-04-20 19:11:18 +02:00
// return loaded size
return cdread ;
2021-04-15 18:18:39 +02:00
} ;
2021-04-20 16:45:11 +02:00
void SwitchLevel ( const char * const LevelName , u_long * LoadAddress , LEVEL * curLevel , LEVEL * loadLevel ) {
ScrRst ( ) ;
2021-04-27 17:12:00 +02:00
//~ LoadLevelCD( LevelName, LoadAddress );
2021-04-20 16:45:11 +02:00
LvlPtrSet ( curLevel , loadLevel ) ;
for ( int k = 0 ; k < * curLevel - > meshes_length ; k + + ) {
LoadTexture ( curLevel - > meshes [ k ] - > tim_data , curLevel - > meshes [ k ] - > tim ) ;
}
} ;
2021-04-10 14:22:48 +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
2021-04-16 20:22:03 +02:00
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
2021-04-10 14:22:48 +02:00
LoadImage ( tparam - > prect , tparam - > paddr ) ; // Transfer the data from memory to VRAM at position prect.x, prect.y
2021-04-16 20:22:03 +02:00
DrawSync ( 0 ) ; // Wait for the drawing to end
2021-04-10 14:22:48 +02:00
if ( tparam - > mode & 0x8 ) { // check 4th bit // If 4th bit == 1, TIM has a CLUT
2021-04-16 20:22:03 +02:00
LoadImage ( tparam - > crect , tparam - > caddr ) ; // Load it to VRAM at position crect.x, crect.y
DrawSync ( 0 ) ; // Wait for drawing to end
2021-04-10 14:22:48 +02:00
}
} ;