From a4ee0c9279d75f5d4e387fcac1c6cce46957b3e0 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 26 Apr 2022 10:04:07 +0200 Subject: find dd dynamically, make duration configurable --- cmake/FindSameBoy.cmake | 2 +- cmake/FindSameBoyBootRom.cmake | 5 +- src/get_image_for_rom.c | 6 +- src/get_image_for_rom.h | 2 + src/main.c | 143 +++++++++++++++++++++++------------------ 5 files changed, 88 insertions(+), 70 deletions(-) diff --git a/cmake/FindSameBoy.cmake b/cmake/FindSameBoy.cmake index 6f507e4..1fcf2ac 100644 --- a/cmake/FindSameBoy.cmake +++ b/cmake/FindSameBoy.cmake @@ -11,7 +11,7 @@ endif() set(SAMEBOY_PATH ${PROJECT_SOURCE_DIR}/deps/SameBoy) if(NOT EXISTS ${SAMEBOY_PATH}/README.md) - message(FATAL_ERROR "Unable to find SameBoy.\n Make sure to run `git submodule update --init --recursive`") + message(FATAL_ERROR "Unable to find SameBoy.\n Make sure to download the submodules") endif() diff --git a/cmake/FindSameBoyBootRom.cmake b/cmake/FindSameBoyBootRom.cmake index 5aadef3..ac2b3c5 100644 --- a/cmake/FindSameBoyBootRom.cmake +++ b/cmake/FindSameBoyBootRom.cmake @@ -13,7 +13,8 @@ add_executable(pb12 ${SAMEBOY_BOOTROM_PATH}/pb12.c) find_program(RGBASM rgbasm REQUIRED) find_program(RGBLINK rgblink REQUIRED) find_program(RGBGFX rgbgfx REQUIRED) -mark_as_advanced(RGBASM RGBLINK RGBGFX) +find_program(DD dd REQUIRED) +mark_as_advanced(RGBASM RGBLINK RGBGFX DD) # custom build step add_custom_command( @@ -34,7 +35,7 @@ add_custom_command( COMMAND ${RGBLINK} -o ${SAMEBOY_BOOTROM_VERSION}.tmp2 ${SAMEBOY_BOOTROM_VERSION}.tmp COMMAND - dd if=${SAMEBOY_BOOTROM_VERSION}.tmp2 of=${SAMEBOY_BOOTROM_VERSION}.bin count=1 bs=${SAMEBOY_BOOTROM_SIZE} + ${DD} if=${SAMEBOY_BOOTROM_VERSION}.tmp2 of=${SAMEBOY_BOOTROM_VERSION}.bin count=1 bs=${SAMEBOY_BOOTROM_SIZE} DEPENDS SameBoyLogo.pb12 VERBATIM ) diff --git a/src/get_image_for_rom.c b/src/get_image_for_rom.c index a4da508..21eaf5e 100644 --- a/src/get_image_for_rom.c +++ b/src/get_image_for_rom.c @@ -7,7 +7,7 @@ #include "get_image_for_rom.h" -#define LENGTH 60 * 10 +unsigned int duration = 60*10; struct local_data { unsigned long frames; @@ -30,10 +30,10 @@ static void vblank(GB_gameboy_t *gb) struct local_data *local_data = (struct local_data *)GB_get_user_data(gb); - if (local_data->frames == LENGTH) { + if (local_data->frames == duration) { local_data->running = false; } - else if (local_data->frames == LENGTH - 1) { + else if (local_data->frames == duration - 1) { GB_set_rendering_disabled(gb, false); } diff --git a/src/get_image_for_rom.h b/src/get_image_for_rom.h index da9c8bf..2fe26b5 100644 --- a/src/get_image_for_rom.h +++ b/src/get_image_for_rom.h @@ -3,6 +3,8 @@ #include #include +extern unsigned int duration; + typedef bool (*cancel_callback_t)(void*); int get_image_for_rom_alt(const char *filename, const unsigned char *buffer, size_t size, uint32_t *output, uint8_t *cgb_flag); diff --git a/src/main.c b/src/main.c index 5d3291f..6d37dc8 100644 --- a/src/main.c +++ b/src/main.c @@ -87,10 +87,6 @@ static void scale_image(const uint32_t* input, const signed input_width, const s int main (int argc , char** argv) { -#define str(x) #x -#define xstr(x) str(x) - fprintf(stderr, "SameBoy Thumbnailer v" xstr(VERSION) "\n"); - const uint8_t BITMAP_WIDTH = 160; const uint8_t BITMAP_HEIGHT = 144; const uint8_t SCALE_MULTIPLIER = 4; @@ -101,7 +97,7 @@ int main (int argc , char** argv) const uint16_t X_OFFSET = 192; const uint16_t Y_OFFSET = 298; - int size = 128; + int size = 256; char* input = NULL; char* output = NULL; @@ -123,6 +119,17 @@ int main (int argc , char** argv) continue; } + if (strcmp(argv[i], "--duration") == 0) { + unsigned int arg_dur = atoi(argv[++i]); + if (arg_dur < 1) { + fprintf(stderr, "Duration cannot be smaller than 1\n"); + return -1; + } + fprintf(stderr, "Setting duration to %i seconds\n", arg_dur); + duration = arg_dur * 60; + continue; + } + if (strcmp(argv[i], "--boot") == 0) { bootrom_path = argv[++i]; fprintf(stderr, "Using boot ROM %s\n", bootrom_path); @@ -141,76 +148,84 @@ int main (int argc , char** argv) } } - if (input && output) { - uint32_t bitmap[BITMAP_WIDTH*BITMAP_HEIGHT]; - uint8_t cgbFlag = 0; + if (!input || !output) + { + fprintf(stderr, + "Usage: %s input output [options]\n" + "\n" + "Options:\n" + "\t--size [%i]\t\tset the size of the emulated screen\n" + "\t--duration [%i]\tset duration of emulation in seconds\n" + "\t--boot [path]\tspecify an external bootrom\n", + argv[0], size, duration/10); + + return 0; + } - if (bootrom_path ? get_image_for_rom(input, bootrom_path, bitmap, &cgbFlag) - : get_image_for_rom_alt(input, bootrom, bootrom_size, bitmap, &cgbFlag)) { - return -1; - } + uint32_t bitmap[BITMAP_WIDTH*BITMAP_HEIGHT]; + uint8_t cgbFlag = 0; - unsigned template_width, template_height; - uint32_t* template; - unsigned char* template_data; - size_t template_size; - - switch (cgbFlag) { - case 0xC0: - template_data = ColorCartridgeTemplate_png; - template_size = ColorCartridgeTemplate_png_size; - break; - - case 0x80: - template_data = UniversalCartridgeTemplate_png; - template_size = UniversalCartridgeTemplate_png_size; - break; - - default: - template_data = CartridgeTemplate_png; - template_size = CartridgeTemplate_png_size; - } + if (bootrom_path ? get_image_for_rom(input, bootrom_path, bitmap, &cgbFlag) + : get_image_for_rom_alt(input, bootrom, bootrom_size, bitmap, &cgbFlag)) { + return -1; + } - if (decode32(&template, &template_width, &template_height, template_data, template_size)) { - fprintf(stderr, "Failed to decode template\n"); - return -1; - } + unsigned template_width, template_height; + uint32_t* template; + unsigned char* template_data; + size_t template_size; + + switch (cgbFlag) { + case 0xC0: + template_data = ColorCartridgeTemplate_png; + template_size = ColorCartridgeTemplate_png_size; + break; + + case 0x80: + template_data = UniversalCartridgeTemplate_png; + template_size = UniversalCartridgeTemplate_png_size; + break; + + default: + template_data = CartridgeTemplate_png; + template_size = CartridgeTemplate_png_size; + } + + if (decode32(&template, &template_width, &template_height, template_data, template_size)) { + fprintf(stderr, "Failed to decode template\n"); + return -1; + } - uint32_t* canvas = local_malloc(template_width * template_height * sizeof(uint32_t)); - // clear the canvas so we don't end up with garbage data somewhere - memset(canvas, 0, template_width * template_height * sizeof(uint32_t)); + uint32_t* canvas = local_malloc(template_width * template_height * sizeof(uint32_t)); + // clear the canvas so we don't end up with garbage data somewhere + memset(canvas, 0, template_width * template_height * sizeof(uint32_t)); - uint32_t* screen = local_malloc(UPSCALED_WIDTH * UPSCALED_HEIGHT * sizeof(uint32_t)); - scale_image(bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, screen, SCALE_MULTIPLIER, 1); + uint32_t* screen = local_malloc(UPSCALED_WIDTH * UPSCALED_HEIGHT * sizeof(uint32_t)); + scale_image(bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, screen, SCALE_MULTIPLIER, 1); - for (signed y = 0; y < UPSCALED_HEIGHT; y++) { - for (signed x = 0; x < UPSCALED_WIDTH; x++) { - canvas[x + X_OFFSET + ((y + Y_OFFSET) * template_height)] = screen[x + (y * UPSCALED_WIDTH)]; - } + for (signed y = 0; y < UPSCALED_HEIGHT; y++) { + for (signed x = 0; x < UPSCALED_WIDTH; x++) { + canvas[x + X_OFFSET + ((y + Y_OFFSET) * template_height)] = screen[x + (y * UPSCALED_WIDTH)]; } + } - free(screen); + free(screen); - for (signed y = 0; y < template_height; y++) { - for (signed x = 0; x < template_width; x++) { - canvas[x + (y * template_width)] = alpha_blend( - canvas[x + (y * template_width)], - template[x + (y * template_width)] - ); - } + for (signed y = 0; y < template_height; y++) { + for (signed x = 0; x < template_width; x++) { + canvas[x + (y * template_width)] = alpha_blend( + canvas[x + (y * template_width)], + template[x + (y * template_width)] + ); } + } - uint32_t* final = local_malloc(size * size * sizeof(uint32_t)); - scale_image(canvas, template_width, template_height, final, (double)size/template_width, template_width/size); + uint32_t* final = local_malloc(size * size * sizeof(uint32_t)); + scale_image(canvas, template_width, template_height, final, (double)size/template_width, template_width/size); - encode32_file(output, final, size, size); + encode32_file(output, final, size, size); - free(final); - free(canvas); - free(template); - } - else - { - fprintf(stderr, "Usage: %s [--size size of output] [--boot path to boot ROM] input output...\n", argv[0]); - } + free(final); + free(canvas); + free(template); } -- cgit v1.2.3