From 609e3eea7c9954a032e22fbf0c0f9754464aa440 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Wed, 20 Dec 2017 17:06:03 -0600 Subject: [PATCH] Add size option, functions to get width and height --- include/log.h | 1 + include/option.h | 3 +++ src/option.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/include/log.h b/include/log.h index b3c2df4..247ce2c 100644 --- a/include/log.h +++ b/include/log.h @@ -12,6 +12,7 @@ enum log_errcode { ERR_OOM, // out of memory ERR_OOB, // out of bounds ERR_BADFILE, + ERR_BADOPT, // bad option (e.g. from getopt) ERR_GFXINIT, // couldn't initialize graphics ERR_GFXOP, // we couldn't execute a specific graphic operation }; diff --git a/include/option.h b/include/option.h index 48caf95..e63fbce 100644 --- a/include/option.h +++ b/include/option.h @@ -15,5 +15,8 @@ extern void option_print_help(); extern int option_read_file(int, const char *); extern void option_set_error(const char *); extern void option_set_input(int, FILE *); +extern int option_set_size(const char *); +extern int option_get_width(); +extern int option_get_height(); #endif diff --git a/src/option.c b/src/option.c index 85eeb44..b166859 100644 --- a/src/option.c +++ b/src/option.c @@ -12,6 +12,7 @@ #include #include "option.h" +#include "log.h" /* * These are the file inputs we may have to the system. What their @@ -32,15 +33,22 @@ static FILE *input2 = NULL; */ static char error_buffer[ERRBUF_SIZE] = ""; +/* + * The default width and height of the window + */ +static int width = 640; +static int height = 480; + /* * These are all of the options we allow in our long-form options. It's * a bit faster to identify them by integer symbols than to do string * comparisons. */ enum options { - HELP, DISK1, DISK2, + HELP, + SIZE, }; /* @@ -50,6 +58,7 @@ static struct option long_options[] = { { "disk1", 1, NULL, DISK1 }, { "disk2", 1, NULL, DISK2 }, { "help", 0, NULL, HELP }, + { "size", 1, NULL, SIZE }, }; /* @@ -113,6 +122,12 @@ option_parse(int argc, char **argv) // The help option should terminate normal execution return 0; + + case SIZE: + if (option_set_size(optarg) != OK) { + return 0; + } + break; } // We seem to have a request to load a file, so let's do so. @@ -201,5 +216,34 @@ option_print_help() fprintf(stderr, "\ --disk1=FILE Load FILE into disk drive 1\n\ --disk2=FILE Load FILE into disk drive 2\n\ - --help Print this help message\n"); + --help Print this help message\n\ + --size=WIDTHxHEIGHT Use WIDTH and HEIGHT for window size\n\ + (only 640x480 and 800x600 are supported)\n"); +} + +int +option_set_size(const char *size) +{ + if (strcmp(size, "640x480") == 0) { + return OK; + } else if (strcmp(size, "800x600") == 0) { + width = 800; + height = 600; + return OK; + } + + snprintf(error_buffer, ERRBUF_SIZE, "Ignoring bad window size: %s", size); + return ERR_BADOPT; +} + +int +option_get_width() +{ + return width; +} + +int +option_get_height() +{ + return height; }