textpsys

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