564 lines
15 KiB
C++
564 lines
15 KiB
C++
#include "parameters.h"
|
|
|
|
#include <azur/azur.h>
|
|
#include <azur/gint/render.h>
|
|
#include <gint/drivers/r61524.h>
|
|
#include <gint/rtc.h>
|
|
|
|
#include <gint/clock.h>
|
|
#include <gint/kmalloc.h>
|
|
|
|
#include <gint/usb-ff-bulk.h>
|
|
#include <gint/usb.h>
|
|
#include <libprof.h>
|
|
|
|
#include <cstdint>
|
|
#include <fxlibc/printf.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <num/num.h>
|
|
|
|
#include "extrakeyboard.h"
|
|
#include "pinball_entities.h"
|
|
#include "stdint-gcc.h"
|
|
#include "tables.h"
|
|
#include "utilities.h"
|
|
#include "vector2D.h"
|
|
|
|
#include <vector>
|
|
|
|
#include <math.h>
|
|
#include "tables.h"
|
|
|
|
bool screenshot = false;
|
|
bool record = false;
|
|
bool textoutput = false;
|
|
bool exitToOS = false;
|
|
|
|
uint8_t texttodraw = 1;
|
|
|
|
#define SCALE_PIXEL 1
|
|
#define X_RESOL (DWIDTH / SCALE_PIXEL)
|
|
#define Y_RESOL (DHEIGHT / SCALE_PIXEL)
|
|
|
|
float elapsedTime = 0.0f;
|
|
uint32_t time_update = 0, time_render = 0;
|
|
prof_t perf_update, perf_render;
|
|
|
|
static kmalloc_arena_t extended_ram = {0};
|
|
static kmalloc_arena_t *_uram;
|
|
kmalloc_gint_stats_t *_uram_stats;
|
|
kmalloc_gint_stats_t *extram_stats;
|
|
|
|
KeyboardExtra MyKeyboard;
|
|
|
|
Scene MyPinball;
|
|
|
|
libnum::num32 flipperHeight;
|
|
libnum::num32 cScale;
|
|
libnum::num32 simWidth;
|
|
libnum::num32 simHeight;
|
|
|
|
/* return the scaled x component of a vector */
|
|
uint16_t CX(Vector2D pos) { return (int) (pos.x * cScale); }
|
|
|
|
/* return the scaled y component of a vector */
|
|
uint16_t CY(Vector2D pos) { return (int) (libnum::num32(azrp_height) - pos.y * cScale); }
|
|
|
|
/* create the pinball board */
|
|
void SetupScene( int which_table ) {
|
|
|
|
if (which_table==0) Setup_Table_0();
|
|
else if (which_table==1) Setup_Table_1();
|
|
else if (which_table==2) Setup_Table_2();
|
|
else Setup_Table_0();
|
|
}
|
|
|
|
void HandleBallBallCollision( Ball *ball1, Ball *ball2 )
|
|
{
|
|
libnum::num32 restitution = MIN( ball1->restitution, ball2->restitution );
|
|
Vector2D dir = ball2->pos - ball1->pos;
|
|
libnum::num32 d = dir.Length();
|
|
if ( d==libnum::num32(0) || d > (ball1->radius + ball2->radius) ) return;
|
|
|
|
dir.Normalise();
|
|
|
|
libnum::num32 corr = (ball1->radius + ball2->radius-d) / libnum::num32(2);
|
|
ball1->pos.Add(dir, -corr);
|
|
ball2->pos.Add(dir, corr);
|
|
|
|
libnum::num32 v1 = ball1->vel.Dot(dir);
|
|
libnum::num32 v2 = ball2->vel.Dot(dir);
|
|
|
|
libnum::num32 m1 = ball1->mass;
|
|
libnum::num32 m2 = ball2->mass;
|
|
|
|
libnum::num32 newV1 = (m1*v1 + m2*v2 - m2*(v1-v2)*restitution / (m1+m2) );
|
|
libnum::num32 newV2 = (m1*v1 + m2*v2 - m1*(v2-v1)*restitution / (m1+m2) );
|
|
|
|
ball1->vel.Add(dir, newV1-v1 );
|
|
ball2->vel.Add(dir, newV2-v2 );
|
|
}
|
|
|
|
void HandleBallObstacleCollision( Ball *ball, Obstacle obstacle )
|
|
{
|
|
Vector2D dir = ball->pos - obstacle.pos;
|
|
libnum::num32 d = dir.Length();
|
|
if ( d==libnum::num32(0) || d > (ball->radius + obstacle.radius) ) return;
|
|
|
|
dir.Normalise();
|
|
|
|
libnum::num32 corr = ball->radius + obstacle.radius-d;
|
|
ball->pos.Add(dir, corr);
|
|
|
|
libnum::num32 v = ball->vel.Dot(dir);
|
|
ball->vel.Add(dir, obstacle.pushVel-v );
|
|
|
|
MyPinball.score++;
|
|
}
|
|
|
|
void HandleBallIslandCollision( Ball *ball, std::vector<Vector2D> island, libnum::num32 rad )
|
|
{
|
|
int mod = island.size();
|
|
for(int i=0; i<mod; i++ )
|
|
{
|
|
|
|
Vector2D closest = ClosestPointOnSegment( ball->pos, island[i], island[(i+1)%mod] );
|
|
|
|
Vector2D dir;
|
|
dir.SubtractVectors( ball->pos, closest );
|
|
libnum::num32 d = dir.Length();
|
|
if ( d==libnum::num32(0) || d > (ball->radius + rad) ) return;
|
|
|
|
dir.Normalise();
|
|
|
|
libnum::num32 corr = ball->radius + rad - d;
|
|
ball->pos.Add(dir, corr);
|
|
|
|
/* Update velocity */
|
|
|
|
Vector2D radius = closest.Clone();
|
|
radius.Add( dir, rad );
|
|
radius.Subtract( island[i], libnum::num32(1) );
|
|
|
|
|
|
libnum::num32 v = ball->vel.Dot( dir );
|
|
libnum::num32 newV = ABS(v) * ball->restitution;
|
|
ball->vel.Add( dir, newV - v );
|
|
}
|
|
}
|
|
|
|
|
|
void HandleBallFlipperCollision( Ball *ball, Flipper flipper )
|
|
{
|
|
Vector2D closest = ClosestPointOnSegment( ball->pos, flipper.pos, flipper.getTip() );
|
|
|
|
Vector2D dir;
|
|
dir.SubtractVectors( ball->pos, closest );
|
|
libnum::num32 d = dir.Length();
|
|
if ( d==libnum::num32(0) || d > (ball->radius + flipper.radius) ) return;
|
|
|
|
dir.Normalise();
|
|
|
|
libnum::num32 corr = ball->radius + flipper.radius-d;
|
|
ball->pos.Add(dir, corr);
|
|
|
|
/* Update velocity */
|
|
|
|
Vector2D radius = closest.Clone();
|
|
radius.Add( dir, flipper.radius );
|
|
radius.Subtract( flipper.pos, libnum::num32(1) );
|
|
|
|
Vector2D surfaceVel = radius.PerpCW();
|
|
surfaceVel.Scale( flipper.currentAngularVelocity );
|
|
|
|
libnum::num32 v = ball->vel.Dot( dir );
|
|
libnum::num32 newV = surfaceVel.Dot( dir );
|
|
|
|
ball->vel.Add( dir, newV - v );
|
|
}
|
|
|
|
|
|
void HandleBallBorderCollision( Ball *ball, std::vector<Vector2D> border )
|
|
{
|
|
int mod = border.size();
|
|
|
|
if (mod<3) return;
|
|
|
|
/* Find closest segment */
|
|
|
|
Vector2D d, closest, ab, normal;
|
|
|
|
libnum::num32 minDist = libnum::num32(0);
|
|
|
|
for( int i=0; i<mod; i++)
|
|
{
|
|
Vector2D a = border[i];
|
|
Vector2D b = border[(i+1) % mod];
|
|
Vector2D c = ClosestPointOnSegment( ball->pos, a, b );
|
|
d.SubtractVectors(ball->pos, c);
|
|
libnum::num32 dist = d.Length();
|
|
|
|
if (i==0 || dist <minDist)
|
|
{
|
|
minDist = dist;
|
|
closest.Set( c );
|
|
ab.SubtractVectors(b, a);
|
|
normal = ab.PerpCW();
|
|
}
|
|
}
|
|
|
|
/* Push out */
|
|
|
|
d.SubtractVectors( ball->pos, closest );
|
|
libnum::num32 dist = d.Length();
|
|
|
|
if (dist == libnum::num32(0))
|
|
{
|
|
d.Set( normal );
|
|
dist = normal.Length();
|
|
}
|
|
d.Normalise();
|
|
|
|
if( d.Dot( normal ) >= libnum::num32(0) )
|
|
{
|
|
if ( dist > ball->radius ) return;
|
|
|
|
ball->pos.Add( d, ball->radius - dist );
|
|
}
|
|
else ball->pos.Add( d, -(ball->radius + dist) );
|
|
|
|
|
|
/* Update velocity */
|
|
libnum::num32 v = ball->vel.Dot( d );
|
|
libnum::num32 newV = ABS(v) * ball->restitution;
|
|
|
|
ball->vel.Add( d, newV - v );
|
|
}
|
|
|
|
|
|
|
|
static void hook_prefrag(int id, void *fragment, int size) {
|
|
if (!screenshot && !record)
|
|
return;
|
|
|
|
/* Screenshot takes precedence */
|
|
char const *type = screenshot ? "image" : "video";
|
|
|
|
int pipe = usb_ff_bulk_output();
|
|
|
|
if (id == 0) {
|
|
usb_fxlink_header_t h;
|
|
usb_fxlink_image_t sh;
|
|
int size = azrp_width * azrp_height * 2;
|
|
|
|
usb_fxlink_fill_header(&h, "fxlink", type, size + sizeof sh);
|
|
sh.width = htole32(azrp_width);
|
|
sh.height = htole32(azrp_height);
|
|
sh.pixel_format = htole32(USB_FXLINK_IMAGE_RGB565);
|
|
|
|
usb_write_sync(pipe, &h, sizeof h, false);
|
|
usb_write_sync(pipe, &sh, sizeof sh, false);
|
|
}
|
|
|
|
usb_write_sync(pipe, fragment, size, false);
|
|
|
|
if (id == azrp_frag_count - 1) {
|
|
usb_commit_sync(pipe);
|
|
screenshot = false;
|
|
}
|
|
}
|
|
|
|
static void update(float dt)
|
|
{
|
|
MyPinball.dt = libnum::num32( dt );
|
|
|
|
|
|
for( int i=0; i<MyPinball.flippers.size(); i++ ) MyPinball.flippers[i].Simulate( MyPinball.dt );
|
|
|
|
for( int i=0; i<MyPinball.balls.size(); i++ )
|
|
{
|
|
MyPinball.balls[i].Simulate( MyPinball.dt, MyPinball.gravity );
|
|
|
|
for( int j=0; j<MyPinball.balls.size(); j++ ) HandleBallBallCollision( &MyPinball.balls[i], &MyPinball.balls[j] );
|
|
|
|
for( int j=0; j<MyPinball.obstacles.size(); j++ ) HandleBallObstacleCollision( &MyPinball.balls[i], MyPinball.obstacles[j] );
|
|
|
|
for( int j=0; j<MyPinball.islands.size(); j++ )
|
|
{
|
|
HandleBallIslandCollision( &MyPinball.balls[i], MyPinball.islands[j], libnum::num(0.01) );
|
|
}
|
|
|
|
for( int j=0; j<MyPinball.flippers.size(); j++ ) HandleBallFlipperCollision( &MyPinball.balls[i], MyPinball.flippers[j] );
|
|
|
|
HandleBallBorderCollision( &MyPinball.balls[i], MyPinball.borders );
|
|
}
|
|
}
|
|
|
|
static void render(void) {
|
|
|
|
azrp_clear(C_BLACK);
|
|
|
|
int mod = MyPinball.borders.size();
|
|
for( int i=0; i<MyPinball.borders.size(); i++ ) azrp_line( CX(MyPinball.borders[i]), CY(MyPinball.borders[i]), CX(MyPinball.borders[(i+1) % mod]), CY(MyPinball.borders[(i+1) % mod]), C_WHITE );
|
|
|
|
for( int i=0; i<MyPinball.obstacles.size(); i++ ) azrp_filledcircle( CX(MyPinball.obstacles[i].pos), CY(MyPinball.obstacles[i].pos), (int) (MyPinball.obstacles[i].radius*cScale), MyPinball.obstacles[i].color );
|
|
|
|
for( int i=0; i<MyPinball.islands.size(); i++ )
|
|
{
|
|
int temp = MyPinball.islands[i].size();
|
|
for( int j=0; j<MyPinball.islands[i].size(); j++ ) azrp_line( CX(MyPinball.islands[i][j]), CY(MyPinball.islands[i][j]), CX(MyPinball.islands[i][(j+1)%temp]), CY(MyPinball.islands[i][(j+1)%temp]), C_WHITE );
|
|
}
|
|
|
|
|
|
for( int i=0; i<MyPinball.balls.size(); i++ ) azrp_filledcircle( CX(MyPinball.balls[i].pos), CY(MyPinball.balls[i].pos), (int) (MyPinball.balls[i].radius*cScale), MyPinball.balls[i].color );
|
|
|
|
for( int i=0; i<MyPinball.flippers.size(); i++ )
|
|
{
|
|
Vector2D start = MyPinball.flippers[i].pos;
|
|
Vector2D end = MyPinball.flippers[i].getTip();
|
|
|
|
Vector2D SE;
|
|
SE.Set( end-start );
|
|
Vector2D Norm;
|
|
Norm.Set( SE.PerpCW() );
|
|
Norm.Normalise();
|
|
|
|
Vector2D A = start.Clone();
|
|
A.Add( Norm, MyPinball.flippers[i].radius);
|
|
|
|
Vector2D B = end.Clone();
|
|
B.Add( Norm, MyPinball.flippers[i].radius);
|
|
|
|
Vector2D C = end.Clone();
|
|
C.Add( Norm, -MyPinball.flippers[i].radius);
|
|
|
|
Vector2D D = start.Clone();
|
|
D.Add( Norm, -MyPinball.flippers[i].radius);
|
|
|
|
int Xpoly[4] = { CX(A), CX(B), CX(C), CX(D) };
|
|
int Ypoly[4] = { CY(A), CY(B), CY(C), CY(D) };
|
|
|
|
azrp_filledpoly(Xpoly, Ypoly, 4, MyPinball.flippers[i].color );
|
|
|
|
azrp_filledcircle( CX(start), CY(start), (int) (MyPinball.flippers[i].radius*cScale), MyPinball.flippers[i].color );
|
|
azrp_filledcircle( CX(end), CY(end), (int) (MyPinball.flippers[i].radius*cScale), MyPinball.flippers[i].color );
|
|
azrp_line(CX(start), CY(start), CX(end), CY(end), MyPinball.flippers[i].color );
|
|
}
|
|
|
|
|
|
azrp_draw_text(150, 0, "FPS = %.0f - Mem Free = %d", (float)(1.0f / elapsedTime), _uram_stats->free_memory + extram_stats->free_memory);
|
|
|
|
azrp_draw_text(150, 20, "Score : %d", MyPinball.score );
|
|
|
|
azrp_draw_text(150, 40, "Ball1 : " );
|
|
azrp_draw_text(200, 40, "X = : %.2f", (float) MyPinball.balls[0].pos.x );
|
|
azrp_draw_text(200, 50, "Y = : %.2f", (float) MyPinball.balls[0].pos.y );
|
|
|
|
azrp_draw_text(150, 70, "Ball2 : " );
|
|
azrp_draw_text(200, 70, "X = : %.2f", (float) MyPinball.balls[1].pos.x );
|
|
azrp_draw_text(200, 80, "Y = : %.2f", (float) MyPinball.balls[1].pos.y );
|
|
}
|
|
|
|
|
|
|
|
static void get_inputs(float dt) {
|
|
|
|
/* EXIT THE GAME */
|
|
if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) &&
|
|
MyKeyboard.IsKeyHoldPressed(MYKEY_EXIT)) {
|
|
exitToOS = true;
|
|
};
|
|
|
|
/* LEFT FLIPPER */
|
|
if (MyKeyboard.IsKeyPressed(MYKEY_F1)) {
|
|
for(int i=0; i<MyPinball.flippers.size();i++)
|
|
if (MyPinball.flippers[i].side == LEFT)
|
|
MyPinball.flippers[i].touchIdentifier = libnum::num32(0);
|
|
}
|
|
else {
|
|
for(int i=0; i<MyPinball.flippers.size();i++)
|
|
if (MyPinball.flippers[i].side == LEFT)
|
|
MyPinball.flippers[i].touchIdentifier = libnum::num32(-1);
|
|
}
|
|
|
|
/* RIGHT FLIPPER */
|
|
if (MyKeyboard.IsKeyPressed(MYKEY_F6)) {
|
|
for(int i=0; i<MyPinball.flippers.size();i++)
|
|
if (MyPinball.flippers[i].side == RIGHT)
|
|
MyPinball.flippers[i].touchIdentifier = libnum::num32(0);
|
|
}
|
|
else {
|
|
for(int i=0; i<MyPinball.flippers.size();i++)
|
|
if (MyPinball.flippers[i].side == RIGHT)
|
|
MyPinball.flippers[i].touchIdentifier = libnum::num32(-1);
|
|
}
|
|
|
|
|
|
/* RESET THE GAME */
|
|
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) &&
|
|
MyKeyboard.IsKeyHoldPressed(MYKEY_F2)) {
|
|
SetupScene(0);
|
|
}
|
|
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) &&
|
|
MyKeyboard.IsKeyHoldPressed(MYKEY_F3)) {
|
|
SetupScene(1);
|
|
}
|
|
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) &&
|
|
MyKeyboard.IsKeyHoldPressed(MYKEY_F4)) {
|
|
SetupScene(2);
|
|
}
|
|
|
|
|
|
|
|
#if (DEBUG_MODE)
|
|
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
|
|
MyKeyboard.IsKeyPressedEvent(MYKEY_7) && usb_is_open()) {
|
|
screenshot = true;
|
|
};
|
|
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
|
|
MyKeyboard.IsKeyPressedEvent(MYKEY_8) && usb_is_open()) {
|
|
record = true;
|
|
};
|
|
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
|
|
MyKeyboard.IsKeyPressedEvent(MYKEY_9) && usb_is_open()) {
|
|
record = false;
|
|
};
|
|
#endif
|
|
|
|
/* we can have either LEFT or RIGHT or NONE OF THEM pressed for the direction
|
|
*/
|
|
}
|
|
|
|
bool AddMoreRAM(void) {
|
|
/* allow more RAM */
|
|
char const *osv = (char *)0x80020020;
|
|
|
|
if ((!strncmp(osv, "03.", 3) && osv[3] <= '8') &&
|
|
gint[HWCALC] == HWCALC_FXCG50) // CG-50
|
|
{
|
|
extended_ram.name = "extram";
|
|
extended_ram.is_default = true;
|
|
extended_ram.start = (void *)0x8c200000;
|
|
extended_ram.end = (void *)0x8c4e0000;
|
|
|
|
kmalloc_init_arena(&extended_ram, true);
|
|
kmalloc_add_arena(&extended_ram);
|
|
return true;
|
|
} else if (gint[HWCALC] == HWCALC_PRIZM) // CG-10/20
|
|
{
|
|
|
|
extended_ram.name = "extram";
|
|
extended_ram.is_default = true;
|
|
|
|
uint16_t *vram1, *vram2;
|
|
dgetvram(&vram1, &vram2);
|
|
dsetvram(vram1, vram1);
|
|
|
|
extended_ram.start = vram2;
|
|
extended_ram.end = (char *)vram2 + 396 * 224 * 2;
|
|
|
|
kmalloc_init_arena(&extended_ram, true);
|
|
kmalloc_add_arena(&extended_ram);
|
|
return true;
|
|
|
|
} else if (gint[HWCALC] == HWCALC_FXCG_MANAGER) // CG-50 EMULATOR
|
|
{
|
|
|
|
extended_ram.name = "extram";
|
|
extended_ram.is_default = true;
|
|
extended_ram.start = (void *)0x88200000;
|
|
extended_ram.end = (void *)0x884e0000;
|
|
|
|
kmalloc_init_arena(&extended_ram, true);
|
|
kmalloc_add_arena(&extended_ram);
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void FreeMoreRAM(void) {
|
|
memset(extended_ram.start, 0,
|
|
(char *)extended_ram.end - (char *)extended_ram.start);
|
|
}
|
|
|
|
int main(void) {
|
|
exitToOS = false;
|
|
|
|
_uram = kmalloc_get_arena("_uram");
|
|
|
|
bool canWeAllocate3Mb = AddMoreRAM();
|
|
|
|
__printf_enable_fp();
|
|
__printf_enable_fixed();
|
|
|
|
azrp_config_scale(SCALE_PIXEL);
|
|
|
|
azrp_shader_clear_configure();
|
|
azrp_shader_image_rgb16_configure();
|
|
azrp_shader_image_p8_configure();
|
|
azrp_shader_image_p4_configure();
|
|
azrp_shader_line_configure();
|
|
azrp_shader_circle_configure();
|
|
|
|
azrp_hook_set_prefrag(hook_prefrag);
|
|
|
|
usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL};
|
|
usb_open(interfaces, GINT_CALL_NULL);
|
|
|
|
SetupScene(0);
|
|
|
|
prof_init();
|
|
|
|
do {
|
|
perf_update = prof_make();
|
|
prof_enter(perf_update);
|
|
|
|
{
|
|
// all the stuff to be update should be put here
|
|
MyKeyboard.Update(elapsedTime);
|
|
get_inputs(elapsedTime);
|
|
|
|
update(elapsedTime);
|
|
|
|
// update the RAM consumption status
|
|
_uram_stats = kmalloc_get_gint_stats(_uram);
|
|
extram_stats = kmalloc_get_gint_stats(&extended_ram);
|
|
}
|
|
|
|
prof_leave(perf_update);
|
|
time_update = prof_time(perf_update);
|
|
|
|
perf_render = prof_make();
|
|
prof_enter(perf_render);
|
|
|
|
{
|
|
// all the stuff to be rendered should be put here
|
|
render();
|
|
|
|
azrp_update();
|
|
}
|
|
|
|
prof_leave(perf_render);
|
|
time_render = prof_time(perf_render);
|
|
|
|
|
|
/* elapsedTime expressed in microseconds when coming from the libprof high accuracy time measurement */
|
|
//elapsedTime = ((float)(time_update + time_render)) / 1000000.0f;
|
|
|
|
elapsedTime = ((float) 1.0f/60.0f);
|
|
|
|
} while (exitToOS == false);
|
|
|
|
prof_quit();
|
|
usb_close();
|
|
|
|
if (canWeAllocate3Mb)
|
|
FreeMoreRAM();
|
|
|
|
return 1;
|
|
}
|