2023-10-23 19:32:30 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
#include <unordered_map>
|
2023-10-28 19:46:14 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <filesystem>
|
2023-10-23 19:32:30 +00:00
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
#include "../../ultramodern/ultra64.h"
|
|
|
|
#include "../../ultramodern/ultramodern.hpp"
|
2023-10-23 19:32:30 +00:00
|
|
|
#define SDL_MAIN_HANDLED
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "SDL.h"
|
|
|
|
#else
|
|
|
|
#include "SDL2/SDL.h"
|
|
|
|
#include "SDL2/SDL_syswm.h"
|
|
|
|
#endif
|
|
|
|
|
2023-11-05 19:34:20 +00:00
|
|
|
#include "recomp_ui.h"
|
2023-11-24 22:10:21 +00:00
|
|
|
#include "recomp_input.h"
|
2023-11-05 19:34:20 +00:00
|
|
|
|
2023-10-23 19:32:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#include "SDL_syswm.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" void init();
|
2023-11-13 00:40:02 +00:00
|
|
|
/*extern "C"*/ void start(ultramodern::WindowHandle window_handle, const ultramodern::audio_callbacks_t* audio_callbacks, const ultramodern::input_callbacks_t* input_callbacks);
|
2023-10-23 19:32:30 +00:00
|
|
|
|
|
|
|
template<typename... Ts>
|
|
|
|
void exit_error(const char* str, Ts ...args) {
|
|
|
|
// TODO pop up an error
|
|
|
|
((void)fprintf(stderr, str, args), ...);
|
|
|
|
assert(false);
|
|
|
|
std::quick_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
ultramodern::gfx_callbacks_t::gfx_data_t create_gfx() {
|
2023-11-11 22:42:07 +00:00
|
|
|
SDL_SetHint(SDL_HINT_WINDOWS_DPI_AWARENESS, "system");
|
2023-10-23 19:32:30 +00:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) > 0) {
|
|
|
|
exit_error("Failed to initialize SDL2: %s\n", SDL_GetError());
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:20:12 +00:00
|
|
|
SDL_Window* window;
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
ultramodern::WindowHandle create_window(ultramodern::gfx_callbacks_t::gfx_data_t) {
|
2023-11-11 22:42:07 +00:00
|
|
|
window = SDL_CreateWindow("Recomp", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_RESIZABLE );
|
2023-10-23 19:32:30 +00:00
|
|
|
|
|
|
|
if (window == nullptr) {
|
|
|
|
exit_error("Failed to create window: %s\n", SDL_GetError());
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SysWMinfo wmInfo;
|
|
|
|
SDL_VERSION(&wmInfo.version);
|
|
|
|
SDL_GetWindowWMInfo(window, &wmInfo);
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2023-11-13 00:40:02 +00:00
|
|
|
return ultramodern::WindowHandle{ wmInfo.info.win.window, GetCurrentThreadId() };
|
2023-10-23 19:32:30 +00:00
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
static_assert(false && "Unimplemented");
|
|
|
|
#elif defined(__linux__)
|
2023-11-13 00:40:02 +00:00
|
|
|
return ultramodern::WindowHandle{ wmInfo.info.x11.display, wmInfo.info.x11.window };
|
2023-10-23 19:32:30 +00:00
|
|
|
#else
|
|
|
|
static_assert(false && "Unimplemented");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_gfx(void*) {
|
2023-11-24 22:10:21 +00:00
|
|
|
recomp::handle_events();
|
2023-10-23 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_AudioDeviceID audio_device = 0;
|
|
|
|
static uint32_t sample_rate = 48000;
|
|
|
|
|
|
|
|
void queue_samples(int16_t* audio_data, size_t sample_count) {
|
|
|
|
// Buffer for holding the output of swapping the audio channels. This is reused across
|
|
|
|
// calls to reduce runtime allocations.
|
|
|
|
static std::vector<float> swap_buffer;
|
|
|
|
|
|
|
|
// Make sure the swap buffer is large enough to hold all the incoming audio data.
|
|
|
|
if (sample_count > swap_buffer.size()) {
|
|
|
|
swap_buffer.resize(sample_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the audio from 16-bit values to floats and swap the audio channels into the
|
|
|
|
// swap buffer to correct for the address xor caused by endianness handling.
|
|
|
|
for (size_t i = 0; i < sample_count; i += 2) {
|
|
|
|
swap_buffer[i + 0] = audio_data[i + 1] * (0.5f / 32768.0f);
|
|
|
|
swap_buffer[i + 1] = audio_data[i + 0] * (0.5f / 32768.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue the swapped audio data.
|
|
|
|
SDL_QueueAudio(audio_device, swap_buffer.data(), sample_count * sizeof(swap_buffer[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int channel_count = 2;
|
|
|
|
constexpr int bytes_per_frame = channel_count * sizeof(float);
|
|
|
|
|
|
|
|
size_t get_frames_remaining() {
|
|
|
|
constexpr float buffer_offset_frames = 1.0f;
|
|
|
|
// Get the number of remaining buffered audio bytes.
|
|
|
|
uint32_t buffered_byte_count = SDL_GetQueuedAudioSize(audio_device);
|
|
|
|
|
|
|
|
// Adjust the reported count to be some number of refreshes in the future, which helps ensure that
|
|
|
|
// there are enough samples even if the audio thread experiences a small amount of lag. This prevents
|
|
|
|
// audio popping on games that use the buffered audio byte count to determine how many samples
|
|
|
|
// to generate.
|
|
|
|
uint32_t frames_per_vi = (sample_rate / 60);
|
|
|
|
if (buffered_byte_count > (buffer_offset_frames * bytes_per_frame * frames_per_vi)) {
|
|
|
|
buffered_byte_count -= (buffer_offset_frames * bytes_per_frame * frames_per_vi);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffered_byte_count = 0;
|
|
|
|
}
|
|
|
|
// Convert from byte count to sample count.
|
|
|
|
return buffered_byte_count / bytes_per_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_frequency(uint32_t freq) {
|
|
|
|
if (audio_device != 0) {
|
|
|
|
SDL_CloseAudioDevice(audio_device);
|
|
|
|
}
|
|
|
|
SDL_AudioSpec spec_desired{
|
|
|
|
.freq = (int)freq,
|
|
|
|
.format = AUDIO_F32,
|
|
|
|
.channels = channel_count,
|
|
|
|
.silence = 0, // calculated
|
|
|
|
.samples = 0x100, // Fairly small sample count to reduce the latency of internal buffering
|
|
|
|
.padding = 0, // unused
|
|
|
|
.size = 0, // calculated
|
|
|
|
.callback = nullptr,//feed_audio, // Use a callback as QueueAudio causes popping
|
|
|
|
.userdata = nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
audio_device = SDL_OpenAudioDevice(nullptr, false, &spec_desired, nullptr, 0);
|
|
|
|
if (audio_device == 0) {
|
|
|
|
exit_error("SDL error opening audio device: %s\n", SDL_GetError());
|
|
|
|
}
|
|
|
|
SDL_PauseAudioDevice(audio_device, 0);
|
|
|
|
sample_rate = freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// Set up console output to accept UTF-8 on windows
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
|
|
|
|
|
|
// Change to a font that supports Japanese characters
|
|
|
|
CONSOLE_FONT_INFOEX cfi;
|
|
|
|
cfi.cbSize = sizeof cfi;
|
|
|
|
cfi.nFont = 0;
|
|
|
|
cfi.dwFontSize.X = 0;
|
|
|
|
cfi.dwFontSize.Y = 16;
|
|
|
|
cfi.FontFamily = FF_DONTCARE;
|
|
|
|
cfi.FontWeight = FW_NORMAL;
|
|
|
|
wcscpy_s(cfi.FaceName, L"NSimSun");
|
|
|
|
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
|
|
|
|
#else
|
|
|
|
std::setlocale(LC_ALL, "en_US.UTF-8");
|
|
|
|
#endif
|
|
|
|
|
2023-10-28 19:46:14 +00:00
|
|
|
printf("Current dir: %ls\n", std::filesystem::current_path().c_str());
|
|
|
|
|
2023-10-23 19:32:30 +00:00
|
|
|
// Initialize SDL audio.
|
|
|
|
SDL_InitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
// Pick an initial dummy sample rate; this will be set by the game later to the true sample rate.
|
|
|
|
set_frequency(sample_rate);
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
ultramodern::gfx_callbacks_t gfx_callbacks{
|
2023-10-23 19:32:30 +00:00
|
|
|
.create_gfx = create_gfx,
|
|
|
|
.create_window = create_window,
|
|
|
|
.update_gfx = update_gfx,
|
|
|
|
};
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
ultramodern::audio_callbacks_t audio_callbacks{
|
2023-10-23 19:32:30 +00:00
|
|
|
.queue_samples = queue_samples,
|
|
|
|
.get_frames_remaining = get_frames_remaining,
|
|
|
|
.set_frequency = set_frequency,
|
|
|
|
};
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
ultramodern::input_callbacks_t input_callbacks{
|
2023-12-13 07:06:56 +00:00
|
|
|
.poll_input = recomp::poll_inputs,
|
2023-11-24 22:10:21 +00:00
|
|
|
.get_input = recomp::get_n64_input,
|
2023-10-23 19:32:30 +00:00
|
|
|
};
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
ultramodern::start({}, audio_callbacks, input_callbacks, gfx_callbacks);
|
2023-10-23 19:32:30 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|