lemur/ui.c

209 lines
5.8 KiB
C
Raw Normal View History

#include "audio.h"
#include "controller.h"
#include "emulation.h"
2024-10-30 03:25:26 +00:00
#include "external/tinyfiledialogs.h"
#include <stdio.h>
#include <stdlib.h>
#include "ui.h"
#include "window.h"
struct UIContext {
EmulationContext emu;
WindowContext win;
AudioContext aud;
ControllerState ctrl;
};
UIContext *uiInit() {
UIContext *ui = malloc(sizeof(UIContext));
if (emuInit(&ui->emu)) {
goto destroy_ui;
}
if (windowInit(&ui->win, "Shrooms VB")) {
goto destroy_emu;
}
if (audioInit(&ui->aud)) {
goto destroy_window;
}
ctrlInit(&ui->ctrl);
return ui;
destroy_window:
windowDestroy(&ui->win);
destroy_emu:
emuDestroy(&ui->emu);
destroy_ui:
free(ui);
return NULL;
}
void uiDestroy(UIContext *ui) {
audioDestroy(&ui->aud);
windowDestroy(&ui->win);
emuDestroy(&ui->emu);
free(ui);
}
int uiLoadGame(UIContext *ui, const char *path) {
FILE *file = fopen(path, "rb");
uint8_t *rom;
long fileSize;
uint32_t romSize;
if (!file) {
perror("could not open file");
return -1;
}
if (fseek(file, 0, SEEK_END)) {
perror("could not seek file end");
return -1;
}
fileSize = ftell(file);
if (fileSize == -1) {
perror("could not read file size");
return -1;
}
if (fseek(file, 0, SEEK_SET)) {
perror("could not seek file start");
return -1;
}
romSize = (uint32_t) fileSize;
rom = malloc(romSize);
if (!rom) {
perror("could not allocate ROM");
return -1;
}
fread(rom, 1, romSize, file);
if (ferror(file)) {
perror("could not read file");
return -1;
}
if (fclose(file)) {
perror("could not close file");
return -1;
}
emuLoadGame(&ui->emu, rom, romSize);
return 0;
}
2024-11-01 03:40:40 +00:00
static const char *MENU_ITEMS[4] = {
"File",
"Emulation",
2024-11-01 03:40:40 +00:00
"Video",
NULL
};
2024-10-30 03:25:26 +00:00
static const char *ROM_EXTENSIONS[2] = {
"*.vb",
NULL
};
2024-10-30 02:33:48 +00:00
typedef enum status_t {
status_paused,
status_running
} status_t;
2024-11-01 01:21:05 +00:00
int uiRun(UIContext *ui, bool running) {
2024-11-01 01:56:58 +00:00
static uint8_t leftEye[384*224] = {0};
static uint8_t rightEye[384*224] = {0};
2024-11-01 01:21:05 +00:00
status_t status = running ? status_running : status_paused;
2024-11-01 01:56:58 +00:00
windowUpdate(&ui->win, leftEye, rightEye);
while (1) {
struct nk_context *ctx;
SDL_Event event;
void *samples;
uint32_t bytes;
ctx = ui->win.nk;
2024-10-30 02:33:48 +00:00
if (status == status_running) {
emuTick(&ui->emu);
}
if (emuReadPixels(&ui->emu, leftEye, rightEye)) {
windowUpdate(&ui->win, leftEye, rightEye);
}
emuReadSamples(&ui->emu, &samples, &bytes);
if (bytes) {
audioUpdate(&ui->aud, samples, bytes);
}
windowDisplayBegin(&ui->win);
/* GUI */
if (windowGuiBegin(&ui->win, "Shrooms VB")) {
windowMenubarBegin(&ui->win, MENU_ITEMS);
2024-11-01 04:52:27 +00:00
if (windowMenuBegin(&ui->win, "File", 100)) {
if (windowMenuItemLabel(&ui->win, "Open ROM")) {
2024-10-30 03:25:26 +00:00
char *file = tinyfd_openFileDialog("Pick a ROM", NULL, 1, ROM_EXTENSIONS, "Virtual Boy ROM files", false);
if (file) {
uiLoadGame(ui, file);
status = status_running;
}
}
2024-11-01 04:52:27 +00:00
if (windowMenuItemLabel(&ui->win, "Quit")) {
SDL_Event QuitEvent;
QuitEvent.type = SDL_QUIT;
QuitEvent.quit.timestamp = SDL_GetTicks();
SDL_PushEvent(&QuitEvent);
}
2024-11-01 04:52:27 +00:00
windowMenuEnd(&ui->win);
}
2024-11-01 04:52:27 +00:00
if (windowMenuBegin(&ui->win, "Emulation", 100)) {
2024-10-30 02:33:48 +00:00
const char *label = status == status_paused ? "Resume" : "Pause";
2024-11-01 04:52:27 +00:00
if (windowMenuItemLabel(&ui->win, label)) {
2024-10-30 02:33:48 +00:00
if (status == status_paused)
status = status_running;
else
status = status_paused;
}
2024-11-01 04:52:27 +00:00
if (windowMenuItemLabel(&ui->win, "Reset")) {
2024-10-30 02:33:48 +00:00
emuReset(&ui->emu);
2024-11-01 01:56:58 +00:00
status = emuIsGameLoaded(&ui->emu) ? status_running : status_paused;
}
2024-11-01 04:52:27 +00:00
windowMenuEnd(&ui->win);
}
2024-11-01 04:52:27 +00:00
if (windowMenuBegin(&ui->win, "Video", 100)) {
2024-11-01 03:40:40 +00:00
float multiplier = windowGetScreenSizeMultiplier(&ui->win);
2024-11-01 04:52:27 +00:00
if (windowMenuItemLabelChecked(&ui->win, "x1", multiplier == 1.0f)) {
2024-11-01 03:40:40 +00:00
windowSetScreenSizeMultiplier(&ui->win, 1.0f);
}
2024-11-01 04:52:27 +00:00
if (windowMenuItemLabelChecked(&ui->win, "x2", multiplier == 2.0f)) {
2024-11-01 03:40:40 +00:00
windowSetScreenSizeMultiplier(&ui->win, 2.0f);
}
2024-11-01 04:52:27 +00:00
if (windowMenuItemLabelChecked(&ui->win, "x3", multiplier == 3.0f)) {
2024-11-01 03:40:40 +00:00
windowSetScreenSizeMultiplier(&ui->win, 3.0f);
}
2024-11-01 04:52:27 +00:00
if (windowMenuItemLabelChecked(&ui->win, "x4", multiplier == 4.0f)) {
2024-11-01 03:40:40 +00:00
windowSetScreenSizeMultiplier(&ui->win, 4.0f);
}
2024-11-01 04:52:27 +00:00
windowMenuEnd(&ui->win);
2024-11-01 03:40:40 +00:00
}
windowMenubarEnd(&ui->win);
}
windowGuiEnd(&ui->win);
windowDisplayEnd(&ui->win);
nk_input_begin(ctx);
while (SDL_PollEvent(&event)) {
nk_sdl_handle_event(&event);
if (event.type == SDL_QUIT) {
return 0;
}
if (event.type == SDL_KEYDOWN) {
ctrlKeyDown(&ui->ctrl, event.key.keysym.sym);
}
if (event.type == SDL_KEYUP) {
ctrlKeyUp(&ui->ctrl, event.key.keysym.sym);
}
}
nk_input_end(ctx);
emuSetKeys(&ui->emu, ctrlKeys(&ui->ctrl));
}
}