Shmup/src/main.cpp

615 lines
16 KiB
C++

#include "config.h"
#include <azur/azur.h>
#include <azur/gint/render.h>
#include <gint/drivers/r61524.h>
#include <gint/rtc.h>
#include <gint/clock.h>
#if(MORE_RAM)
#include <gint/kmalloc.h>
#endif
#include <libprof.h>
#if(USB)
#include <gint/usb.h>
#include <gint/usb-ff-bulk.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fxlibc/printf.h>
#include <cstdint>
#include <num/num.h>
#include "utilities/fast_trig.h"
#include "utilities/extrakeyboard.h"
#include "utilities/utilities.h"
#include "shmup/collections.h"
#include "shaders/MyAzurShaders.h"
#include "shmup/player.h"
#include "shmup/particles.h"
#include "shmup/bullet.h"
#include "shmup/enemy.h"
#include "shmup/bonus.h"
#include "shmup/impact.h"
#include "shmup/background.h"
#include "shmup/boss.h"
#include "shmup/trajectory.h"
#include <vector>
#include <algorithm>
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)
#define BIAS 1
#define NOBIAS (1-BIAS)
float elapsedTime = 0.0f;
uint32_t time_update=0, time_render=0;
prof_t perf_update, perf_render;
#if(MORE_RAM)
static kmalloc_arena_t extended_ram = { 0 };
static kmalloc_arena_t *_uram;
kmalloc_gint_stats_t *_uram_stats;
kmalloc_gint_stats_t *extram_stats;
#endif
std::vector<Particle*> MyParticles;
std::vector<Bullet*> MyPlayerBullets;
std::vector<Bullet*> MyEnemiesBullets;
std::vector<Enemy*> MyEnemies;
std::vector<Impact*> MyImpacts;
std::vector<Bonus*> MyBonuses;
std::vector<Trajectory*> MyTrajectories;
Background MyBackground;
Player *MyPlayer;
Boss *MyBoss;
KeyboardExtra MyKeyboard;
#if(USB)
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;
}
}
#endif
static void update( float dt )
{
// all update stuff depending on time will be done here
MyPlayer->Update( dt );
for(unsigned int i=0; i<MyImpacts.size(); i++)
{
MyImpacts[i]->Update( dt );
// Check if the property toberemoved has been set to "true" for particle deletion
if (MyImpacts[i]->toberemoved == true)
{
delete( MyImpacts[i] );
MyImpacts.erase( MyImpacts.begin() + i );
}
}
for(unsigned int i=0; i<MyEnemies.size(); i++)
{
MyEnemies[i]->Update( dt );
// Check if there is a collision with the current enemy
//MyPlayer->Test_Collision( MyEnemies[i] );
// Check if the property toberemoved has been set to "true" for particle deletion
if (MyEnemies[i]->toberemoved == true)
{
Bonus *b = new Bonus( (int) MyEnemies[i]->x, (int) MyEnemies[i]->y, rand() % 2 );
MyBonuses.push_back( b );
Create_Explosion( (int) MyEnemies[i]->x, (int) MyEnemies[i]->y );
delete( MyEnemies[i] );
MyEnemies.erase( MyEnemies.begin() + i );
}
}
if (MyBoss!=nullptr)
{
MyBoss->Update( dt );
if (MyBoss->toberemoved == true)
{
Bonus *b = new Bonus( (int) MyBoss->x, (int) MyBoss->y, rand() % 2 );
MyBonuses.push_back( b );
Create_Explosion( (int) MyBoss->x, (int) MyBoss->y );
delete( MyBoss );
MyBoss=nullptr;
}
}
for(unsigned int i=0; i<MyParticles.size(); i++)
{
MyParticles[i]->Update( dt );
// Check if the property toberemoved has been set to "true" for particle deletion
if (MyParticles[i]->toberemoved == true)
{
delete( MyParticles[i] );
MyParticles.erase( MyParticles.begin() + i );
}
}
for(unsigned int i=0; i<MyPlayerBullets.size(); i++)
{
MyPlayerBullets[i]->Update( dt );
for( unsigned j=0; j<MyEnemies.size(); j++ )
{
if(MyEnemies[j]->Test_Impact(MyPlayerBullets[i])==true)
{
//TODO : we can create a list of impacts here, to be rendered later on
Create_Impact( (int) MyPlayerBullets[i]->x, (int) MyPlayerBullets[i]->y );
}
}
if (MyBoss)
if(MyBoss->Test_Impact(MyPlayerBullets[i])==true)
{
//TODO : we can create a list of impacts here, to be rendered later on
Create_Impact( (int) MyPlayerBullets[i]->x, (int) MyPlayerBullets[i]->y );
}
// Check if the property toberemoved has been set to "true" for particle deletion
if (MyPlayerBullets[i]->toberemoved == true)
{
delete( MyPlayerBullets[i] );
MyPlayerBullets.erase( MyPlayerBullets.begin() + i );
}
}
for(unsigned int i=0; i<MyEnemiesBullets.size(); i++)
{
MyEnemiesBullets[i]->Update( dt );
if(MyPlayer->Test_Impact(MyEnemiesBullets[i])==true)
{
Create_Impact( (int) MyEnemiesBullets[i]->x, (int) MyEnemiesBullets[i]->y );
}
// Check if the property toberemoved has been set to "true" for particle deletion
if (MyEnemiesBullets[i]->toberemoved == true)
{
delete( MyEnemiesBullets[i] );
MyEnemiesBullets.erase( MyEnemiesBullets.begin() + i );
}
}
for(unsigned int i=0; i<MyBonuses.size(); i++)
{
MyBonuses[i]->Update( dt );
if (MyPlayer->Test_Impact(MyBonuses[i]) == true)
{
// TODO : put stuff to highlight the bonus
}
if (MyBonuses[i]->toberemoved == true)
{
delete( MyBonuses[i] );
MyBonuses.erase( MyBonuses.begin() + i );
}
}
MyBackground.Update( dt );
}
bool drawstars = true;
bool drawback = true;
static void render( void )
{
if (drawstars) azrp_starfield();
if (drawback) MyBackground.Render( );
for(auto& b : MyPlayerBullets)
b->Render();
for(auto& b : MyEnemiesBullets)
b->Render();
for(auto& e : MyEnemies)
e->Render();
if (MyBoss!=nullptr)
MyBoss->Render();
for( auto& i : MyImpacts)
i->Render();
for(auto& p : MyParticles)
p->Render();
for(auto& b : MyBonuses)
b->Render();
MyPlayer->Render();
#if(BIAS)
if (texttodraw>=1) Azur_draw_text(1,01, "FPS = %.0f", (float) (1000000.0f / elapsedTime) );
//if (texttodraw>=1) Azur_draw_text(1,11, "Part.= %d - Bull.= %d", MyParticles.size(), MyPlayerBullets.size() );
//if (texttodraw>=1 && !MyEnemies.empty()) Azur_draw_text(1,21, "Ennmy Life= %d", MyEnemies[0]->life );
if (texttodraw>=2) Azur_draw_text(1,31, "Update = %.3f ms", (float) time_update / 1000.0f );
if (texttodraw>=2) Azur_draw_text(1,41, "Render = %.3f ms", (float) time_render / 1000.0f );
if (texttodraw>=2) Azur_draw_text(1,51, ">Total = %.0f ms", (float) elapsedTime / 1000.0f );
#if(MORE_RAM)
if (texttodraw>=3) Azur_draw_text(1,81, "Mem Used : %d", _uram_stats->used_memory + extram_stats->used_memory);
if (texttodraw>=3) Azur_draw_text(1,91, "Mem Free : %d", _uram_stats->free_memory + extram_stats->free_memory);
if (texttodraw>=3) Azur_draw_text(1,101, "Mem Peak Used : %d", _uram_stats->peak_used_memory + extram_stats->peak_used_memory );
#endif
#endif
}
static void get_inputs( float dt )
{
uint32_t tempshoot = rtc_ticks();
if(MyKeyboard.IsKeyPressed(MYKEY_F1))
{
if (MyPlayer->Shoot_OK(tempshoot, BULLET_NORMAL)) Create_Player_Shoot(0);
}
if(MyKeyboard.IsKeyPressed(MYKEY_F2))
{
if (MyPlayer->Shoot_OK(tempshoot, BULLET_BLUE)) Create_Player_Shoot(1);
}
if(MyKeyboard.IsKeyPressedEvent(MYKEY_F3))
{
if (MyPlayer->Shoot_OK(tempshoot, BULLET_LASER)) Create_Player_Shoot(2);
}
if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyHoldPressed(MYKEY_EXIT)) {exitToOS = true; };
#if(USB)
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_7) && usb_is_open() ) {screenshot = true;};
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_8) && usb_is_open()) {record = true; };
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_9) && usb_is_open()) {record = false; };
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_DEL) && usb_is_open()) {textoutput = true;};
#endif
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_F1)) {texttodraw=0;}
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_F2)) {texttodraw=1;}
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_F3)) {texttodraw=2;}
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_F4)) {texttodraw=3;}
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_F5))
{
drawback = !drawback;
drawstars = ! drawstars;
}
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_F6))
{
azrp_starfield_close( );
azrp_starfield_init( 250 );
}
if(MyKeyboard.IsKeyPressed(MYKEY_LEFT)) { MyPlayer->Go_Left( dt ); }
if(MyKeyboard.IsKeyPressed(MYKEY_RIGHT)) { MyPlayer->Go_Right( dt ); }
if(MyKeyboard.IsKeyPressed(MYKEY_UP)) { MyPlayer->Go_Up( dt ); }
if(MyKeyboard.IsKeyPressed(MYKEY_DOWN)) { MyPlayer->Go_Down( dt ); }
}
bool AddMoreRAM( void )
{
#if(MORE_RAM && !CALCEMU)
/* allow more RAM */
char const *osv = (char*) 0x80020020;
if((!strncmp(osv, "03.", 3) && osv[3] <= '6') && 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 false;
}
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;
}
#elif (MORE_RAM && CALCEMU)
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
return false;
#endif
return false;
}
void FreeMoreRAM( void )
{
#if(MORE_RAM)
memset(extended_ram.start, 0, (char *)extended_ram.end - (char *)extended_ram.start);
#endif
}
/*
#define DBGCRSH 1
#include <gint/keyboard.h>
int a=0;
void debug_crash( int value )
{
dprint( 1, 1+a*10, C_BLACK, "point = %d : REACHED", value );
a++;
dupdate();
getkey();
}
void debug_crash_msg( char *chain )
{
dprint( 1, 1+a*10, C_BLACK, "point = %s : REACHED", chain );
a++;
dupdate();
getkey();
}
*/
int main(void)
{
exitToOS = false;
#if(MORE_RAM)
_uram = kmalloc_get_arena("_uram");
#endif
bool canWeAllocateMoreRam = AddMoreRAM();
__printf_enable_fp();
__printf_enable_fixed();
azrp_config_scale(SCALE_PIXEL);
#if(USB)
azrp_hook_set_prefrag(hook_prefrag);
#endif
azrp_starfield_init( 250 );
Create_Enemies( );
MyPlayer = new Player( azrp_width/4, azrp_height/2, 0);
/*
MyBoss = new Boss( 3*azrp_width/4, azrp_height/2, 0);
Point2D *A = new Point2D( 348, 112 );
Point2D *B = new Point2D( 371, 199 );
Point2D *C = new Point2D( 198, 149 );
Point2D *D = new Point2D( 25, 199 );
Point2D *E = new Point2D( 25, 25 );
Point2D *F = new Point2D( 198, 75 );
Point2D *G = new Point2D( 371, 25 );
Trajectory *MyTrajectory= new Trajectory();
MyTrajectory->AddPoint( A );
MyTrajectory->AddPoint( B );
MyTrajectory->AddPoint( C );
MyTrajectory->AddPoint( D );
MyTrajectory->AddPoint( E );
MyTrajectory->AddPoint( F );
MyTrajectory->AddPoint( G );
MyBoss->hasTrajectory = true;
MyBoss->pathToFollow = MyTrajectory;
*/
/*
#if(DBGCRSH)
debug_crash( 1 );
#endif
*/
#if(USB)
usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL };
usb_open(interfaces, GINT_CALL_NULL);
#endif
prof_init();
do
{
perf_update = prof_make();
prof_enter(perf_update);
{
// all the stuff to be update should be put here
MyKeyboard.Update( elapsedTime );
// read inputs from the player
get_inputs( elapsedTime );
// update as per the time spend to do the loop
update( elapsedTime );
// update the RAM consumption status
#if(MORE_RAM)
_uram_stats = kmalloc_get_gint_stats(_uram);
extram_stats = kmalloc_get_gint_stats(&extended_ram);
#endif
}
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
azrp_clear( C_BLACK );
render();
azrp_update();
}
prof_leave(perf_render);
time_render = prof_time(perf_render);
elapsedTime = ((float) (time_update+time_render));
#if(DEBUG_MODE && USB)
if (textoutput && usb_is_open())
{
// to add here what must be sent to USB for Text mode debugging
char texttosend[1024];
sprintf( texttosend, "%s", "Hello !!!" );
usb_fxlink_text(texttosend, 0);
textoutput = false;
}
#endif
}
while (exitToOS==false);
Clean_Everything();
// TODO - make it clean for the future
delete( MyBoss );
delete( MyPlayer );
azrp_starfield_close( );
prof_quit();
#if(USB)
usb_close();
#endif
if (canWeAllocateMoreRam) FreeMoreRAM( );
return 1;
}