summary refs log tree commit diff
path: root/src/main.c
diff options
context:
space:
mode:
authorzlago2024-09-30 15:57:29 +0200
committerzlago2024-09-30 15:57:29 +0200
commite4ad2c9362254ab3213c4cb7c743b6bbd72b6346 (patch)
treed839f778a15f65b09fb0f0b773a500168e75afcc /src/main.c
parent45512bbc85188e3adb4eda597d0d2fa5530de651 (diff)
commandline parameters
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c125
1 files changed, 116 insertions, 9 deletions
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 <getopt.h>
+
 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));