diff --git a/src/audio/external.c b/src/audio/external.c index df2bf050..b62fe6ff 100644 --- a/src/audio/external.c +++ b/src/audio/external.c @@ -709,6 +709,17 @@ void func_eu_802e9bec(s32 player, s32 channel, s32 arg2) { struct SPTask *create_next_audio_frame_task(void) { return NULL; } +void create_next_audio_buffer(s16 *samples, u32 num_samples) { + gAudioFrameCount++; + if (sGameLoopTicked != 0) { + update_game_sound(); + sGameLoopTicked = 0; + } + s32 writtenCmds; + synthesis_execute(gAudioCmdBuffers[0], &writtenCmds, samples, num_samples); + gAudioRandom = ((gAudioRandom + gAudioFrameCount) * gAudioFrameCount); + decrease_sample_dma_ttls(); +} #endif /** diff --git a/src/audio/external.h b/src/audio/external.h index 7ba566e2..5e514450 100644 --- a/src/audio/external.h +++ b/src/audio/external.h @@ -60,7 +60,7 @@ void play_race_fanfare(void); void play_toads_jingle(void); void sound_reset(u8 presetId); void audio_set_sound_mode(u8 arg0); - +void create_next_audio_buffer(s16 *samples, u32 num_samples); void audio_init(void); // in load.c #if defined(VERSION_EU) || defined(VERSION_SH) diff --git a/src/game/game_init.c b/src/game/game_init.c index dce0f8a7..27332d65 100644 --- a/src/game/game_init.c +++ b/src/game/game_init.c @@ -245,27 +245,27 @@ void make_viewport_clip_rect(Vp *viewport) { * If you plan on using gSPLoadUcode, make sure to add OS_TASK_LOADABLE to the flags member. */ void create_gfx_task_structure(void) { - s32 entries = gDisplayListHead - gGfxPool->buffer; + // s32 entries = gDisplayListHead - gGfxPool->buffer; - gGfxSPTask->msgqueue = &gGfxVblankQueue; - gGfxSPTask->msg = OS_MESG_32(2); - gGfxSPTask->task.t.type = M_GFXTASK; - gGfxSPTask->task.t.ucode_boot = rspF3DBootStart; - gGfxSPTask->task.t.ucode_boot_size = ((u8 *) rspF3DBootEnd - (u8 *) rspF3DBootStart); - gGfxSPTask->task.t.flags = 0; - gGfxSPTask->task.t.ucode = rspF3DStart; - gGfxSPTask->task.t.ucode_data = rspF3DDataStart; - gGfxSPTask->task.t.ucode_size = SP_UCODE_SIZE; // (this size is ignored) - gGfxSPTask->task.t.ucode_data_size = SP_UCODE_DATA_SIZE; - gGfxSPTask->task.t.dram_stack = (u64 *) gGfxSPTaskStack; - gGfxSPTask->task.t.dram_stack_size = SP_DRAM_STACK_SIZE8; - gGfxSPTask->task.t.output_buff = gGfxSPTaskOutputBuffer; - gGfxSPTask->task.t.output_buff_size = - (u64 *)((u8 *) gGfxSPTaskOutputBuffer + sizeof(gGfxSPTaskOutputBuffer)); - gGfxSPTask->task.t.data_ptr = (u64 *) &gGfxPool->buffer; - gGfxSPTask->task.t.data_size = entries * sizeof(Gfx); - gGfxSPTask->task.t.yield_data_ptr = (u64 *) gGfxSPTaskYieldBuffer; - gGfxSPTask->task.t.yield_data_size = OS_YIELD_DATA_SIZE; + // gGfxSPTask->msgqueue = &gGfxVblankQueue; + // gGfxSPTask->msg = OS_MESG_32(2); + // gGfxSPTask->task.t.type = M_GFXTASK; + // gGfxSPTask->task.t.ucode_boot = rspF3DBootStart; + // gGfxSPTask->task.t.ucode_boot_size = ((u8 *) rspF3DBootEnd - (u8 *) rspF3DBootStart); + // gGfxSPTask->task.t.flags = 0; + // gGfxSPTask->task.t.ucode = rspF3DStart; + // gGfxSPTask->task.t.ucode_data = rspF3DDataStart; + // gGfxSPTask->task.t.ucode_size = SP_UCODE_SIZE; // (this size is ignored) + // gGfxSPTask->task.t.ucode_data_size = SP_UCODE_DATA_SIZE; + // gGfxSPTask->task.t.dram_stack = (u64 *) gGfxSPTaskStack; + // gGfxSPTask->task.t.dram_stack_size = SP_DRAM_STACK_SIZE8; + // gGfxSPTask->task.t.output_buff = gGfxSPTaskOutputBuffer; + // gGfxSPTask->task.t.output_buff_size = + // (u64 *)((u8 *) gGfxSPTaskOutputBuffer + sizeof(gGfxSPTaskOutputBuffer)); + // gGfxSPTask->task.t.data_ptr = (u64 *) &gGfxPool->buffer; + // gGfxSPTask->task.t.data_size = entries * sizeof(Gfx); + // gGfxSPTask->task.t.yield_data_ptr = (u64 *) gGfxSPTaskYieldBuffer; + // gGfxSPTask->task.t.yield_data_size = OS_YIELD_DATA_SIZE; } /** @@ -640,8 +640,10 @@ void setup_game_memory(void) { /** * Main game loop thread. Runs forever as long as the game continues. */ -void thread5_game_loop(UNUSED void *arg) { - struct LevelCommand *addr; + +struct LevelCommand *addr; + +void thread5_game_loop() { setup_game_memory(); #if ENABLE_RUMBLE @@ -661,12 +663,13 @@ void thread5_game_loop(UNUSED void *arg) { play_music(SEQ_PLAYER_SFX, SEQUENCE_ARGS(0, SEQ_SOUND_PLAYER), 0); set_sound_mode(save_file_get_sound_mode()); render_init(); +} - while (TRUE) { - // If the reset timer is active, run the process to reset the game. +void thread5_iteration(void){ + // If the reset timer is active, run the process to reset the game. if (gResetTimer != 0) { draw_reset_bars(); - continue; + return; } profiler_log_thread5_time(THREAD5_START); @@ -692,5 +695,4 @@ void thread5_game_loop(UNUSED void *arg) { // amount of free space remaining. print_text_fmt_int(180, 20, "BUF %d", gGfxPoolEnd - (u8 *) gDisplayListHead); } - } -} +} \ No newline at end of file diff --git a/src/game/game_init.h b/src/game/game_init.h index a3764b00..988fd733 100644 --- a/src/game/game_init.h +++ b/src/game/game_init.h @@ -68,5 +68,6 @@ void end_master_display_list(void); void render_init(void); void select_gfx_pool(void); void display_and_vsync(void); +void thread5_iteration(void); #endif // GAME_INIT_H diff --git a/src/game/main.h b/src/game/main.h index f313973e..5d0b300b 100644 --- a/src/game/main.h +++ b/src/game/main.h @@ -1,6 +1,7 @@ #ifndef MAIN_H #define MAIN_H +#include #include "config.h" struct RumbleData { diff --git a/src/port/Game.c b/src/port/Game.c deleted file mode 100644 index 09813e1f..00000000 --- a/src/port/Game.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int main(){ - printf("Yaay we run\n"); -} \ No newline at end of file diff --git a/src/port/Game.cpp b/src/port/Game.cpp new file mode 100644 index 00000000..01e04e83 --- /dev/null +++ b/src/port/Game.cpp @@ -0,0 +1,77 @@ +#include + +#include "sm64.h" +#include +#include "Globals.h" + +#define SAMPLES_HIGH 544 +#define SAMPLES_LOW 528 +#define AUDIO_FRAMES_PER_UPDATE 2 +#define NUM_AUDIO_CHANNELS 2 + +extern "C" { +#include "game/main.h" +#include "game/game_init.h" +#include "audio/external.h" +#include "segments.h" +s8 D_8032C650 = 0; +s8 gResetTimer = 0; +u32 gNumVblanks = 0; +s8 gNmiResetBarsTimer = 0; +u64 osClockRate = 62500000; + +s8 sAudioEnabled = TRUE; +s8 gShowProfiler = FALSE; +s8 gShowDebugText = FALSE; +s8 gDebugLevelSelect = FALSE; + +OSIoMesg gDmaIoMesg; +OSMesg gMainReceivedMesg; +OSMesgQueue gDmaMesgQueue; +OSMesgQueue gSIEventMesgQueue; +} + +extern "C" { +void set_vblank_handler(s32 index, VblankHandler *handler, OSMesgQueue *queue, OSMesg msg) {} +void dispatch_audio_sptask(SPTask *spTask) {} + +void alloc_pool(void) { + void *start = (void *) SEG_POOL_START; + void *end = (void *) SEG_POOL_END; + + main_pool_init(start, end); + gEffectsMemoryPool = mem_pool_init(0x4000, MEMORY_POOL_LEFT); +} + +void exec_display_list(SPTask *spTask) { + if(gDisplayListHead == nullptr){ + return; + } + GameEngine_RunCommands((Gfx*) spTask->task.t.data_ptr); +} +} + +void push_frame(void) { + GameEngine_StartFrame(); + thread5_iteration(); + + int samples_left = AudioPlayerBuffered(); + u32 num_audio_samples = samples_left < AudioPlayerGetDesiredBuffered() ? SAMPLES_HIGH : SAMPLES_LOW; + + s16 audio_buffer[SAMPLES_HIGH * NUM_AUDIO_CHANNELS * 2]; + for (int i = 0; i < AUDIO_FRAMES_PER_UPDATE; i++) { + create_next_audio_buffer(audio_buffer + i * (num_audio_samples * 2), num_audio_samples); + } + + AudioPlayerPlayFrame((u8*)audio_buffer, 2 * num_audio_samples * 4); +} + +int main(){ + GameEngine_Create(); + alloc_pool(); + audio_init(); + sound_init(); + + thread5_game_loop(NULL); + GameEngine_ProcessFrame(push_frame); +} \ No newline at end of file diff --git a/src/port/Globals.cpp b/src/port/Globals.cpp index de5a7b3a..215231ea 100644 --- a/src/port/Globals.cpp +++ b/src/port/Globals.cpp @@ -14,7 +14,6 @@ GameEngine::GameEngine(){ this->context->GetWindow()->SetMaximumFrameLatency(1); } -extern "C" { void GameEngine_Create(void){ GameEngine::Instance = new GameEngine(); } @@ -30,6 +29,4 @@ void GameEngine_RunCommands(Gfx* Commands) { void GameEngine_ProcessFrame(void (*run_one_game_iter)(void)) { GameEngine::Instance->context->GetWindow()->MainLoop(run_one_game_iter); -} - } \ No newline at end of file diff --git a/src/port/Globals.h b/src/port/Globals.h index cfc5b67d..31d45c45 100644 --- a/src/port/Globals.h +++ b/src/port/Globals.h @@ -1,7 +1,5 @@ #pragma once -#ifdef __cplusplus - #include #include @@ -16,11 +14,7 @@ public: void run(); }; -#else - void GameEngine_Create(void); void GameEngine_StartFrame(void); void GameEngine_RunCommands(Gfx* Commands); -void GameEngine_ProcessFrame(void (*run_one_game_iter)(void)); - -#endif \ No newline at end of file +void GameEngine_ProcessFrame(void (*run_one_game_iter)(void)); \ No newline at end of file