update to SDL3 preview 3.1.6 and mgba 0.10.4
This commit is contained in:
parent
6e29bc4485
commit
3e6bf957f7
2
SDL
2
SDL
|
@ -1 +1 @@
|
|||
Subproject commit 7ff9be739827a53763b393897a371674d45b053d
|
||||
Subproject commit 78cc5c173404488d80751af226d1eaf67033bcc4
|
175
main.c
175
main.c
|
@ -1,3 +1,4 @@
|
|||
#include "SDL3/SDL_camera.h"
|
||||
#define SDL_MAIN_USE_CALLBACKS 1
|
||||
#include "SDL3/SDL_blendmode.h"
|
||||
#include "SDL3/SDL_error.h"
|
||||
|
@ -16,7 +17,7 @@
|
|||
#include "mgba/core/core.h"
|
||||
#include "mgba/core/interface.h"
|
||||
#include "mgba/feature/commandline.h"
|
||||
#include "mgba-util/image.h"
|
||||
//#include "mgba-util/image.h"
|
||||
|
||||
#define NUM_CHANNELS 3
|
||||
#define FRAMESKIP_LIMIT 20
|
||||
|
@ -34,9 +35,9 @@ static SDL_Renderer* renderer = NULL;
|
|||
static SDL_Camera* camera = NULL;
|
||||
static SDL_CameraSpec spec;
|
||||
static SDL_Texture* textures[NUM_CHANNELS] = { NULL, NULL, NULL };
|
||||
static SDL_bool texture_updated = SDL_FALSE;
|
||||
static SDL_CameraDeviceID front_camera = 0;
|
||||
static SDL_CameraDeviceID back_camera = 0;
|
||||
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 };
|
||||
|
@ -60,8 +61,6 @@ enum mColorFormat pixfmt_sdl_to_mgba(uint32_t sdl_fmt) {
|
|||
case SDL_PIXELFORMAT_XBGR8888: return mCOLOR_XBGR8;
|
||||
case SDL_PIXELFORMAT_XRGB1555: return mCOLOR_RGB5;
|
||||
case SDL_PIXELFORMAT_XRGB8888: return mCOLOR_XRGB8;
|
||||
/* does this even make sense? */
|
||||
case SDL_PIXELFORMAT_INDEX8: return mCOLOR_PAL8;
|
||||
/* others don't quite match between mgba and SDL */
|
||||
default: return mCOLOR_ANY;
|
||||
}
|
||||
|
@ -107,8 +106,8 @@ void myStopRequestImageBlue(struct mImageSource* self) {
|
|||
void myRequestImageRed(struct mImageSource* self, const void** buf, size_t* stride, enum mColorFormat* fmt) {
|
||||
SDL_Surface* source = filtered[0];
|
||||
if (source != NULL) {
|
||||
*fmt = pixfmt_sdl_to_mgba(source->format->format);
|
||||
*stride = source->pitch / (BYTES_PER_PIXEL / source->format->bytes_per_pixel);
|
||||
*fmt = pixfmt_sdl_to_mgba(source->format);
|
||||
*stride = source->pitch / (BYTES_PER_PIXEL / SDL_BYTESPERPIXEL(source->format));
|
||||
*buf = source->pixels;
|
||||
}
|
||||
}
|
||||
|
@ -116,8 +115,8 @@ void myRequestImageRed(struct mImageSource* self, const void** buf, size_t* stri
|
|||
void myRequestImageGreen(struct mImageSource* self, const void** buf, size_t* stride, enum mColorFormat* fmt) {
|
||||
SDL_Surface* source = filtered[1];
|
||||
if (source != NULL) {
|
||||
*fmt = pixfmt_sdl_to_mgba(source->format->format);
|
||||
*stride = source->pitch / (BYTES_PER_PIXEL / source->format->bytes_per_pixel);
|
||||
*fmt = pixfmt_sdl_to_mgba(source->format);
|
||||
*stride = source->pitch / (BYTES_PER_PIXEL / SDL_BYTESPERPIXEL(source->format));
|
||||
*buf = source->pixels;
|
||||
}
|
||||
}
|
||||
|
@ -125,31 +124,31 @@ void myRequestImageGreen(struct mImageSource* self, const void** buf, size_t* st
|
|||
void myRequestImageBlue(struct mImageSource* self, const void** buf, size_t* stride, enum mColorFormat* fmt) {
|
||||
SDL_Surface* source = filtered[2];
|
||||
if (source != NULL) {
|
||||
*fmt = pixfmt_sdl_to_mgba(source->format->format);
|
||||
*stride = source->pitch / (BYTES_PER_PIXEL / source->format->bytes_per_pixel);
|
||||
*fmt = pixfmt_sdl_to_mgba(source->format);
|
||||
*stride = source->pitch / (BYTES_PER_PIXEL / SDL_BYTESPERPIXEL(source->format));
|
||||
*buf = source->pixels;
|
||||
}
|
||||
}
|
||||
|
||||
struct mImageSource gb_img_src[3];
|
||||
|
||||
int SDL_AppInit(int argc, char *argv[]) {
|
||||
SDL_AppResult SDL_AppInit(void** appstate, int argc, char *argv[]) {
|
||||
int devcount = 0;
|
||||
int i;
|
||||
unsigned w, h;
|
||||
|
||||
SDL_CameraDeviceID *devices;
|
||||
SDL_CameraDeviceID devid;
|
||||
SDL_CameraID *devices;
|
||||
SDL_CameraID devid;
|
||||
|
||||
struct mArguments args;
|
||||
bool parsed = mArgumentsParse(&args, argc, argv, NULL, 0);
|
||||
|
||||
/* Enable standard application logging */
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
|
||||
if (!parsed) {
|
||||
SDL_Log("Couldn't parse arguments");
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
gb_img_src[0].requestImage = myRequestImageRed;
|
||||
|
@ -166,12 +165,12 @@ int SDL_AppInit(int argc, char *argv[]) {
|
|||
struct mCore* core = mCoreFind(args.fname);
|
||||
if (!core || !core->init(core)) {
|
||||
SDL_Log("Couldn't initialize mgba core!");
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!mCoreLoadFile(core, args.fname)) {
|
||||
SDL_Log("Failed to load ROM");
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
mCoreConfigInit(&core->config, NULL);
|
||||
|
@ -188,39 +187,42 @@ int SDL_AppInit(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
/* without loss of generality */
|
||||
cores[0]->currentVideoSize(cores[0], &w, &h);
|
||||
cores[0]->desiredVideoDimensions(cores[0], &w, &h);
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA) != 0) {
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA)) {
|
||||
SDL_Log("Couldn't initialize SDL3: %s", SDL_GetError());
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow("cgbwebcam", w, h, SDL_WINDOW_RESIZABLE);
|
||||
if (!window) {
|
||||
SDL_Log("Couldn't create window: %s", SDL_GetError());
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
renderer = SDL_CreateRenderer(window, NULL, SDL_RENDERER_PRESENTVSYNC);
|
||||
renderer = SDL_CreateRenderer(window, NULL);
|
||||
if (!renderer) {
|
||||
SDL_Log("Couldn't create renderer: %s", SDL_GetError());
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
|
||||
SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE);
|
||||
|
||||
devices = SDL_GetCameraDevices(&devcount);
|
||||
devices = SDL_GetCameras(&devcount);
|
||||
if (!devices) {
|
||||
SDL_Log("SDL_GetCameraDevices failed: %s", SDL_GetError());
|
||||
return -1;
|
||||
SDL_Log("SDL_GetCameras failed: %s", SDL_GetError());
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_Log("Saw %d camera devices.", devcount);
|
||||
for (i = 0; i < devcount; i++) {
|
||||
const SDL_CameraDeviceID device = devices[i];
|
||||
char *name = SDL_GetCameraDeviceName(device);
|
||||
const SDL_CameraPosition position = SDL_GetCameraDevicePosition(device);
|
||||
const SDL_CameraID device = devices[i];
|
||||
const char *name = SDL_GetCameraName(device);
|
||||
const SDL_CameraPosition position = SDL_GetCameraPosition(device);
|
||||
const char *posstr = "";
|
||||
int j, formats_len = 0;
|
||||
SDL_CameraSpec** formats = SDL_GetCameraSupportedFormats(device, &formats_len);
|
||||
|
||||
if (position == SDL_CAMERA_POSITION_FRONT_FACING) {
|
||||
front_camera = device;
|
||||
posstr = "[front-facing] ";
|
||||
|
@ -228,27 +230,42 @@ int SDL_AppInit(int argc, char *argv[]) {
|
|||
back_camera = device;
|
||||
posstr = "[back-facing] ";
|
||||
}
|
||||
SDL_Log(" - Camera #%d: %s %s", i, posstr, name);
|
||||
SDL_free(name);
|
||||
}
|
||||
SDL_Log(" - %sCamera #%d: %s", posstr, i, name);
|
||||
SDL_free((void*)name);
|
||||
|
||||
devid = front_camera ? front_camera : devices[0]; /* no front-facing? just take the first one. */
|
||||
for (j = 0; j < formats_len; j++) {
|
||||
SDL_CameraSpec* fmt = formats[j];
|
||||
SDL_Log(" - Format %d: %s %dx%d @ %d/%d",
|
||||
j,
|
||||
SDL_GetPixelFormatName(fmt->format),
|
||||
fmt->width,
|
||||
fmt->height,
|
||||
fmt->framerate_numerator,
|
||||
fmt->framerate_denominator
|
||||
);
|
||||
spec = *fmt;
|
||||
}
|
||||
SDL_free(formats);
|
||||
}
|
||||
|
||||
/* HACK: no front-facing? just use hardcoded index til we have selection menu. */
|
||||
devid = front_camera ? front_camera : devices[0];
|
||||
SDL_free(devices);
|
||||
|
||||
if (!devid) {
|
||||
SDL_Log("No cameras available?");
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
spec.format = SDL_PIXELFORMAT_XBGR1555;
|
||||
spec.width = 320;
|
||||
spec.height = 240;
|
||||
spec.interval_numerator = 1;
|
||||
spec.interval_denominator = 30;
|
||||
camera = SDL_OpenCameraDevice(devid, &spec);
|
||||
//spec.width = 320;
|
||||
//spec.height = 240;
|
||||
spec.framerate_numerator = 30;
|
||||
spec.framerate_denominator = 1;
|
||||
camera = SDL_OpenCamera(devid, &spec);
|
||||
if (!camera) {
|
||||
SDL_Log("Failed to open camera device: %s", SDL_GetError());
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
for (i = 0; i < NUM_CHANNELS; i++) {
|
||||
|
@ -264,7 +281,7 @@ int SDL_AppInit(int argc, char *argv[]) {
|
|||
texture = SDL_CreateTextureFromSurface(renderer, screens[0]);
|
||||
if (!texture) {
|
||||
SDL_Log("Couldn't create texture: %s", SDL_GetError());
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
|
||||
textures[i] = texture;
|
||||
|
@ -303,18 +320,18 @@ int SDL_AppInit(int argc, char *argv[]) {
|
|||
SDL_SetTextureColorMod(textures[1], 0, 255, 0);
|
||||
SDL_SetTextureColorMod(textures[2], 0, 0, 255);
|
||||
|
||||
return 0; /* start the main app loop. */
|
||||
return SDL_APP_CONTINUE; /* start the main app loop. */
|
||||
}
|
||||
|
||||
static int FlipCamera(void) {
|
||||
static SDL_AppResult FlipCamera(void) {
|
||||
static Uint64 last_flip = 0;
|
||||
if ((SDL_GetTicks() - last_flip) < 3000) { /* must wait at least 3 seconds between flips. */
|
||||
return 0;
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
if (camera) {
|
||||
const SDL_CameraDeviceID current = SDL_GetCameraInstanceID(camera);
|
||||
SDL_CameraDeviceID nextcam = 0;
|
||||
const SDL_CameraID current = SDL_GetCameraID(camera);
|
||||
SDL_CameraID nextcam = 0;
|
||||
if (current == front_camera) {
|
||||
nextcam = back_camera;
|
||||
} else if (current == back_camera) {
|
||||
|
@ -326,32 +343,32 @@ static int FlipCamera(void) {
|
|||
|
||||
SDL_CloseCamera(camera);
|
||||
|
||||
camera = SDL_OpenCameraDevice(nextcam, NULL);
|
||||
camera = SDL_OpenCamera(nextcam, NULL);
|
||||
if (!camera) {
|
||||
SDL_Log("Failed to open camera device: %s", SDL_GetError());
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
last_flip = SDL_GetTicks();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
int SDL_AppEvent(const SDL_Event *event) {
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
||||
switch (event->type) {
|
||||
case SDL_EVENT_KEY_DOWN: {
|
||||
const SDL_Keycode sym = event->key.keysym.sym;
|
||||
const SDL_Keycode sym = event->key.key;
|
||||
switch (sym) {
|
||||
case SDLK_ESCAPE: case SDLK_AC_BACK: return 1;
|
||||
case SDLK_ESCAPE: case SDLK_AC_BACK: return SDL_APP_SUCCESS;
|
||||
case SDLK_SPACE: FlipCamera(); 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_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;
|
||||
}
|
||||
|
@ -359,14 +376,14 @@ int SDL_AppEvent(const SDL_Event *event) {
|
|||
}
|
||||
|
||||
case SDL_EVENT_KEY_UP: {
|
||||
const SDL_Keycode sym = event->key.keysym.sym;
|
||||
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_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;
|
||||
}
|
||||
|
@ -379,25 +396,25 @@ int SDL_AppEvent(const SDL_Event *event) {
|
|||
|
||||
case SDL_EVENT_QUIT:
|
||||
SDL_Log("Quit!");
|
||||
return 1;
|
||||
return SDL_APP_SUCCESS;
|
||||
|
||||
case SDL_EVENT_CAMERA_DEVICE_APPROVED:
|
||||
SDL_Log("Camera approved!");
|
||||
if (SDL_GetCameraFormat(camera, &spec) < 0) {
|
||||
if (!SDL_GetCameraFormat(camera, &spec)) {
|
||||
SDL_Log("Couldn't get camera spec: %s", SDL_GetError());
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_EVENT_CAMERA_DEVICE_DENIED:
|
||||
SDL_Log("Camera denied!");
|
||||
return -1;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
int SDL_AppIterate(void) {
|
||||
SDL_AppResult SDL_AppIterate(void* appstate) {
|
||||
int i;
|
||||
static int since_last_present = 0;
|
||||
|
||||
|
@ -406,28 +423,28 @@ int SDL_AppIterate(void) {
|
|||
SDL_Surface *frame = camera ? SDL_AcquireCameraFrame(camera, ×tampNS) : NULL;
|
||||
|
||||
if (frame) {
|
||||
struct SDL_Rect srcrect = frame->clip_rect;
|
||||
int w = srcrect.h * scaled->clip_rect.w / scaled->clip_rect.h;
|
||||
struct SDL_Rect srcrect, dstrect;
|
||||
SDL_GetSurfaceClipRect(frame, &srcrect);
|
||||
SDL_GetSurfaceClipRect(scaled, &dstrect);
|
||||
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 = SDL_FALSE;
|
||||
texture_updated = false;
|
||||
|
||||
if (w >= srcrect.w) {
|
||||
if (w <= srcrect.w) {
|
||||
srcrect.x = (srcrect.w - w) / 2;
|
||||
srcrect.w = w;
|
||||
} else {
|
||||
int h = srcrect.w * scaled->clip_rect.h / scaled->clip_rect.w;
|
||||
int h = srcrect.w * dstrect.h / dstrect.w;
|
||||
srcrect.y = (srcrect.h - h) / 2;
|
||||
srcrect.h = h;
|
||||
}
|
||||
if (SDL_BlitSurfaceScaled(frame, &srcrect, scaled, &scaled->clip_rect, SDL_SCALEMODE_NEAREST) < 0) {
|
||||
if (!SDL_BlitSurfaceScaled(frame, &srcrect, scaled, &dstrect, SDL_SCALEMODE_NEAREST)) {
|
||||
SDL_Log("failed to scale: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
if (SDL_ReleaseCameraFrame(camera, frame) < 0) {
|
||||
SDL_Log("err SDL_ReleaseCameraFrame: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
SDL_ReleaseCameraFrame(camera, frame);
|
||||
|
||||
SDL_SetSurfaceColorMod(scaled, 255, 0, 0);
|
||||
SDL_BlitSurface(scaled, NULL, filtered[0], NULL);
|
||||
SDL_SetSurfaceColorMod(scaled, 0, 255, 0);
|
||||
|
@ -458,7 +475,7 @@ int SDL_AppIterate(void) {
|
|||
SDL_Surface* screen = screens[i];
|
||||
SDL_Texture* texture = textures[i];
|
||||
SDL_UpdateTexture(texture, NULL, screen->pixels, screen->pitch);
|
||||
texture_updated = SDL_TRUE;
|
||||
texture_updated = true;
|
||||
SDL_RenderTexture(renderer, texture, NULL, NULL);
|
||||
}
|
||||
|
||||
|
@ -477,10 +494,10 @@ int SDL_AppIterate(void) {
|
|||
}
|
||||
}
|
||||
|
||||
return 0; /* keep iterating. */
|
||||
return SDL_APP_CONTINUE; /* keep iterating. */
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void) {
|
||||
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
|
||||
int i;
|
||||
for (i = 0; i < NUM_CHANNELS; i++) {
|
||||
mCoreConfigDeinit(&cores[i]->config);
|
||||
|
|
2
mgba
2
mgba
|
@ -1 +1 @@
|
|||
Subproject commit 3571b112dc821d3612e5073afb8b6be41a35dc55
|
||||
Subproject commit 9a1d2442a303cde4e434f8841b040b9dae689062
|
Loading…
Reference in New Issue