From e4ad2c9362254ab3213c4cb7c743b6bbd72b6346 Mon Sep 17 00:00:00 2001 From: zlago Date: Mon, 30 Sep 2024 15:57:29 +0200 Subject: commandline parameters --- src/main.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 116 insertions(+), 9 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 0e53a68..b2244d2 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,8 @@ #include "main.h" +#include + SDL_Window *window = NULL; SDL_Renderer *renderer = NULL; @@ -37,14 +39,109 @@ struct entity player[1] = {0}; struct entity *player_new(void); int player_property(struct entity *const restrict entity, char const *const restrict property, char const *const restrict value); -int main(int argc, char **argv) { +int main(int const argc, char *const *const argv) { + struct option opts[] = { + {"help", 0, NULL, 'h'}, + {"scale", 1, NULL, 's'}, + {"resizable", 2, NULL, 'r'}, + {"fullscreen", 2, NULL, 'f'}, + {"maximize", 2, NULL, 'm'}, + {"border", 2, NULL, 'b'}, + {NULL, 0, NULL, 0 }, + }; + int opt, li; + unsigned scale = 0, flags = 0, error = 0; + while ((opt = getopt_long(argc, argv, "h", opts, &li)) != -1) { + switch (opt) { + case 'h': + printf("usage:\n\t%s\n\t%s [mod.zip]\n", argv[0], argv[0]); + return 0; + + case 's': + scale = atoi(optarg); + break; + + case 'r': + if (optarg == NULL || strcmp(optarg, "true") == 0) { + flags |= SDL_WINDOW_RESIZABLE; + } else if (strcmp(optarg, "false") == 0) { + flags &= ~SDL_WINDOW_RESIZABLE; + } else { + fprintf(stderr, "%s=%s -- expected 'true' or 'false'\n", opts[li].name, optarg); + error = 1; + } + break; + + case 'f': + if (optarg == NULL || strcmp(optarg, "true") == 0) { + flags |= SDL_WINDOW_FULLSCREEN; + } else if (strcmp(optarg, "false") == 0) { + flags &= ~SDL_WINDOW_FULLSCREEN; + } else { + fprintf(stderr, "%s=%s -- expected 'true' or 'false'\n", opts[li].name, optarg); + error = 1; + } + break; + + case 'm': + if (optarg == NULL || strcmp(optarg, "true") == 0) { + flags |= SDL_WINDOW_MAXIMIZED; + } else if (strcmp(optarg, "false") == 0) { + flags &= ~SDL_WINDOW_MAXIMIZED; + } else { + fprintf(stderr, "%s=%s -- expected 'true' or 'false'\n", opts[li].name, optarg); + error = 1; + } + break; + + case 'b': + if (optarg == NULL || strcmp(optarg, "true") == 0) { + flags &= ~SDL_WINDOW_BORDERLESS; + } else if (strcmp(optarg, "false") == 0) { + flags |= SDL_WINDOW_BORDERLESS; + } else { + fprintf(stderr, "%s=%s -- expected 'true' or 'false'\n", opts[li].name, optarg); + error = 1; + } + break; + + default: + error = 1; + } + } + if (error) { + return EXIT_FAILURE; + } + if (SDL_Init(INIT_SUBSYSTEMS)) { fprintf(stderr, "failed to initialize SDL2: %s\n", SDL_GetError()); return EXIT_FAILURE; } SDL_StopTextInput(); + if (scale == 0) { + SDL_DisplayMode dm; + if (SDL_GetDesktopDisplayMode(0, &dm) != 0) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "couldnt get desktop size", SDL_GetError(), NULL); + fprintf(stderr, "info: couldnt get desktop size %s\n", SDL_GetError()); + flags |= SDL_WINDOW_RESIZABLE; + } else { + int x = dm.w / 2 / WINDOW_WIDTH; + int y = dm.h / 2 / WINDOW_HEIGHT; + if (x < y) { + scale = x; + } else { + scale = y; + } + if (scale == 0) { + scale = 1; + } + if (dm.refresh_rate != 60) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "refresh rate", "this game currently only runs well on 60Hz displays", NULL); + } + } + } - window = SDL_CreateWindow(":3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN); + window = SDL_CreateWindow(":3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH * scale, WINDOW_HEIGHT * scale, flags | SDL_WINDOW_HIDDEN); if (window == NULL) { goto end; } @@ -57,13 +154,23 @@ int main(int argc, char **argv) { { res_init(); - void *a = util_executableRelativePath("assets.res", *argv, 0); - puts(a); - if (loadResources(a)) { - fputs("loading resources failed\n", stderr); - return 1; + if (optind == argc) { + void *a = util_executableRelativePath("assets.res", *argv, 0); + puts(a); + if (loadResources(a)) { + fputs("loading resources failed\n", stderr); + return 1; + } + free(a); + } else { + while (optind < argc) { + if (loadResources(argv[optind])) { + fprintf(stderr, "loading %s failed\n", argv[optind]); + return 1; + } + optind++; + } } - free(a); struct blob map = res_get_map("untitled"); tilemap = tilemap_load(map.data, map.size); //memmem(map.data + tilemap->byte_count, map.size - tilemap->byte_count, (char []) {0, 0}, 2); @@ -71,7 +178,7 @@ int main(int argc, char **argv) { SDL_SetRenderTarget(renderer, NULL); } - unsigned error; + /* unsigned error; // identical variable is declared higher up */ struct plum_image *image = plum_load_image(game_icon, game_icon_end - game_icon, PLUM_COLOR_32 | PLUM_ALPHA_INVERT, &error); if (image == NULL) { fprintf(stderr, "error: libplum: %s\n", plum_get_error_text(error)); -- cgit 1.4.1-2-gfad0