textpsys

view src/main.cc @ 0:a4ffd9e6984c

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Aug 2015 09:13:48 +0300
parents
children 57c6f7b70126
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 "tbomb.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(!tbomb_init()) {
42 return 1;
43 }
44 atexit(tbomb_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 tbomb_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;
95 case ' ':
96 tbomb_dbg();
97 break;
98 }
99 }
101 bool parse_args(int argc, char **argv)
102 {
103 for(int i=1; i<argc; i++) {
104 if(argv[i][0] == '-' && argv[i][2] == 0) {
105 switch(argv[i][1]) {
106 case 'f':
107 fullscreen = true;
108 break;
110 case 's':
111 if(sscanf(argv[++i], "%dx%d", &win_width, &win_height) != 2) {
112 fprintf(stderr, "-s must be followed by a resolution WxH\n");
113 return false;
114 }
115 break;
117 case 'h':
118 printf("options:\n");
119 printf(" -s WxH set window size\n");
120 printf(" -f fullscreen\n");
121 printf(" -h print this usage info and exit\n");
122 exit(0);
124 default:
125 fprintf(stderr, "invalid option: %s\n", argv[i]);
126 return false;
127 }
128 } else {
129 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
130 return false;
131 }
132 }
133 return true;
134 }