yet fewer global static vars - just the ones for libmgba callbacks

This commit is contained in:
lif 2024-12-16 01:33:41 -08:00
parent fc9acf32d6
commit 1a331e8853
1 changed files with 65 additions and 61 deletions

126
main.c
View File

@ -33,22 +33,25 @@
#error "unknown pixel format"
#endif
static SDL_Texture* textures[NUM_CHANNELS] = { NULL, NULL, NULL };
static bool texture_updated = false;
static SDL_CameraID front_camera = 0;
static SDL_CameraID back_camera = 0;
static SDL_Surface* scaled = NULL;
static SDL_Surface* filtered[NUM_CHANNELS] = { NULL, NULL, NULL };
static SDL_Surface* screens[NUM_CHANNELS] = { NULL, NULL, NULL };
static struct mCore* cores[NUM_CHANNELS] = { NULL, NULL, NULL };
static unsigned keys = 0;
struct AppState {
SDL_Camera* camera;
SDL_Window* window;
SDL_Renderer* renderer;
int cam_idx, spec_idx;
SDL_CameraID front_camera;
SDL_CameraID back_camera;
bool texture_updated;
SDL_Texture* textures[NUM_CHANNELS];
SDL_Surface* screens[NUM_CHANNELS];
struct mCore* cores[NUM_CHANNELS];
unsigned keys;
};
enum mColorFormat pixfmt_sdl_to_mgba(uint32_t sdl_fmt) {
@ -190,10 +193,10 @@ bool SelectCamera(struct AppState* st) {
}
if (position == SDL_CAMERA_POSITION_FRONT_FACING) {
front_camera = device;
st->front_camera = device;
posstr = "[frontfacing]";
} else if (position == SDL_CAMERA_POSITION_BACK_FACING) {
back_camera = device;
st->back_camera = device;
posstr = "[backfacing]";
}
@ -295,11 +298,11 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char *argv[]) {
mCoreLoadConfig(core);
cores[i] = core;
st->cores[i] = core;
}
/* without loss of generality */
cores[0]->desiredVideoDimensions(cores[0], &w, &h);
st->cores[0]->desiredVideoDimensions(st->cores[0], &w, &h);
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA)) {
SDL_Log("Couldn't initialize SDL3: %s", SDL_GetError());
@ -322,21 +325,21 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char *argv[]) {
for (i = 0; i < NUM_CHANNELS; i++) {
int j;
struct mCore* core = cores[i];
struct mCore* core = st->cores[i];
SDL_Texture* texture;
SDL_Surface* screen = SDL_CreateSurface(w, h, SCREEN_FMT);
core->setVideoBuffer(core, screen->pixels, screen->pitch / BYTES_PER_PIXEL);
screens[i] = screen;
st->screens[i] = screen;
/* Create texture with appropriate format */
texture = SDL_CreateTextureFromSurface(st->renderer, screens[0]);
texture = SDL_CreateTextureFromSurface(st->renderer, st->screens[0]);
if (!texture) {
SDL_Log("Couldn't create texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
textures[i] = texture;
st->textures[i] = texture;
core->setPeripheral(core, mPERIPH_IMAGE_SOURCE, &gb_img_src[i]);
@ -368,9 +371,9 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char *argv[]) {
SDL_Log("prepared core %d", i);
}
SDL_SetTextureColorMod(textures[0], 255, 0, 0);
SDL_SetTextureColorMod(textures[1], 0, 255, 0);
SDL_SetTextureColorMod(textures[2], 0, 0, 255);
SDL_SetTextureColorMod(st->textures[0], 255, 0, 0);
SDL_SetTextureColorMod(st->textures[1], 0, 255, 0);
SDL_SetTextureColorMod(st->textures[2], 0, 0, 255);
return SDL_APP_CONTINUE; /* start the main app loop. */
}
@ -384,10 +387,10 @@ static SDL_AppResult FlipCamera(struct AppState* st) {
if (st->camera) {
const SDL_CameraID current = SDL_GetCameraID(st->camera);
SDL_CameraID nextcam = 0;
if (current == front_camera) {
nextcam = back_camera;
} else if (current == back_camera) {
nextcam = front_camera;
if (current == st->front_camera) {
nextcam = st->back_camera;
} else if (current == st->back_camera) {
nextcam = st->front_camera;
}
if (nextcam) {
@ -446,14 +449,14 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
switch (sym) {
case SDLK_ESCAPE: case SDLK_AC_BACK: return SDL_APP_SUCCESS;
case SDLK_SPACE: FlipCamera(st); break;
case SDLK_UP: keys |= 0x40; break;
case SDLK_DOWN: keys |= 0x80; break;
case SDLK_LEFT: keys |= 0x20; break;
case SDLK_RIGHT: keys |= 0x10; break;
case SDLK_A: case SDLK_Z: keys |= 1; break;
case SDLK_B: case SDLK_X: keys |= 2; break;
case SDLK_RETURN: keys |= 8; break;
case SDLK_BACKSPACE: keys |= 4; break;
case SDLK_UP: st->keys |= 0x40; break;
case SDLK_DOWN: st->keys |= 0x80; break;
case SDLK_LEFT: st->keys |= 0x20; break;
case SDLK_RIGHT: st->keys |= 0x10; break;
case SDLK_A: case SDLK_Z: st->keys |= 1; break;
case SDLK_B: case SDLK_X: st->keys |= 2; break;
case SDLK_RETURN: st->keys |= 8; break;
case SDLK_BACKSPACE: st->keys |= 4; break;
}
break;
}
@ -461,14 +464,14 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
case SDL_EVENT_KEY_UP: {
const SDL_Keycode sym = event->key.key;
switch (sym) {
case SDLK_UP: keys &= ~0x40; break;
case SDLK_DOWN: keys &= ~0x80; break;
case SDLK_LEFT: keys &= ~0x20; break;
case SDLK_RIGHT: keys &= ~0x10; break;
case SDLK_A: case SDLK_Z: keys &= ~1; break;
case SDLK_B: case SDLK_X: keys &= ~2; break;
case SDLK_RETURN: keys &= ~8; break;
case SDLK_BACKSPACE: keys &= ~4; break;
case SDLK_UP: st->keys &= ~0x40; break;
case SDLK_DOWN: st->keys &= ~0x80; break;
case SDLK_LEFT: st->keys &= ~0x20; break;
case SDLK_RIGHT: st->keys &= ~0x10; break;
case SDLK_A: case SDLK_Z: st->keys &= ~1; break;
case SDLK_B: case SDLK_X: st->keys &= ~2; break;
case SDLK_RETURN: st->keys &= ~8; break;
case SDLK_BACKSPACE: st->keys &= ~4; break;
}
break;
}
@ -495,10 +498,10 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
SDL_Log("Camera denied!");
return SDL_APP_FAILURE;
/*
case SDL_EVENT_WINDOW_RESIZED: {
st->texture_updated = false;
/*
Sint32 winw = event->window.data1, winh = event->window.data2;
SDL_UpdateWindowSurface(window);
if (TODO: need to correct aspect ratio?) {
if (!SDL_SetWindowSize(window, winw/2, winh)) {
SDL_Log("Failed to resize window to %dx%d: %s", winw, winh, SDL_GetError());
@ -507,9 +510,9 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
SDL_Log("Resized window to %dx%d", winw, winh);
}
}
*/
break;
}
*/
default:
break;
@ -530,7 +533,7 @@ SDL_AppResult SDL_AppIterate(void* appstate) {
}
SDL_Renderer* renderer = st->renderer;
int i;
static int since_last_present = 0;
@ -545,7 +548,7 @@ SDL_AppResult SDL_AppIterate(void* appstate) {
int w = srcrect.h * dstrect.w / dstrect.h;
/*SDL_Log("new frame %d x %d %s", frame->w, frame->h, SDL_GetPixelFormatName(frame->format->format));*/
texture_updated = false;
st->texture_updated = false;
if (w <= srcrect.w) {
srcrect.x = (srcrect.w - w) / 2;
@ -572,26 +575,26 @@ SDL_AppResult SDL_AppIterate(void* appstate) {
if (since_last_present < FRAMESKIP_LIMIT) {
for (i = 0; i < NUM_CHANNELS; i++) {
struct mCore* core = cores[i];
core->setKeys(core, keys);
struct mCore* core = st->cores[i];
core->setKeys(core, st->keys);
core->runFrame(core);
}
/*hack*/
if (keys && since_last_present < FRAMESKIP_LIMIT - 8) {
if (st->keys && since_last_present < FRAMESKIP_LIMIT - 8) {
since_last_present = FRAMESKIP_LIMIT - 8;
}
}
if (!texture_updated) {
if (!st->texture_updated) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
/* Update SDL_Texture with last video frame (only once per new frame) */
for (i = 0; i < NUM_CHANNELS; i++) {
SDL_Surface* screen = screens[i];
SDL_Texture* texture = textures[i];
SDL_Surface* screen = st->screens[i];
SDL_Texture* texture = st->textures[i];
SDL_UpdateTexture(texture, NULL, screen->pixels, screen->pitch);
texture_updated = true;
st->texture_updated = true;
SDL_RenderTexture(renderer, texture, NULL, NULL);
}
@ -600,13 +603,13 @@ SDL_AppResult SDL_AppIterate(void* appstate) {
} else {
since_last_present++;
if (since_last_present >= FRAMESKIP_LIMIT) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for (i = 0; i < NUM_CHANNELS; i++) {
SDL_Texture *texture = textures[i];
SDL_RenderTexture(renderer, texture, NULL, NULL);
}
SDL_RenderPresent(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for (i = 0; i < NUM_CHANNELS; i++) {
SDL_Texture *texture = st->textures[i];
SDL_RenderTexture(renderer, texture, NULL, NULL);
}
SDL_RenderPresent(renderer);
}
}
@ -617,9 +620,10 @@ void SDL_AppQuit(void* appstate, SDL_AppResult result) {
struct AppState* st = (struct AppState*)appstate;
int i;
for (i = 0; i < NUM_CHANNELS; i++) {
mCoreConfigDeinit(&cores[i]->config);
cores[i]->deinit(cores[i]);
SDL_DestroyTexture(textures[i]);
struct mCore* core = st->cores[i];
mCoreConfigDeinit(&core->config);
core->deinit(core);
SDL_DestroyTexture(st->textures[i]);
}
SDL_CloseCamera(st->camera);