shrooms-vb-native/ui.c

218 lines
5.9 KiB
C
Raw Permalink 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");
2024-11-02 17:25:37 +00:00
uint8_t *rom = NULL;
long fileSize;
uint32_t romSize;
2024-11-02 17:25:37 +00:00
int result;
if (!file) {
perror("could not open file");
return -1;
}
if (fseek(file, 0, SEEK_END)) {
perror("could not seek file end");
2024-11-02 17:25:37 +00:00
goto close_file;
}
fileSize = ftell(file);
if (fileSize == -1) {
perror("could not read file size");
2024-11-02 17:25:37 +00:00
goto close_file;
}
if (fseek(file, 0, SEEK_SET)) {
perror("could not seek file start");
2024-11-02 17:25:37 +00:00
goto close_file;
}
romSize = (uint32_t) fileSize;
rom = malloc(romSize);
if (!rom) {
perror("could not allocate ROM");
2024-11-02 17:25:37 +00:00
goto close_file;
}
fread(rom, 1, romSize, file);
if (ferror(file)) {
perror("could not read file");
2024-11-02 17:25:37 +00:00
goto free_rom;
}
2024-11-02 17:25:37 +00:00
result = fclose(file);
file = NULL;
if (result) {
perror("could not close file");
2024-11-02 17:25:37 +00:00
goto free_rom;
}
emuLoadGame(&ui->emu, rom, romSize);
return 0;
2024-11-02 17:25:37 +00:00
free_rom:
free(rom);
close_file:
if (file) fclose(file);
return -1;
}
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));
}
}