diff options
| author | KiritoDv <kiritodev01@gmail.com> | 2024-05-01 10:09:49 -0600 |
|---|---|---|
| committer | KiritoDv <kiritodev01@gmail.com> | 2024-05-01 10:09:49 -0600 |
| commit | a2c3feb67a2f8afb1cebd0e77e4a3e23db2c28c4 (patch) | |
| tree | 4250a53478f87b8b07e16481a634ef16550ddb43 /src/port/Engine.cpp | |
| parent | d5264825f0a7ab67f53fca8857cbd24dd86ee86d (diff) | |
WIP Compilation
Diffstat (limited to 'src/port/Engine.cpp')
| -rw-r--r-- | src/port/Engine.cpp | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/port/Engine.cpp b/src/port/Engine.cpp new file mode 100644 index 000000000..f3466be60 --- /dev/null +++ b/src/port/Engine.cpp @@ -0,0 +1,194 @@ +#include "Engine.h" +#include "ui/ImguiUI.h" +#include "ZAPDUtils/Utils/StringHelper.h" +#include "libultraship/src/Context.h" +#include "resource/type/ResourceType.h" +#include "resource/importers/GenericArrayFactory.h" +#include "resource/importers/Vec3fFactory.h" +#include "resource/importers/Vec3sFactory.h" + +#include <Fast3D/gfx_pc.h> +#include <Fast3D/gfx_rendering_api.h> +#include <SDL2/SDL.h> + +#include <utility> + +extern "C" { +float gInterpolationStep = 0.0f; +#include <macros.h> +} + +GameEngine* GameEngine::Instance; + +GameEngine::GameEngine() { + std::vector<std::string> OTRFiles; + if (const std::string cube_path = LUS::Context::GetPathRelativeToAppDirectory("sp.otr"); std::filesystem::exists(cube_path)) { + OTRFiles.push_back(cube_path); + } + if (const std::string sm64_otr_path = LUS::Context::GetPathRelativeToAppBundle("sm64.otr"); std::filesystem::exists(sm64_otr_path)) { + OTRFiles.push_back(sm64_otr_path); + } + if (const std::string patches_path = LUS::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)) { + if (StringHelper::IEquals(p.path().extension().string(), ".otr")) { + OTRFiles.push_back(p.path().generic_string()); + } + } + } + } + + this->context = LUS::Context::CreateInstance("Spaghettify", "skart64", "spaghettify.cfg.json", OTRFiles, {}, 3); + + auto loader = context->GetResourceManager()->GetResourceLoader(); + 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); +} + +void GameEngine::Create(){ + const auto instance = Instance = new GameEngine(); + GameUI::SetupGuiElements(); +#if defined(__SWITCH__) || defined(__WIIU__) + CVarRegisterInteger("gControlNav", 1); // always enable controller nav on switch/wii u +#endif +} + +void GameEngine::Destroy(){ + +} + +bool ShouldClearTextureCacheAtEndOfFrame = false; + +void GameEngine::StartFrame() const{ + using LUS::KbScancode; + const int32_t dwScancode = this->context->GetWindow()->GetLastScancode(); + this->context->GetWindow()->SetLastScancode(-1); + + switch (dwScancode) { + case KbScancode::LUS_KB_TAB: { + // Toggle HD Assets + CVarSetInteger("gAltAssets", !CVarGetInteger("gAltAssets", 0)); + ShouldClearTextureCacheAtEndOfFrame = true; + break; + } + default: break; + } + this->context->GetWindow()->StartFrame(); +} + +void GameEngine::ProcessFrame(void (*run_one_game_iter)()) const { + this->context->GetWindow()->MainLoop(run_one_game_iter); +} + +void GameEngine::RunCommands(Gfx* Commands) { + gfx_run(Commands, {}); + gfx_end_frame(); + + if (ShouldClearTextureCacheAtEndOfFrame) { + gfx_texture_cache_clear(); + ShouldClearTextureCacheAtEndOfFrame = false; + } +} + +void GameEngine::ProcessGfxCommands(Gfx* commands) { + RunCommands(commands); + Instance->context->GetWindow()->SetTargetFps(30); + Instance->context->GetWindow()->SetMaximumFrameLatency(1); +} + +extern "C" uint32_t GameEngine_GetSampleRate() { + auto player = LUS::Context::GetInstance()->GetAudio()->GetAudioPlayer(); + if (player == nullptr) { + return 0; + } + + if (!player->IsInitialized()) { + return 0; + } + + return player->GetSampleRate(); +} + +extern "C" uint32_t GameEngine_GetSamplesPerFrame(){ + return SAMPLES_PER_FRAME; +} + +// End + +extern "C" float GameEngine_GetAspectRatio() { + return gfx_current_dimensions.aspect_ratio; +} + +extern "C" uint32_t GameEngine_GetGameVersion() { + return 0x00000001; +} + +static const char* sOtrSignature = "__OTR__"; + +extern "C" uint8_t GameEngine_OTRSigCheck(const char* data) { + if(data == nullptr) { + return 0; + } + return strncmp(data, sOtrSignature, strlen(sOtrSignature)) == 0; +} + +// struct TimedEntry { +// uint64_t duration; +// TimerAction action; +// int32_t* address; +// int32_t value; +// bool active; +// }; + +// std::vector<TimedEntry> gTimerTasks; + + +// uint64_t Timer_GetCurrentMillis() { +// return SDL_GetTicks(); +// } + +// extern "C" s32 Timer_CreateTask(u64 time, TimerAction action, s32* address, s32 value) { +// const auto millis = Timer_GetCurrentMillis(); +// TimedEntry entry = { +// .duration = millis + CYCLES_TO_MSEC_PC(time), +// .action = action, +// .address = address, +// .value = value, +// .active = true, +// }; + +// gTimerTasks.push_back(entry); + +// return gTimerTasks.size() - 1; +// } + +extern "C" void Timer_Increment(int32_t* address, int32_t value) { + *address += value; +} + +extern "C" void Timer_SetValue(int32_t* address, int32_t value) { + *address = value; +} + +// void Timer_CompleteTask(TimedEntry& task) { +// if (task.action != nullptr) { +// task.action(task.address, task.value); +// } +// task.active = false; +// } + +// extern "C" void Timer_Update() { + +// if(gTimerTasks.empty()) { +// return; +// } + +// const auto millis = Timer_GetCurrentMillis(); + +// for (auto& task : gTimerTasks) { +// if (task.active && millis >= task.duration) { +// Timer_CompleteTask(task); +// } +// } +// } |
