Skip to content

Commit cd5a588

Browse files
committed
Improvements
- Added CD rotation animation in Audio Test using GTE - Added angle conversion table for fixed point - Updated tiles texture
1 parent cac4fbe commit cd5a588

File tree

2 files changed

+161
-30
lines changed

2 files changed

+161
-30
lines changed

main.c

Lines changed: 161 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <stdlib.h>
3838
#include <psxcd.h>
3939
#include <hwregs_c.h>
40+
#include <inline_c.h>
4041

4142
#include "stream.h"
4243

@@ -123,6 +124,9 @@ typedef struct {
123124
#define START_VEL 1
124125
#define START_TRACK 0
125126

127+
const int CENTERX = SCREEN_XRES >> 1;
128+
const int CENTERY = SCREEN_YRES >> 1;
129+
126130
enum menuChoices
127131
{
128132
STRESS_TEST = 0,
@@ -184,6 +188,11 @@ static uint16_t lastButtons = 0xffff;
184188
static int curVel = START_VEL;
185189
static int vel[4] = {1, 3, 5, 7};
186190

191+
static int anglesTable[360]; //da gradi a fixed point
192+
193+
static int discAngle = 0;
194+
static uint32_t frameCounter = 0;
195+
187196
static char menuChoicesText[NUM_CHOICES][64] =
188197
{
189198
{"STRESS TEST"},
@@ -192,6 +201,17 @@ static char menuChoicesText[NUM_CHOICES][64] =
192201
{"BACK"}
193202
};
194203

204+
void ComputeAngles()
205+
{
206+
//0-360 -> 0-4095
207+
float degree = 1.0f / 360.0f * 4096.0f;
208+
int i;
209+
float d = 0;
210+
211+
for(i = 0; i < 360; i++, d += degree)
212+
anglesTable[i] = d;
213+
}
214+
195215
// This isn't actually required for this example, however it is necessary if the
196216
// stream buffers are going to be allocated into a region of SPU RAM that was
197217
// previously used (to make sure the IRQ is not going to be triggered by any
@@ -408,45 +428,129 @@ void update_position(int *x, int *y, int *dx, int *dy, int w, int h)
408428
*y += *dy;
409429
}
410430

411-
void draw_rectangle(RenderContext* ctx, void** prim, int texture, int ux, int uy, int x, int y, int z, int w, int h, int r, int g, int b)
431+
void DrawRotatedTexturedRectangle(RenderContext* ctx, void** prim, int rt, int ux, int uy, int x0, int y0, int x1, int y1, int z, int r, int g, int b)
412432
{
413-
if(!texture)
414-
{
415-
TILE* tile = new_primitive(ctx, z, sizeof(TILE));
433+
MATRIX mtx;
434+
SVECTOR rot = { 0 };
435+
VECTOR trasl = { 0 };
436+
SVECTOR pos[4];
437+
VECTOR outPos[4];
416438

417-
setTile(tile);
418-
setXY0 (tile, x, y);
419-
setWH (tile, w, h);
420-
setRGB0(tile, r, g, b);
439+
POLY_FT4* poly = (POLY_FT4*)new_primitive(ctx, z, sizeof(POLY_FT4));
421440

422-
*prim = tile;
423-
}
424-
else
425-
{
426-
POLY_FT4* poly = (POLY_FT4*)new_primitive(ctx, z, sizeof(POLY_FT4));
441+
int xMid = (x0 + x1) >> 1;
442+
int yMid = (y0 + y1) >> 1;
443+
444+
int tx = xMid - CENTERX;
445+
int ty = yMid - CENTERY;
427446

428-
setPolyFT4(poly);
447+
pos[0].vx = x0 - xMid; pos[0].vy = y0 - yMid; pos[0].vz = 0; pos[0].pad = 0;
448+
pos[1].vx = x1 - xMid; pos[1].vy = y0 - yMid; pos[1].vz = 0; pos[1].pad = 0;
449+
pos[2].vx = x0 - xMid; pos[2].vy = y1 - yMid; pos[2].vz = 0; pos[2].pad = 0;
450+
pos[3].vx = x1 - xMid; pos[3].vy = y1 - yMid; pos[3].vz = 0; pos[3].pad = 0;
429451

430-
//setXY4 ordine dei vertici
431-
//1---2
432-
//| |
433-
//| |
434-
//3---4
452+
rot.vx = 0;
453+
rot.vy = 0;
454+
rot.vz = rt;
435455

436-
setXY4(poly, x, y,
437-
x + w, y,
438-
x, y + h,
439-
x + w, y + h);
440-
setRGB0(poly, r, g, b);
441-
poly->tpage = getTPage(timImage.mode, 0, timImage.prect->x, timImage.prect->y);
456+
setPolyFT4(poly);
442457

443-
// Set CLUT
444-
setClut(poly, timImage.crect->x, timImage.crect->y);
458+
RotMatrix( &rot, &mtx );
459+
TransMatrix( &mtx, &trasl );
460+
461+
gte_SetRotMatrix( &mtx );
462+
gte_SetTransMatrix( &mtx );
463+
464+
// Load the 3D coordinate of the sprite to GTE
465+
gte_ldv3(&pos[0], &pos[1], &pos[2]);
445466

446-
// Set texture coordinates
447-
setUVWH(poly, ux, uy, 32, 32);
467+
// Rotation, Translation and Perspective Triple
468+
gte_rtpt();
469+
470+
//gte_stsxy0 mette sia x che y essendo allineati in memoria
471+
//vuole puntatore 32 bit ed infatti x e y sono a 16 bit l'uno
472+
gte_stsxy0( &poly->x0 );
473+
gte_stsxy1( &poly->x1 );
474+
gte_stsxy2( &poly->x2 );
475+
476+
gte_ldv0( &pos[3]);
477+
gte_rtps();
478+
gte_stsxy( &poly->x3 );
479+
480+
poly->x0 += tx;
481+
poly->y0 += ty;
482+
483+
poly->x1 += tx;
484+
poly->y1 += ty;
485+
486+
poly->x2 += tx;
487+
poly->y2 += ty;
488+
489+
poly->x3 += tx;
490+
poly->y3 += ty;
491+
492+
setRGB0(poly, r, g, b);
493+
494+
poly->tpage = getTPage(timImage.mode, 0, timImage.prect->x, timImage.prect->y);
495+
496+
// Set CLUT
497+
setClut(poly, timImage.crect->x, timImage.crect->y);
498+
499+
// Set texture coordinates
500+
setUVWH(poly, ux, uy, 31, 31);
501+
502+
*prim = poly;
503+
}
504+
505+
void DrawTexturedRectangle(RenderContext* ctx, void** prim, int ux, int uy, int x, int y, int z, int w, int h, int r, int g, int b)
506+
{
507+
POLY_FT4* poly = (POLY_FT4*)new_primitive(ctx, z, sizeof(POLY_FT4));
508+
509+
setPolyFT4(poly);
510+
511+
//setXY4 ordine dei vertici
512+
//1---2
513+
//| |
514+
//| |
515+
//3---4
516+
517+
setXY4(poly, x, y,
518+
x + w, y,
519+
x, y + h,
520+
x + w, y + h);
521+
setRGB0(poly, r, g, b);
522+
poly->tpage = getTPage(timImage.mode, 0, timImage.prect->x, timImage.prect->y);
523+
524+
// Set CLUT
525+
setClut(poly, timImage.crect->x, timImage.crect->y);
526+
527+
// Set texture coordinates
528+
setUVWH(poly, ux, uy, 32, 32);
529+
530+
*prim = poly;
531+
}
448532

449-
*prim = poly;
533+
void DrawSimpleRectangle(RenderContext* ctx, void** prim, int x, int y, int z, int w, int h, int r, int g, int b)
534+
{
535+
TILE* tile = new_primitive(ctx, z, sizeof(TILE));
536+
537+
setTile(tile);
538+
setXY0 (tile, x, y);
539+
setWH (tile, w, h);
540+
setRGB0(tile, r, g, b);
541+
542+
*prim = tile;
543+
}
544+
545+
void draw_rectangle(RenderContext* ctx, void** prim, int texture, int ux, int uy, int x, int y, int z, int w, int h, int r, int g, int b)
546+
{
547+
if(!texture)
548+
{
549+
DrawSimpleRectangle(ctx, prim, x, y, z, w, h, r, g, b);
550+
}
551+
else
552+
{
553+
DrawTexturedRectangle(ctx, prim, ux, uy, x, y, z, w, h, r, g, b);
450554
}
451555
}
452556

@@ -577,6 +681,24 @@ void DrawAudioTest(RenderContext* ctx)
577681
drawTextList(ctx, xPos, &yPos, 0, TRACK_LIST_DY * 2, "[TRIANGLE] CHANGE TRACK");
578682

579683
drawTextList(ctx, xPos, &yPos, 0, TRACK_LIST_DY, "PAUSE AND RESUME IF IT DOESN'T START!");
684+
685+
int x0 = SCREEN_XRES - 32 - 8;
686+
int y0 = 16;
687+
int x1 = x0 + 16;
688+
int y1 = y0 + 16;
689+
690+
//ogni 500 ms gira il disco
691+
if(!pausedTracks[currentTrackIndex] && frameCounter % 30 == 0)
692+
{
693+
int angle = discAngle + anglesTable[90]; //90 gradi
694+
695+
if(angle >= anglesTable[359])
696+
angle = 0;
697+
698+
discAngle = angle;
699+
}
700+
701+
DrawRotatedTexturedRectangle(ctx, &tiles[0], discAngle, 64, 0, x0, y0, x1, y1, 0, 128, 128, 128);
580702
}
581703

582704
void DrawMenu(RenderContext* ctx)
@@ -960,9 +1082,16 @@ int main(int argc, const char **argv) {
9601082
RenderContext ctx;
9611083
setup_context(&ctx, SCREEN_XRES, SCREEN_YRES, 63, 0, 127);
9621084

1085+
ComputeAngles();
1086+
9631087
//inizializzo CD stream
9641088
CdInit();
9651089

1090+
//inizializzo coprocessore GEOMETRY TRANSFORMATION ENGINE
1091+
InitGeom();
1092+
gte_SetGeomOffset( CENTERX, CENTERY );
1093+
gte_SetGeomScreen( CENTERX );
1094+
9661095
LoadTextures();
9671096
LoadAudioTracks(&ctx);
9681097

@@ -980,6 +1109,8 @@ int main(int argc, const char **argv) {
9801109
DrawCurrentMode(&ctx);
9811110

9821111
flip_buffers(&ctx);
1112+
1113+
frameCounter++;
9831114
}
9841115

9851116
return 0;

tilesc.tim

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)