Don't force users to load rom from terminal or by dragging and dropping onto executable file. Instead, open a blank window and use SDL's drag&drop feature

This commit is contained in:
SimoneN64 2023-07-10 20:48:13 +02:00
parent 008f0bfb84
commit 53873c75cc
3 changed files with 169 additions and 143 deletions

View file

@ -47,7 +47,7 @@ class Emulator {
static constexpr u32 width = 400;
static constexpr u32 height = 240 * 2; // * 2 because 2 screens
ROMType romType = ROMType::None;
bool running = true;
bool running = true, romLoaded = false;
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
HttpServer httpServer;

View file

@ -90,18 +90,19 @@ void Emulator::run() {
httpServer.startHttpServer();
#endif
while (running) {
if(romLoaded) {
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
pollHttpServer();
pollHttpServer();
#endif
runFrame(); // Run 1 frame of instructions
gpu.display(); // Display graphics
runFrame(); // Run 1 frame of instructions
gpu.display(); // Display graphics
ServiceManager& srv = kernel.getServiceManager();
// Send VBlank interrupts
srv.sendGPUInterrupt(GPUInterrupt::VBlank0);
srv.sendGPUInterrupt(GPUInterrupt::VBlank1);
ServiceManager& srv = kernel.getServiceManager();
// Send VBlank interrupts
srv.sendGPUInterrupt(GPUInterrupt::VBlank0);
srv.sendGPUInterrupt(GPUInterrupt::VBlank1);
}
SDL_Event event;
while (SDL_PollEvent(&event)) {
namespace Keys = HID::Keys;
@ -113,104 +114,111 @@ void Emulator::run() {
return;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_l: srv.pressKey(Keys::A); break;
case SDLK_k: srv.pressKey(Keys::B); break;
case SDLK_o: srv.pressKey(Keys::X); break;
case SDLK_i: srv.pressKey(Keys::Y); break;
if(romLoaded) {
switch (event.key.keysym.sym) {
case SDLK_l: srv.pressKey(Keys::A); break;
case SDLK_k: srv.pressKey(Keys::B); break;
case SDLK_o: srv.pressKey(Keys::X); break;
case SDLK_i: srv.pressKey(Keys::Y); break;
case SDLK_q: srv.pressKey(Keys::L); break;
case SDLK_p: srv.pressKey(Keys::R); break;
case SDLK_q: srv.pressKey(Keys::L); break;
case SDLK_p: srv.pressKey(Keys::R); break;
case SDLK_RIGHT: srv.pressKey(Keys::Right); break;
case SDLK_LEFT: srv.pressKey(Keys::Left); break;
case SDLK_UP: srv.pressKey(Keys::Up); break;
case SDLK_DOWN: srv.pressKey(Keys::Down); break;
case SDLK_RIGHT: srv.pressKey(Keys::Right); break;
case SDLK_LEFT: srv.pressKey(Keys::Left); break;
case SDLK_UP: srv.pressKey(Keys::Up); break;
case SDLK_DOWN: srv.pressKey(Keys::Down); break;
case SDLK_w:
srv.setCirclepadY(0x9C);
keyboardAnalogY = true;
break;
case SDLK_w:
srv.setCirclepadY(0x9C);
keyboardAnalogY = true;
break;
case SDLK_a:
srv.setCirclepadX(-0x9C);
keyboardAnalogX = true;
break;
case SDLK_a:
srv.setCirclepadX(-0x9C);
keyboardAnalogX = true;
break;
case SDLK_s:
srv.setCirclepadY(-0x9C);
keyboardAnalogY = true;
break;
case SDLK_s:
srv.setCirclepadY(-0x9C);
keyboardAnalogY = true;
break;
case SDLK_d:
srv.setCirclepadX(0x9C);
keyboardAnalogX = true;
break;
case SDLK_d:
srv.setCirclepadX(0x9C);
keyboardAnalogX = true;
break;
case SDLK_RETURN: srv.pressKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.pressKey(Keys::Select); break;
case SDLK_RETURN: srv.pressKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.pressKey(Keys::Select); break;
}
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_l: srv.releaseKey(Keys::A); break;
case SDLK_k: srv.releaseKey(Keys::B); break;
case SDLK_o: srv.releaseKey(Keys::X); break;
case SDLK_i: srv.releaseKey(Keys::Y); break;
if(romLoaded) {
switch (event.key.keysym.sym) {
case SDLK_l: srv.releaseKey(Keys::A); break;
case SDLK_k: srv.releaseKey(Keys::B); break;
case SDLK_o: srv.releaseKey(Keys::X); break;
case SDLK_i: srv.releaseKey(Keys::Y); break;
case SDLK_q: srv.releaseKey(Keys::L); break;
case SDLK_p: srv.releaseKey(Keys::R); break;
case SDLK_q: srv.releaseKey(Keys::L); break;
case SDLK_p: srv.releaseKey(Keys::R); break;
case SDLK_RIGHT: srv.releaseKey(Keys::Right); break;
case SDLK_LEFT: srv.releaseKey(Keys::Left); break;
case SDLK_UP: srv.releaseKey(Keys::Up); break;
case SDLK_DOWN: srv.releaseKey(Keys::Down); break;
case SDLK_RIGHT: srv.releaseKey(Keys::Right); break;
case SDLK_LEFT: srv.releaseKey(Keys::Left); break;
case SDLK_UP: srv.releaseKey(Keys::Up); break;
case SDLK_DOWN: srv.releaseKey(Keys::Down); break;
// Err this is probably not ideal
case SDLK_w:
case SDLK_s:
srv.setCirclepadY(0);
keyboardAnalogY = false;
break;
// Err this is probably not ideal
case SDLK_w:
case SDLK_s:
srv.setCirclepadY(0);
keyboardAnalogY = false;
break;
case SDLK_a:
case SDLK_d:
srv.setCirclepadX(0);
keyboardAnalogX = false;
break;
case SDLK_a:
case SDLK_d:
srv.setCirclepadX(0);
keyboardAnalogX = false;
break;
case SDLK_RETURN: srv.releaseKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.releaseKey(Keys::Select); break;
}
break;
case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
const s32 x = event.button.x;
const s32 y = event.button.y;
// Check if touch falls in the touch screen area
if (y >= 240 && y <= 480 && x >= 40 && x < 40 + 320) {
// Convert to 3DS coordinates
u16 x_converted = static_cast<u16>(x) - 40;
u16 y_converted = static_cast<u16>(y) - 240;
srv.setTouchScreenPress(x_converted, y_converted);
} else {
srv.releaseTouchScreen();
case SDLK_RETURN: srv.releaseKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.releaseKey(Keys::Select); break;
}
}
break;
case SDL_MOUSEBUTTONDOWN:
if(romLoaded) {
if (event.button.button == SDL_BUTTON_LEFT) {
const s32 x = event.button.x;
const s32 y = event.button.y;
// Check if touch falls in the touch screen area
if (y >= 240 && y <= 480 && x >= 40 && x < 40 + 320) {
// Convert to 3DS coordinates
u16 x_converted = static_cast<u16>(x) - 40;
u16 y_converted = static_cast<u16>(y) - 240;
srv.setTouchScreenPress(x_converted, y_converted);
} else {
srv.releaseTouchScreen();
}
} else if (event.button.button == SDL_BUTTON_RIGHT) {
holdingRightClick = true;
}
} else if (event.button.button == SDL_BUTTON_RIGHT) {
holdingRightClick = true;
}
break;
}
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) {
srv.releaseTouchScreen();
} else if (event.button.button == SDL_BUTTON_RIGHT) {
holdingRightClick = false;
if(romLoaded) {
if (event.button.button == SDL_BUTTON_LEFT) {
srv.releaseTouchScreen();
} else if (event.button.button == SDL_BUTTON_RIGHT) {
holdingRightClick = false;
}
}
break;
@ -230,76 +238,90 @@ void Emulator::run() {
break;
case SDL_CONTROLLERBUTTONUP:
case SDL_CONTROLLERBUTTONDOWN: {
u32 key = 0;
case SDL_CONTROLLERBUTTONDOWN:
if(romLoaded) {
u32 key = 0;
switch (event.cbutton.button) {
case SDL_CONTROLLER_BUTTON_A: key = Keys::B; break;
case SDL_CONTROLLER_BUTTON_B: key = Keys::A; break;
case SDL_CONTROLLER_BUTTON_X: key = Keys::Y; break;
case SDL_CONTROLLER_BUTTON_Y: key = Keys::X; break;
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: key = Keys::L; break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: key = Keys::R; break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: key = Keys::Left; break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: key = Keys::Right; break;
case SDL_CONTROLLER_BUTTON_DPAD_UP: key = Keys::Up; break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: key = Keys::Down; break;
case SDL_CONTROLLER_BUTTON_BACK: key = Keys::Select; break;
case SDL_CONTROLLER_BUTTON_START: key = Keys::Start; break;
}
switch (event.cbutton.button) {
case SDL_CONTROLLER_BUTTON_A: key = Keys::B; break;
case SDL_CONTROLLER_BUTTON_B: key = Keys::A; break;
case SDL_CONTROLLER_BUTTON_X: key = Keys::Y; break;
case SDL_CONTROLLER_BUTTON_Y: key = Keys::X; break;
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: key = Keys::L; break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: key = Keys::R; break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: key = Keys::Left; break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: key = Keys::Right; break;
case SDL_CONTROLLER_BUTTON_DPAD_UP: key = Keys::Up; break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: key = Keys::Down; break;
case SDL_CONTROLLER_BUTTON_BACK: key = Keys::Select; break;
case SDL_CONTROLLER_BUTTON_START: key = Keys::Start; break;
}
if (key != 0) {
if (event.cbutton.state == SDL_PRESSED) {
srv.pressKey(key);
} else {
srv.releaseKey(key);
if (key != 0) {
if (event.cbutton.state == SDL_PRESSED) {
srv.pressKey(key);
} else {
srv.releaseKey(key);
}
}
}
}
break;
// Detect mouse motion events for gyroscope emulation
case SDL_MOUSEMOTION: {
// We use right click to indicate we want to rotate the console. If right click is not held, then this is not a gyroscope rotation
if (!holdingRightClick) break;
case SDL_MOUSEMOTION:
if(romLoaded) {
// We use right click to indicate we want to rotate the console. If right click is not held, then this is not a gyroscope rotation
if (!holdingRightClick) break;
// Relative motion since last mouse motion event
const s32 motionX = event.motion.xrel;
const s32 motionY = event.motion.yrel;
// Relative motion since last mouse motion event
const s32 motionX = event.motion.xrel;
const s32 motionY = event.motion.yrel;
// The gyroscope involves lots of weird math I don't want to bother with atm
// So up until then, we will set the gyroscope euler angles to fixed values based on the direction of the relative motion
const s32 roll = motionX > 0 ? 0x7f : -0x7f;
const s32 pitch = motionY > 0 ? 0x7f : -0x7f;
srv.setRoll(roll);
srv.setPitch(pitch);
// The gyroscope involves lots of weird math I don't want to bother with atm
// So up until then, we will set the gyroscope euler angles to fixed values based on the direction of the relative motion
const s32 roll = motionX > 0 ? 0x7f : -0x7f;
const s32 pitch = motionY > 0 ? 0x7f : -0x7f;
srv.setRoll(roll);
srv.setPitch(pitch);
}
break;
case SDL_DROPFILE: {
char *droppedDir = event.drop.file;
if(droppedDir) {
loadROM(droppedDir);
free(droppedDir);
}
}
break;
}
}
if (gameController != nullptr) {
const s16 stickX = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX);
const s16 stickY = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY);
constexpr s16 deadzone = 3276;
constexpr s16 maxValue = 0x9C;
constexpr s16 div = 0x8000 / maxValue;
if(romLoaded) {
if (gameController != nullptr) {
const s16 stickX = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX);
const s16 stickY = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY);
constexpr s16 deadzone = 3276;
constexpr s16 maxValue = 0x9C;
constexpr s16 div = 0x8000 / maxValue;
// Avoid overriding the keyboard's circlepad input
if (abs(stickX) < deadzone && !keyboardAnalogX) {
srv.setCirclepadX(0);
} else {
srv.setCirclepadX(stickX / div);
}
// Avoid overriding the keyboard's circlepad input
if (abs(stickX) < deadzone && !keyboardAnalogX) {
srv.setCirclepadX(0);
} else {
srv.setCirclepadX(stickX / div);
}
if (abs(stickY) < deadzone && !keyboardAnalogY) {
srv.setCirclepadY(0);
} else {
srv.setCirclepadY(-(stickY / div));
if (abs(stickY) < deadzone && !keyboardAnalogY) {
srv.setCirclepadY(0);
} else {
srv.setCirclepadY(-(stickY / div));
}
}
srv.updateInputs(cpu.getTicks());
}
// Update inputs in the HID module
srv.updateInputs(cpu.getTicks());
SDL_GL_SwapWindow(window);
}
}
@ -346,6 +368,7 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
romType = ROMType::None;
}
romLoaded = success;
return success;
}
@ -390,6 +413,7 @@ bool Emulator::loadELF(std::ifstream& file) {
if (entrypoint.value() & 1) {
Helpers::panic("Misaligned ELF entrypoint. TODO: Check if ELFs can boot in thumb mode");
}
return true;
}

View file

@ -5,11 +5,13 @@ int main (int argc, char *argv[]) {
emu.initGraphicsContext();
auto romPath = std::filesystem::current_path() / (argc > 1 ? argv[1] : "teapot.elf");
if (!emu.loadROM(romPath)) {
// For some reason just .c_str() doesn't show the proper path
Helpers::panic("Failed to load ROM file: %s", romPath.string().c_str());
}
if(argc > 1) {
auto romPath = std::filesystem::current_path() / argv[1];
if (!emu.loadROM(romPath)) {
// For some reason just .c_str() doesn't show the proper path
Helpers::panic("Failed to load ROM file: %s", romPath.string().c_str());
}
}
emu.run();
emu.run();
}