You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
4.7 KiB

/*
Copyright 2020-2021 Brad Parker
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <iostream>
#include "core.h"
void Core::unload() {
if(m_isRunning) {
m_isRunning = false;
std::cout << "retro_unload_game";
m_retroUnloadGame();
std::cout << "." << std::endl;
}
if(m_isInited) {
m_isInited = false;
std::cout << "retro_deinit";
m_retroDeinit();
std::cout << "." << std::endl;
}
if(m_isLoaded) {
m_isLoaded = false;
if(m_library.isLoaded()) {
std::cout << "a library is loaded" << std::endl;
if(!m_library.unload()) {
std::cerr << "could not unload library" << std::endl;
}else{
std::cout << "unloaded library" << std::endl;
}
}else{
std::cerr << "library not loaded" << std::endl;
}
}
}
bool Core::load(QString corePath) {
m_library.setFileName(QFileInfo(corePath).absoluteFilePath());
std::cout << "loading library: " << qUtf8Printable(QFileInfo(m_library.fileName()).absoluteFilePath()) << std::endl;
if (!m_library.load()) {
std::cerr << "library could not be loaded: " << qUtf8Printable(m_library.errorString()) << std::endl;
return false;
}else{
std::cout << "library was loaded successfully" << std::endl;
}
m_isLoaded = true;
#define resolv(name, ptr, sym) \
if(!(ptr = reinterpret_cast<name>(m_library.resolve(sym)))) { \
std::cerr << "could not resolve " #sym " function" << std::endl; \
return false; \
}
resolv(RetroInit, m_retroInit, "retro_init");
resolv(RetroDeinit, m_retroDeinit, "retro_deinit");
resolv(RetroRun, m_retroRun, "retro_run");
resolv(RetroReset, m_retroReset, "retro_reset");
resolv(RetroLoadGame, m_retroLoadGame, "retro_load_game");
resolv(RetroUnloadGame, m_retroUnloadGame, "retro_unload_game");
resolv(RetroGetMemoryData, m_retroGetMemoryData, "retro_get_memory_data");
resolv(RetroGetMemorySize, m_retroGetMemorySize, "retro_get_memory_size");
resolv(RetroSerialize, m_retroSerialize, "retro_serialize");
resolv(RetroSerializeSize, m_retroSerializeSize, "retro_serialize_size");
resolv(RetroUnserialize, m_retroUnserialize, "retro_unserialize");
resolv(RetroGetSystemAVInfo, m_retroGetSystemAVInfo, "retro_get_system_av_info");
resolv(RetroGetSystemInfo, m_retroGetSystemInfo, "retro_get_system_info");
resolv(RetroSetControllerPortDevice, m_retroSetControllerPortDevice, "retro_set_controller_port_device");
resolv(RetroSetEnvironment, m_retroSetEnvironment, "retro_set_environment");
resolv(RetroSetVideoRefresh, m_retroSetVideoRefresh, "retro_set_video_refresh");
resolv(RetroSetAudioSample, m_retroSetAudioSample, "retro_set_audio_sample");
resolv(RetroSetAudioSampleBatch, m_retroSetAudioSampleBatch, "retro_set_audio_sample_batch");
resolv(RetroSetInputPoll, m_retroSetInputPoll, "retro_set_input_poll");
resolv(RetroSetInputState, m_retroSetInputState, "retro_set_input_state");
m_isResolved = true;
std::cout << "resolved necessary functions" << std::endl;
std::cout << "setting callback functions" << std::endl;
retro_environment_t environmentCallback = [](unsigned cmd, void *data) -> bool { return Core::instance()->environment(cmd, data); };
retro_video_refresh_t videoRefreshCallback = [](const void *data, unsigned width, unsigned height, size_t pitch) { return Core::instance()->videoRefresh(data, width, height, pitch); };
retro_audio_sample_t audioSampleCallback = [](int16_t left, int16_t right) { return Core::instance()->audioSample(left, right); };
retro_audio_sample_batch_t audioSampleBatchCallback = [](const int16_t *data, size_t frames) { return Core::instance()->audioSampleBatch(data, frames); };
retro_input_poll_t inputPollCallback = []() { return Core::instance()->inputPoll(); };
retro_input_state_t inputStateCallback = [](unsigned port, unsigned device, unsigned index, unsigned id) { return Core::instance()->inputState(port, device, index, id); };
m_retroSetEnvironment(environmentCallback);
m_retroSetVideoRefresh(videoRefreshCallback);
m_retroSetAudioSample(audioSampleCallback);
m_retroSetAudioSampleBatch(audioSampleBatchCallback);
m_retroSetInputPoll(inputPollCallback);
m_retroSetInputState(inputStateCallback);
std::cout << "retro_init." << std::endl;
m_retroInit();
m_isInited = true;
return true;
}