fullscreen
authorJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 30 Dec 2021 12:57:31 +0000 (14:57 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 30 Dec 2021 12:57:31 +0000 (14:57 +0200)
.gitignore
src/cfgopt.c [new file with mode: 0644]
src/cfgopt.h [new file with mode: 0644]
src/demo.c
src/demo.h
src/pc/main.c [new file with mode: 0644]
src/pc/main_glut.c [deleted file]

index fdf3b67..bb59f12 100644 (file)
@@ -9,3 +9,4 @@ keystore.jks
 data
 apkbuild
 *.exe
+*.cfg
diff --git a/src/cfgopt.c b/src/cfgopt.c
new file mode 100644 (file)
index 0000000..9f702a9
--- /dev/null
@@ -0,0 +1,141 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#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 <name>  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<argc; i++) {
+               if(argv[i][0] == '-') {
+                       if(strcmp(argv[i], "-music") == 0) {
+                               opt.music = 1;
+                       } else if(strcmp(argv[i], "-nomusic") == 0) {
+                               opt.music = 0;
+                       } else if(strcmp(argv[i], "-scr") == 0 || strcmp(argv[i], "-screen") == 0) {
+                               scrname = argv[++i];
+                       } else if(strcmp(argv[i], "-vsync") == 0) {
+                               opt.vsync = 1;
+                       } else if(strcmp(argv[i], "-novsync") == 0) {
+                               opt.vsync = 0;
+                       } else if(strcmp(argv[i], "-fs") == 0) {
+                               opt.fullscreen = 1;
+                       } else if(strcmp(argv[i], "-win") == 0) {
+                               opt.fullscreen = 0;
+                       } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
+                               printf(usagefmt, argv[0]);
+                               exit(0);
+                       } else {
+                               fprintf(stderr, "invalid option: %s\n", argv[i]);
+                               fprintf(stderr, usagefmt, argv[0]);
+                               return -1;
+                       }
+               } else {
+                       if(scrname) {
+                               fprintf(stderr, "unexpected option: %s\n", argv[i]);
+                               fprintf(stderr, usagefmt, argv[0]);
+                               return -1;
+                       }
+                       scrname = argv[i];
+               }
+       }
+
+       if(scrname) {
+               opt.scrname = scrname;
+       }
+       return 0;
+}
+
+
+static char *strip_space(char *s)
+{
+       int len;
+       char *end;
+
+       while(*s && isspace(*s)) ++s;
+       if(!*s) return 0;
+
+       if((end = strrchr(s, '#'))) {
+               --end;
+       } else {
+               len = strlen(s);
+               end = s + len - 1;
+       }
+
+       while(end > 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 (file)
index 0000000..bedb4e8
--- /dev/null
@@ -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_ */
index e17d7ec..4acbb8c 100644 (file)
@@ -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;
 }
index e58f071..c002d06 100644 (file)
@@ -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 (file)
index 0000000..6d46e5f
--- /dev/null
@@ -0,0 +1,132 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#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 (file)
index a21cbca..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#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;
-}