Shmup/src/main.cpp

492 lines
13 KiB
C++

#define DEBUG_MODE 1
#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 <libprof.h>
#include <gint/usb.h>
#include <gint/usb-ff-bulk.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fxlibc/printf.h>
#include <cstdint>
#include <num/num.h>
#include "fast_trig.h"
#include "extrakeyboard.h"
#include "collections.h"
#include "MyAzurShaders.h"
#include "player.h"
#include "utilities.h"
#include "particles.h"
#include "bullet.h"
#include "enemy.h"
#include "impact.h"
#include "background.h"
#include "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;
static kmalloc_arena_t extended_ram = { 0 };
static kmalloc_arena_t *_uram;
kmalloc_gint_stats_t *_uram_stats;
kmalloc_gint_stats_t *extram_stats;
std::vector<Particle*> MyParticles;
std::vector<Bullet*> MyPlayerBullets;
std::vector<Bullet*> MyEnemiesBullets;
std::vector<Enemy*> MyEnemies;
std::vector<Impact*> MyImpacts;
Background MyBackground;
Player *MyPlayer;
KeyboardExtra MyKeyboard;
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, 4, false);
usb_write_sync(pipe, &sh, sizeof sh, 4, false);
}
usb_write_sync(pipe, fragment, size, 4, false);
if(id == azrp_frag_count - 1) {
usb_commit_sync(pipe);
screenshot = false;
}
}
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)
{
Create_Explosion( (int) MyEnemies[i]->x, (int) MyEnemies[i]->y );
delete( MyEnemies[i] );
MyEnemies.erase( MyEnemies.begin() + i );
}
}
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 );
}
}
// 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)
{
//TODO : we can create a list of impacts here, to be rendered later on
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 );
}
}
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();
for( auto& i : MyImpacts)
i->Render();
for(auto& p : MyParticles)
p->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 (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
}
static void get_inputs( float dt )
{
uint8_t speed = 4;
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(DEBUG_MODE)
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_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 )
{
/* 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;
}
}
void FreeMoreRAM( void )
{
memset(extended_ram.start, 0, (char *)extended_ram.end - (char *)extended_ram.start);
}
/*
#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;
_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_hook_set_prefrag(hook_prefrag);
azrp_starfield_init( 250 );
Create_Enemies( );
MyPlayer = new Player( azrp_width/4, azrp_height/2, 0);
/*
#if(DBGCRSH)
debug_crash( 1 );
#endif
*/
usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL };
usb_open(interfaces, GINT_CALL_NULL);
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
_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
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)
if (textoutput && usb_is_open())
{
// to add here what must be sent to USB for Text mode debugging
char texttosend[1024];
for(int i=-720; i<=720; i++)
{
sprintf( texttosend, "i=%d - Sin(i)=%f - Cos(i)=%f - Tan(i)=%f\n", i, (float) FastSinInt(i), (float) FastCosInt(i), (float) FastTanInt(i) );
usb_fxlink_text(texttosend, 0);
}
textoutput = false;
}
#endif
}
while (exitToOS==false);
Clean_Everything();
azrp_starfield_close( );
prof_quit();
usb_close();
FreeMoreRAM( );
return 1;
}