audio work

This commit is contained in:
Sonic Dreamcaster 2024-11-14 20:23:27 -03:00
parent 4ae2fc22c1
commit 11a148a047
10 changed files with 140 additions and 105 deletions

View File

@ -387,7 +387,7 @@ typedef enum {
* @param drumPtr (s32) the ptr to the `Drum` struct
*/
#define AUDIOCMD_GLOBAL_SET_DRUM_FONT(fontId, drumId, drumPtr) \
AudioThread_QueueCmdS32(AUDIO_MK_CMD(AUDIOCMD_OP_GLOBAL_SET_DRUM_FONT, fontId, drumId, 0), drumPtr)
AudioThread_QueueCmdPtr(AUDIO_MK_CMD(AUDIOCMD_OP_GLOBAL_SET_DRUM_FONT, fontId, drumId, 0), drumPtr)
/**
* Set a soundeffect ptr within a soundfont
@ -397,7 +397,7 @@ typedef enum {
* @param soundEffectPtr (s32) the ptr to the `SoundEffect` struct
*/
#define AUDIOCMD_GLOBAL_SET_SFX_FONT(fontId, soundEffectId, soundEffectPtr) \
AudioThread_QueueCmdS32(AUDIO_MK_CMD(AUDIOCMD_OP_GLOBAL_SET_SFX_FONT, fontId, soundEffectId, 0), soundEffectPtr)
AudioThread_QueueCmdPtr(AUDIO_MK_CMD(AUDIOCMD_OP_GLOBAL_SET_SFX_FONT, fontId, soundEffectId, 0), soundEffectPtr)
/**
* Set an instrument ptr within a soundfont
@ -407,7 +407,7 @@ typedef enum {
* @param instPtr (s32) the ptr to the `Instrument` struct
*/
#define AUDIOCMD_GLOBAL_SET_INSTRUMENT_FONT(fontId, instId, instPtr) \
AudioThread_QueueCmdS32(AUDIO_MK_CMD(AUDIOCMD_OP_GLOBAL_SET_INSTRUMENT_FONT, fontId, instId, 0), instPtr)
AudioThread_QueueCmdPtr(AUDIO_MK_CMD(AUDIOCMD_OP_GLOBAL_SET_INSTRUMENT_FONT, fontId, instId, 0), instPtr)
/**
* Pop the persistent cache of the specified table
@ -507,7 +507,7 @@ typedef enum {
* @param functionPtr (s32) address of the function to run once every audio frame
*/
#define AUDIOCMD_GLOBAL_SET_CUSTOM_UPDATE_FUNCTION(functionPtr) \
AudioThread_QueueCmdS32(AUDIO_MK_CMD(AUDIOCMD_OP_GLOBAL_SET_CUSTOM_UPDATE_FUNCTION, 0, 0, 0), functionPtr)
AudioThread_QueueCmdPtr(AUDIO_MK_CMD(AUDIOCMD_OP_GLOBAL_SET_CUSTOM_UPDATE_FUNCTION, 0, 0, 0), functionPtr)
/**
* Asynchronously load a sequence

View File

@ -776,18 +776,10 @@ typedef struct {
void* data;
f32 asFloat;
s32 asInt;
struct {
u8 pad2[2];
u16 asUShort;
};
struct {
u8 pad1[3];
s8 asSbyte;
};
struct {
u8 pad0[3];
u8 asUbyte;
};
uintptr_t asPtr;
u16 asUShort;
s8 asSbyte;
u8 asUbyte;
u32 asUInt;
};
} AudioCmd; // size = 0x8
@ -1034,10 +1026,10 @@ Acmd* AudioSynth_Update(Acmd* aList, s32* cmdCount, s16* aiBufStart, s32 aiBufLe
// audio_effects
void Audio_SequencePlayerProcessSound(SequencePlayer* seqplayer);
void func_80013A18(Note* note);
void Audio_NoteVibratoUpdate(Note* note);
void Audio_NoteVibratoInit(Note* note);
void func_80013B6C(AdsrState* adsr, EnvelopePoint* envelope, s16* arg2);
f32 func_80013B90(AdsrState* adsr);
void Audio_AdsrInit(AdsrState* adsr, EnvelopePoint* envelope, s16* arg2);
f32 Audio_AdsrUpdate(AdsrState* adsr);
// audio_heap
void AudioHeap_DiscardFont(s32 fontId);

View File

@ -361,15 +361,15 @@ func_80012ED4 = 0x80012ED4;
func_80012F24 = 0x80012F24;
Audio_AllocNote = 0x8001301C;
func_800132E8 = 0x800132E8;
func_80013400 = 0x80013400;
Audio_SequenceChannelProcessSound = 0x80013400;
Audio_SequencePlayerProcessSound = 0x800135A8;
func_80013708 = 0x80013708;
func_800137DC = 0x800137DC;
func_80013820 = 0x80013820;
func_80013A18 = 0x80013A18;
Audio_GetPortamentoFreqScale = 0x80013708;
Audio_GetVibratoPitchChange = 0x800137DC;
Audio_GetVibratoFreqScale = 0x80013820;
Audio_NoteVibratoUpdate = 0x80013A18;
Audio_NoteVibratoInit = 0x80013A84;
func_80013B6C = 0x80013B6C;
func_80013B90 = 0x80013B90;
Audio_AdsrInit = 0x80013B6C;
Audio_AdsrUpdate = 0x80013B90;

View File

@ -209,10 +209,10 @@ AudioSpec gAudioSpecs[] = {
/* 21 */
{ 32000, 2, 32, ARRAY_COUNT(D_800C7628), D_800C7628, 0x7FFF, 0x1200, 0x1100, 0, 0x5000, 0x2400, 0, 0x1B000,
0x00000 },
/* 22 */
/* 22 TITLE */
{ 32000, 1, 32, ARRAY_COUNT(D_800C7638), D_800C7638, 0x7FFF, 0x1200, 0x1100, 0, 0x5000, 0x2400, 0, 0x1B000,
0x00000 },
/* 23 */
/* 23 OPTION */
{ 32000, 1, 32, ARRAY_COUNT(D_800C7648), D_800C7648, 0x7FFF, 0x1200, 0xA000, 0, 0x5B00, 0x1D00, 0, 0x00000,
0x00000 },
/* 24 */

View File

@ -4,13 +4,13 @@
static const char devstr[] = "Audio:Envp: overflow %f\n";
void func_80013400(SequenceChannel* channel, s32 updateVolume) {
void Audio_SequenceChannelProcessSound(SequenceChannel* channel, s32 updateVolume) {
s32 i;
if (channel->changes.s.volume || updateVolume) {
f32 channelVolume = channel->volume * channel->volumeMod * channel->seqPlayer->appliedFadeVolume;
if (channel->seqPlayer->muted && (channel->muteBehavior & 0x20)) {
if (channel->seqPlayer->muted && (channel->muteBehavior & MUTE_BEHAVIOR_SOFTEN)) {
channelVolume = channel->seqPlayer->muteVolumeMod * channelVolume;
}
channel->appliedVolume = SQ(channelVolume);
@ -66,13 +66,13 @@ void Audio_SequencePlayerProcessSound(SequencePlayer* seqplayer) {
}
for (i = 0; i < 16; i++) {
if ((IS_SEQUENCE_CHANNEL_VALID(seqplayer->channels[i]) == 1) && (seqplayer->channels[i]->enabled == 1)) {
func_80013400(seqplayer->channels[i], seqplayer->recalculateVolume);
Audio_SequenceChannelProcessSound(seqplayer->channels[i], seqplayer->recalculateVolume);
}
}
seqplayer->recalculateVolume = false;
}
f32 func_80013708(Portamento* portamento) {
f32 Audio_GetPortamentoFreqScale(Portamento* portamento) {
u32 temp;
f32 temp2;
@ -85,7 +85,7 @@ f32 func_80013708(Portamento* portamento) {
return temp2;
}
s16 func_800137DC(VibratoState* vibrato) {
s16 Audio_GetVibratoPitchChange(VibratoState* vibrato) {
s32 index;
vibrato->time += (s32) vibrato->rate;
@ -93,7 +93,7 @@ s16 func_800137DC(VibratoState* vibrato) {
return vibrato->curve[index] >> 8;
}
f32 func_80013820(VibratoState* vibrato) {
f32 Audio_GetVibratoFreqScale(VibratoState* vibrato) {
s32 ret;
f32 temp;
f32 temp2;
@ -131,18 +131,18 @@ f32 func_80013820(VibratoState* vibrato) {
if (vibrato->depth == 0.0f) {
return 1.0f;
}
ret = func_800137DC(vibrato);
ret = Audio_GetVibratoPitchChange(vibrato);
temp = vibrato->depth / 4096.0f;
temp2 = 1.0f + temp * (gBendPitchOneOctaveFrequencies[0x80 + ret] - 1.0f);
return temp2;
}
void func_80013A18(Note* note) {
void Audio_NoteVibratoUpdate(Note* note) {
if (note->playbackState.portamento.mode != 0) {
note->playbackState.portamentoFreqMod = func_80013708(&note->playbackState.portamento);
note->playbackState.portamentoFreqMod = Audio_GetPortamentoFreqScale(&note->playbackState.portamento);
}
if ((note->playbackState.vibratoState.active != 0) && (note->playbackState.parentLayer != NO_LAYER)) {
note->playbackState.vibratoFreqMod = func_80013820(&note->playbackState.vibratoState);
note->playbackState.vibratoFreqMod = Audio_GetVibratoFreqScale(&note->playbackState.vibratoState);
}
}
@ -174,7 +174,7 @@ void Audio_NoteVibratoInit(Note* note) {
noteState->portamento = noteState->parentLayer->portamento;
}
void func_80013B6C(AdsrState* adsr, EnvelopePoint* envelope, s16* arg2) {
void Audio_AdsrInit(AdsrState* adsr, EnvelopePoint* envelope, s16* arg2) {
adsr->action.asByte = 0;
adsr->state = 0;
adsr->delay = 0;
@ -183,7 +183,7 @@ void func_80013B6C(AdsrState* adsr, EnvelopePoint* envelope, s16* arg2) {
adsr->current = 0.0f;
}
f32 func_80013B90(AdsrState* adsr) {
f32 Audio_AdsrUpdate(AdsrState* adsr) {
u8 action = adsr->action.asByte;
u8 state = adsr->state;
@ -200,7 +200,7 @@ f32 func_80013B90(AdsrState* adsr) {
adsr->state = ADSR_STATE_LOOP;
case_ADSR_STATE_LOOP:
case ADSR_STATE_LOOP:
adsr->delay = BSWAP16(adsr->envelope[adsr->envIndex].delay);
adsr->delay = (s16)BSWAP16(adsr->envelope[adsr->envIndex].delay);
switch (adsr->delay) {
case ADSR_DISABLE:
adsr->state = ADSR_STATE_DISABLED;
@ -209,7 +209,7 @@ f32 func_80013B90(AdsrState* adsr) {
adsr->state = ADSR_STATE_HANG;
break;
case ADSR_GOTO:
adsr->envIndex = BSWAP16(adsr->envelope[adsr->envIndex].arg);
adsr->envIndex = (s16)BSWAP16(adsr->envelope[adsr->envIndex].arg);
goto case_ADSR_STATE_LOOP;
case ADSR_RESTART:
adsr->state = ADSR_STATE_INITIAL;

View File

@ -218,10 +218,10 @@ Drum* Audio_GetDrum(s32 fontId, s32 drumId) {
void func_80011EB8(Note* note) {
if (note->playbackState.parentLayer->adsr.decayIndex == 0) {
func_80013B6C(&note->playbackState.adsr, note->playbackState.parentLayer->channel->adsr.envelope,
Audio_AdsrInit(&note->playbackState.adsr, note->playbackState.parentLayer->channel->adsr.envelope,
&note->playbackState.adsrVolModUnused);
} else {
func_80013B6C(&note->playbackState.adsr, note->playbackState.parentLayer->adsr.envelope,
Audio_AdsrInit(&note->playbackState.adsr, note->playbackState.parentLayer->adsr.envelope,
&note->playbackState.adsrVolModUnused);
}
note->playbackState.adsr.state = 1;
@ -323,8 +323,8 @@ void Audio_ProcessNotes(void) {
goto next;
}
temp_fs0 = func_80013B90(&playbackState->adsr);
func_80013A18(note);
temp_fs0 = Audio_AdsrUpdate(&playbackState->adsr);
Audio_NoteVibratoUpdate(note);
attr = &playbackState->attributes;
if ((playbackState->unk_04 == 1) || (playbackState->unk_04 == 2)) {
sp70.freqMod = attr->freqMod;

View File

@ -1243,7 +1243,7 @@ void AudioSeq_SequencePlayerProcessSequence(SequencePlayer* seqPlayer) {
s32 pad3;
// @port:
gSeqLoadStatus[seqPlayer->seqId] = 2;
gSeqLoadStatus[seqPlayer->seqId] = LOAD_STATUS_COMPLETE;
if (!seqPlayer->enabled) {
return;

View File

@ -317,6 +317,14 @@ void AudioThread_QueueCmd(AudioCmd cmd) {
}
}
void AudioThread_QueueCmdPtr(u32 opArgs, void* data) {
AudioCmd cmd = {
.opArgs = opArgs,
.asPtr = data,
};
AudioThread_QueueCmd(cmd);
}
void AudioThread_QueueCmdF32(u32 opArgs, f32 val) {
AudioCmd cmd = { .opArgs = opArgs, .asFloat = val };
AudioThread_QueueCmd(cmd);

View File

@ -357,6 +357,7 @@ void Game_SetScene(void) {
extern u8 gAudioSpecId;
extern int audBuffer;
extern AudioBufferParameters gAudioBufferParams;
extern int countermin;
void Game_Update(void) {
s32 i;
u8 partialFill;
@ -625,6 +626,8 @@ void Game_Update(void) {
}
RCP_SetupDL(&gMasterDisp, SETUPDL_83);
gDPSetPrimColor(gMasterDisp++, 0, 0, 255, 255, 0, 255);
Graphics_DisplaySmallText(10, 190, 1.0f, 1.0f, "SEC:");
Graphics_DisplaySmallNumber(90, 190, countermin);
Graphics_DisplaySmallText(10, 200, 1.0f, 1.0f, "AUDIOBUF:");
Graphics_DisplaySmallNumber(90, 200, audBuffer);
Graphics_DisplaySmallText(10, 210, 1.0f, 1.0f, "AUDIOSPEC:");

View File

@ -37,28 +37,26 @@ extern uint16_t gFPS;
float gInterpolationStep = 0.0f;
#include <sf64thread.h>
#include <macros.h>
void AudioThread_CreateNextAudioBuffer(int16_t *samples, uint32_t num_samples);
void AudioThread_CreateNextAudioBuffer(int16_t* samples, uint32_t num_samples);
}
GameEngine* GameEngine::Instance;
static GamePool MemoryPool = {
.chunk = 1024 * 512,
.cursor = 0,
.length = 0,
.memory = nullptr
};
static GamePool MemoryPool = { .chunk = 1024 * 512, .cursor = 0, .length = 0, .memory = nullptr };
GameEngine::GameEngine() {
std::vector<std::string> OTRFiles;
if (const std::string cube_path = Ship::Context::GetPathRelativeToAppDirectory("starship.otr"); std::filesystem::exists(cube_path)) {
if (const std::string cube_path = Ship::Context::GetPathRelativeToAppDirectory("starship.otr");
std::filesystem::exists(cube_path)) {
OTRFiles.push_back(cube_path);
}
if (const std::string sm64_otr_path = Ship::Context::GetPathRelativeToAppDirectory("sf64.otr"); std::filesystem::exists(sm64_otr_path)) {
if (const std::string sm64_otr_path = Ship::Context::GetPathRelativeToAppDirectory("sf64.otr");
std::filesystem::exists(sm64_otr_path)) {
OTRFiles.push_back(sm64_otr_path);
}
if (const std::string patches_path = Ship::Context::GetPathRelativeToAppDirectory("mods"); !patches_path.empty() && std::filesystem::exists(patches_path)) {
if (const std::string patches_path = Ship::Context::GetPathRelativeToAppDirectory("mods");
!patches_path.empty() && std::filesystem::exists(patches_path)) {
if (std::filesystem::is_directory(patches_path)) {
for (const auto&p: std::filesystem::recursive_directory_iterator(patches_path)) {
for (const auto& p : std::filesystem::recursive_directory_iterator(patches_path)) {
if (StringHelper::IEquals(p.path().extension().string(), ".otr")) {
OTRFiles.push_back(p.path().generic_string());
}
@ -66,32 +64,56 @@ GameEngine::GameEngine() {
}
}
this->context = Ship::Context::CreateInstance("Starship", "ship", "starship.cfg.json", OTRFiles, {}, 3, { 32000, 1024, 2480 });
this->context =
Ship::Context::CreateInstance("Starship", "ship", "starship.cfg.json", OTRFiles, {}, 3, { 32000, 1024, 2480 });
auto loader = context->GetResourceManager()->GetResourceLoader();
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryAnimV0>(), RESOURCE_FORMAT_BINARY, "Animation", static_cast<uint32_t>(SF64::ResourceType::AnimData), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinarySkeletonV0>(), RESOURCE_FORMAT_BINARY, "Skeleton", static_cast<uint32_t>(SF64::ResourceType::Skeleton), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryLimbV0>(), RESOURCE_FORMAT_BINARY, "Limb", static_cast<uint32_t>(SF64::ResourceType::Limb), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryMessageV0>(), RESOURCE_FORMAT_BINARY, "Message", static_cast<uint32_t>(SF64::ResourceType::Message), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryMessageLookupV0>(), RESOURCE_FORMAT_BINARY, "MessageTable", static_cast<uint32_t>(SF64::ResourceType::MessageTable), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryEnvSettingsV0>(), RESOURCE_FORMAT_BINARY, "EnvSettings", static_cast<uint32_t>(SF64::ResourceType::Environment), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryObjectInitV0>(), RESOURCE_FORMAT_BINARY, "ObjectInit", static_cast<uint32_t>(SF64::ResourceType::ObjectInit), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryHitboxV0>(), RESOURCE_FORMAT_BINARY, "Hitbox", static_cast<uint32_t>(SF64::ResourceType::Hitbox), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryScriptV0>(), RESOURCE_FORMAT_BINARY, "Script", static_cast<uint32_t>(SF64::ResourceType::Script), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryScriptCMDV0>(), RESOURCE_FORMAT_BINARY, "ScriptCMD", static_cast<uint32_t>(SF64::ResourceType::ScriptCmd), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryColPolyV0>(), RESOURCE_FORMAT_BINARY, "ColPoly", static_cast<uint32_t>(SF64::ResourceType::ColPoly), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryVec3fV0>(), RESOURCE_FORMAT_BINARY, "Vec3f", static_cast<uint32_t>(SF64::ResourceType::Vec3f), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryVec3sV0>(), RESOURCE_FORMAT_BINARY, "Vec3s", static_cast<uint32_t>(SF64::ResourceType::Vec3s), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryGenericArrayV0>(), RESOURCE_FORMAT_BINARY, "GenericArray", static_cast<uint32_t>(SF64::ResourceType::GenericArray), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryTextureV0>(), RESOURCE_FORMAT_BINARY, "Texture", static_cast<uint32_t>(LUS::ResourceType::Texture), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryTextureV1>(), RESOURCE_FORMAT_BINARY, "Texture", static_cast<uint32_t>(LUS::ResourceType::Texture), 1);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryVertexV0>(), RESOURCE_FORMAT_BINARY, "Vertex", static_cast<uint32_t>(LUS::ResourceType::Vertex), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryDisplayListV0>(), RESOURCE_FORMAT_BINARY, "DisplayList", static_cast<uint32_t>(LUS::ResourceType::DisplayList), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryMatrixV0>(), RESOURCE_FORMAT_BINARY, "Matrix", static_cast<uint32_t>(LUS::ResourceType::Matrix), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryBlobV0>(), RESOURCE_FORMAT_BINARY, "Blob", static_cast<uint32_t>(LUS::ResourceType::Blob), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryAnimV0>(), RESOURCE_FORMAT_BINARY,
"Animation", static_cast<uint32_t>(SF64::ResourceType::AnimData), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinarySkeletonV0>(), RESOURCE_FORMAT_BINARY,
"Skeleton", static_cast<uint32_t>(SF64::ResourceType::Skeleton), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryLimbV0>(), RESOURCE_FORMAT_BINARY,
"Limb", static_cast<uint32_t>(SF64::ResourceType::Limb), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryMessageV0>(), RESOURCE_FORMAT_BINARY,
"Message", static_cast<uint32_t>(SF64::ResourceType::Message), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryMessageLookupV0>(),
RESOURCE_FORMAT_BINARY, "MessageTable",
static_cast<uint32_t>(SF64::ResourceType::MessageTable), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryEnvSettingsV0>(),
RESOURCE_FORMAT_BINARY, "EnvSettings",
static_cast<uint32_t>(SF64::ResourceType::Environment), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryObjectInitV0>(), RESOURCE_FORMAT_BINARY,
"ObjectInit", static_cast<uint32_t>(SF64::ResourceType::ObjectInit), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryHitboxV0>(), RESOURCE_FORMAT_BINARY,
"Hitbox", static_cast<uint32_t>(SF64::ResourceType::Hitbox), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryScriptV0>(), RESOURCE_FORMAT_BINARY,
"Script", static_cast<uint32_t>(SF64::ResourceType::Script), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryScriptCMDV0>(), RESOURCE_FORMAT_BINARY,
"ScriptCMD", static_cast<uint32_t>(SF64::ResourceType::ScriptCmd), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryColPolyV0>(), RESOURCE_FORMAT_BINARY,
"ColPoly", static_cast<uint32_t>(SF64::ResourceType::ColPoly), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryVec3fV0>(), RESOURCE_FORMAT_BINARY,
"Vec3f", static_cast<uint32_t>(SF64::ResourceType::Vec3f), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryVec3sV0>(), RESOURCE_FORMAT_BINARY,
"Vec3s", static_cast<uint32_t>(SF64::ResourceType::Vec3s), 0);
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryGenericArrayV0>(),
RESOURCE_FORMAT_BINARY, "GenericArray",
static_cast<uint32_t>(SF64::ResourceType::GenericArray), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryTextureV0>(), RESOURCE_FORMAT_BINARY,
"Texture", static_cast<uint32_t>(LUS::ResourceType::Texture), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryTextureV1>(), RESOURCE_FORMAT_BINARY,
"Texture", static_cast<uint32_t>(LUS::ResourceType::Texture), 1);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryVertexV0>(), RESOURCE_FORMAT_BINARY,
"Vertex", static_cast<uint32_t>(LUS::ResourceType::Vertex), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryDisplayListV0>(), RESOURCE_FORMAT_BINARY,
"DisplayList", static_cast<uint32_t>(LUS::ResourceType::DisplayList), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryMatrixV0>(), RESOURCE_FORMAT_BINARY,
"Matrix", static_cast<uint32_t>(LUS::ResourceType::Matrix), 0);
loader->RegisterResourceFactory(std::make_shared<LUS::ResourceFactoryBinaryBlobV0>(), RESOURCE_FORMAT_BINARY,
"Blob", static_cast<uint32_t>(LUS::ResourceType::Blob), 0);
}
void GameEngine::Create(){
void GameEngine::Create() {
const auto instance = Instance = new GameEngine();
instance->AudioInit();
GameUI::SetupGuiElements();
@ -100,13 +122,13 @@ void GameEngine::Create(){
#endif
}
void GameEngine::Destroy(){
void GameEngine::Destroy() {
free(MemoryPool.memory);
}
bool ShouldClearTextureCacheAtEndOfFrame = false;
void GameEngine::StartFrame() const{
void GameEngine::StartFrame() const {
using Ship::KbScancode;
const int32_t dwScancode = this->context->GetWindow()->GetLastScancode();
this->context->GetWindow()->SetLastScancode(-1);
@ -118,7 +140,8 @@ void GameEngine::StartFrame() const{
ShouldClearTextureCacheAtEndOfFrame = true;
break;
}
default: break;
default:
break;
}
this->context->GetWindow()->StartFrame();
}
@ -128,13 +151,14 @@ void GameEngine::StartFrame() const{
#define NUM_AUDIO_CHANNELS 2
#define SAMPLES_PER_FRAME (SAMPLES_HIGH * NUM_AUDIO_CHANNELS * 3)
s16 audio_buffer[SAMPLES_PER_FRAME*2] = {0};
s16 audio_buffer[SAMPLES_PER_FRAME * 2] = { 0 };
extern "C" s32 audBuffer = 0;
#include <sf64audio_provisional.h>
extern "C" volatile s32 gAudioTaskCountQ;
int frames = 0;
extern "C" int countermin = 0;
void GameEngine::HandleAudioThread() {
while (audio.running) {
{
@ -147,23 +171,31 @@ void GameEngine::HandleAudioThread() {
}
}
#define AUDIO_FRAMES_PER_UPDATE (gVIsPerFrame > 0 ? gVIsPerFrame : 1 )
#define AUDIO_FRAMES_PER_UPDATE (gVIsPerFrame > 0 ? gVIsPerFrame : 1)
std::unique_lock<std::mutex> Lock(audio.mutex);
int samples_left = AudioPlayerBuffered();
u32 num_audio_samples = samples_left < AudioPlayerGetDesiredBuffered() ? 560 : 528;
for (int i = 0; i < 2; i++) {
AudioThread_CreateNextAudioBuffer(audio_buffer + i * (num_audio_samples * NUM_AUDIO_CHANNELS), num_audio_samples);
frames++;
if (frames > 60) {
countermin++;
}
AudioPlayerPlayFrame((u8 *) audio_buffer, num_audio_samples * (sizeof(int16_t) * NUM_AUDIO_CHANNELS * AUDIO_FRAMES_PER_UPDATE));
for (int i = 0; i < 2; i++) {
AudioThread_CreateNextAudioBuffer(audio_buffer + i * (num_audio_samples * NUM_AUDIO_CHANNELS),
num_audio_samples);
}
AudioPlayerPlayFrame((u8*) audio_buffer,
num_audio_samples * (sizeof(int16_t) * NUM_AUDIO_CHANNELS * AUDIO_FRAMES_PER_UPDATE));
audio.processing = false;
audio.cv_from_thread.notify_one();
}
}
void GameEngine::StartAudioFrame(){
void GameEngine::StartAudioFrame() {
{
std::unique_lock<std::mutex> Lock(audio.mutex);
audio.processing = true;
@ -171,7 +203,7 @@ void GameEngine::StartAudioFrame(){
audio.cv_to_thread.notify_one();
}
void GameEngine::EndAudioFrame(){
void GameEngine::EndAudioFrame() {
{
std::unique_lock<std::mutex> Lock(audio.mutex);
while (audio.processing) {
@ -242,7 +274,7 @@ void GameEngine::ProcessGfxCommands(Gfx* commands) {
while (time + original_fps <= next_original_frame) {
time += original_fps;
if (time != next_original_frame) {
mtx_replacements.push_back(FrameInterpolation_Interpolate((float)time / next_original_frame));
mtx_replacements.push_back(FrameInterpolation_Interpolate((float) time / next_original_frame));
} else {
mtx_replacements.emplace_back();
}
@ -269,7 +301,7 @@ void GameEngine::ProcessGfxCommands(Gfx* commands) {
last_update_rate = gVIsPerFrame;
}
uint32_t GameEngine::GetInterpolationFPS(){
uint32_t GameEngine::GetInterpolationFPS() {
if (Ship::Context::GetInstance()->GetWindow()->GetWindowBackend() == Ship::WindowBackend::FAST3D_DXGI_DX11) {
return CVarGetInteger("gInterpolationFPS", 20);
}
@ -295,7 +327,7 @@ extern "C" uint32_t GameEngine_GetSampleRate() {
return player->GetSampleRate();
}
extern "C" uint32_t GameEngine_GetSamplesPerFrame(){
extern "C" uint32_t GameEngine_GetSamplesPerFrame() {
return SAMPLES_PER_FRAME;
}
@ -312,7 +344,7 @@ extern "C" uint32_t GameEngine_GetGameVersion() {
static const char* sOtrSignature = "__OTR__";
extern "C" uint8_t GameEngine_OTRSigCheck(const char* data) {
if(data == nullptr) {
if (data == nullptr) {
return 0;
}
return strncmp(data, sOtrSignature, strlen(sOtrSignature)) == 0;
@ -343,7 +375,6 @@ struct TimedEntry {
std::vector<TimedEntry> gTimerTasks;
uint64_t Timer_GetCurrentMillis() {
return SDL_GetTicks();
}
@ -380,7 +411,7 @@ void Timer_CompleteTask(TimedEntry& task) {
extern "C" void Timer_Update() {
if(gTimerTasks.empty()) {
if (gTimerTasks.empty()) {
return;
}
@ -427,11 +458,11 @@ extern "C" uint32_t OTRGetGameRenderHeight() {
}
extern "C" int16_t OTRGetRectDimensionFromLeftEdge(float v) {
return ((int)floorf(OTRGetDimensionFromLeftEdge(v)));
return ((int) floorf(OTRGetDimensionFromLeftEdge(v)));
}
extern "C" int16_t OTRGetRectDimensionFromRightEdge(float v) {
return ((int)ceilf(OTRGetDimensionFromRightEdge(v)));
return ((int) ceilf(OTRGetDimensionFromRightEdge(v)));
}
extern "C" int32_t OTRConvertHUDXToScreenX(int32_t v) {
@ -458,19 +489,20 @@ extern "C" void* GameEngine_Malloc(size_t size) {
const auto chunk = MemoryPool.chunk;
if(size == 0) {
if (size == 0) {
return nullptr;
}
if(MemoryPool.cursor + size < MemoryPool.length) {
if (MemoryPool.cursor + size < MemoryPool.length) {
const auto res = static_cast<uint8_t*>(MemoryPool.memory) + MemoryPool.cursor;
MemoryPool.cursor += size;
//SPDLOG_INFO("Allocating {} into memory pool", size);
// SPDLOG_INFO("Allocating {} into memory pool", size);
return res;
}
MemoryPool.length += chunk;
MemoryPool.memory = MemoryPool.memory == nullptr ? malloc(MemoryPool.length) : realloc(MemoryPool.memory, MemoryPool.length);
MemoryPool.memory =
MemoryPool.memory == nullptr ? malloc(MemoryPool.length) : realloc(MemoryPool.memory, MemoryPool.length);
memset(static_cast<uint8_t*>(MemoryPool.memory) + MemoryPool.length, 0, MemoryPool.length - chunk);
SPDLOG_INFO("Memory pool resized from {} to {}", MemoryPool.length - chunk, MemoryPool.length);
return GameEngine_Malloc(size);