From 4eb99fee67be5561a3c4d532c69ce6f453f24313 Mon Sep 17 00:00:00 2001 From: ABelliqueux Date: Mon, 26 Jul 2021 18:00:40 +0200 Subject: [PATCH] Remove empty lines, useless global initialization --- hello_2pads/hello_2pads.c | 118 +--------------- hello_cube/hello_cube.c | 2 +- hello_light/hello_light.c | 3 +- hello_multivag/hello_multivag.c | 96 ------------- hello_pad/hello_pad.c | 76 +--------- hello_poly/hello_poly.c | 83 +---------- hello_poly_ft/hello_poly_ft.c | 26 +--- hello_poly_fun/hello_poly_fun.c | 194 +------------------------- hello_poly_gt/hello_poly_gt.c | 79 +---------- hello_poly_gt_tw/hello_poly_gt_tw.c | 90 +----------- hello_poly_inline/hello_poly_inline.c | 9 +- hello_poly_subdiv/hello_poly_subdiv.c | 9 +- hello_sio/hello_sio.c | 2 +- hello_sprt/hello_sprt.c | 78 ----------- hello_tile/hello_tile.c | 46 +----- hello_vag/hello_vag.c | 81 ----------- hello_world/hello_world.c | 27 ---- 17 files changed, 25 insertions(+), 994 deletions(-) diff --git a/hello_2pads/hello_2pads.c b/hello_2pads/hello_2pads.c index 6a9323d..c7bc98f 100644 --- a/hello_2pads/hello_2pads.c +++ b/hello_2pads/hello_2pads.c @@ -12,51 +12,33 @@ #include #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 #define SCREENYRES 240 - #define CENTERX SCREENXRES/2 #define CENTERY SCREENYRES/2 - #define MARGINX 32 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 7 // Text Field Height - #define OTLEN 8 // Ordering Table Length - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes - -char primbuff[2][32768] = {1}; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes - +char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] - short db = 0; // index of which buffer is used, values 0, 1 - - // Pad stuff - // 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; - // 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. @@ -66,90 +48,63 @@ typedef struct 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; - Controller_Buffer controllers[2]; // Buffers for reading controllers - Controller_Data theControllers[8]; // Processed controller data - void init(void) { ResetGraph(0); - 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 (VMODE) { SetVideoMode(MODE_PAL); disp[0].screen.y += 8; disp[1].screen.y += 8; } - SetDispMask(1); // Display on screen - setRGB0(&draw[0], 50, 50, 50); setRGB0(&draw[1], 50, 50, 50); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - DrawOTag(&ot[db][OTLEN - 1]); - db = !db; - nextpri = primbuff[db]; } - 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]; @@ -158,147 +113,97 @@ void read_controller( Controller_Data *c, unsigned char *buf, int port ) // get 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; } } - int main(void) { - TILE * PADL; // Tile primitives TILE * TRIGGERL; - TILE * PADR; TILE * TRIGGERR; - TILE * START, * SELECT; - init(); - InitPAD(controllers[0].pad, 34, controllers[1].pad, 34); - StartPAD(); - - while (1) { - read_controller( &theControllers[0], &controllers[0].pad[0], 0 ); // Read controllers read_controller( &theControllers[1], &controllers[1].pad[0], 1 ); - ClearOTagR(ot[db], OTLEN); - // D-cross - PADL = (TILE *)nextpri; - setTile(PADL); setRGB0(PADL, 80, 180, 255); setXY0(PADL, CENTERX - 80, CENTERY); setWH(PADL, 24, 24); - - addPrim(ot[db], PADL); - nextpri += sizeof(TILE); - // L1+L2 - TRIGGERL = (TILE *)nextpri; - setTile(TRIGGERL); setRGB0(TRIGGERL, 255, 0, 0); setXY0(TRIGGERL, CENTERX - 80, CENTERY - 80); setWH(TRIGGERL, 24, 24); - - addPrim(ot[db], TRIGGERL); - nextpri += sizeof(TILE); - // /\, X, O, [] - PADR = (TILE *)nextpri; - setTile(PADR); setRGB0(PADR, 0, 255, 0); setXY0(PADR, CENTERX + 50, CENTERY); setWH(PADR, 24, 24); - addPrim(ot[db], PADR); - nextpri += sizeof(TILE); - // R1+R2 - TRIGGERR = (TILE *)nextpri; - setTile(TRIGGERR); setRGB0(TRIGGERR, 255, 0, 255); setXY0(TRIGGERR, CENTERX + 50, CENTERY -80); setWH(TRIGGERR, 24, 24); - addPrim(ot[db], TRIGGERR); - nextpri += sizeof(TILE); - // START + SELECT - START = (TILE *)nextpri; - setTile(START); setRGB0(START, 240, 240, 240); setXY0(START, CENTERX - 16, CENTERY - 36); setWH(START, 24, 24); - addPrim(ot[db], START); - nextpri += sizeof(TILE); - // D-pad - switch(theControllers[0].button1){ - case 0xDF: // Right PADL->x0 = CENTERX - 64; break; @@ -311,34 +216,25 @@ int main(void) case 0xBF: // Down PADL->y0 = CENTERY + 16; break; - // Start & Select - case 0xF7: START->w = 32; START->h = 32;START->x0 -= 4;START->y0 -= 4; // START break; case 0xFE: // SELECT START->r0 = 0; break; - // Dualshock L3 + R3 - case 0xFD: // L3 TRIGGERL->w += 10; TRIGGERL->h += 10; break; - case 0xFB: //R3 TRIGGERR->w += 10; TRIGGERR->h += 10; break; - } - // Buttons - switch(theControllers[0].button2){ - case 0xDF: // ⭘ PADR->x0 = CENTERX + 66; break; @@ -351,40 +247,30 @@ int main(void) case 0xBF: // ╳ PADR->y0 = CENTERY + 16; break; - // Shoulder buttons - case 0xFB: // L1 TRIGGERL->y0 = CENTERY - 64; break; case 0xFE: // L2 TRIGGERL->y0 = CENTERY - 96; break; - case 0xF7: // R1 TRIGGERR->y0 = CENTERY - 96; break; case 0xFD: // R2 TRIGGERR->y0 = CENTERY - 64; break; - - // Mouse buttons - case 0xF4: // Mouse Left click PADL->w += 10; PADL->h += 10; break; - case 0xF8: // Mouse Right click PADL->w -= 10; PADL->h -= 10; break; } - - FntPrint("Hello 2 pads!\n\n"); - FntPrint( "Pad 1 : %02x\nButtons:%02x %02x, Stick:%02x %02x %02x %02x\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, // @@ -393,7 +279,6 @@ int main(void) theControllers[0].analog1, theControllers[0].analog2, theControllers[0].analog3 ); - FntPrint( "Pad 2 : %02x\nButtons:%02x %02x, Stick:%02x %02x %02x %02x\n", theControllers[1].type, // Controller type : 00 == none, 41 == standard, 73 == analog/dualshock, 12 == mouse, 23 == steering wheel, 63 == gun, 53 == analog joystick theControllers[1].button1, // @@ -403,7 +288,6 @@ int main(void) theControllers[1].analog2, // L3 horizontal theControllers[1].analog3 ); // L3 vertical FntFlush(-1); - display(); } return 0; diff --git a/hello_cube/hello_cube.c b/hello_cube/hello_cube.c index 11af38d..cbad93f 100644 --- a/hello_cube/hello_cube.c +++ b/hello_cube/hello_cube.c @@ -28,7 +28,7 @@ DISPENV disp[2]; DRAWENV draw[2]; u_long ot[2][OTLEN]; // Ordering table (contains addresses to primitives) -char primbuff[2][PRIMBUFFLEN] = {0}; // Primitive list // That's our prim buffer +char primbuff[2][PRIMBUFFLEN]; // Primitive list // That's our prim buffer char * nextpri = primbuff[0]; // Primitive counter short db = 0; // Current buffer counter // Prototypes diff --git a/hello_light/hello_light.c b/hello_light/hello_light.c index bea4946..896b927 100644 --- a/hello_light/hello_light.c +++ b/hello_light/hello_light.c @@ -29,7 +29,7 @@ DISPENV disp[2]; DRAWENV draw[2]; u_long ot[2][OTLEN]; // Ordering table (contains addresses to primitives) -char primbuff[2][PRIMBUFFLEN] = {0}; // Primitive list // That's our prim buffer +char primbuff[2][PRIMBUFFLEN]; // Primitive list // That's our prim buffer char * nextpri = primbuff[0]; // Primitive counter short db = 0; // Current buffer counter long t, p, OTz, Flag; // t == vertex count, p == depth cueing interpolation value, OTz == value to create Z-ordered OT, Flag == see LibOver47.pdf, p.143 @@ -65,7 +65,6 @@ MATRIX cmat = { // Prototypes void init(void); void display(void); -//~ void LoadTexture(u_long * tim, TIM_IMAGE * tparam); void init(){ // Reset the GPU before doing anything and the controller PadInit(0); diff --git a/hello_multivag/hello_multivag.c b/hello_multivag/hello_multivag.c index 7e38c79..ddb17eb 100644 --- a/hello_multivag/hello_multivag.c +++ b/hello_multivag/hello_multivag.c @@ -20,44 +20,29 @@ // libover47.pdf, p.271 // libref47.pdf, p.980 // URLS : http://psx.arthus.net/code/VAG/ - #include #include - #include #include #include - - // Sound system #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 #define SCREENYRES 240 - #define CENTERX SCREENXRES/2 #define CENTERY SCREENYRES/2 - #define MARGINX 0 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 7 // Text Field Height - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - short db = 0; // index of which buffer is used, values 0, 1 - // Sound stuff - #define MALLOC_MAX 3 // Max number of time we can call SpuMalloc - //~ // convert Little endian to Big endian #define SWAP_ENDIAN32(x) (((x)>>24) | (((x)>>8) & 0xFF00) | (((x)<<8) & 0x00FF0000) | ((x)<<24)) - typedef struct VAGheader{ // All the values in this header must be big endian char id[4]; // VAGp 4 bytes -> 1 char * 4 unsigned int version; // 4 bytes @@ -68,123 +53,82 @@ typedef struct VAGheader{ // All the values in this header must be big end char name[16]; // 16 bytes -> 1 char * 16 // Waveform data after that }VAGhdr; - SpuCommonAttr commonAttributes; // structure for changing common voice attributes SpuVoiceAttr voiceAttributes ; // structure for changing individual voice attributes - u_long hello_spu_address; // address allocated in memory for first sound file u_long poly_spu_address; // address allocated in memory for second sound file - // DEBUG : these allow printing values for debugging - u_long hello_spu_start_address; u_long hello_get_start_addr; u_long hello_transSize; - u_long poly_spu_start_address; u_long poly_get_start_addr; u_long poly_transSize; - #define HELLO SPU_0CH // Play first vag on channel 0 #define POLY SPU_2CH // Play second vag on channel 2 - // Memory management table ; allow MALLOC_MAX calls to SpuMalloc() - ibref47.pdf p.1044 char spu_malloc_rec[SPU_MALLOC_RECSIZ * (2 + MALLOC_MAX+1)]; - // VAG files - // We're using GrumpyCoder's Nugget wrapper to compile the code with a modern GCC : https://github.com/grumpycoders/pcsx-redux/tree/main/src/mips/psyq // To include binary files in the exe, add your VAG files to the SRCS variable in Makefile // and in common.mk, add this rule to include *.vag files : // //~ %.o: %.vag //~ $(PREFIX)-objcopy -I binary --set-section-alignment .data=4 --rename-section .data=.rodata,alloc,load,readonly,data,contents -O elf32-tradlittlemips -B mips $< $@ - - // hello.vag - 44100 Khz extern unsigned char _binary____VAG_hello_vag_start[]; // filename must begin with _binary____ followed by the full path, with . and / replaced, and then suffixed with _ and end with _start[]; or end[]; extern unsigned char _binary____VAG_hello_vag_end[]; // https://discord.com/channels/642647820683444236/663664210525290507/780866265077383189 - // poly.vag - 44100 Khz extern unsigned char _binary____VAG_poly_vag_start[]; extern unsigned char _binary____VAG_poly_vag_end[]; - - void initGraph(void) { ResetGraph(0); - 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 (VMODE) { SetVideoMode(MODE_PAL); disp[0].screen.y += 8; disp[1].screen.y += 8; } - SetDispMask(1); // Display on screen - setRGB0(&draw[0], 50, 50, 50); setRGB0(&draw[1], 50, 50, 50); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(8, 60, 304, 200, 0, 500 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - db = !db; - } - - // Audio initialisation & functions - void initSnd(void){ - SpuInitMalloc(MALLOC_MAX, spu_malloc_rec); // Maximum number of blocks, mem. management table address. - commonAttributes.mask = (SPU_COMMON_MVOLL | SPU_COMMON_MVOLR); // Mask which attributes to set commonAttributes.mvol.left = 0x3fff; // Master volume left commonAttributes.mvol.right = 0x3fff; // see libref47.pdf, p.1058 - SpuSetCommonAttr(&commonAttributes); // set attributes - SpuSetIRQ(SPU_OFF); } - u_long sendVAGtoRAM(unsigned int VAG_data_size, unsigned char *VAG_data){ u_long size; - SpuSetTransferMode(SpuTransByDMA); // DMA transfer; can do other processing during transfer - size = SpuWrite (VAG_data + sizeof(VAGhdr), VAG_data_size); // transfer VAG_data_size bytes from VAG_data address to sound buffer - SpuIsTransferCompleted (SPU_TRANSFER_WAIT); // Checks whether transfer is completed and waits for completion - return size; } - void setVoiceAttr(unsigned int pitch, long channel, unsigned long soundAddr ){ - voiceAttributes.mask= //~ Attributes (bit string, 1 bit per attribute) ( SPU_VOICE_VOLL | @@ -200,84 +144,53 @@ void setVoiceAttr(unsigned int pitch, long channel, unsigned long soundAddr ){ SPU_VOICE_ADSR_RR | SPU_VOICE_ADSR_SL ); - voiceAttributes.voice = channel; //~ Voice (low 24 bits are a bit string, 1 bit per voice ) - voiceAttributes.volume.left = 0x1000; //~ Volume voiceAttributes.volume.right = 0x1000; //~ Volume - voiceAttributes.pitch = pitch; //~ Interval (set pitch) voiceAttributes.addr = soundAddr; //~ Waveform data start address - voiceAttributes.a_mode = SPU_VOICE_LINEARIncN; //~ Attack rate mode = Linear Increase - see libref47.pdf p.1091 voiceAttributes.s_mode = SPU_VOICE_LINEARIncN; //~ Sustain rate mode = Linear Increase voiceAttributes.r_mode = SPU_VOICE_LINEARDecN; //~ Release rate mode = Linear Decrease - voiceAttributes.ar = 0x0; //~ Attack rate voiceAttributes.dr = 0x0; //~ Decay rate voiceAttributes.rr = 0x0; //~ Release rate voiceAttributes.sr = 0x0; //~ Sustain rate voiceAttributes.sl = 0xf; //~ Sustain level - SpuSetVoiceAttr(&voiceAttributes); // set attributes - } - void playSFX(unsigned long fx){ SpuSetKey(SpuOn, fx); } - int main(void) { short counter = 0; - const VAGhdr * HellofileHeader = (VAGhdr *) _binary____VAG_hello_vag_start; // get header of first VAG file const VAGhdr * PolyfileHeader = (VAGhdr *) _binary____VAG_poly_vag_start; // get header of second VAG file - // From libover47.pdf : // The sampling frequency of the original audio file can be used to determine the pitch // at which to play the VAG. pitch = (sampling frequency << 12)/44100L // Ex: 44.1kHz=0x1000 22.05kHz=0x800 etc - unsigned int Hellopitch = (SWAP_ENDIAN32(HellofileHeader->samplingFrequency) << 12) / 44100L; unsigned int Polypitch = (SWAP_ENDIAN32(PolyfileHeader->samplingFrequency) << 12) / 44100L; - SpuInit(); // Initialize SPU. Called only once. - initSnd(); - // First VAG - hello_spu_address = SpuMalloc(SWAP_ENDIAN32(HellofileHeader->dataSize)); // Allocate an area of dataSize bytes in the sound buffer. - hello_spu_start_address = SpuSetTransferStartAddr(hello_spu_address); // Sets a starting address in the sound buffer - hello_get_start_addr = SpuGetTransferStartAddr(); // SpuGetTransferStartAddr() returns current sound buffer transfer start address. - hello_transSize = sendVAGtoRAM(SWAP_ENDIAN32(HellofileHeader->dataSize), _binary____VAG_hello_vag_start); - // First VAG - poly_spu_address = SpuMalloc(SWAP_ENDIAN32(PolyfileHeader->dataSize)); // Allocate an area of dataSize bytes in the sound buffer. - poly_spu_start_address = SpuSetTransferStartAddr(poly_spu_address); // Sets a starting address in the sound buffer - poly_get_start_addr = SpuGetTransferStartAddr(); // SpuGetTransferStartAddr() returns current sound buffer transfer start address. - poly_transSize = sendVAGtoRAM(SWAP_ENDIAN32(PolyfileHeader->dataSize), _binary____VAG_poly_vag_start); - - // set VAG to channel - setVoiceAttr(Hellopitch, HELLO, hello_spu_address); // SPU_0CH == hello - setVoiceAttr(Polypitch, POLY, poly_spu_address); // SPU_2CH == poly - initGraph(); - while (1) { - if(!counter){ playSFX(HELLO); // Play first VAG counter = 240; @@ -288,8 +201,6 @@ int main(void) if(counter == 80){ playSFX(HELLO|POLY); // Play both VAGs simultaneously } - - FntPrint("First VAG:"); FntPrint("\nPitch : %08x-%dKhz", Hellopitch, (SWAP_ENDIAN32(HellofileHeader->samplingFrequency)) ); FntPrint("\nSet Start addr : %08x", hello_spu_address); @@ -297,7 +208,6 @@ int main(void) FntPrint("\nGet Start addr : %08x", hello_get_start_addr); FntPrint("\nSend size : %08x", SWAP_ENDIAN32(HellofileHeader->dataSize)); FntPrint("\nReturn size : %08x\n", hello_transSize); - FntPrint("\nSecond VAG:"); FntPrint("\nPitch : %08x-%dKhz", Polypitch, (SWAP_ENDIAN32(HellofileHeader->samplingFrequency)) ); FntPrint("\nSet Start addr : %08x", poly_spu_address); @@ -305,16 +215,10 @@ int main(void) FntPrint("\nGet Start addr : %08x", poly_get_start_addr); FntPrint("\nSend size : %08x", SWAP_ENDIAN32(PolyfileHeader->dataSize)); FntPrint("\nReturn size : %08x\n", poly_transSize); - - FntPrint("\nCounter : %d\n", counter); - FntFlush(-1); - counter --; - display(); - } return 0; } diff --git a/hello_pad/hello_pad.c b/hello_pad/hello_pad.c index 21281db..d26c387 100644 --- a/hello_pad/hello_pad.c +++ b/hello_pad/hello_pad.c @@ -8,43 +8,28 @@ #include #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 #define SCREENYRES 240 - #define CENTERX SCREENXRES/2 #define CENTERY SCREENYRES/2 - #define MARGINX 32 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 2 // Text Field Height - #define OTLEN 8 // Ordering Table Length - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes - -char primbuff[2][32768] = {1}; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes - +char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] - short db = 0; // index of which buffer is used, values 0, 1 - void init(void) { ResetGraph(0); - 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 (VMODE) { SetVideoMode(MODE_PAL); @@ -52,160 +37,101 @@ void init(void) disp[1].screen.y += 8; } SetDispMask(1); // Display on screen - setRGB0(&draw[0], 50, 50, 50); setRGB0(&draw[1], 50, 50, 50); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - DrawOTag(&ot[db][OTLEN - 1]); - db = !db; - nextpri = primbuff[db]; } - - int main(void) { - TILE * PADL; // Tile primitives TILE * TRIGGERL; - TILE * PADR; TILE * TRIGGERR; - TILE * START, * SELECT; - int pad = 0; - init(); - PadInit(0); // Initialize pad. Mode is always 0 - while (1) { ClearOTagR(ot[db], OTLEN); - // D-cross - PADL = (TILE *)nextpri; - setTile(PADL); setRGB0(PADL, 0, 0, 255); setXY0(PADL, CENTERX - 80, CENTERY); setWH(PADL, 24, 24); - - addPrim(ot[db], PADL); - nextpri += sizeof(TILE); - // L1+L2 - TRIGGERL = (TILE *)nextpri; - setTile(TRIGGERL); setRGB0(TRIGGERL, 255, 0, 0); setXY0(TRIGGERL, CENTERX - 80, CENTERY - 80); setWH(TRIGGERL, 24, 24); - - addPrim(ot[db], TRIGGERL); - nextpri += sizeof(TILE); - // /\, X, O, [] - PADR = (TILE *)nextpri; - setTile(PADR); setRGB0(PADR, 0, 255, 0); setXY0(PADR, CENTERX + 50, CENTERY); setWH(PADR, 24, 24); - addPrim(ot[db], PADR); - nextpri += sizeof(TILE); - // R1+R2 - TRIGGERR = (TILE *)nextpri; - setTile(TRIGGERR); setRGB0(TRIGGERR, 255, 0, 255); setXY0(TRIGGERR, CENTERX + 50, CENTERY -80); setWH(TRIGGERR, 24, 24); - addPrim(ot[db], TRIGGERR); - nextpri += sizeof(TILE); - // START + SELECT - START = (TILE *)nextpri; - setTile(START); setRGB0(START, 240, 240, 240); setXY0(START, CENTERX - 16, CENTERY - 36); setWH(START, 24, 24); - addPrim(ot[db], START); - nextpri += sizeof(TILE); - // Pad stuff - pad = PadRead(0); // Read pads input. id is unused, always 0. // PadRead() returns a 32 bit value, where input from pad 1 is stored in the low 2 bytes and input from pad 2 is stored in the high 2 bytes. (https://matiaslavik.wordpress.com/2015/02/13/diving-into-psx-development/) - // D-pad - if(pad & PADLup) {PADL->y0 = CENTERY - 16;} // 🡩 // To access pad 2, use ( pad >> 16 & PADLup)... if(pad & PADLdown) {PADL->y0 = CENTERY + 16;} // 🡫 if(pad & PADLright){PADL->x0 = CENTERX - 64;} // 🡪 if(pad & PADLleft) {PADL->x0 = CENTERX - 96;} // 🡨 - // Buttons - if(pad & PADRup) {PADR->y0 = CENTERY - 16;} // △ if(pad & PADRdown) {PADR->y0 = CENTERY + 16;} // ╳ if(pad & PADRright){PADR->x0 = CENTERX + 66;} // ⭘ if(pad & PADRleft) {PADR->x0 = CENTERX + 34;} // ⬜ - // Shoulder buttons - if(pad & PADL1){TRIGGERL->y0 = CENTERY - 64;} // L1 if(pad & PADL2){TRIGGERL->y0 = CENTERY - 96;} // L2 if(pad & PADR1){TRIGGERR->y0 = CENTERY - 64;} // R1 if(pad & PADR2){TRIGGERR->y0 = CENTERY - 96;} // R2 - // Start & Select - if(pad & PADstart){START->w = 32; START->h = 32;START->x0 -= 4;START->y0 -= 4;} // START if(pad & PADselect){START->r0 = 0;} // SELECT - FntPrint("Hello Pad!"); - FntFlush(-1); - display(); } return 0; diff --git a/hello_poly/hello_poly.c b/hello_poly/hello_poly.c index 6e551da..404357f 100644 --- a/hello_poly/hello_poly.c +++ b/hello_poly/hello_poly.c @@ -4,182 +4,109 @@ // // From ../psyq/addons/graphics/MESH/RMESH/TUTO0.C : // Schnappy 2021 - #include #include #include #include #include #include - - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 // Screen width #define SCREENYRES 240 // Screen height - #define CENTERX SCREENXRES/2 // Center of screen on x #define CENTERY SCREENYRES/2 // Center of screen on y - #define MARGINX 0 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 7 // Text Field Height - #define OTLEN 8 // Ordering Table Length - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes - char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes - char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] - short db = 0; // index of which buffer is used, values 0, 1 - - -MATRIX identity(int num) // generate num x num matrix -{ - int row, col; - MATRIX matrix; - - for (row = 0; row < num; row++) - { - for (col = 0; col < num; col++) - { - if (row == col) - matrix.m[row][col] = 4096; - else - matrix.m[row][col] = 0; - } - } - return matrix; -} - void init(void) { ResetGraph(0); - // Initialize and setup the GTE InitGeom(); SetGeomOffset(CENTERX,CENTERY); SetGeomScreen(CENTERX); - 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 (VMODE) - { + if (VMODE) { SetVideoMode(MODE_PAL); - disp[0].screen.y += 8; - disp[1].screen.y += 8; + disp[0].disp.y = disp[1].disp.y = 8; } - SetDispMask(1); // Display on screen + SetDispMask(1); setRGB0(&draw[0], 50, 50, 50); setRGB0(&draw[1], 50, 50, 50); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - DrawOTag(&ot[db][OTLEN - 1]); - db = !db; - nextpri = primbuff[db]; } - - int main(void) { - - MATRIX IDMATRIX = identity(3); // Generate 3x3 identity matrix - POLY_F4 *poly = {0}; // pointer to a POLY_F4 SVECTOR RotVector = {0, 0, 0}; // Initialize rotation vector {x, y, z} VECTOR MovVector = {0, 0, CENTERX, 0}; // Initialize translation vector {x, y, z} VECTOR ScaleVector ={ONE, ONE, ONE}; // ONE is define as 4096 in libgte.h - SVECTOR VertPos[4] = { // Set initial vertices position relative to 0,0 - see here : https://psx.arthus.net/docs/poly_f4.jpg {-32, -32, 1 }, // Vert 1 {-32, 32, 1 }, // Vert 2 { 32, -32, 1 }, // Vert 3 { 32, 32, 1 } // Vert 4 }; - MATRIX PolyMatrix = IDMATRIX; - + MATRIX PolyMatrix = {0}; long polydepth; long polyflag; long OTz; - init(); - while (1) { ClearOTagR(ot[db], OTLEN); - poly = (POLY_F4 *)nextpri; // Set poly to point to the address of the next primitiv in the buffer - // Set transform matrices for this polygon - RotMatrix(&RotVector, &PolyMatrix); // Apply rotation matrix TransMatrix(&PolyMatrix, &MovVector); ScaleMatrix(&PolyMatrix, &ScaleVector); // Apply translation matrix - SetRotMatrix(&PolyMatrix); // Set default rotation matrix SetTransMatrix(&PolyMatrix); // Set default transformation matrix - setPolyF4(poly); // Initialize poly as a POLY_F4 setRGB0(poly, 255, 0, 255); // Set poly color - - - // RotTransPers - + // RotTransPers using 4 calls //~ OTz = RotTransPers(&VertPos[0], (long*)&poly->x0, &polydepth, &polyflag); //~ RotTransPers(&VertPos[1], (long*)&poly->x1, &polydepth, &polyflag); //~ RotTransPers(&VertPos[2], (long*)&poly->x2, &polydepth, &polyflag); //~ RotTransPers(&VertPos[3], (long*)&poly->x3, &polydepth, &polyflag); - // RotTransPers4 equivalent - OTz = RotTransPers4( &VertPos[0], &VertPos[1], &VertPos[2], &VertPos[3], (long*)&poly->x0, (long*)&poly->x1, (long*)&poly->x2, (long*)&poly->x3, &polydepth, &polyflag ); // Perform coordinate and perspective transformation for 4 vertices - RotVector.vy += 4; RotVector.vz += 4; // Apply rotation on Z-axis. On PSX, the Z-axis is pointing away from the screen. - addPrim(ot[db], poly); // add poly to the Ordering table - nextpri += sizeof(POLY_F4); // increment nextpri address with size of a POLY_F4 struct - FntPrint("Hello Poly !"); - FntFlush(-1); - display(); } return 0; diff --git a/hello_poly_ft/hello_poly_ft.c b/hello_poly_ft/hello_poly_ft.c index 798e65f..2e8e581 100644 --- a/hello_poly_ft/hello_poly_ft.c +++ b/hello_poly_ft/hello_poly_ft.c @@ -43,26 +43,6 @@ extern unsigned long _binary____TIM_bousai_tim_end[]; extern unsigned long _binary____TIM_bousai_tim_length; TIM_IMAGE bousai; - - -MATRIX identity(int num) // generate num x num matrix -{ - int row, col; - MATRIX matrix; - - for (row = 0; row < num; row++) - { - for (col = 0; col < num; col++) - { - if (row == col) - matrix.m[row][col] = 4096; - else - matrix.m[row][col] = 0; - } - } - return matrix; -} - 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 @@ -132,9 +112,7 @@ void display(void) int main(void) { - - MATRIX IDMATRIX = identity(3); // Generate 3x3 identity matrix - + POLY_FT4 *poly = {0}; // pointer to a POLY_G4 SVECTOR RotVector = {0, 0, 0}; // Initialize rotation vector {x, y, z} VECTOR MovVector = {0, 0, CENTERX/2, 0}; // Initialize translation vector {x, y, z, pad} @@ -146,7 +124,7 @@ int main(void) { 32, -32, 1 }, // Vert 3 { 32, 32, 1 } // Vert 4 }; - MATRIX PolyMatrix = IDMATRIX; + MATRIX PolyMatrix = {0}; long polydepth; long polyflag; diff --git a/hello_poly_fun/hello_poly_fun.c b/hello_poly_fun/hello_poly_fun.c index ff87374..266ea8f 100644 --- a/hello_poly_fun/hello_poly_fun.c +++ b/hello_poly_fun/hello_poly_fun.c @@ -2,51 +2,35 @@ // Credits : Schnappy //With great help from Jaby smoll Seamonstah, Nicolas Noble, NDR008, paul, sickle on https://discord.com/invite/Zd82yXvs // 11/2020 - #include #include #include #include #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SPIN 16 // Rotation speed increment - #define SCREENXRES 320 #define SCREENYRES 240 - #define CENTERX SCREENXRES/2 #define CENTERY SCREENYRES/2 - #define MARGINX 10 // margins for text display #define MARGINY 4 - #define FONTSIZE 8 * 7 // Text Field Height - #define OTLEN 16 // Ordering Table Length - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes - char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes - char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] - short db = 0; // index of which buffer is used, values 0, 1 - CVECTOR BgColor[3] = {20, 20, 20}; - struct polygon { POLY_F4 * poly_f4; CVECTOR color; short width; short height; - //~ VECTOR PosV_L; // Not used anymore SVECTOR RotV_L; VECTOR TransV_L; VECTOR ScaleV_L; @@ -58,118 +42,66 @@ struct polygon short rotSpeed; int otz; }; - void init(void) { ResetGraph(0); - // Initialize and setup the GTE : Not needed ? - InitGeom(); SetGeomOffset(CENTERX,CENTERY); SetGeomScreen(CENTERX); - PadInit(0); - 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 (VMODE) { SetVideoMode(MODE_PAL); disp[0].screen.y += 8; disp[1].screen.y += 8; - } + } SetDispMask(1); // Display on screen - setRGB0(&draw[0], BgColor->r, BgColor->g, BgColor->b); setRGB0(&draw[1], BgColor->r, BgColor->g, BgColor->b); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - DrawOTag(&ot[db][OTLEN - 1]); - db = !db; - nextpri = primbuff[db]; } - - void pivotPoint(SVECTOR VertPos[3],short width,short height, SVECTOR pivot){ - // Not very efficient I think - VertPos[0].vx = -pivot.vx; VertPos[0].vy = -pivot.vy; VertPos[0].vz = 1; - VertPos[1].vx = width - pivot.vx; VertPos[1].vy = -pivot.vy; VertPos[1].vz = 1; - VertPos[2].vx = -pivot.vx; VertPos[2].vy = height-pivot.vy; VertPos[2].vz = 1; - VertPos[3].vx = width - pivot.vx; VertPos[3].vy = height - pivot.vy; VertPos[3].vz = 1; } - - -MATRIX identity(int num) -{ - int row, col; - MATRIX matrix; - - for (row = 0; row < num; row++) - { - for (col = 0; col < num; col++) - { - if (row == col) - matrix.m[row][col] = 4096; - else - matrix.m[row][col] = 0; - } - } - return matrix; -} - int main(void) { - - MATRIX IDMATRIX = identity(3); - u_short BtnTimer = 0; // Timer to limit pad input rate u_short polyCount = 1; // current polygon index - int otz; // z-index - struct polygon *CurrentPoly; // points to the address of selected polygon - - // White cursor : shows which polygon is selected - struct polygon cursorS = { cursorS.poly_f4, {255, 255, 255}, // color @@ -184,11 +116,9 @@ int main(void) {-1, 1, 1}, { 1, 1, 1} }, - IDMATRIX // Matrix + {0} // Matrix }; - //Red - struct polygon polyS = { polyS.poly_f4, {255, 0, 0}, // color @@ -203,15 +133,12 @@ int main(void) {0, 0, 0}, {0, 0, 0} }, - IDMATRIX, // Matrix + {0}, // Matrix 0,0, // depth, flag 8, // rotSpeed 1 // z-index }; - - //Yellow - struct polygon poly1S = { poly1S.poly_f4, {255, 187, 0}, // color @@ -226,15 +153,12 @@ int main(void) {0, 0, 0}, {0, 0, 0} }, - IDMATRIX, // Matrix + {0}, // Matrix 0,0, // depth, flag -12, // rotSpeed 2 // z-index }; - - //Green - struct polygon poly2S = { poly2S.poly_f4, {0, 255, 153}, // color @@ -249,15 +173,12 @@ int main(void) {0, 0, 0}, {0, 0, 0} }, - IDMATRIX, // Matrix + {0}, // Matrix 0,0, // depth, flag -6, // rotSpeed 3 // z-index }; - - //Blue - struct polygon poly3S = { poly3S.poly_f4, {112, 254, 254}, // color @@ -272,64 +193,45 @@ int main(void) {0, 0, 0}, {0, 0, 0} }, - IDMATRIX, // Matrix + {0}, // Matrix 0,0, //depth, flag 256, //rotSpeed 4 // z-index }; - - ///// - CurrentPoly = &polyS; - pivotPoint(polyS.Verts, polyS.width, polyS.height, polyS.PivotV_L); pivotPoint(poly1S.Verts, poly1S.width, poly1S.height, poly1S.PivotV_L); pivotPoint(poly2S.Verts, poly2S.width, poly2S.height, poly2S.PivotV_L); pivotPoint(poly3S.Verts, poly3S.width, poly3S.height, poly3S.PivotV_L); - init(); - while (1) { ClearOTagR(ot[db], OTLEN); - cursorS.poly_f4 = (POLY_F4 *)nextpri; - RotMatrix(&cursorS.RotV_L , &cursorS.Matrix); TransMatrix(&cursorS.Matrix, &CurrentPoly->TransV_L); - SetRotMatrix(&cursorS.Matrix); SetTransMatrix(&cursorS.Matrix); - setPolyF4(cursorS.poly_f4); setRGB0(cursorS.poly_f4,cursorS.color.r,cursorS.color.g,cursorS.color.b); //~ setXY4(cursorS, MovVector.vx-1, MovVector.vy-1 ,MovVector.vx + 1, MovVector.vy -1,MovVector.vx-1, MovVector.vy+1,MovVector.vx+1, MovVector.vy+1); - RotTransPers4( &cursorS.Verts[0], &cursorS.Verts[1], &cursorS.Verts[2], &cursorS.Verts[3], (long*)&cursorS.poly_f4->x0, (long*)&cursorS.poly_f4->x1, (long*)&cursorS.poly_f4->x2, (long*)&cursorS.poly_f4->x3, &cursorS.depth, &cursorS.flag ); - addPrim(ot[db], cursorS.poly_f4); - nextpri += sizeof(POLY_F4); - ///// Red - polyS.poly_f4 = (POLY_F4 *)nextpri; - polyS.RotV_L.vz += polyS.rotSpeed; - RotMatrix(&polyS.RotV_L, &polyS.Matrix); TransMatrix(&polyS.Matrix, &polyS.TransV_L); ScaleMatrix(&polyS.Matrix, &polyS.ScaleV_L); - SetRotMatrix(&polyS.Matrix); SetTransMatrix(&polyS.Matrix); - setPolyF4(polyS.poly_f4); setRGB0(polyS.poly_f4, polyS.color.r,polyS.color.g,polyS.color.b); RotTransPers4( @@ -338,25 +240,16 @@ int main(void) &polyS.depth, &polyS.flag ); - addPrim(ot[db]+polyS.otz, polyS.poly_f4); - nextpri += sizeof(POLY_F4); - ///// Yellow - poly1S.poly_f4 = (POLY_F4 *)nextpri; - poly1S.RotV_L.vz += poly1S.rotSpeed; - RotMatrix(&poly1S.RotV_L, &poly1S.Matrix); TransMatrix(&poly1S.Matrix, &poly1S.TransV_L); ScaleMatrix(&poly1S.Matrix, &poly1S.ScaleV_L); - - SetRotMatrix(&poly1S.Matrix); SetTransMatrix(&poly1S.Matrix); - setPolyF4(poly1S.poly_f4); setRGB0(poly1S.poly_f4, poly1S.color.r,poly1S.color.g,poly1S.color.b); RotTransPers4( @@ -365,26 +258,16 @@ int main(void) &poly1S.depth, &poly1S.flag ); - addPrim(ot[db]+poly1S.otz, poly1S.poly_f4); - nextpri += sizeof(POLY_F4); - - ///// Green - poly2S.poly_f4 = (POLY_F4 *)nextpri; - poly2S.RotV_L.vz += poly2S.rotSpeed; - RotMatrix(&poly2S.RotV_L, &poly2S.Matrix); TransMatrix(&poly2S.Matrix, &poly2S.TransV_L); ScaleMatrix(&poly2S.Matrix, &poly2S.ScaleV_L); - - SetRotMatrix(&poly2S.Matrix); SetTransMatrix(&poly2S.Matrix); - setPolyF4(poly2S.poly_f4); setRGB0(poly2S.poly_f4, poly2S.color.r,poly2S.color.g,poly2S.color.b); RotTransPers4( @@ -393,24 +276,16 @@ int main(void) &poly2S.depth, &poly2S.flag ); - addPrim(ot[db]+poly2S.otz, poly2S.poly_f4); - nextpri += sizeof(POLY_F4); - ///// Blue - poly3S.poly_f4 = (POLY_F4 *)nextpri; - poly3S.RotV_L.vz += poly3S.rotSpeed; - RotMatrix(&poly3S.RotV_L, &poly3S.Matrix); TransMatrix(&poly3S.Matrix, &poly3S.TransV_L); ScaleMatrix(&poly3S.Matrix, &poly3S.ScaleV_L); - SetRotMatrix(&poly3S.Matrix); SetTransMatrix(&poly3S.Matrix); - setPolyF4(poly3S.poly_f4); setRGB0(poly3S.poly_f4, poly3S.color.r,poly3S.color.g,poly3S.color.b); RotTransPers4( @@ -419,19 +294,11 @@ int main(void) &poly3S.depth, &poly3S.flag ); - addPrim(ot[db]+poly3S.otz, poly3S.poly_f4); - nextpri += sizeof(POLY_F4); - - // Pad stuff - - int pad = PadRead(0); // init pad - // Right D-pad - if(pad & PADRup){ if (CurrentPoly->PivotV_L.vy >= 0){ CurrentPoly->PivotV_L.vy -= 1; @@ -441,7 +308,6 @@ int main(void) CurrentPoly->PivotV_L.vy = CurrentPoly->PivotV_L.vy; } }; - if(pad & PADRdown){ if (CurrentPoly->PivotV_L.vy <= CurrentPoly->height ){ CurrentPoly->PivotV_L.vy += 1; @@ -451,7 +317,6 @@ int main(void) CurrentPoly->PivotV_L.vy = CurrentPoly->PivotV_L.vy; } }; - if(pad & PADRleft){ if (CurrentPoly->PivotV_L.vx >= 0){ CurrentPoly->PivotV_L.vx -= 1; @@ -461,7 +326,6 @@ int main(void) CurrentPoly->PivotV_L.vx = CurrentPoly->PivotV_L.vx; } }; - if(pad & PADRright){ if (CurrentPoly->PivotV_L.vx <= CurrentPoly->width ){ CurrentPoly->PivotV_L.vx += 1; @@ -471,13 +335,9 @@ int main(void) CurrentPoly->PivotV_L.vx = CurrentPoly->PivotV_L.vx; } }; - // R1, R2, L2, L2 - if(pad & PADR1){ - if(BtnTimer == 0){ - if (polyCount < 4){ CurrentPoly -= 1; BtnTimer = 10; @@ -489,9 +349,7 @@ int main(void) } } } - if(pad & PADR2){ - if(BtnTimer == 0){ if(CurrentPoly->otz < 5 ){ CurrentPoly->otz += 1; @@ -502,9 +360,7 @@ int main(void) } } } - if(pad & PADL1){ - if(BtnTimer == 0){ if (CurrentPoly->rotSpeed <= 320){ CurrentPoly->rotSpeed += 8; @@ -512,9 +368,7 @@ int main(void) BtnTimer = 10; } } - if(pad & PADL2){ - if(BtnTimer == 0){ if (CurrentPoly->rotSpeed >= -320){ CurrentPoly->rotSpeed -= 8; @@ -522,84 +376,50 @@ int main(void) BtnTimer = 10; } } - // Left D-Pad - if(pad & PADLup){ - if(BtnTimer == 0){ CurrentPoly->TransV_L.vy -= 1; //~ BtnTimer = 2; } } if(pad & PADLdown){ - if(BtnTimer == 0){ CurrentPoly->TransV_L.vy += 1; //~ BtnTimer = 2; } } if(pad & PADLleft){ - if(BtnTimer == 0){ CurrentPoly->TransV_L.vx -= 1; //~ BtnTimer = 2; } } if(pad & PADLright){ - if(BtnTimer == 0){ CurrentPoly->TransV_L.vx += 1; //~ BtnTimer = 2; } } if(pad & PADstart){ - if(BtnTimer == 0){ CurrentPoly->ScaleV_L.vx += 100; CurrentPoly->ScaleV_L.vy += 100; //~ CurrentPoly->TransV_L.vz += 1; - } } if(pad & PADselect){ - if(BtnTimer == 0){ CurrentPoly->ScaleV_L.vx -= 100; CurrentPoly->ScaleV_L.vy -= 100; //~ CurrentPoly->TransV_L.vz -= 1; - } } - // Btn_timer decrement - if(BtnTimer > 0){ BtnTimer -= 1; } - - // Debug stuff - - // Display Rotation matrix - - //~ FntPrint("Rotmatrix:\n%d %d %d\n%d %d %d\n%d %d %d \n", - //~ Poly1Matrix.m[0][0], Poly1Matrix.m[0][1], Poly1Matrix.m[0][2], - //~ Poly1Matrix.m[1][0], Poly1Matrix.m[1][1], Poly1Matrix.m[1][2], - //~ Poly1Matrix.m[2][0], Poly1Matrix.m[2][1], Poly1Matrix.m[2][2]); - - // Display Mem adress and values of verticess - //~ FntPrint("cur:%x\n 0:%x\n 1:%x\n 2:%x\n 3:%x\n", CurrentPoly, &polyS, &poly1S, &poly2S, &poly3S); - //~ FntPrint("timer:%d polyCount:%d speed:%d", BtnTimer, polyCount, CurrentPoly->rotSpeed ); - - //~ FntPrint("&poly->x0 Addr:%x Value:%d \n&poly->y0 Addr:%x Value:%d \n&poly->x1 Addr:%x Value:%d", - //~ (long)&poly->x0, poly->x0, - //~ (long)&poly->y0, poly->y0, - //~ (long)&poly->x1, poly->x1); - - //~ FntPrint("otz : %d\n" , CurrentPoly->rotSpeed); - // On-screen instructions - FntPrint("\ D-Pad:move polygon.\n\ [],X,O,\/\\ : Move pivot point.\n\ @@ -608,9 +428,7 @@ R1 : select polygon\n\ R2 : change z-index\n\ Start,Select : Scale polygon +/-\ "); - FntFlush(-1); - display(); } return 0; diff --git a/hello_poly_gt/hello_poly_gt.c b/hello_poly_gt/hello_poly_gt.c index 1726f98..43a3ebd 100644 --- a/hello_poly_gt/hello_poly_gt.c +++ b/hello_poly_gt/hello_poly_gt.c @@ -4,95 +4,52 @@ // // From ../psyq/addons/graphics/MESH/RMESH/TUTO0.C : // Schnappy 2021 - - #include #include #include #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 // Screen width #define SCREENYRES 240 // Screen height - #define CENTERX SCREENXRES/2 // Center of screen on x #define CENTERY SCREENYRES/2 // Center of screen on y - #define MARGINX 32 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 5 // Text Field Height - #define OTLEN 8 // Ordering Table Length - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes - char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes - char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] - short db = 0; // index of which buffer is used, values 0, 1 - // 16bpp TIM extern unsigned long _binary____TIM_bousai_tim_start[]; extern unsigned long _binary____TIM_bousai_tim_end[]; extern unsigned long _binary____TIM_bousai_tim_length; - TIM_IMAGE bousai; - - -MATRIX identity(int num) // generate num x num matrix -{ - int row, col; - MATRIX matrix; - - for (row = 0; row < num; row++) - { - for (col = 0; col < num; col++) - { - if (row == col) - matrix.m[row][col] = 4096; - else - matrix.m[row][col] = 0; - } - } - return matrix; -} - 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 } } - void init(void) { ResetGraph(0); - // Initialize and setup the GTE - InitGeom(); SetGeomOffset(CENTERX,CENTERY); SetGeomScreen(CENTERX); - 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 (VMODE) { SetVideoMode(MODE_PAL); @@ -102,102 +59,68 @@ void init(void) SetDispMask(1); // Display on screen setRGB0(&draw[0], 128, 128, 128); setRGB0(&draw[1], 128, 128, 128); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - DrawOTag(&ot[db][OTLEN - 1]); - db = !db; - nextpri = primbuff[db]; } - - int main(void) { int i; - - MATRIX IDMATRIX = identity(3); // Generate 3x3 identity matrix - POLY_GT4 *poly = {0}; // pointer to a POLY_G4 SVECTOR RotVector = {0, 0, 0}; // Initialize rotation vector {x, y, z} VECTOR MovVector = {0, 0, 120, 0}; // Initialize translation vector {x, y, z} - SVECTOR VertPos[4] = { // Set initial vertices position relative to 0,0 - see here : https://psx.arthus.net/docs/poly_f4.jpg {-32, -32, 0 }, // Vert 1 {-32, 32, 0 }, // Vert 2 { 32, -32, 0 }, // Vert 3 { 32, 32, 0 } // Vert 4 }; - MATRIX PolyMatrix = IDMATRIX; - + MATRIX PolyMatrix = {0}; DR_TPAGE * bousai_tpage; - long polydepth; long polyflag; - init(); - LoadTexture(_binary____TIM_bousai_tim_start, &bousai); - while (1) { ClearOTagR(ot[db], OTLEN); - poly = (POLY_GT4 *)nextpri; // Set poly to point to the address of the next primitiv in the buffer - // Set transform matrices for this polygon - RotMatrix(&RotVector, &PolyMatrix); // Apply rotation matrix TransMatrix(&PolyMatrix, &MovVector); // Apply translation matrix - SetRotMatrix(&PolyMatrix); // Set default rotation matrix SetTransMatrix(&PolyMatrix); // Set default transformation matrix - setPolyGT4(poly); // Initialize poly as a POLY_F4 poly->tpage = getTPage(bousai.mode&0x3, 0, bousai.prect->x, bousai.prect->y); - setRGB0(poly, 128, 128, 128); // Set vertice 1 color setRGB1(poly, 255, 0, 0); // Set vertice 2 color setRGB2(poly, 0, 255, 0); // Set vertice 3 color setRGB3(poly, 0, 0, 255); // Set vertice 4 color - RotTransPers4( &VertPos[0], &VertPos[1], &VertPos[2], &VertPos[3], (long*)&poly->x0, (long*)&poly->x1, (long*)&poly->x2, (long*)&poly->x3, &polydepth, &polyflag ); // Perform coordinate and perspective transformation for 4 vertices - setUV4(poly, 0, 0, 0, 144, 144, 0, 144, 144); // Set UV coordinates in order Top Left, Bottom Left, Top Right, Bottom Right - RotVector.vx += 8; // Apply rotation on Z-axis. On PSX, the Z-axis is pointing away from the screen. - addPrim(ot[db], poly); // add poly to the Ordering table - nextpri += sizeof(POLY_GT4); // increment nextpri address with size of a POLY_F4 struct - FntPrint("Hello textured shaded !"); - FntFlush(-1); - display(); } return 0; diff --git a/hello_poly_gt_tw/hello_poly_gt_tw.c b/hello_poly_gt_tw/hello_poly_gt_tw.c index 69e5e85..283a61c 100644 --- a/hello_poly_gt_tw/hello_poly_gt_tw.c +++ b/hello_poly_gt_tw/hello_poly_gt_tw.c @@ -4,95 +4,52 @@ // // From ../psyq/addons/graphics/MESH/RMESH/TUTO0.C : // Schnappy 2021 - - #include #include #include #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 // Screen width #define SCREENYRES 240 // Screen height - #define CENTERX SCREENXRES/2 // Center of screen on x #define CENTERY SCREENYRES/2 // Center of screen on y - #define MARGINX 32 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 5 // Text Field Height - #define OTLEN 8 // Ordering Table Length - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes - char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes - char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] - short db = 0; // index of which buffer is used, values 0, 1 - // 16bpp TIM extern unsigned long _binary____TIM_bousai_tim_start[]; extern unsigned long _binary____TIM_bousai_tim_end[]; extern unsigned long _binary____TIM_bousai_tim_length; - TIM_IMAGE bousai; - - -MATRIX identity(int num) // generate num x num matrix -{ - int row, col; - MATRIX matrix; - - for (row = 0; row < num; row++) - { - for (col = 0; col < num; col++) - { - if (row == col) - matrix.m[row][col] = 4096; - else - matrix.m[row][col] = 0; - } - } - return matrix; -} - 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 } } - void init(void) { ResetGraph(0); - // Initialize and setup the GTE - InitGeom(); SetGeomOffset(CENTERX,CENTERY); SetGeomScreen(CENTERX); - 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 (VMODE) { SetVideoMode(MODE_PAL); @@ -100,124 +57,79 @@ void init(void) disp[1].screen.y += 8; } SetDispMask(1); // Display on screen - setRGB0(&draw[0], 128, 128, 128); setRGB0(&draw[1], 128, 128, 128); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - DrawOTag(&ot[db][OTLEN - 1]); - db = !db; - nextpri = primbuff[db]; } - - int main(void) { int i; - - MATRIX IDMATRIX = identity(3); // Generate 3x3 identity matrix - POLY_GT4 *poly = {0}; // pointer to a POLY_G4 SVECTOR RotVector = {0, 0, 0}; // Initialize rotation vector {x, y, z} VECTOR MovVector = {0, 0, 120, 0}; // Initialize translation vector {x, y, z} - SVECTOR VertPos[4] = { // Set initial vertices position relative to 0,0 - see here : https://psx.arthus.net/docs/poly_f4.jpg {-32, -32, 0 }, // Vert 1 {-32, 32, 0 }, // Vert 2 { 32, -32, 0 }, // Vert 3 { 32, 32, 0 } // Vert 4 }; - MATRIX PolyMatrix = IDMATRIX; - + MATRIX PolyMatrix = {0}; DR_TPAGE * bousai_tpage; - long polydepth; long polyflag; - // Texture window - DR_MODE * dr_mode; // Pointer to dr_mode prim - RECT tws = {64, 32, 32, 32}; // Texture window coordinates : x, y, w, h // See libref47.pdf, p.242, 7-6, table 7-2 for possible values init(); - LoadTexture(_binary____TIM_bousai_tim_start, &bousai); - while (1) { ClearOTagR(ot[db], OTLEN); - - poly = (POLY_GT4 *)nextpri; // Set poly to point to the address of the next primitiv in the buffer - // Set transform matrices for this polygon - RotMatrix(&RotVector, &PolyMatrix); // Apply rotation matrix TransMatrix(&PolyMatrix, &MovVector); // Apply translation matrix - SetRotMatrix(&PolyMatrix); // Set default rotation matrix SetTransMatrix(&PolyMatrix); // Set default transformation matrix - setPolyGT4(poly); // Initialize poly as a POLY_F4 poly->tpage = getTPage(bousai.mode&0x3, 0, bousai.prect->x, bousai.prect->y); - setRGB0(poly, 128, 128, 128); // Set vertice 1 color setRGB1(poly, 255, 0, 0); // Set vertice 2 color setRGB2(poly, 0, 255, 0); // Set vertice 3 color setRGB3(poly, 0, 0, 255); // Set vertice 4 color - RotTransPers4( &VertPos[0], &VertPos[1], &VertPos[2], &VertPos[3], (long*)&poly->x0, (long*)&poly->x1, (long*)&poly->x2, (long*)&poly->x3, &polydepth, &polyflag ); // Perform coordinate and perspective transformation for 4 vertices - setUV4(poly, 0, 0, 0, 144, 144, 0, 144, 144); // Set UV coordinates in order Top Left, Bottom Left, Top Right, Bottom Right - RotVector.vy += 14; // Apply rotation on Z-axis. On PSX, the Z-axis is pointing away from the screen. - addPrim(ot[db], poly); // add poly to the Ordering table - nextpri += sizeof(POLY_GT4); // increment nextpri address with size of a POLY_GT4 struct - // drawing mode primitive - dr_mode = (DR_MODE *)nextpri; // initialize drawing mode primitive - setDrawMode(dr_mode, 1, 0, getTPage(bousai.mode&0x3, 0, bousai.prect->x, bousai.prect->y), &tws); //set texture window - addPrim(ot[db], dr_mode); - nextpri += sizeof(DR_MODE); // increment nextpri address with size of a DR_MODE struct - - - FntPrint("Hello textured shaded !"); - FntFlush(-1); - display(); } return 0; diff --git a/hello_poly_inline/hello_poly_inline.c b/hello_poly_inline/hello_poly_inline.c index 84e772c..5fb264a 100644 --- a/hello_poly_inline/hello_poly_inline.c +++ b/hello_poly_inline/hello_poly_inline.c @@ -34,7 +34,7 @@ DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes -char primbuff[2][32768] = {0}; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes +char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] short db = 0; // index of which buffer is used, values 0, 1 @@ -53,12 +53,7 @@ void init(void) SetDefDrawEnv(&draw[0], 0, SCREENYRES, SCREENXRES, SCREENYRES); SetDefDrawEnv(&draw[1], 0, 0, SCREENXRES, SCREENYRES); // If PAL, use 320x256, hence 256 - 240 = 16 / 2 = 8 px vertical offset - if (VMODE) - { - SetVideoMode(MODE_PAL); - disp[0].screen.y += 8; - disp[1].screen.y += 8; - } + if (VMODE) SetVideoMode(MODE_PAL); SetDispMask(1); // Set background color setRGB0(&draw[0], 50, 50, 50); diff --git a/hello_poly_subdiv/hello_poly_subdiv.c b/hello_poly_subdiv/hello_poly_subdiv.c index 169b76c..f417316 100644 --- a/hello_poly_subdiv/hello_poly_subdiv.c +++ b/hello_poly_subdiv/hello_poly_subdiv.c @@ -34,7 +34,7 @@ DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes -char primbuff[2][32768] = {1}; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes +char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] short db = 0; // index of which buffer is used, values 0, 1 @@ -55,12 +55,7 @@ void init(void) SetDefDrawEnv(&draw[0], 0, SCREENYRES, SCREENXRES, SCREENYRES); SetDefDrawEnv(&draw[1], 0, 0, SCREENXRES, SCREENYRES); // If PAL, use 320x256, hence 256 - 240 = 16 / 2 = 8 px vertical offset - if (VMODE) - { - SetVideoMode(MODE_PAL); - disp[0].screen.y += 8; - disp[1].screen.y += 8; - } + if (VMODE) SetVideoMode(MODE_PAL); SetDispMask(1); // Set background color setRGB0(&draw[0], 50, 50, 50); diff --git a/hello_sio/hello_sio.c b/hello_sio/hello_sio.c index 816f8ae..75a1a9b 100644 --- a/hello_sio/hello_sio.c +++ b/hello_sio/hello_sio.c @@ -29,7 +29,7 @@ DISPENV disp[2]; DRAWENV draw[2]; u_long ot[2][OTLEN]; // Ordering table (contains addresses to primitives) -char primbuff[2][PRIMBUFFLEN] = {0}; // Primitive list // That's our prim buffer +char primbuff[2][PRIMBUFFLEN]; // Primitive list // That's our prim buffer char * nextpri = primbuff[0]; // Primitive counter short db = 0; // Current buffer counter // SIO diff --git a/hello_sprt/hello_sprt.c b/hello_sprt/hello_sprt.c index c9a8105..0778eac 100644 --- a/hello_sprt/hello_sprt.c +++ b/hello_sprt/hello_sprt.c @@ -7,229 +7,151 @@ #include #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 #define SCREENYRES 240 - #define CENTERX SCREENXRES/2 #define CENTERY SCREENYRES/2 - #define MARGINX 32 // margins for text display #define MARGINY 44 - #define FONTSIZE 8 * 3 // Text Field Height - #define OTLEN 8 // Ordering Table Length - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes - char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes - char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] - short db = 0; // index of which buffer is used, values 0, 1 - // Embed TIM files - // See https://github.com/ABelliqueux/nolibgs_hello_worlds#embedding-binary-data-in-a-ps-exe - // 16bpp TIM extern unsigned long _binary____TIM_TIM16_tim_start[]; extern unsigned long _binary____TIM_TIM16_tim_end[]; extern unsigned long _binary____TIM_TIM16_tim_length; - // 8bpp TIM extern unsigned long _binary____TIM_TIM8_tim_start[]; extern unsigned long _binary____TIM_TIM8_tim_end[]; extern unsigned long _binary____TIM_TIM8_TIM_length; - // 4bpp TIM extern unsigned long _binary____TIM_TIM4_tim_start[]; extern unsigned long _binary____TIM_TIM4_tim_end[]; extern unsigned long _binary____TIM_TIM4_tim_length; - - TIM_IMAGE TIM_16; TIM_IMAGE TIM_8; TIM_IMAGE TIM_4; - - 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 } } - void init(void) { ResetGraph(0); - 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 (VMODE) { SetVideoMode(MODE_PAL); disp[0].screen.y += 8; disp[1].screen.y += 8; } - SetDispMask(1); // Display on screen - setRGB0(&draw[0], 50, 50, 50); setRGB0(&draw[1], 50, 50, 50); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); - - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - DrawOTag(&ot[db][OTLEN - 1]); - db = !db; - nextpri = primbuff[db]; } - - int main(void) { - SPRT * sprt_16b; // Define 3 pointers to SPRT struct SPRT * sprt_8b; SPRT * sprt_4b; - DR_TPAGE * tpage_16b; // Define 3 pointers to DR_TPAGE struct. We need three because our images are on three DR_TPAGE * tpage_8b; // different texture pages. DR_TPAGE * tpage_4b; - init(); - LoadTexture(_binary____TIM_TIM16_tim_start, &TIM_16); // Load everything to vram LoadTexture(_binary____TIM_TIM8_tim_start, &TIM_8); LoadTexture(_binary____TIM_TIM4_tim_start, &TIM_4); - while (1) { ClearOTagR(ot[db], OTLEN); - // Loading a 16 bit TIM - sprt_16b = (SPRT *)nextpri; // Cast whats at nexpri as a SPRT named sprt_16b - setSprt(sprt_16b); // Initialize the SPRT struct setRGB0(sprt_16b, 128, 128, 128); // Set RGB color. 128,128,128 is neutral. You can color the image by adjusting these values setXY0(sprt_16b, 28, MARGINY); // Set sprite position setWH(sprt_16b, 64, 128 ); // Set sprite width and height - addPrim(ot[db], sprt_16b); // add the sprite primitive to the ordering table - nextpri += sizeof(SPRT); // increment nextpri so that it points just after sprt_16b in the primitive buffer - // Set Texture page for the 16bit tim : 768, 0 - No CLUT - // Note : You need to use setDrawTPage each time you want to use a texture that's on a different texture page - tpage_16b = (DR_TPAGE*)nextpri; - setDrawTPage(tpage_16b, 0, 1, // Set the Texture Page the texture we want resides on. getTPage(TIM_16.mode&0x3, 0, // Here we are using bitmasking to deduce the picture mode : &0x3 TIM_16.prect->x, TIM_16.prect->y)); // In binary, 3 is 11, so we only keep the first two bits // Values can be 00 (0), 01 (1), 10(2), respectively, 4bpp, 8bpp, 15bpp, 24bpp. See Fileformat47.pdf, p.180 // Similarly, we could use bitmasking to deduce if there is a CLUT by bitmasking the 4th bit : if(TIM_IMAGE.mode & 0x8) LoadImage... : - addPrim(ot[db], tpage_16b); // add the sprite primitive to the ordering table - nextpri += sizeof(DR_TPAGE); // Advance next primitive address - // Loading a 8 bit TIM - sprt_8b = (SPRT *)nextpri; - setSprt(sprt_8b); setRGB0(sprt_8b, 128, 128, 128); setXY0(sprt_8b, sprt_16b->x0 + sprt_16b->w + 32, MARGINY); setWH(sprt_8b, 64, 128 ); setClut(sprt_8b, TIM_8.crect->x, TIM_8.crect->y); // Only difference here is we set the CLUT to the position of the VRAM we loaded the palette earlier (see LoadTexture()) - addPrim(ot[db], sprt_8b); - nextpri += sizeof(SPRT); - // Set Texture page for the 8bit tim : 512, 256 - CLUT is at 0, 480 - tpage_8b = (DR_TPAGE*)nextpri; - setDrawTPage(tpage_8b, 0, 1, getTPage(TIM_8.mode&0x3, 0, TIM_8.prect->x, TIM_8.prect->y)); - addPrim(ot[db], tpage_8b); nextpri += sizeof(DR_TPAGE); - // Loading a 4 bit TIM - sprt_4b = (SPRT *)nextpri; - setSprt(sprt_4b); setRGB0(sprt_4b, 128, 128, 128); setXY0(sprt_4b, sprt_8b->x0 + sprt_8b->w + 32, MARGINY); setWH(sprt_4b, 64, 128 ); setClut(sprt_4b, TIM_4.crect->x, TIM_4.crect->y); - addPrim(ot[db], sprt_4b); - nextpri += sizeof(SPRT); - // Set Texture page for the 4bit tim : 512, 256 - CLUT is at 0, 480 - tpage_4b = (DR_TPAGE*)nextpri; - setDrawTPage(tpage_4b, 0, 1, getTPage(TIM_4.mode&0x3, 0, TIM_4.prect->x, TIM_4.prect->y)); - addPrim(ot[db], tpage_4b); - nextpri += sizeof(DR_TPAGE); - FntPrint("16 Bit! "); FntPrint("8 Bit! "); FntPrint("4 Bit!\n\n"); FntPrint("Check VRAM in emu to see the dif"); - FntFlush(-1); - display(); } return 0; diff --git a/hello_tile/hello_tile.c b/hello_tile/hello_tile.c index 29c5bce..f74c2f9 100644 --- a/hello_tile/hello_tile.c +++ b/hello_tile/hello_tile.c @@ -6,84 +6,57 @@ #include #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 #define SCREENYRES 240 - #define CENTERX SCREENXRES/2 #define CENTERY SCREENYRES/2 - #define MARGINX 0 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 7 // Text Field Height - #define OTLEN 8 // Ordering Table Length - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes - -char primbuff[2][32768] = {0};// double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes - +char primbuff[2][32768];// double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0] - short db = 0; // index of which buffer is used, values 0, 1 - void init(void) { ResetGraph(0); - 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 (VMODE) { SetVideoMode(MODE_PAL); disp[0].screen.y += 8; disp[1].screen.y += 8; } - SetDispMask(1); // Display on screen - setRGB0(&draw[0], 50, 50, 50); setRGB0(&draw[1], 50, 50, 50); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); // We're using a reverse OT, so we want to display the last item first. See PsyQ's LibRef47.pdf, p.277 DrawOTag(&ot[db][OTLEN - 1]); // Uncomment the following line to use a regular oredered OT. Uncomment l.100 accordingly //~ DrawOTag(ot[db]); - db = !db; - nextpri = primbuff[db]; } - - int main(void) { // These two tiles are added at the same OT index @@ -91,61 +64,44 @@ int main(void) TILE * pink_tile; // This one is added at a different OT index TILE * yellow_tile; - init(); - while (1) { // Initialize the reversed ordering table. This means the elements at index OTLEN - 1 is drawn first. ClearOTagR(ot[db], OTLEN); // Use regular order OT, uncomment l.77 accordingly //~ ClearOTag(ot[db], OTLEN); - // yellow_tile is before pink and blue tile in the code, // and it displays behind because it is added to a different ot index (od[db] + OTLEN - 1) // Using a Regular or Reverse OT will have an effect on drawing order. (See lines 77 and 100) - yellow_tile = (TILE * ) nextpri; // yellow_tile is a pointer to primbuf content at adress nextpri, that's cast (type converted) to a TILE struc. - setTile(yellow_tile); // initialize the TILE structure ( fill the length and tag(?) value ) setXY0(yellow_tile, CENTERX - 32 , CENTERY - 48); // Set X,Y setWH(yellow_tile, 128, 40); // Set Width, Height setRGB0(yellow_tile, 255, 255, 0); // Set color addPrim(ot[db] + OTLEN - 1, yellow_tile); // Add primitive to ordering table - nextpri += sizeof(TILE); - // blue_tile added at od[db] + OTLEN - 2 - blue_tile = (TILE * ) nextpri; // blue_tile is a pointer to primbuf content at adress nextpri, that's cast (type converted) to a blue_tile struc. - setTile(blue_tile); // initialize the blue_tile structure ( fill the length and tag(?) value ) setXY0(blue_tile, CENTERX - 16, CENTERY - 32); // Set X,Y setWH(blue_tile, 32, 64); // Set Width, Height setRGB0(blue_tile, 60, 180, 255); // Set color addPrim(ot[db] + OTLEN - 2, blue_tile); // Add primitive to ordering table - nextpri += sizeof(TILE); // Increment the adress nextpri points to by the size of TILE struct - // pink_tile is after blue_tile in the code, // so it is drawn before, thus under blue_tile. // However, it is added at the same ot index (od[db] + OTLEN - 2) // so using a Regular or Reverse OT won't have an effect on drawing order. - pink_tile = (TILE * ) nextpri; // pink_tile is a pointer to primbuf content at adress nextpri, that's cast (type converted) to a TILE struc. - setTile(pink_tile); // initialize the TILE structure ( fill the length and tag(?) value ) setXY0(pink_tile, CENTERX, CENTERY - 64); // Set X,Y setWH(pink_tile, 64, 64); // Set Width, Height setRGB0(pink_tile, 255, 32, 255); // Set color addPrim(ot[db] + OTLEN - 2, pink_tile); // Add primitive to ordering table - nextpri += sizeof(TILE); - FntPrint("Hello tile !"); - FntFlush(-1); - display(); } return 0; diff --git a/hello_vag/hello_vag.c b/hello_vag/hello_vag.c index fe0b5ee..a2dcc9f 100644 --- a/hello_vag/hello_vag.c +++ b/hello_vag/hello_vag.c @@ -20,44 +20,29 @@ // libover47.pdf, p.271 // libref47.pdf, p.980 // URLS : http://psx.arthus.net/code/VAG/ - #include #include - #include #include #include - - // Sound system #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 #define SCREENYRES 240 - #define CENTERX SCREENXRES/2 #define CENTERY SCREENYRES/2 - #define MARGINX 0 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 7 // Text Field Height - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - short db = 0; // index of which buffer is used, values 0, 1 - // Sound stuff - #define MALLOC_MAX 3 // Max number of time we can call SpuMalloc - //~ // convert Little endian to Big endian #define SWAP_ENDIAN32(x) (((x)>>24) | (((x)>>8) & 0xFF00) | (((x)<<8) & 0x00FF0000) | ((x)<<24)) - typedef struct VAGheader{ // All the values in this header must be big endian char id[4]; // VAGp 4 bytes -> 1 char * 4 unsigned int version; // 4 bytes @@ -68,111 +53,74 @@ typedef struct VAGheader{ // All the values in this header must be big end char name[16]; // 16 bytes -> 1 char * 16 // Waveform data after that }VAGhdr; - SpuCommonAttr commonAttributes; // structure for changing common voice attributes SpuVoiceAttr voiceAttributes ; // structure for changing individual voice attributes - u_long vag_spu_address; // address allocated in memory for first sound file - // DEBUG : these allow printing values for debugging - u_long spu_start_address; u_long get_start_addr; u_long transSize; - // Memory management table ; allow MALLOC_MAX calls to SpuMalloc() - ibref47.pdf p.1044 char spu_malloc_rec[SPU_MALLOC_RECSIZ * (2 + MALLOC_MAX+1)]; - // VAG files - // We're using GrumpyCoder's Nugget wrapper to compile the code with a modern GCC : https://github.com/grumpycoders/pcsx-redux/tree/main/src/mips/psyq // To include binary files in the exe, add your VAG files to the SRCS variable in Makefile // and in common.mk, add this rule to include *.vag files : // //~ %.o: %.vag //~ $(PREFIX)-objcopy -I binary --set-section-alignment .data=4 --rename-section .data=.rodata,alloc,load,readonly,data,contents -O elf32-tradlittlemips -B mips $< $@ - - // hello_poly.vag - 44100 Khz extern unsigned char _binary____VAG_hello_poly_vag_start[]; // filename must begin with _binary_ followed by the full path, with . and / replaced, and then suffixed with _ and end with _start[]; or end[]; extern unsigned char _binary____VAG_hello_poly_vag_end[]; // Going up one directory level is 4 '_' : ____ as ./ is replaced by __ // https://discord.com/channels/642647820683444236/663664210525290507/780866265077383189 - void initGraph(void) { ResetGraph(0); - 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 (VMODE) { SetVideoMode(MODE_PAL); disp[0].screen.y += 8; disp[1].screen.y += 8; } - SetDispMask(1); // Display on screen - setRGB0(&draw[0], 50, 50, 50); setRGB0(&draw[1], 50, 50, 50); - draw[0].isbg = 1; draw[1].isbg = 1; - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - FntLoad(960, 0); FntOpen(8, 60, 304, 200, 0, 500 ); - } - void display(void) { DrawSync(0); VSync(0); - PutDispEnv(&disp[db]); PutDrawEnv(&draw[db]); - db = !db; - } - - // Audio initialisation & functions - void initSnd(void){ - SpuInitMalloc(MALLOC_MAX, spu_malloc_rec); // Maximum number of blocks, mem. management table address. - commonAttributes.mask = (SPU_COMMON_MVOLL | SPU_COMMON_MVOLR); // Mask which attributes to set commonAttributes.mvol.left = 0x3fff; // Master volume left commonAttributes.mvol.right = 0x3fff; // see libref47.pdf, p.1058 - SpuSetCommonAttr(&commonAttributes); // set attributes - SpuSetIRQ(SPU_OFF); } - u_long sendVAGtoRAM(unsigned int VAG_data_size, unsigned char *VAG_data){ u_long size; - SpuSetTransferMode(SpuTransByDMA); // DMA transfer; can do other processing during transfer - size = SpuWrite (VAG_data + sizeof(VAGhdr), VAG_data_size); // transfer VAG_data_size bytes from VAG_data address to sound buffer - SpuIsTransferCompleted (SPU_TRANSFER_WAIT); // Checks whether transfer is completed and waits for completion - return size; } - void setVoiceAttr(unsigned int pitch, long channel, unsigned long soundAddr ){ - voiceAttributes.mask= //~ Attributes (bit string, 1 bit per attribute) ( SPU_VOICE_VOLL | @@ -188,74 +136,49 @@ void setVoiceAttr(unsigned int pitch, long channel, unsigned long soundAddr ){ SPU_VOICE_ADSR_RR | SPU_VOICE_ADSR_SL ); - voiceAttributes.voice = channel; //~ Voice (low 24 bits are a bit string, 1 bit per voice ) - voiceAttributes.volume.left = 0x1000; //~ Volume voiceAttributes.volume.right = 0x1000; //~ Volume - voiceAttributes.pitch = pitch; //~ Interval (set pitch) voiceAttributes.addr = soundAddr; //~ Waveform data start address - voiceAttributes.a_mode = SPU_VOICE_LINEARIncN; //~ Attack rate mode = Linear Increase - see libref47.pdf p.1091 voiceAttributes.s_mode = SPU_VOICE_LINEARIncN; //~ Sustain rate mode = Linear Increase voiceAttributes.r_mode = SPU_VOICE_LINEARDecN; //~ Release rate mode = Linear Decrease - voiceAttributes.ar = 0x0; //~ Attack rate voiceAttributes.dr = 0x0; //~ Decay rate voiceAttributes.rr = 0x0; //~ Release rate voiceAttributes.sr = 0x0; //~ Sustain rate voiceAttributes.sl = 0xf; //~ Sustain level - SpuSetVoiceAttr(&voiceAttributes); // set attributes - } - void playSFX(void){ SpuSetKey(SpuOn,SPU_0CH); // Set several channels by ORing each channel bit ; ex : SpuSetKey(SpuOn,SPU_0CH | SPU_3CH | SPU_8CH); channels 0, 3, 8 are on. } - int main(void) { short counter = 0; - const VAGhdr * VAGfileHeader = (VAGhdr *) _binary____VAG_hello_poly_vag_start; // get header of VAG file - // From libover47.pdf : // The sampling frequency of the original audio file can be used to determine the pitch // at which to play the VAG. pitch = (sampling frequency << 12)/44100L // Ex: 44.1kHz=0x1000 22.05kHz=0x800 etc - unsigned int pitch = (SWAP_ENDIAN32(VAGfileHeader->samplingFrequency) << 12) / 44100L; - SpuInit(); // Initialize SPU. Called only once. - initSnd(); - //~ // First VAG - vag_spu_address = SpuMalloc(SWAP_ENDIAN32(VAGfileHeader->dataSize)); // Allocate an area of dataSize bytes in the sound buffer. - spu_start_address = SpuSetTransferStartAddr(vag_spu_address); // Sets a starting address in the sound buffer - get_start_addr = SpuGetTransferStartAddr(); // SpuGetTransferStartAddr() returns current sound buffer transfer start address. - transSize = sendVAGtoRAM(SWAP_ENDIAN32(VAGfileHeader->dataSize), _binary____VAG_hello_poly_vag_start); - // set VAG to channel - setVoiceAttr(pitch, SPU_0CH, vag_spu_address); - initGraph(); - while (1) { - if(!counter){ playSFX(); counter = 180; } - FntPrint("\nPitch : %08x-%dKhz", pitch, (SWAP_ENDIAN32(VAGfileHeader->samplingFrequency)) ); FntPrint("\nSet Start addr : %08x", vag_spu_address); FntPrint("\nReturn start addr : %08x", spu_start_address); @@ -263,13 +186,9 @@ int main(void) FntPrint("\nSend size : %08x", SWAP_ENDIAN32(VAGfileHeader->dataSize)); FntPrint("\nReturn size : %08x\n", transSize); FntPrint("\nCounter : %d\n", counter); - FntFlush(-1); - counter --; - display(); - } return 0; } diff --git a/hello_world/hello_world.c b/hello_world/hello_world.c index 24c2a73..1b17143 100644 --- a/hello_world/hello_world.c +++ b/hello_world/hello_world.c @@ -1,87 +1,60 @@ // Demonstrate DISP/DRAW env, font setup, and display a text. // Schnappy 2020 // Based on Lameguy64 tutorial : http://lameguy64.net/svn/pstutorials/chapter1/1-display.html - #include #include #include #include #include - #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL - #define SCREENXRES 320 // Screen width #define SCREENYRES 240 // Screen height - #define CENTERX SCREENXRES/2 // Center of screen on x #define CENTERY SCREENYRES/2 // Center of screen on y - #define MARGINX 0 // margins for text display #define MARGINY 32 - #define FONTSIZE 8 * 7 // Text Field Height - DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DRAWENV draw[2]; - short db = 0; // index of which buffer is used, values 0, 1 - void init(void) { ResetGraph(0); // Initialize drawing engine with a complete reset (0) - SetDefDispEnv(&disp[0], 0, 0 , SCREENXRES, SCREENYRES); // Set display area for both &disp[0] and &disp[1] SetDefDispEnv(&disp[1], 0, SCREENYRES, SCREENXRES, SCREENYRES); // &disp[0] is on top of &disp[1] - SetDefDrawEnv(&draw[0], 0, SCREENYRES, SCREENXRES, SCREENYRES); // Set draw for both &draw[0] and &draw[1] SetDefDrawEnv(&draw[1], 0, 0 , SCREENXRES, SCREENYRES); // &draw[0] is below &draw[1] - if (VMODE) // PAL { SetVideoMode(MODE_PAL); disp[0].screen.y += 8; // add offset : 240 + 8 + 8 = 256 disp[1].screen.y += 8; } - SetDispMask(1); // Display on screen - setRGB0(&draw[0], 50, 50, 50); // set color for first draw area setRGB0(&draw[1], 50, 50, 50); // set color for second draw area - draw[0].isbg = 1; // set mask for draw areas. 1 means repainting the area with the RGB color each frame draw[1].isbg = 1; - PutDispEnv(&disp[db]); // set the disp and draw environnments PutDrawEnv(&draw[db]); - FntLoad(960, 0); // Load font to vram at 960,0(+128) FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); // FntOpen(x, y, width, height, black_bg, max. nbr. chars - } - void display(void) { DrawSync(0); // Wait for all drawing to terminate VSync(0); // Wait for the next vertical blank - PutDispEnv(&disp[db]); // set alternate disp and draw environnments PutDrawEnv(&draw[db]); - db = !db; // flip db value (0 or 1) } - - int main(void) { - init(); // execute init() - while (1) // infinite loop { FntPrint("Hello world !"); // Send string to print stream - FntFlush(-1); // Draw printe stream - display(); // Execute display() } return 0;