From 39dec6b602af5f02cc5297c21da7207debff52b8 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 30 Dec 2021 14:57:31 +0200 Subject: [PATCH] fullscreen --- .gitignore | 1 + src/cfgopt.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cfgopt.h | 16 ++++++ src/demo.c | 8 +++ src/demo.h | 1 + src/pc/main.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ src/pc/main_glut.c | 101 ------------------------------------- 7 files changed, 299 insertions(+), 101 deletions(-) create mode 100644 src/cfgopt.c create mode 100644 src/cfgopt.h create mode 100644 src/pc/main.c delete mode 100644 src/pc/main_glut.c diff --git a/.gitignore b/.gitignore index fdf3b67..bb59f12 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ keystore.jks data apkbuild *.exe +*.cfg diff --git a/src/cfgopt.c b/src/cfgopt.c new file mode 100644 index 0000000..9f702a9 --- /dev/null +++ b/src/cfgopt.c @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include "cfgopt.h" + +struct options opt = { + 0, /* screen name */ + 0, /* fullscreen */ + 1, /* vsync */ + 0, /* music */ +}; + +static const char *usagefmt = "Usage: %s [options]\n" + "Options:\n" + " -music/-nomusic toggle music playback\n" + " -scr,-screen ignore demoscript, run specific screen\n" + " -fs/-win run fullscreen/windowed\n" + " -vsync/-novsync toggle vsync\n" + " -h,-help print usage and exit\n"; + +int parse_args(int argc, char **argv) +{ + int i; + char *scrname = 0; + + for(i=1; i s && isspace(*end)) *end-- = 0; + return end > s ? s : 0; +} + +static int bool_value(char *s) +{ + char *ptr = s; + while(*ptr) { + *ptr = tolower(*ptr); + ++ptr; + } + + return strcmp(s, "true") == 0 || strcmp(s, "yes") == 0 || strcmp(s, "1") == 0; +} + +int load_config(const char *fname) +{ + FILE *fp; + char buf[256]; + int nline = 0; + + if(!(fp = fopen(fname, "rb"))) { + return 0; /* just ignore missing config files */ + } + + while(fgets(buf, sizeof buf, fp)) { + char *line, *key, *value; + + ++nline; + if(!(line = strip_space(buf))) { + continue; + } + + if(!(value = strchr(line, '='))) { + fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline); + return -1; + } + *value++ = 0; + + if(!(key = strip_space(line)) || !(value = strip_space(value))) { + fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline); + return -1; + } + + if(strcmp(line, "music") == 0) { + opt.music = bool_value(value); + } else if(strcmp(line, "screen") == 0) { + opt.scrname = strdup(value); + } else if(strcmp(line, "vsync") == 0) { + opt.vsync = bool_value(value); + } else if(strcmp(line, "fullscreen") == 0) { + opt.fullscreen = bool_value(value); + } else { + fprintf(stderr, "%s:%d invalid option: %s\n", fname, nline, line); + return -1; + } + } + return 0; +} diff --git a/src/cfgopt.h b/src/cfgopt.h new file mode 100644 index 0000000..bedb4e8 --- /dev/null +++ b/src/cfgopt.h @@ -0,0 +1,16 @@ +#ifndef CFGOPT_H_ +#define CFGOPT_H_ + +struct options { + const char *scrname; + int fullscreen; + int vsync; + int music; +}; + +extern struct options opt; + +int parse_args(int argc, char **argv); +int load_config(const char *fname); + +#endif /* CFGOPT_H_ */ diff --git a/src/demo.c b/src/demo.c index e17d7ec..4acbb8c 100644 --- a/src/demo.c +++ b/src/demo.c @@ -37,6 +37,14 @@ int demo_init(void) if(dsys_init("data/demoscript") == -1) { return -1; } + if(opt.scrname) { + struct demoscreen *scr = dsys_find_screen(opt.scrname); + if(scr) { + dsys_run_screen(scr); + } else { + fprintf(stderr, "ignoring screen option, no such screen: %s\n", opt.scrname); + } + } return 0; } diff --git a/src/demo.h b/src/demo.h index e58f071..c002d06 100644 --- a/src/demo.h +++ b/src/demo.h @@ -6,6 +6,7 @@ #include "demosys.h" #include "assman.h" #include "util.h" +#include "cfgopt.h" enum { KEY_F1 = 128, diff --git a/src/pc/main.c b/src/pc/main.c new file mode 100644 index 0000000..6d46e5f --- /dev/null +++ b/src/pc/main.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include "opengl.h" +#include "miniglut.h" +#include "demo.h" +#include "cfgopt.h" + +static void display(void); +static void keypress(unsigned char key, int x, int y); +static void skeypress(int key, int x, int y); +static void mouse(int bn, int st, int x, int y); +static int translate_key(int key); + +static int prev_xsz, prev_ysz; +static long start_time; + + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + + load_config("demo.cfg"); + if(parse_args(argc, argv) == -1) { + return 1; + } + + glutInitWindowSize(1280, 800); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("Mindlapse"); + + glutDisplayFunc(display); + glutIdleFunc(glutPostRedisplay); + glutReshapeFunc(demo_reshape); + glutKeyboardFunc(keypress); + glutSpecialFunc(skeypress); + glutMouseFunc(mouse); + glutMotionFunc(demo_motion); + + if(opt.fullscreen) { + prev_xsz = glutGet(GLUT_WINDOW_WIDTH); + prev_ysz = glutGet(GLUT_WINDOW_HEIGHT); + glutFullScreen(); + } + + if(demo_init() == -1) { + return 1; + } + atexit(demo_cleanup); + + start_time = glutGet(GLUT_ELAPSED_TIME); + glutMainLoop(); + return 0; +} + +void swap_buffers(void) +{ + glutSwapBuffers(); +} + +static void display(void) +{ + time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time; + + demo_display(); + + glutSwapBuffers(); + assert(glGetError() == GL_NO_ERROR); +} + +static void keypress(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + glutExit(); + break; + + case 'f': + case 'F': + opt.fullscreen ^= 1; + if(opt.fullscreen) { + prev_xsz = glutGet(GLUT_WINDOW_WIDTH); + prev_ysz = glutGet(GLUT_WINDOW_HEIGHT); + glutFullScreen(); + } else { + glutReshapeWindow(prev_xsz, prev_ysz); + } + break; + + default: + demo_keyboard(key, 1); + } +} + +static void skeypress(int key, int x, int y) +{ + if((key = translate_key(key))) { + demo_keyboard(key, 1); + } +} + +static void mouse(int bn, int st, int x, int y) +{ + int bidx = bn - GLUT_LEFT_BUTTON; + int press = st == GLUT_DOWN; + + demo_mouse(bidx, press, x, y); +} + +static int translate_key(int key) +{ + if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) { + return key - GLUT_KEY_F1 + KEY_F1; + } + switch(key) { + case GLUT_KEY_LEFT: + return KEY_LEFT; + case GLUT_KEY_RIGHT: + return KEY_RIGHT; + case GLUT_KEY_UP: + return KEY_UP; + case GLUT_KEY_DOWN: + return KEY_DOWN; + case GLUT_KEY_PAGE_UP: + return KEY_PGUP; + case GLUT_KEY_PAGE_DOWN: + return KEY_PGDOWN; + default: + break; + } + return 0; +} diff --git a/src/pc/main_glut.c b/src/pc/main_glut.c deleted file mode 100644 index a21cbca..0000000 --- a/src/pc/main_glut.c +++ /dev/null @@ -1,101 +0,0 @@ -#include -#include -#include -#include "opengl.h" -#include "miniglut.h" -#include "demo.h" - -static void display(void); -static void keypress(unsigned char key, int x, int y); -static void skeypress(int key, int x, int y); -static void mouse(int bn, int st, int x, int y); -static int translate_key(int key); - -static long start_time; - - -int main(int argc, char **argv) -{ - glutInit(&argc, argv); - glutInitWindowSize(1280, 800); - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); - glutCreateWindow("Mindlapse"); - - glutDisplayFunc(display); - glutIdleFunc(glutPostRedisplay); - glutReshapeFunc(demo_reshape); - glutKeyboardFunc(keypress); - glutSpecialFunc(skeypress); - glutMouseFunc(mouse); - glutMotionFunc(demo_motion); - - if(demo_init() == -1) { - return 1; - } - atexit(demo_cleanup); - - start_time = glutGet(GLUT_ELAPSED_TIME); - glutMainLoop(); - return 0; -} - -void swap_buffers(void) -{ - glutSwapBuffers(); -} - -static void display(void) -{ - time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time; - - demo_display(); - - glutSwapBuffers(); - assert(glGetError() == GL_NO_ERROR); -} - -static void keypress(unsigned char key, int x, int y) -{ - if(key == 27) exit(0); - - demo_keyboard(key, 1); -} - -static void skeypress(int key, int x, int y) -{ - if((key = translate_key(key))) { - demo_keyboard(key, 1); - } -} - -static void mouse(int bn, int st, int x, int y) -{ - int bidx = bn - GLUT_LEFT_BUTTON; - int press = st == GLUT_DOWN; - - demo_mouse(bidx, press, x, y); -} - -static int translate_key(int key) -{ - if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) { - return key - GLUT_KEY_F1 + KEY_F1; - } - switch(key) { - case GLUT_KEY_LEFT: - return KEY_LEFT; - case GLUT_KEY_RIGHT: - return KEY_RIGHT; - case GLUT_KEY_UP: - return KEY_UP; - case GLUT_KEY_DOWN: - return KEY_DOWN; - case GLUT_KEY_PAGE_UP: - return KEY_PGUP; - case GLUT_KEY_PAGE_DOWN: - return KEY_PGDOWN; - default: - break; - } - return 0; -} -- 1.7.10.4