textpsys

view src/main.cc @ 2:4b1360a5d54d

switch between messages, and non-interactive
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 20 Aug 2015 04:52:30 +0300
parents 57c6f7b70126
children b1c8d2784c72
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #ifndef __APPLE__
5 #include <GL/glut.h>
6 #else
7 #include <GLUT/glut.h>
8 #endif
9 #include "effect.h"
11 void display();
12 void idle();
13 void reshape(int x, int y);
14 void keyboard(unsigned char key, int x, int y);
15 bool parse_args(int argc, char **argv);
17 static int win_width = 1280, win_height = 800;
18 static bool fullscreen;
20 static unsigned long start_time;
22 int main(int argc, char **argv)
23 {
24 glutInit(&argc, argv);
25 if(!parse_args(argc, argv)) {
26 return 1;
27 }
28 glutInitWindowSize(win_width, win_height);
29 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
30 glutCreateWindow("textbomb");
32 if(fullscreen) {
33 glutFullScreen();
34 }
36 glutDisplayFunc(display);
37 glutIdleFunc(idle);
38 glutReshapeFunc(reshape);
39 glutKeyboardFunc(keyboard);
41 if(!fx_init()) {
42 return 1;
43 }
44 atexit(fx_cleanup);
46 start_time = glutGet(GLUT_ELAPSED_TIME);
47 glutMainLoop();
48 return 0;
49 }
51 void display()
52 {
53 unsigned long msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
55 glClear(GL_COLOR_BUFFER_BIT);
57 fx_draw(msec);
59 glutSwapBuffers();
60 assert(glGetError() == GL_NO_ERROR);
61 }
63 void idle()
64 {
65 glutPostRedisplay();
66 }
68 void reshape(int x, int y)
69 {
70 float aspect = (float)x / (float)y;
72 glViewport(0, 0, x, y);
74 glMatrixMode(GL_PROJECTION);
75 glLoadIdentity();
76 glScalef(1.0 / aspect, 1.0, 1.0);
77 }
79 void keyboard(unsigned char key, int x, int y)
80 {
81 switch(key) {
82 case 27:
83 exit(0);
85 case 'f':
86 case 'F':
87 fullscreen = !fullscreen;
88 if(fullscreen) {
89 glutFullScreen();
90 } else {
91 glutReshapeWindow(win_width, win_height);
92 }
93 break;
94 }
95 }
97 bool parse_args(int argc, char **argv)
98 {
99 for(int i=1; i<argc; i++) {
100 if(argv[i][0] == '-' && argv[i][2] == 0) {
101 switch(argv[i][1]) {
102 case 'f':
103 fullscreen = true;
104 break;
106 case 's':
107 if(sscanf(argv[++i], "%dx%d", &win_width, &win_height) != 2) {
108 fprintf(stderr, "-s must be followed by a resolution WxH\n");
109 return false;
110 }
111 break;
113 case 'h':
114 printf("options:\n");
115 printf(" -s WxH set window size\n");
116 printf(" -f fullscreen\n");
117 printf(" -h print this usage info and exit\n");
118 exit(0);
120 default:
121 fprintf(stderr, "invalid option: %s\n", argv[i]);
122 return false;
123 }
124 } else {
125 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
126 return false;
127 }
128 }
129 return true;
130 }