clean up c
This commit is contained in:
parent
cbac106862
commit
6928958d30
|
@ -34,5 +34,7 @@ add_subdirectory(mgba)
|
||||||
include_directories(SDL/include)
|
include_directories(SDL/include)
|
||||||
include_directories(mgba/include)
|
include_directories(mgba/include)
|
||||||
|
|
||||||
|
#set_source_files_properties(main.c PROPERTIES COMPILE_FLAGS "-Wall -Werror -pedantic -std=c89")
|
||||||
|
|
||||||
add_executable(cgbwebcam main.c)
|
add_executable(cgbwebcam main.c)
|
||||||
target_link_libraries(cgbwebcam SDL3-static mgba)
|
target_link_libraries(cgbwebcam SDL3-static mgba)
|
||||||
|
|
99
main.c
99
main.c
|
@ -1,5 +1,3 @@
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#define SDL_MAIN_USE_CALLBACKS 1
|
#define SDL_MAIN_USE_CALLBACKS 1
|
||||||
#include "SDL3/SDL_blendmode.h"
|
#include "SDL3/SDL_blendmode.h"
|
||||||
#include "SDL3/SDL_error.h"
|
#include "SDL3/SDL_error.h"
|
||||||
|
@ -22,6 +20,14 @@
|
||||||
#define NUM_CHANNELS 3
|
#define NUM_CHANNELS 3
|
||||||
#define FRAMESKIP_LIMIT 20
|
#define FRAMESKIP_LIMIT 20
|
||||||
|
|
||||||
|
#if BYTES_PER_PIXEL == 4
|
||||||
|
#define SCREEN_FMT SDL_PIXELFORMAT_XBGR8888
|
||||||
|
#elif BYTES_PER_PIXEL == 2
|
||||||
|
#define SCREEN_FMT SDL_PIXELFORMAT_XBGR1555
|
||||||
|
#else
|
||||||
|
#error "unknown pixel format"
|
||||||
|
#endif
|
||||||
|
|
||||||
static SDL_Window* window = NULL;
|
static SDL_Window* window = NULL;
|
||||||
static SDL_Renderer* renderer = NULL;
|
static SDL_Renderer* renderer = NULL;
|
||||||
static SDL_Camera* camera = NULL;
|
static SDL_Camera* camera = NULL;
|
||||||
|
@ -52,9 +58,9 @@ enum mColorFormat pixfmt_sdl_to_mgba(uint32_t sdl_fmt) {
|
||||||
case SDL_PIXELFORMAT_XBGR8888: return mCOLOR_XBGR8;
|
case SDL_PIXELFORMAT_XBGR8888: return mCOLOR_XBGR8;
|
||||||
case SDL_PIXELFORMAT_XRGB1555: return mCOLOR_RGB5;
|
case SDL_PIXELFORMAT_XRGB1555: return mCOLOR_RGB5;
|
||||||
case SDL_PIXELFORMAT_XRGB8888: return mCOLOR_XRGB8;
|
case SDL_PIXELFORMAT_XRGB8888: return mCOLOR_XRGB8;
|
||||||
// does this even make sense?
|
/* does this even make sense? */
|
||||||
case SDL_PIXELFORMAT_INDEX8: return mCOLOR_PAL8;
|
case SDL_PIXELFORMAT_INDEX8: return mCOLOR_PAL8;
|
||||||
// others don't quite match between mgba and SDL
|
/* others don't quite match between mgba and SDL */
|
||||||
default: return mCOLOR_ANY;
|
default: return mCOLOR_ANY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,27 +129,15 @@ void myRequestImageBlue(struct mImageSource* self, const void** buf, size_t* str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct mImageSource gb_img_src[3] = {
|
struct mImageSource gb_img_src[3];
|
||||||
{
|
|
||||||
.requestImage = myRequestImageRed,
|
|
||||||
.startRequestImage = myStartRequestImageRed,
|
|
||||||
.stopRequestImage = myStopRequestImageRed,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.requestImage = myRequestImageGreen,
|
|
||||||
.startRequestImage = myStartRequestImageGreen,
|
|
||||||
.stopRequestImage = myStopRequestImageGreen,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.requestImage = myRequestImageBlue,
|
|
||||||
.startRequestImage = myStartRequestImageBlue,
|
|
||||||
.stopRequestImage = myStopRequestImageBlue,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
int SDL_AppInit(int argc, char *argv[]) {
|
int SDL_AppInit(int argc, char *argv[]) {
|
||||||
int devcount = 0;
|
int devcount = 0;
|
||||||
int i;
|
int i;
|
||||||
|
unsigned w, h;
|
||||||
|
|
||||||
|
SDL_CameraDeviceID *devices;
|
||||||
|
SDL_CameraDeviceID devid;
|
||||||
|
|
||||||
struct mArguments args;
|
struct mArguments args;
|
||||||
bool parsed = mArgumentsParse(&args, argc, argv, NULL, 0);
|
bool parsed = mArgumentsParse(&args, argc, argv, NULL, 0);
|
||||||
|
@ -156,6 +150,16 @@ int SDL_AppInit(int argc, char *argv[]) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gb_img_src[0].requestImage = myRequestImageRed;
|
||||||
|
gb_img_src[0].startRequestImage = myStartRequestImageRed;
|
||||||
|
gb_img_src[0].stopRequestImage = myStopRequestImageRed;
|
||||||
|
gb_img_src[1].requestImage = myRequestImageGreen;
|
||||||
|
gb_img_src[1].startRequestImage = myStartRequestImageGreen;
|
||||||
|
gb_img_src[1].stopRequestImage = myStopRequestImageGreen;
|
||||||
|
gb_img_src[2].requestImage = myRequestImageBlue;
|
||||||
|
gb_img_src[2].startRequestImage = myStartRequestImageBlue;
|
||||||
|
gb_img_src[2].stopRequestImage = myStopRequestImageBlue;
|
||||||
|
|
||||||
for (i = 0; i < NUM_CHANNELS; i++) {
|
for (i = 0; i < NUM_CHANNELS; i++) {
|
||||||
struct mCore* core = mCoreFind(args.fname);
|
struct mCore* core = mCoreFind(args.fname);
|
||||||
if (!core || !core->init(core)) {
|
if (!core || !core->init(core)) {
|
||||||
|
@ -169,7 +173,7 @@ int SDL_AppInit(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
mCoreConfigInit(&core->config, NULL);
|
mCoreConfigInit(&core->config, NULL);
|
||||||
//mCoreConfigLoad(&core->config);
|
/*mCoreConfigLoad(&core->config);*/
|
||||||
|
|
||||||
mArgumentsApply(&args, NULL, 0, &core->config);
|
mArgumentsApply(&args, NULL, 0, &core->config);
|
||||||
mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "detect");
|
mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "detect");
|
||||||
|
@ -180,8 +184,7 @@ int SDL_AppInit(int argc, char *argv[]) {
|
||||||
cores[i] = core;
|
cores[i] = core;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned w, h;
|
/* without loss of generality */
|
||||||
// without loss of generality
|
|
||||||
cores[0]->currentVideoSize(cores[0], &w, &h);
|
cores[0]->currentVideoSize(cores[0], &w, &h);
|
||||||
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA) != 0) {
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA) != 0) {
|
||||||
|
@ -203,7 +206,7 @@ int SDL_AppInit(int argc, char *argv[]) {
|
||||||
|
|
||||||
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
|
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
|
||||||
|
|
||||||
SDL_CameraDeviceID *devices = SDL_GetCameraDevices(&devcount);
|
devices = SDL_GetCameraDevices(&devcount);
|
||||||
if (!devices) {
|
if (!devices) {
|
||||||
SDL_Log("SDL_GetCameraDevices failed: %s", SDL_GetError());
|
SDL_Log("SDL_GetCameraDevices failed: %s", SDL_GetError());
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -226,7 +229,7 @@ int SDL_AppInit(int argc, char *argv[]) {
|
||||||
SDL_free(name);
|
SDL_free(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
const SDL_CameraDeviceID devid = front_camera ? front_camera : devices[0]; /* no front-facing? just take the first one. */
|
devid = front_camera ? front_camera : devices[0]; /* no front-facing? just take the first one. */
|
||||||
SDL_free(devices);
|
SDL_free(devices);
|
||||||
|
|
||||||
if (!devid) {
|
if (!devid) {
|
||||||
|
@ -234,36 +237,28 @@ int SDL_AppInit(int argc, char *argv[]) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_CameraSpec init_spec = {
|
spec.format = SDL_PIXELFORMAT_XBGR1555;
|
||||||
.format = SDL_PIXELFORMAT_XBGR1555,
|
spec.width = 320;
|
||||||
.width = 320,
|
spec.height = 240;
|
||||||
.height = 240,
|
spec.interval_numerator = 1;
|
||||||
.interval_numerator = 1,
|
spec.interval_denominator = 30;
|
||||||
.interval_denominator = 30,
|
camera = SDL_OpenCameraDevice(devid, &spec);
|
||||||
};
|
|
||||||
camera = SDL_OpenCameraDevice(devid, &init_spec);
|
|
||||||
if (!camera) {
|
if (!camera) {
|
||||||
SDL_Log("Failed to open camera device: %s", SDL_GetError());
|
SDL_Log("Failed to open camera device: %s", SDL_GetError());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if BYTES_PER_PIXEL == 4
|
|
||||||
uint32_t fmt = SDL_PIXELFORMAT_XBGR8888;
|
|
||||||
#elif BYTES_PER_PIXEL == 2
|
|
||||||
uint32_t fmt = SDL_PIXELFORMAT_XBGR1555;
|
|
||||||
#else
|
|
||||||
#error "unknown pixel format"
|
|
||||||
#endif
|
|
||||||
for (i = 0; i < NUM_CHANNELS; i++) {
|
for (i = 0; i < NUM_CHANNELS; i++) {
|
||||||
int j;
|
int j;
|
||||||
struct mCore* core = cores[i];
|
struct mCore* core = cores[i];
|
||||||
|
SDL_Texture* texture;
|
||||||
|
SDL_Surface* screen = SDL_CreateSurface(w, h, SCREEN_FMT);
|
||||||
|
|
||||||
SDL_Surface* screen = SDL_CreateSurface(w, h, fmt);
|
|
||||||
core->setVideoBuffer(core, screen->pixels, screen->pitch / BYTES_PER_PIXEL);
|
core->setVideoBuffer(core, screen->pixels, screen->pitch / BYTES_PER_PIXEL);
|
||||||
screens[i] = screen;
|
screens[i] = screen;
|
||||||
|
|
||||||
/* Create texture with appropriate format */
|
/* Create texture with appropriate format */
|
||||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, screens[0]);
|
texture = SDL_CreateTextureFromSurface(renderer, screens[0]);
|
||||||
if (!texture) {
|
if (!texture) {
|
||||||
SDL_Log("Couldn't create texture: %s", SDL_GetError());
|
SDL_Log("Couldn't create texture: %s", SDL_GetError());
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -271,7 +266,7 @@ int SDL_AppInit(int argc, char *argv[]) {
|
||||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
|
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
|
||||||
textures[i] = texture;
|
textures[i] = texture;
|
||||||
|
|
||||||
core->setPeripheral(core, mPERIPH_IMAGE_SOURCE, (void*)&gb_img_src[i]);
|
core->setPeripheral(core, mPERIPH_IMAGE_SOURCE, &gb_img_src[i]);
|
||||||
|
|
||||||
core->reset(core);
|
core->reset(core);
|
||||||
|
|
||||||
|
@ -279,19 +274,19 @@ int SDL_AppInit(int argc, char *argv[]) {
|
||||||
for (j = 0; j < 600; j++) {
|
for (j = 0; j < 600; j++) {
|
||||||
core->runFrame(core);
|
core->runFrame(core);
|
||||||
}
|
}
|
||||||
core->setKeys(core, 1); // title screen
|
core->setKeys(core, 1); /* title screen */
|
||||||
core->runFrame(core);
|
core->runFrame(core);
|
||||||
core->setKeys(core, 0);
|
core->setKeys(core, 0);
|
||||||
for (j = 0; j < 25; j++) {
|
for (j = 0; j < 25; j++) {
|
||||||
core->runFrame(core);
|
core->runFrame(core);
|
||||||
}
|
}
|
||||||
core->setKeys(core, 1); // select 'shoot'
|
core->setKeys(core, 1); /* select 'shoot' */
|
||||||
core->runFrame(core);
|
core->runFrame(core);
|
||||||
core->setKeys(core, 0);
|
core->setKeys(core, 0);
|
||||||
for (j = 0; j < 75; j++) {
|
for (j = 0; j < 75; j++) {
|
||||||
core->runFrame(core);
|
core->runFrame(core);
|
||||||
}
|
}
|
||||||
core->setKeys(core, 1); // select 'shoot' again
|
core->setKeys(core, 1); /* select 'shoot' again */
|
||||||
core->runFrame(core);
|
core->runFrame(core);
|
||||||
core->setKeys(core, 0);
|
core->setKeys(core, 0);
|
||||||
for (j = 0; j < 100; j++) {
|
for (j = 0; j < 100; j++) {
|
||||||
|
@ -388,11 +383,12 @@ int SDL_AppIterate(void) {
|
||||||
SDL_Surface *frame = camera ? SDL_AcquireCameraFrame(camera, ×tampNS) : NULL;
|
SDL_Surface *frame = camera ? SDL_AcquireCameraFrame(camera, ×tampNS) : NULL;
|
||||||
|
|
||||||
if (frame) {
|
if (frame) {
|
||||||
//SDL_Log("new frame %d x %d %s", frame->w, frame->h, SDL_GetPixelFormatName(frame->format->format));
|
|
||||||
texture_updated = SDL_FALSE;
|
|
||||||
|
|
||||||
struct SDL_Rect srcrect = frame->clip_rect;
|
struct SDL_Rect srcrect = frame->clip_rect;
|
||||||
int w = srcrect.h * scaled->clip_rect.w / scaled->clip_rect.h;
|
int w = srcrect.h * scaled->clip_rect.w / scaled->clip_rect.h;
|
||||||
|
|
||||||
|
/*SDL_Log("new frame %d x %d %s", frame->w, frame->h, SDL_GetPixelFormatName(frame->format->format));*/
|
||||||
|
texture_updated = SDL_FALSE;
|
||||||
|
|
||||||
if (w >= srcrect.w) {
|
if (w >= srcrect.w) {
|
||||||
srcrect.x = (srcrect.w - w) / 2;
|
srcrect.x = (srcrect.w - w) / 2;
|
||||||
srcrect.w = w;
|
srcrect.w = w;
|
||||||
|
@ -401,8 +397,7 @@ int SDL_AppIterate(void) {
|
||||||
srcrect.y = (srcrect.h - h) / 2;
|
srcrect.y = (srcrect.h - h) / 2;
|
||||||
srcrect.h = h;
|
srcrect.h = h;
|
||||||
}
|
}
|
||||||
int res = SDL_BlitSurfaceScaled(frame, &srcrect, scaled, &scaled->clip_rect, SDL_SCALEMODE_NEAREST);
|
if (SDL_BlitSurfaceScaled(frame, &srcrect, scaled, &scaled->clip_rect, SDL_SCALEMODE_NEAREST) < 0) {
|
||||||
if (res != 0) {
|
|
||||||
SDL_Log("failed to scale: %s", SDL_GetError());
|
SDL_Log("failed to scale: %s", SDL_GetError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue