Skip to content

Commit

Permalink
Libretro: Complete code formatting (wheremyfoodat#594)
Browse files Browse the repository at this point in the history
* Libretro: Optimize range settings, fix default values

* Libretro: More code formatting

* Libretro: Fix loading of archived roms
  • Loading branch information
jonian authored Sep 5, 2024
1 parent 656b7da commit f1b7830
Showing 1 changed file with 90 additions and 75 deletions.
165 changes: 90 additions & 75 deletions src/libretro_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include <emulator.hpp>
#include <renderer_gl/renderer_gl.hpp>

static retro_environment_t envCallbacks;
static retro_video_refresh_t videoCallbacks;
static retro_environment_t envCallback;
static retro_video_refresh_t videoCallback;
static retro_audio_sample_batch_t audioBatchCallback;
static retro_input_poll_t inputPollCallback;
static retro_input_state_t inputStateCallback;

static retro_hw_render_callback hw_render;
static retro_hw_render_callback hwRender;
static std::filesystem::path savePath;

static bool screenTouched;
Expand All @@ -30,49 +30,49 @@ std::filesystem::path Emulator::getAppDataRoot() {
return std::filesystem::path(savePath / "Emulator Files");
}

static void* GetGLProcAddress(const char* name) {
return (void*)hw_render.get_proc_address(name);
static void* getGLProcAddress(const char* name) {
return (void*)hwRender.get_proc_address(name);
}

static void VideoResetContext() {
static void videoResetContext() {
#ifdef USING_GLES
if (!gladLoadGLES2Loader(reinterpret_cast<GLADloadproc>(GetGLProcAddress))) {
if (!gladLoadGLES2Loader(reinterpret_cast<GLADloadproc>(getGLProcAddress))) {
Helpers::panic("OpenGL ES init failed");
}
#else
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(GetGLProcAddress))) {
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(getGLProcAddress))) {
Helpers::panic("OpenGL init failed");
}
#endif

emulator->initGraphicsContext(nullptr);
}

static void VideoDestroyContext() {
emulator->deinitGraphicsContext();
static void videoDestroyContext() {
emulator->deinitGraphicsContext();
}

static bool SetHWRender(retro_hw_context_type type) {
hw_render.context_type = type;
hw_render.context_reset = VideoResetContext;
hw_render.context_destroy = VideoDestroyContext;
hw_render.bottom_left_origin = true;
static bool setHWRender(retro_hw_context_type type) {
hwRender.context_type = type;
hwRender.context_reset = videoResetContext;
hwRender.context_destroy = videoDestroyContext;
hwRender.bottom_left_origin = true;

switch (type) {
case RETRO_HW_CONTEXT_OPENGL_CORE:
hw_render.version_major = 4;
hw_render.version_minor = 1;
hwRender.version_major = 4;
hwRender.version_minor = 1;

if (envCallbacks(RETRO_ENVIRONMENT_SET_HW_RENDER, &hw_render)) {
if (envCallback(RETRO_ENVIRONMENT_SET_HW_RENDER, &hwRender)) {
return true;
}
break;
case RETRO_HW_CONTEXT_OPENGLES3:
case RETRO_HW_CONTEXT_OPENGL:
hw_render.version_major = 3;
hw_render.version_minor = 1;
hwRender.version_major = 3;
hwRender.version_minor = 1;

if (envCallbacks(RETRO_ENVIRONMENT_SET_HW_RENDER, &hw_render)) {
if (envCallback(RETRO_ENVIRONMENT_SET_HW_RENDER, &hwRender)) {
return true;
}
break;
Expand All @@ -84,18 +84,18 @@ static bool SetHWRender(retro_hw_context_type type) {

static void videoInit() {
retro_hw_context_type preferred = RETRO_HW_CONTEXT_NONE;
envCallbacks(RETRO_ENVIRONMENT_GET_PREFERRED_HW_RENDER, &preferred);
envCallback(RETRO_ENVIRONMENT_GET_PREFERRED_HW_RENDER, &preferred);

if (preferred && SetHWRender(preferred)) return;
if (SetHWRender(RETRO_HW_CONTEXT_OPENGL_CORE)) return;
if (SetHWRender(RETRO_HW_CONTEXT_OPENGL)) return;
if (SetHWRender(RETRO_HW_CONTEXT_OPENGLES3)) return;
if (preferred && setHWRender(preferred)) return;
if (setHWRender(RETRO_HW_CONTEXT_OPENGL_CORE)) return;
if (setHWRender(RETRO_HW_CONTEXT_OPENGL)) return;
if (setHWRender(RETRO_HW_CONTEXT_OPENGLES3)) return;

hw_render.context_type = RETRO_HW_CONTEXT_NONE;
hwRender.context_type = RETRO_HW_CONTEXT_NONE;
}

static bool GetButtonState(uint id) { return inputStateCallback(0, RETRO_DEVICE_JOYPAD, 0, id); }
static float GetAxisState(uint index, uint id) { return inputStateCallback(0, RETRO_DEVICE_ANALOG, index, id); }
static bool getButtonState(uint id) { return inputStateCallback(0, RETRO_DEVICE_JOYPAD, 0, id); }
static float getAxisState(uint index, uint id) { return inputStateCallback(0, RETRO_DEVICE_ANALOG, index, id); }

static void inputInit() {
static const retro_controller_description controllers[] = {
Expand All @@ -108,7 +108,7 @@ static void inputInit() {
{NULL, 0},
};

envCallbacks(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
envCallback(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);

retro_input_descriptor desc[] = {
{0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left"},
Expand All @@ -128,28 +128,43 @@ static void inputInit() {
{0},
};

envCallbacks(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, &desc);
envCallback(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, &desc);
}

static std::string FetchVariable(std::string key, std::string def) {
static std::string fetchVariable(std::string key, std::string def) {
retro_variable var = {nullptr};
var.key = key.c_str();

if (!envCallbacks(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value == nullptr) {
if (!envCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value == nullptr) {
Helpers::warn("Fetching variable %s failed.", key.c_str());
return def;
}

return std::string(var.value);
}

static bool FetchVariableBool(std::string key, bool def) {
return FetchVariable(key, def ? "enabled" : "disabled") == "enabled";
static int fetchVariableInt(std::string key, int def) {
std::string value = fetchVariable(key, std::to_string(def));

if (!value.empty() && std::isdigit(value[0])) {
return std::stoi(value);
}

return 0;
}

static bool fetchVariableBool(std::string key, bool def) {
return fetchVariable(key, def ? "enabled" : "disabled") == "enabled";
}

static int fetchVariableRange(std::string key, int min, int max) {
return std::clamp(fetchVariableInt(key, min), min, max);
}

static void configInit() {
static const retro_variable values[] = {
{"panda3ds_use_shader_jit", "Enable shader JIT; enabled|disabled"},
{"panda3ds_use_shader_jit", EmulatorConfig::shaderJitDefault ? "Enable shader JIT; enabled|disabled"
: "Enable shader JIT; disabled|enabled"},
{"panda3ds_accurate_shader_mul", "Enable accurate shader multiplication; disabled|enabled"},
{"panda3ds_use_ubershader", EmulatorConfig::ubershaderDefault ? "Use ubershaders (No stutter, maybe slower); enabled|disabled"
: "Use ubershaders (No stutter, maybe slower); disabled|enabled"},
Expand All @@ -165,33 +180,33 @@ static void configInit() {
{nullptr, nullptr},
};

envCallbacks(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)values);
envCallback(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)values);
}

static void configUpdate() {
EmulatorConfig& config = emulator->getConfig();

config.rendererType = RendererType::OpenGL;
config.vsyncEnabled = FetchVariableBool("panda3ds_use_vsync", true);
config.shaderJitEnabled = FetchVariableBool("panda3ds_use_shader_jit", true);
config.chargerPlugged = FetchVariableBool("panda3ds_use_charger", true);
config.batteryPercentage = std::clamp(std::stoi(FetchVariable("panda3ds_battery_level", "5")), 0, 100);
config.dspType = Audio::DSPCore::typeFromString(FetchVariable("panda3ds_dsp_emulation", "null"));
config.audioEnabled = FetchVariableBool("panda3ds_use_audio", false);
config.sdCardInserted = FetchVariableBool("panda3ds_use_virtual_sd", true);
config.sdWriteProtected = FetchVariableBool("panda3ds_write_protect_virtual_sd", false);
config.accurateShaderMul = FetchVariableBool("panda3ds_accurate_shader_mul", false);
config.useUbershaders = FetchVariableBool("panda3ds_use_ubershader", true);
config.forceShadergenForLights = FetchVariableBool("panda3ds_ubershader_lighting_override", true);
config.lightShadergenThreshold = std::clamp(std::stoi(FetchVariable("panda3ds_ubershader_lighting_override_threshold", "1")), 1, 8);
config.vsyncEnabled = fetchVariableBool("panda3ds_use_vsync", true);
config.shaderJitEnabled = fetchVariableBool("panda3ds_use_shader_jit", EmulatorConfig::shaderJitDefault);
config.chargerPlugged = fetchVariableBool("panda3ds_use_charger", true);
config.batteryPercentage = fetchVariableRange("panda3ds_battery_level", 5, 100);
config.dspType = Audio::DSPCore::typeFromString(fetchVariable("panda3ds_dsp_emulation", "null"));
config.audioEnabled = fetchVariableBool("panda3ds_use_audio", false);
config.sdCardInserted = fetchVariableBool("panda3ds_use_virtual_sd", true);
config.sdWriteProtected = fetchVariableBool("panda3ds_write_protect_virtual_sd", false);
config.accurateShaderMul = fetchVariableBool("panda3ds_accurate_shader_mul", false);
config.useUbershaders = fetchVariableBool("panda3ds_use_ubershader", EmulatorConfig::ubershaderDefault);
config.forceShadergenForLights = fetchVariableBool("panda3ds_ubershader_lighting_override", true);
config.lightShadergenThreshold = fetchVariableRange("panda3ds_ubershader_lighting_override_threshold", 1, 8);
config.discordRpcEnabled = false;

config.save();
}

static void ConfigCheckVariables() {
static void configCheckVariables() {
bool updated = false;
envCallbacks(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated);
envCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated);

if (updated) {
configUpdate();
Expand All @@ -203,7 +218,7 @@ void retro_get_system_info(retro_system_info* info) {
info->valid_extensions = "3ds|3dsx|elf|axf|cci|cxi|app";
info->library_version = PANDA3DS_VERSION;
info->library_name = "Panda3DS";
info->block_extract = true;
info->block_extract = false;
}

void retro_get_system_av_info(retro_system_av_info* info) {
Expand All @@ -219,11 +234,11 @@ void retro_get_system_av_info(retro_system_av_info* info) {
}

void retro_set_environment(retro_environment_t cb) {
envCallbacks = cb;
envCallback = cb;
}

void retro_set_video_refresh(retro_video_refresh_t cb) {
videoCallbacks = cb;
videoCallback = cb;
}

void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) {
Expand All @@ -242,15 +257,15 @@ void retro_set_input_state(retro_input_state_t cb) {

void retro_init() {
enum retro_pixel_format xrgb888 = RETRO_PIXEL_FORMAT_XRGB8888;
envCallbacks(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &xrgb888);
envCallback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &xrgb888);

char* save_dir = nullptr;
char* saveDir = nullptr;

if (!envCallbacks(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_dir) || save_dir == nullptr) {
if (!envCallback(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &saveDir) || saveDir == nullptr) {
Helpers::warn("No save directory provided by LibRetro.");
savePath = std::filesystem::current_path();
} else {
savePath = std::filesystem::path(save_dir);
savePath = std::filesystem::path(saveDir);
}

emulator = std::make_unique<Emulator>();
Expand Down Expand Up @@ -289,31 +304,31 @@ void retro_reset() {
}

void retro_run() {
ConfigCheckVariables();
configCheckVariables();

renderer->setFBO(hw_render.get_current_framebuffer());
renderer->setFBO(hwRender.get_current_framebuffer());
renderer->resetStateManager();

inputPollCallback();

HIDService& hid = emulator->getServiceManager().getHID();

hid.setKey(HID::Keys::A, GetButtonState(RETRO_DEVICE_ID_JOYPAD_A));
hid.setKey(HID::Keys::B, GetButtonState(RETRO_DEVICE_ID_JOYPAD_B));
hid.setKey(HID::Keys::X, GetButtonState(RETRO_DEVICE_ID_JOYPAD_X));
hid.setKey(HID::Keys::Y, GetButtonState(RETRO_DEVICE_ID_JOYPAD_Y));
hid.setKey(HID::Keys::L, GetButtonState(RETRO_DEVICE_ID_JOYPAD_L));
hid.setKey(HID::Keys::R, GetButtonState(RETRO_DEVICE_ID_JOYPAD_R));
hid.setKey(HID::Keys::Start, GetButtonState(RETRO_DEVICE_ID_JOYPAD_START));
hid.setKey(HID::Keys::Select, GetButtonState(RETRO_DEVICE_ID_JOYPAD_SELECT));
hid.setKey(HID::Keys::Up, GetButtonState(RETRO_DEVICE_ID_JOYPAD_UP));
hid.setKey(HID::Keys::Down, GetButtonState(RETRO_DEVICE_ID_JOYPAD_DOWN));
hid.setKey(HID::Keys::Left, GetButtonState(RETRO_DEVICE_ID_JOYPAD_LEFT));
hid.setKey(HID::Keys::Right, GetButtonState(RETRO_DEVICE_ID_JOYPAD_RIGHT));
hid.setKey(HID::Keys::A, getButtonState(RETRO_DEVICE_ID_JOYPAD_A));
hid.setKey(HID::Keys::B, getButtonState(RETRO_DEVICE_ID_JOYPAD_B));
hid.setKey(HID::Keys::X, getButtonState(RETRO_DEVICE_ID_JOYPAD_X));
hid.setKey(HID::Keys::Y, getButtonState(RETRO_DEVICE_ID_JOYPAD_Y));
hid.setKey(HID::Keys::L, getButtonState(RETRO_DEVICE_ID_JOYPAD_L));
hid.setKey(HID::Keys::R, getButtonState(RETRO_DEVICE_ID_JOYPAD_R));
hid.setKey(HID::Keys::Start, getButtonState(RETRO_DEVICE_ID_JOYPAD_START));
hid.setKey(HID::Keys::Select, getButtonState(RETRO_DEVICE_ID_JOYPAD_SELECT));
hid.setKey(HID::Keys::Up, getButtonState(RETRO_DEVICE_ID_JOYPAD_UP));
hid.setKey(HID::Keys::Down, getButtonState(RETRO_DEVICE_ID_JOYPAD_DOWN));
hid.setKey(HID::Keys::Left, getButtonState(RETRO_DEVICE_ID_JOYPAD_LEFT));
hid.setKey(HID::Keys::Right, getButtonState(RETRO_DEVICE_ID_JOYPAD_RIGHT));

// Get analog values for the left analog stick (Right analog stick is N3DS-only and unimplemented)
float xLeft = GetAxisState(RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
float yLeft = GetAxisState(RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
float xLeft = getAxisState(RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
float yLeft = getAxisState(RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);

hid.setCirclepadX((xLeft / +32767) * 0x9C);
hid.setCirclepadY((yLeft / -32767) * 0x9C);
Expand Down Expand Up @@ -351,7 +366,7 @@ void retro_run() {
hid.updateInputs(emulator->getTicks());
emulator->runFrame();

videoCallbacks(RETRO_HW_FRAME_BUFFER_VALID, emulator->width, emulator->height, 0);
videoCallback(RETRO_HW_FRAME_BUFFER_VALID, emulator->width, emulator->height, 0);
}

void retro_set_controller_port_device(uint port, uint device) {}
Expand Down

0 comments on commit f1b7830

Please sign in to comment.