From 6928958d30d20f625d00f9aef70148cbd872a1bc Mon Sep 17 00:00:00 2001 From: lif <> Date: Sun, 3 Mar 2024 01:41:29 -0800 Subject: [PATCH] clean up c --- CMakeLists.txt | 2 + main.c | 99 ++++++++++++++++++++++++-------------------------- 2 files changed, 49 insertions(+), 52 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a707754..4f03942 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,5 +34,7 @@ add_subdirectory(mgba) include_directories(SDL/include) include_directories(mgba/include) +#set_source_files_properties(main.c PROPERTIES COMPILE_FLAGS "-Wall -Werror -pedantic -std=c89") + add_executable(cgbwebcam main.c) target_link_libraries(cgbwebcam SDL3-static mgba) diff --git a/main.c b/main.c index fc42f57..6fd2918 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,3 @@ -#include - #define SDL_MAIN_USE_CALLBACKS 1 #include "SDL3/SDL_blendmode.h" #include "SDL3/SDL_error.h" @@ -22,6 +20,14 @@ #define NUM_CHANNELS 3 #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_Renderer* renderer = 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_XRGB1555: return mCOLOR_RGB5; case SDL_PIXELFORMAT_XRGB8888: return mCOLOR_XRGB8; - // does this even make sense? + /* does this even make sense? */ 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; } } @@ -123,27 +129,15 @@ void myRequestImageBlue(struct mImageSource* self, const void** buf, size_t* str } } -const struct mImageSource gb_img_src[3] = { - { - .requestImage = myRequestImageRed, - .startRequestImage = myStartRequestImageRed, - .stopRequestImage = myStopRequestImageRed, - }, - { - .requestImage = myRequestImageGreen, - .startRequestImage = myStartRequestImageGreen, - .stopRequestImage = myStopRequestImageGreen, - }, - { - .requestImage = myRequestImageBlue, - .startRequestImage = myStartRequestImageBlue, - .stopRequestImage = myStopRequestImageBlue, - }, -}; +struct mImageSource gb_img_src[3]; int SDL_AppInit(int argc, char *argv[]) { int devcount = 0; int i; + unsigned w, h; + + SDL_CameraDeviceID *devices; + SDL_CameraDeviceID devid; struct mArguments args; bool parsed = mArgumentsParse(&args, argc, argv, NULL, 0); @@ -156,6 +150,16 @@ int SDL_AppInit(int argc, char *argv[]) { 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++) { struct mCore* core = mCoreFind(args.fname); if (!core || !core->init(core)) { @@ -169,7 +173,7 @@ int SDL_AppInit(int argc, char *argv[]) { } mCoreConfigInit(&core->config, NULL); - //mCoreConfigLoad(&core->config); + /*mCoreConfigLoad(&core->config);*/ mArgumentsApply(&args, NULL, 0, &core->config); mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "detect"); @@ -180,8 +184,7 @@ int SDL_AppInit(int argc, char *argv[]) { cores[i] = core; } - unsigned w, h; - // without loss of generality + /* without loss of generality */ cores[0]->currentVideoSize(cores[0], &w, &h); 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_CameraDeviceID *devices = SDL_GetCameraDevices(&devcount); + devices = SDL_GetCameraDevices(&devcount); if (!devices) { SDL_Log("SDL_GetCameraDevices failed: %s", SDL_GetError()); return -1; @@ -226,7 +229,7 @@ int SDL_AppInit(int argc, char *argv[]) { 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); if (!devid) { @@ -234,36 +237,28 @@ int SDL_AppInit(int argc, char *argv[]) { return -1; } - SDL_CameraSpec init_spec = { - .format = SDL_PIXELFORMAT_XBGR1555, - .width = 320, - .height = 240, - .interval_numerator = 1, - .interval_denominator = 30, - }; - camera = SDL_OpenCameraDevice(devid, &init_spec); + spec.format = SDL_PIXELFORMAT_XBGR1555; + spec.width = 320; + spec.height = 240; + spec.interval_numerator = 1; + spec.interval_denominator = 30; + camera = SDL_OpenCameraDevice(devid, &spec); if (!camera) { SDL_Log("Failed to open camera device: %s", SDL_GetError()); 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++) { int j; 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); screens[i] = screen; /* Create texture with appropriate format */ - SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, screens[0]); + texture = SDL_CreateTextureFromSurface(renderer, screens[0]); if (!texture) { SDL_Log("Couldn't create texture: %s", SDL_GetError()); return -1; @@ -271,7 +266,7 @@ int SDL_AppInit(int argc, char *argv[]) { SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD); 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); @@ -279,19 +274,19 @@ int SDL_AppInit(int argc, char *argv[]) { for (j = 0; j < 600; j++) { core->runFrame(core); } - core->setKeys(core, 1); // title screen + core->setKeys(core, 1); /* title screen */ core->runFrame(core); core->setKeys(core, 0); for (j = 0; j < 25; j++) { core->runFrame(core); } - core->setKeys(core, 1); // select 'shoot' + core->setKeys(core, 1); /* select 'shoot' */ core->runFrame(core); core->setKeys(core, 0); for (j = 0; j < 75; j++) { core->runFrame(core); } - core->setKeys(core, 1); // select 'shoot' again + core->setKeys(core, 1); /* select 'shoot' again */ core->runFrame(core); core->setKeys(core, 0); for (j = 0; j < 100; j++) { @@ -388,11 +383,12 @@ int SDL_AppIterate(void) { SDL_Surface *frame = camera ? SDL_AcquireCameraFrame(camera, ×tampNS) : NULL; 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; 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) { srcrect.x = (srcrect.w - w) / 2; srcrect.w = w; @@ -401,8 +397,7 @@ int SDL_AppIterate(void) { srcrect.y = (srcrect.h - h) / 2; srcrect.h = h; } - int res = SDL_BlitSurfaceScaled(frame, &srcrect, scaled, &scaled->clip_rect, SDL_SCALEMODE_NEAREST); - if (res != 0) { + if (SDL_BlitSurfaceScaled(frame, &srcrect, scaled, &scaled->clip_rect, SDL_SCALEMODE_NEAREST) < 0) { SDL_Log("failed to scale: %s", SDL_GetError()); }