Skip to content

Commit

Permalink
Add cmdline arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Oct 11, 2024
1 parent d36616f commit c8389f0
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 60 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10)

project(Sound)
project(sotn)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down Expand Up @@ -30,6 +30,7 @@ endif()
find_package(SDL2 REQUIRED)

set(SOURCE_FILES_PC
src/pc/main.c
src/pc/log.c
src/pc/stubs.c
src/pc/sotn.c
Expand Down
156 changes: 156 additions & 0 deletions src/pc/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
#include <game.h>
#include "pc.h"
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char* allowed_stages[] = {
"no0", "no1", "lib", "cat", "no2", "chi",
"dai", "np3", "cen", "no4", "are", "top",
"nz0", "nz1", "wrp", "no1_alt", "no0_alt", "",
"dre", "nz0_demo", "nz1_demo", "lib_demo", "bo7", "mar",
"bo6", "bo5", "bo4", "bo3", "bo2", "bo1",
"bo0", "st0", "rno0", "rno1", "rlib", "rcat",
"rno2", "rchi", "rdai", "rno3", "rcen", "rno4",
"rare", "rtop", "rnz0", "rnz1", "rwrp", "",
"", "", "", "", "", "rnz1_demo",
"rbo8", "rbo7", "rbo6", "rbo5", "rbo4", "rbo3",
"rbo2", "rbo1", "rbo0", "", "mad", "no3",
"iwa_load", "iga_load", "hagi_load", "sel", "te1", "te2",
"te3", "te4", "te5", "top_alt"};
static const char* allowed_players[] = {"alu", "ric", "mar"};
static const char* allowed_tests[] = {"sndlib"};
#define PARSE_PARAM(param, allowed) parseStrParam(param, allowed, LEN(allowed))
static int parseIntParam(const char* param) {
long i = strtol(param, NULL, 10);
if (i != LONG_MIN && i != LONG_MAX && i >= 0) {
return (int)i;
}
return -1;
}
static int parseStrParam(
const char* param, const char* allowedValues[], int n) {
long i;
if (isdigit(param[0])) {
i = strtol(param, NULL, 10);
if (i != LONG_MIN && i != LONG_MAX && i >= 0) {
return (int)i;
}
} else {
for (i = 0; i < n; i++) {
if (allowedValues[i][0] == '\0') {
continue;
}
if (!strcmp(param, allowedValues[i])) {
return i;
}
}
}
return -1;
}
static void printHelp(void) {
printf("Usage: ./sotn [OPTIONS]\n");
printf("Options:\n");
printf(" --disk <path> file name of the second track\n");
printf(" --stage <stage> stage name or ID (e.g., nz0)\n");
printf(" --player <name> player name or ID (e.g. ric)\n");
printf(" --scale <number> game resolution integer scale (default 2)\n");
printf(" --test <mode> run automated tests\n");
printf(" sndlib test sound library\n");
printf(" --help show this help message\n");
}
static void printAllowedParams(const char* allowedValues[], int n) {
int i;
printf("allowed params are: ");
for (i = 0; i < n - 1; i++) {
if (allowedValues[i][0] == '\0') {
continue;
}
printf("%s, ", allowedValues[i]);
}
printf("%s\n", allowedValues[i - 1]);
}
static bool parseArgs(
struct InitGameParams* outParams, int argc, char* argv[]) {
assert(!!outParams);
outParams->diskPath = NULL;
outParams->testMode = NO_TEST;
outParams->stage = -1;
outParams->player = -1;
outParams->scale = 2;

for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0) {
printHelp();
exit(0);
}
if (strcmp(argv[i], "--disk") == 0 && i + 1 < argc) {
outParams->diskPath = argv[++i];
} else if (strcmp(argv[i], "--stage") == 0 && i + 1 < argc) {
outParams->stage = PARSE_PARAM(argv[++i], allowed_stages);
if (outParams->stage < 0) {
printf("stage '%s' is invalid or not recognized\n", argv[i]);
printAllowedParams(allowed_stages, LEN(allowed_stages));
return false;
}
} else if (strcmp(argv[i], "--player") == 0 && i + 1 < argc) {
outParams->player = PARSE_PARAM(argv[++i], allowed_players);
if (outParams->player < 0) {
printf("player '%s' is invalid or not recognized\n", argv[i]);
printAllowedParams(allowed_players, LEN(allowed_players));
return false;
}
} else if (strcmp(argv[i], "--scale") == 0 && i + 1 < argc) {
outParams->scale = parseIntParam(argv[++i]);
if (outParams->scale < 1 || outParams->scale > 16) {
printf("invalid resolution scale %s\n", argv[i]);
return false;
}
} else if (strcmp(argv[i], "--test") == 0 && i + 1 < argc) {
outParams->testMode = PARSE_PARAM(argv[++i], allowed_tests) + 1;
if (outParams->testMode < 0) {
printf("test '%s' is invalid or not recognized\n", argv[i]);
printAllowedParams(allowed_tests, LEN(allowed_tests));
return false;
}
} else {
printf("argument %s not recognized", argv[i]);
return false;
}
}
return true;
}

static void testSndLib(void) {
#ifdef WANT_LIBSND_LLE
run_tests();
exit(0);
#else
printf("this test is only available for LLE builds\n");
exit(-1);
#endif
}

int main(int argc, char* argv[]) {
struct InitGameParams params;
if (!parseArgs(&params, argc, argv)) {
printHelp();
return -1;
}
switch (params.testMode) {
case NO_TEST:
break;
case TEST_SNDLIB:
testSndLib();
break;
}
if (!InitGame(&params)) {
return -1;
}
MainGame();
ResetGame();
}
16 changes: 0 additions & 16 deletions src/pc/null.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,4 @@ DRAWENV* MyPutDrawEnv(DRAWENV* env) {}

void MyDrawOTag(OT_TYPE* p) {}

int main(int argc, char* argv[]) {
const char* filename;

if (argc < 2) {
filename = "disks/sotn.us.bin";
} else {
filename = argv[1];
}
OpenCd(filename);

if (InitGame()) {
MainGame();
}
ResetGame();
}

int MyMoveImage(RECT* rect, int x, int y) {}
9 changes: 0 additions & 9 deletions src/pc/pc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ bool g_IsQuitRequested; // controls whenever MainGame should quit
bool g_TimedExit = false; // should we exit after some time?
int g_TimeLimit = 0; // number of frames before exiting

FILE* cd_fp = NULL;
void OpenCd(char* filename) {
cd_fp = fopen(filename, "rb");

if (!cd_fp) {
DEBUGF("Couldn't open CD.\n");
}
}

int CdReading();
void ExecCd();
void MyAudioCallback(void* data, u8* buffer, int length) {
Expand Down
18 changes: 17 additions & 1 deletion src/pc/pc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@

#define DISP_WIDTH 256
#define DISP_HEIGHT 256
#define SCREEN_SCALE 4

#define VRAM_W 1024
#define VRAM_H 512
#define VRAM_STRIDE 2048

enum TestMode {
NO_TEST,
TEST_SNDLIB,
};
struct InitGameParams {
const char* diskPath;
enum TestMode testMode;
int stage;
int player;
int scale;
};

struct FileOpenRead {
const char* filename;
FILE* file;
Expand All @@ -42,6 +53,11 @@ typedef struct FileUseContent {
void* param;
} FileLoad;

extern struct InitGameParams g_GameParams;
bool InitGame(struct InitGameParams* params);
void MainGame(void);
void ResetGame(void);

bool FileOpenRead(
bool (*cb)(const struct FileOpenRead*), const char* filename, void* param);
int FileReadToBuf(const char* filename, void* dst, int offset, size_t maxlen);
Expand Down
3 changes: 2 additions & 1 deletion src/pc/psxsdk/libcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ int CdControl(u_char com, u_char* param, u_char* result) {
CdlLOC* pos;

if (!cd_fp) {
DEBUGF("Cd not open.\n");
WARNF("Cd not open.\n");
return 1;
}

switch (com) {
Expand Down
7 changes: 4 additions & 3 deletions src/pc/render_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ void ShowVram4bpp(void) {
SDL_DestroyTexture(t);
}

extern struct InitGameParams g_GameParams;
int GlDrawSync(int mode) {
switch (g_DebugSdl) {
case DEBUG_SDL_SHOW_VRAM_16bpp:
Expand All @@ -146,7 +147,7 @@ int GlDrawSync(int mode) {
}

SDL_RenderPresent(g_Renderer);
SDL_RenderSetScale(g_Renderer, SCREEN_SCALE, SCREEN_SCALE);
SDL_RenderSetScale(g_Renderer, g_GameParams.scale, g_GameParams.scale);

// SDL event handling
SDL_Event event;
Expand All @@ -168,8 +169,8 @@ int GlDrawSync(int mode) {
}

DISPENV* GlPutDispEnv(DISPENV* env) {
int w = env->disp.w * SCREEN_SCALE;
int h = env->disp.h * SCREEN_SCALE;
int w = env->disp.w * g_GameParams.scale;
int h = env->disp.h * g_GameParams.scale;
if (g_WndWidth == w && g_WndHeight == h) {
return env;
}
Expand Down
7 changes: 4 additions & 3 deletions src/pc/render_soft.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ void CopyVram(void) {
SDL_DestroyTexture(t);
}

extern struct InitGameParams g_GameParams;
int SoftDrawSync(int mode) {
CopyVram();

SDL_RenderPresent(g_Renderer);
SDL_RenderSetScale(g_Renderer, SCREEN_SCALE, SCREEN_SCALE);
SDL_RenderSetScale(g_Renderer, g_GameParams.scale, g_GameParams.scale);

// SDL event handling
SDL_Event event;
Expand All @@ -87,8 +88,8 @@ int SoftDrawSync(int mode) {
}

DISPENV* SoftPutDispEnv(DISPENV* env) {
int w = env->disp.w * SCREEN_SCALE;
int h = env->disp.h * SCREEN_SCALE;
int w = env->disp.w * g_GameParams.scale;
int h = env->disp.h * g_GameParams.scale;
if (g_WndWidth == w && g_WndHeight == h) {
return env;
}
Expand Down
23 changes: 2 additions & 21 deletions src/pc/sdl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "sdl_defs.h"
#include "sdl2_macros.h"

extern struct InitGameParams g_GameParams;
extern bool g_IsQuitRequested;
extern u16 g_RawVram[VRAM_W * VRAM_H];
SDL_Window* g_Window = NULL;
Expand Down Expand Up @@ -40,7 +41,7 @@ bool InitPlatform() {

g_Window = SDL_CreateWindow(
"SOTN", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
DISP_WIDTH * SCREEN_SCALE, DISP_HEIGHT * SCREEN_SCALE,
DISP_WIDTH * g_GameParams.scale, DISP_HEIGHT * g_GameParams.scale,
SDL_WINDOW_SHOWN);
if (!g_Window) {
ERRORF("SDL_CreateWindow: %s", SDL_GetError());
Expand Down Expand Up @@ -227,23 +228,3 @@ u_long MyPadRead(int id) {

return pressed;
}

int main(int argc, char* argv[]) {
const char* filename;

if (argc < 2) {
filename = "disks/sotn.us.bin";
} else if (argc == 2 && !strcmp(argv[1], "test")) {
#ifdef WANT_LIBSND_LLE
run_tests();
#endif
} else {
filename = argv[1];
}
OpenCd(filename);

if (InitGame()) {
MainGame();
}
ResetGame();
}
Loading

0 comments on commit c8389f0

Please sign in to comment.