diff --git a/Makefile b/Makefile index 0c1e951..c45afd4 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ TARGET = main TYPE = ps-exe SRCS = main.c \ +pad.c \ math.c \ camera.c \ physics.c \ diff --git a/defines.h b/defines.h index 546b50b..3b57be3 100644 --- a/defines.h +++ b/defines.h @@ -23,3 +23,36 @@ #define SCALE 4 #define PRIMBUFFLEN 4096 * sizeof(POLY_GT4) // Maximum number of POLY_GT3 primitives + + +// Pad defines + +// Applied on PADL + +#define PadSelect ( 1 ) + +#define PadStart ( 1 << 3 ) + +// Up, Right, Down, Left will be used on PADL (left side of pad )and PADR (right side of pad) + +#define PadUp ( 1 << 4 ) + +#define PadRight ( 1 << 5 ) + +#define PadDown ( 1 << 6 ) + +#define PadLeft ( 1 << 7 ) + +#define PadR3 ( 1 << 2 ) + +#define PadL3 ( 1 << 1 ) + +// Triggers applied on PADR + +#define PadShldL1 ( 1 << 2 ) + +#define PadShldL2 ( 1 ) + +#define PadShldR1 ( 1 << 3 ) + +#define PadShldR2 ( 1 << 1 ) diff --git a/main.c b/main.c index 8294b2a..219f49c 100644 --- a/main.c +++ b/main.c @@ -23,14 +23,14 @@ #define _WCHAR_T #include "psx.h" +#include "pad.h" #include "math.h" #include "camera.h" #include "physics.h" #include "graphics.h" #include "space.h" - -#define USECD +//~ #define USECD // START OVERLAY @@ -70,7 +70,8 @@ extern u_long __lvl1_end; #include "levels/level1.h" -short level = 1; +short level = 0; + short levelHasChanged = 0; static char* overlayFile; @@ -116,18 +117,24 @@ CAMERA camera = {0}; long time = 0; -//~ const int gravity = 10; - int camMode = 2; //Pad +Controller_Buffer controllers[2]; // Buffers for reading controllers + +Controller_Data theControllers[8]; // Processed controller data + int pressed = 0; u_short timer = 0; // Cam stuff +int angle = 0; + +int angleCam = 0; + int lerping = 0; short curCamAngle = 0; @@ -353,6 +360,10 @@ int main() { init(disp, draw, db, cmat, &BGc, &BKc); + InitPAD(controllers[0].pad, 34, controllers[1].pad, 34); + + StartPAD(); + generateTable(); VSyncCallback(callback); @@ -393,7 +404,7 @@ int main() { VECTOR objAngleToCam = {0, 0, 0, 0}; - int angle = 0; //PSX units = 4096 == 360° = 2Pi + //~ int angle = 0; //PSX units = 4096 == 360° = 2Pi int dist = 0; //PSX units @@ -584,7 +595,7 @@ int main() { // Clear Secondary OT ClearOTagR(ot[db], OTLEN); - + // timeB = time; time ++; @@ -683,7 +694,7 @@ int main() { //~ applyVector(&InvCamPos, -1,-1,-1, *=); - angle = -actorPtr->rot.vy / 2; + angle = -(actorPtr->rot.vy / 2) + angleCam; //~ angle = actorPtr->rot->vy; @@ -1234,6 +1245,7 @@ int main() { //~ FntPrint("CurNode : %x\nIndex: %d", curNode, curNode->siblings->index); FntPrint("Time : %d dt :%d\n", VSync(-1) / 60, dt); + //~ FntPrint("%d\n", curCamAngle ); //~ FntPrint("%x\n", primbuff[db]); @@ -1257,7 +1269,22 @@ int main() { void callback() { - u_short pad = PadRead(0); + + // Pad 1 + + read_controller( &theControllers[0], &controllers[0].pad[0], 0 ); // Read controllers + + // Pad 2 + + read_controller( &theControllers[1], &controllers[1].pad[0], 1 ); + + //~ u_short pad = PadRead(0); + + //~ u_short pad = 0; + + u_char PADL = ~theControllers[0].button1; + + u_char PADR = ~theControllers[0].button2; static u_short lastPad; @@ -1292,7 +1319,7 @@ void callback() { } - if ( pad & PADR1 && !timer ) { + if ( PADR & PadShldR1 && !timer ) { if (!camPtr->tim_data){ @@ -1332,30 +1359,30 @@ void callback() { } } - lastPad = pad; + lastPad = PADR; timer = 10; } - if ( !(pad & PADR1) && lastPad & PADR1 ) { + //~ if ( !(PADR & PadShldR1) && lastPad & PadShldR1 ) { - //~ pressed = 0; + //pressed = 0; - } + //~ } - if ( pad & PADL2 ) { + if ( PADR & PadShldL2 ) { lgtang.vy += 32; } - if ( pad & PADL1 ) { + if ( PADR & PadShldL1 ) { lgtang.vz += 32; } - if ( pad & PADRup && !timer ){ + if ( PADR & PadUp && !timer ){ if (actorPtr->isPrism){ @@ -1369,25 +1396,29 @@ void callback() { timer = 10; - lastPad = pad; + lastPad = PADR; } - if ( pad & PADRdown && !timer ){ - //~ if (actorPtr->body->gForce.vy >= 0 && actorPtr->body->position.vy >= actorPtr->body->min.vy ){ - //~ forceApplied -= 150; - //~ } + if ( PADR & PadDown && !timer ){ + + if (actorPtr->body->gForce.vy >= 0 && actorPtr->body->position.vy >= actorPtr->body->min.vy ){ + + forceApplied -= 150; + + } + cursor = div - 15; timer = 30; - lastPad = pad; + lastPad = PADR; } - if ( !(pad & PADRdown) && lastPad & PADRdown ) { + if ( !(PADR & PadDown) && lastPad & PadDown ) { //~ lastPad = pad; } - if ( pad & PADRleft && !timer ) { + if ( PADR & PadLeft && !timer ) { if (actorPtr->anim->interpolate){ @@ -1401,60 +1432,110 @@ void callback() { timer = 10; - lastPad = pad; + lastPad = PADR; } - if ( pad & PADLup ) { + // Analog stick L up + + if ( theControllers[0].analog3 >= 0 && theControllers[0].analog3 < 108 ) { + + actorPtr->body->gForce.vz = getVectorTo(fVecActor, actorPtr->pos).vz * (128 - theControllers[0].analog3 ) >> 15 ; + + actorPtr->body->gForce.vx = -getVectorTo(fVecActor, actorPtr->pos).vx * (128 - theControllers[0].analog3 ) >> 15 ; + + lastPad = PADL; + } + + // Analog stick L down + + if ( theControllers[0].analog3 > 148 && theControllers[0].analog3 <= 255 ) { + + actorPtr->body->gForce.vz = -getVectorTo(fVecActor, actorPtr->pos).vz * ( theControllers[0].analog3 - 128 ) >> 15 ; + + actorPtr->body->gForce.vx = getVectorTo(fVecActor, actorPtr->pos).vx * ( theControllers[0].analog3 - 128 ) >> 15 ; + + lastPad = PADL; + } + + // Analog stick L dead zone + + if ( theControllers[0].analog3 > 108 && theControllers[0].analog3 < 148 ) { + + actorPtr->body->gForce.vz = 0; + + actorPtr->body->gForce.vx = 0; + + } + + // Analog stick L left + + if ( theControllers[0].analog2 >= 0 && theControllers[0].analog2 < 108 ) { + + actorPtr->rot.vy -= ( 40 * ( 128 - theControllers[0].analog2 ) ) >> 7 ; + + } + + // Analog stick L right + + if ( theControllers[0].analog2 > 148 && theControllers[0].analog2 <= 255 ) { + + actorPtr->rot.vy += ( 40 * ( theControllers[0].analog2 - 128 ) ) >> 7 ; + + } + + if ( PADL & PadUp ) { actorPtr->body->gForce.vz = getVectorTo(fVecActor, actorPtr->pos).vz >> 8 ; actorPtr->body->gForce.vx = -getVectorTo(fVecActor, actorPtr->pos).vx >> 8 ; - lastPad = pad; + lastPad = PADL; } - if ( !(pad & PADLup) && lastPad & PADLup) { + if ( !(PADL & PadUp) && lastPad & PadUp) { actorPtr->body->gForce.vz = 0; actorPtr->body->gForce.vx = 0; + + lastPad = PADL; } - if ( pad & PADLdown ) { + if ( PADL & PadDown ) { actorPtr->body->gForce.vz = -getVectorTo(fVecActor, actorPtr->pos).vz >> 8 ; actorPtr->body->gForce.vx = getVectorTo(fVecActor, actorPtr->pos).vx >> 8 ; - lastPad = pad; + lastPad = PADL; } - if ( !(pad & PADLdown) && lastPad & PADLdown) { + if ( !( PADL & PadDown ) && lastPad & PadDown) { actorPtr->body->gForce.vz = 0; actorPtr->body->gForce.vx = 0; - lastPad = pad; + lastPad = PADL; } - if ( pad & PADLleft ) { + if ( PADL & PadLeft ) { actorPtr->rot.vy -= 32; - lastPad = pad; + lastPad = PADL; } - if ( pad & PADLright ) { + if ( PADL & PadRight ) { actorPtr->rot.vy += 32; - lastPad = pad; + lastPad = PADL; } - if ( pad & PADselect && !timer ) { + if ( PADL & PadSelect && !timer ) { if (!levelHasChanged){ @@ -1464,12 +1545,41 @@ void callback() { } timer = 30; - lastPad = pad; + lastPad = PADL; } - FntPrint("level :%d", level); + if(camMode == 0){ + + if ( theControllers[0].analog0 >= 0 && theControllers[0].analog0 < 108) { + + //~ angleCam -= 16; + angleCam -= ( 16 * ( 128 - theControllers[0].analog0 ) ) >> 7 ; + + } + + if ( theControllers[0].analog0 > 148 && theControllers[0].analog0 <= 255) { + + //~ angleCam += 16; + angleCam += ( 16 * ( theControllers[0].analog0 - 128 ) ) >> 7 ; + + } + + } + + //~ FntPrint("level :%d", level); + + FntPrint("PADL :%d \n", angleCam ); + + FntPrint( "Pad 1 : %02x\nButtons:%02x %02x, Stick:%02d %02d %02d %02d\n", + theControllers[0].type, // Controller type : 00 == none, 41 == standard, 73 == analog/dualshock, 12 == mouse, 23 == steering wheel, 63 == gun, 53 == analog joystick + theControllers[0].button1, // + theControllers[0].button2, + theControllers[0].analog0, // R3 hor : left: 0 7F right: 7F FF dz 78 83 + theControllers[0].analog1, // R3 vert : up : 0 7F down : 7F FF : dz 83 86 + theControllers[0].analog2, // L3 hor : left : 0 7F right: 7F FF : dz 69 81 68 - 8E + theControllers[0].analog3 ); // L3 vert : up : 0 7F down : 7F FF : dz 74 8D if ( cursor ) { diff --git a/pad.c b/pad.c new file mode 100644 index 0000000..9369c7b --- /dev/null +++ b/pad.c @@ -0,0 +1,77 @@ +#include "pad.h" + +void get_digital_direction( Controller_Data *c, int buttondata ) // get analog stick values +{ +int i; + + i = ~(buttondata); + + if( i & 0x80 ) + c->xpos -= 1; + + if( i & 0x20 ) + c->xpos += 1; + + if( i & 0x40 ) + c->ypos += 1; + + if( i & 0x10 ) + c->ypos -= 1; +} + +void read_controller( Controller_Data *c, unsigned char *buf, int port ) // get the raw values from controller +{ + register int mouse_x, mouse_y, x; + register Gun_Position *g; + + c->status = buf[0]; // Copy over raw controller data + c->type = buf[1]; + c->button1 = buf[2]; + c->button2 = buf[3]; + c->analog0 = buf[4]; + c->analog1 = buf[5]; + c->analog2 = buf[6]; + c->analog3 = buf[7]; + + if( buf[0] == 0xff ) // If controller returns BAD status then bail on it. + { + c->type = 0; + return; + } + + // Look at the controller type code & process controller data as indicated + + switch( c->type ) + { + case 0x12: // Sony Mouse + mouse_x = buf[4]; + mouse_y = buf[5]; + + if( mouse_x & 0x80 ) + mouse_x |= 0xffffff80; + if( mouse_y & 0x80 ) + mouse_y |= 0xffffff80; + + c->xpos += mouse_x; + c->ypos += mouse_y; + break; + + case 0x23: // Namco negCon + // Steering wheel + // Sankyo Pachinko controler + get_digital_direction( c, buf[2] ); + break; + + case 0x53: // Analog 2-stick + get_digital_direction( c, buf[2] ); + break; + + case 0x41: // Standard Sony PAD controller + get_digital_direction( c, buf[2] ); + break; + + default: // If don't know what it is, treat it like standard controller + get_digital_direction( c, buf[2] ); + break; + } +} diff --git a/pad.h b/pad.h new file mode 100644 index 0000000..6506b61 --- /dev/null +++ b/pad.h @@ -0,0 +1,37 @@ +#pragma once + +// Structure for storing processed controller data + +typedef struct +{ + int xpos, ypos; // Stored position for sprite(s) + int xpos2, ypos2; // controlled by this controller. + + unsigned char status; // These 8 values are obtained + unsigned char type; // directly from the controller + unsigned char button1; // buffer we installed with InitPAD. + unsigned char button2; + unsigned char analog0; + unsigned char analog1; + unsigned char analog2; + unsigned char analog3; +} Controller_Data; + +// All-purpose controller data buffer + +typedef struct +{ + unsigned char pad[34]; // 8-bytes w/o Multi-Tap, 34-bytes w/Multi-Tap +} Controller_Buffer; + +// Structure for RAW hardware-based light gun position values + +typedef struct +{ + unsigned short v_count; // Y-axis (vertical scan counter) + unsigned short h_count; // H-axis (horizontal pixel clock value) +} Gun_Position; + +void get_digital_direction( Controller_Data *c, int buttondata ); + +void read_controller( Controller_Data *c, unsigned char *buf, int port ); diff --git a/psx.c b/psx.c index ebd6026..c6d5d8e 100644 --- a/psx.c +++ b/psx.c @@ -4,14 +4,18 @@ void init(DISPENV disp[2], DRAWENV draw[2], short db, MATRIX * cmat, CVECTOR * B ResetCallback(); - // Reset the GPU before doing anything and the controller - + // Init pad + + //~ PadInit(0); + + //~ InitPAD(controllers[0].pad, 34, controllers[1].pad, 34); + + //~ StartPAD(); + + // Reset the GPU + ResetGraph(0); - PadInit(0); - - //~ CdInit(); - // Initialize and setup the GTE InitGeom(); @@ -101,9 +105,9 @@ void display(DISPENV * disp, DRAWENV * draw, u_long * otdisc, char * primbuff, c // https://stackoverflow.com/questions/3526503/how-to-set-pointer-reference-through-a-function - //~ //DrawSync(0); + DrawSync(0); - VSync(2); // Using VSync 2 insures constant framerate. 0 makes the fr polycount dependant. + VSync(0); // Using VSync 2 insures constant framerate. 0 makes the fr polycount dependant. ResetGraph(1); diff --git a/psx.h b/psx.h index 77c1153..3dfc03b 100644 --- a/psx.h +++ b/psx.h @@ -1,11 +1,14 @@ #pragma once #include +#include #include -#include #include +#include +#include #include + #include "defines.h" #include "custom_types.h"