Skip to content

Instantly share code, notes, and snippets.

@jakobrs
Created February 2, 2020 19:32
Show Gist options
  • Save jakobrs/758823416171d5529ca858c22751d676 to your computer and use it in GitHub Desktop.
Save jakobrs/758823416171d5529ca858c22751d676 to your computer and use it in GitHub Desktop.
Adds basic argument handling to VVVVVV
From 4c74fd47290cb0c4f00c700943a5b47e1651b013 Mon Sep 17 00:00:00 2001
From: jakobrs <[email protected]>
Date: Sat, 1 Feb 2020 12:12:54 +0100
Subject: [PATCH] Add basic argument handling
---
desktop_version/src/FileSystemUtils.cpp | 13 +++-
desktop_version/src/FileSystemUtils.h | 2 +-
desktop_version/src/main.cpp | 88 +++++++++++++++++++++++--
3 files changed, 94 insertions(+), 9 deletions(-)
diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp
index 2a8f7f2..3e3f4ca 100644
--- a/desktop_version/src/FileSystemUtils.cpp
+++ b/desktop_version/src/FileSystemUtils.cpp
@@ -39,7 +39,7 @@ void PLATFORM_getOSDirectory(char* output);
void PLATFORM_migrateSaveData(char* output);
void PLATFORM_copyFile(const char *oldLocation, const char *newLocation);
-int FILESYSTEM_init(char *argvZero)
+int FILESYSTEM_init(char *argvZero, const char *assets)
{
char output[MAX_PATH];
int mkdirResult;
@@ -79,8 +79,15 @@ int FILESYSTEM_init(char *argvZero)
}
/* Mount the stock content last */
- strcpy(output, PHYSFS_getBaseDir());
- strcat(output, "data.zip");
+ if (assets != NULL)
+ strcpy(output, assets);
+ else {
+ strcpy(output, PHYSFS_getBaseDir());
+ strcat(output, "data.zip");
+ }
+
+ printf("Assets: %s\n", output);
+
if (!PHYSFS_mount(output, NULL, 1))
{
puts("Error: data.zip missing!");
diff --git a/desktop_version/src/FileSystemUtils.h b/desktop_version/src/FileSystemUtils.h
index d3c551e..d0e40c2 100644
--- a/desktop_version/src/FileSystemUtils.h
+++ b/desktop_version/src/FileSystemUtils.h
@@ -6,7 +6,7 @@
#include "tinyxml.h"
-int FILESYSTEM_init(char *argvZero);
+int FILESYSTEM_init(char *argvZero, const char *assets);
void FILESYSTEM_deinit();
char *FILESYSTEM_getUserSaveDirectory();
diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp
index 4dba0fa..596d237 100644
--- a/desktop_version/src/main.cpp
+++ b/desktop_version/src/main.cpp
@@ -42,12 +42,92 @@ KeyPoll key;
mapclass map;
entityclass obj;
+int parseArgs(int argc, char *argv[], char*& renderer, char*& assets) {
+ int curindex = 1;
+
+ while (curindex < argc) {
+ char *curarg = argv[curindex];
+
+ if (strcmp(curarg, "--help") == 0) {
+ printf("VVVVVV version $VERSION (thelettervsixtim.es)\n");
+ printf("Usage: VVVVVV [options]\n");
+ printf("\n");
+ printf("Options:\n");
+ printf(" --help Show this help message\n");
+ printf(" --renderer <renderer> What render driver to use\n");
+ printf(" --assets <assets> Absolute location of data.zip\n");
+
+ return 1;
+ } else if (strcmp(curarg, "--renderer") == 0) {
+ curindex++;
+
+ if (curindex == argc) { // Missing argument
+ printf("Option --renderer requires an argument.\n");
+ printf("Run VVVVVV --help for help.\n");
+
+ return 1;
+ }
+
+ //SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER, argv[curindex], SDL_HINT_OVERRIDE);
+ renderer = argv[curindex];
+ } else if (strcmp(curarg, "--assets") == 0) {
+ curindex++;
+
+ if (curindex == argc) {
+ printf("Option --assets requires an argument.\n");
+ printf("Run VVVVVV --help for help.\n");
+
+ return 1;
+ }
+
+ assets = argv[curindex];
+ } else if (strcmp(curarg, "-renderer") == 0) {
+ curindex++;
+
+ if (curindex == argc) {
+ printf("Legacy option -renderer requires an argument.\n");
+ printf("Please switch to --renderer instead.\n");
+ printf("Run VVVVVV --help for help.\n");
+
+ return 1;
+ }
+
+ renderer = argv[curindex];
+ } else {
+ if (curarg[0] == '-')
+ if (curarg[1] == '-')
+ printf("Unrecognised option %s\n", curarg);
+ else
+ printf("I have no idea what you're doing.\n");
+ else
+ printf("VVVVVV takes no non-option arguments.\n");
+
+ return 1;
+ }
+
+ curindex++;
+ }
+
+ return 0;
+}
+
int main(int argc, char *argv[])
{
- if(!FILESYSTEM_init(argv[0]))
+ char *renderer = NULL,
+ *assets = NULL;
+
+ {
+ int res = parseArgs(argc, argv, renderer, assets);
+
+ if (res)
+ return res;
+ }
+
+ if(!FILESYSTEM_init(argv[0], assets))
{
return 1;
}
+
SDL_Init(
SDL_INIT_VIDEO |
SDL_INIT_AUDIO |
@@ -55,10 +135,8 @@ int main(int argc, char *argv[])
SDL_INIT_GAMECONTROLLER
);
- if (argc > 2 && strcmp(argv[1], "-renderer") == 0)
- {
- SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER, argv[2], SDL_HINT_OVERRIDE);
- }
+ if (renderer != NULL)
+ SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER, renderer, SDL_HINT_OVERRIDE);
NETWORK_init();
--
2.23.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment