Skip to content

Commit

Permalink
cli,video: add --width and --height arguments to set initial window size
Browse files Browse the repository at this point in the history
  • Loading branch information
Akaricchi committed May 1, 2024
1 parent 8d5ffe4 commit 4986c89
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ int cli_args(int argc, char **argv, CLIAction *a) {
{{"credits", no_argument, 0, 'c'}, "Show the credits scene and exit"},
{{"renderer", required_argument, 0, OPT_RENDERER}, "Choose the rendering backend", renderer_list},
{{"populate-cache", no_argument, 0, OPT_POPCACHE}, "Attempt to load all available resources, populating the cache, then exit"},
{{"width", required_argument, 0, 'W'}, "Set window width", "WIDTH"},
{{"height", required_argument, 0, 'H'}, "Set window height", "HEIGHT"},
{{"help", no_argument, 0, 'h'}, "Print help and exit"},
{{"version", no_argument, 0, 'v'}, "Print version and exit"},
{ 0 }
Expand Down Expand Up @@ -257,6 +259,12 @@ int cli_args(int argc, char **argv, CLIAction *a) {
case OPT_UNLOCKALL:
a->unlock_all = true;
break;
case 'W':
a->width = strtol(optarg, NULL, 10);
break;
case 'H':
a->height = strtol(optarg, NULL, 10);
break;
default:
UNREACHABLE;
}
Expand Down
2 changes: 2 additions & 0 deletions src/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct CLIAction {
CutsceneID cutscene;
bool force_intro;
bool unlock_all;
int width;
int height;
};

int cli_args(int argc, char **argv, CLIAction *a);
Expand Down
5 changes: 4 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ static void main_post_vfsinit(CallChainResult ccr) {
time_init();
init_global(&ctx->cli);
events_init();
video_init();
video_init(&(VideoInitParams) {
.width = ctx->cli.width,
.height = ctx->cli.height,
});
filewatch_init();
res_init();
r_post_init();
Expand Down
25 changes: 21 additions & 4 deletions src/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ static void video_init_framedump(void) {
memcpy(video.framedump.name_prefix, framedump_dir, video.framedump.name_prefix_len);
}

void video_init(void) {
void video_init(const VideoInitParams *params) {
video_init_sdl();

const char *driver = SDL_GetCurrentVideoDriver();
Expand Down Expand Up @@ -1007,11 +1007,28 @@ void video_init(void) {

r_init();

int w, h;
bool fullscreen;

if(params->width > 0 || params->height > 0) {
w = params->width;
h = params->height;
fullscreen = false;

if(w <= 0) {
w = rint(h * VIDEO_ASPECT_RATIO);
} else if(h <= 0) {
h = rint(w / VIDEO_ASPECT_RATIO);
}
} else {
w = config_get_int(CONFIG_VID_WIDTH);
h = config_get_int(CONFIG_VID_HEIGHT);
fullscreen = config_get_int(CONFIG_FULLSCREEN);
}

video_set_mode(
config_get_int(CONFIG_VID_DISPLAY),
config_get_int(CONFIG_VID_WIDTH),
config_get_int(CONFIG_VID_HEIGHT),
config_get_int(CONFIG_FULLSCREEN),
w, h, fullscreen,
config_get_int(CONFIG_VID_RESIZABLE)
);

Expand Down
7 changes: 6 additions & 1 deletion src/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ enum {

#define SCREEN_SIZE { SCREEN_W, SCREEN_H }

typedef struct VideoInitParams {
int width;
int height;
} VideoInitParams;

typedef union VideoMode {
// NOTE: These really should be floats, since this represents abstract screen coordinates, not pixels.
// However, SDL's API expects integers everywhere, so it does not really make sense.
Expand Down Expand Up @@ -62,7 +67,7 @@ typedef enum VideoCapabilityState {
VIDEO_CURRENTLY_UNAVAILABLE,
} VideoCapabilityState;

void video_init(void);
void video_init(const VideoInitParams *params);
void video_post_init(void);
void video_shutdown(void);
void video_set_mode(uint display, uint w, uint h, bool fs, bool resizable);
Expand Down

0 comments on commit 4986c89

Please sign in to comment.