textpsys

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.cc	Wed Aug 19 09:13:48 2015 +0300
     1.3 @@ -0,0 +1,134 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <assert.h>
     1.7 +#ifndef __APPLE__
     1.8 +#include <GL/glut.h>
     1.9 +#else
    1.10 +#include <GLUT/glut.h>
    1.11 +#endif
    1.12 +#include "tbomb.h"
    1.13 +
    1.14 +void display();
    1.15 +void idle();
    1.16 +void reshape(int x, int y);
    1.17 +void keyboard(unsigned char key, int x, int y);
    1.18 +bool parse_args(int argc, char **argv);
    1.19 +
    1.20 +static int win_width = 1280, win_height = 800;
    1.21 +static bool fullscreen;
    1.22 +
    1.23 +static unsigned long start_time;
    1.24 +
    1.25 +int main(int argc, char **argv)
    1.26 +{
    1.27 +	glutInit(&argc, argv);
    1.28 +	if(!parse_args(argc, argv)) {
    1.29 +		return 1;
    1.30 +	}
    1.31 +	glutInitWindowSize(win_width, win_height);
    1.32 +	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    1.33 +	glutCreateWindow("textbomb");
    1.34 +
    1.35 +	if(fullscreen) {
    1.36 +		glutFullScreen();
    1.37 +	}
    1.38 +
    1.39 +	glutDisplayFunc(display);
    1.40 +	glutIdleFunc(idle);
    1.41 +	glutReshapeFunc(reshape);
    1.42 +	glutKeyboardFunc(keyboard);
    1.43 +
    1.44 +	if(!tbomb_init()) {
    1.45 +		return 1;
    1.46 +	}
    1.47 +	atexit(tbomb_cleanup);
    1.48 +
    1.49 +	start_time = glutGet(GLUT_ELAPSED_TIME);
    1.50 +	glutMainLoop();
    1.51 +	return 0;
    1.52 +}
    1.53 +
    1.54 +void display()
    1.55 +{
    1.56 +	unsigned long msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
    1.57 +
    1.58 +	glClear(GL_COLOR_BUFFER_BIT);
    1.59 +
    1.60 +	tbomb_draw(msec);
    1.61 +
    1.62 +	glutSwapBuffers();
    1.63 +	assert(glGetError() == GL_NO_ERROR);
    1.64 +}
    1.65 +
    1.66 +void idle()
    1.67 +{
    1.68 +	glutPostRedisplay();
    1.69 +}
    1.70 +
    1.71 +void reshape(int x, int y)
    1.72 +{
    1.73 +	float aspect = (float)x / (float)y;
    1.74 +
    1.75 +	glViewport(0, 0, x, y);
    1.76 +
    1.77 +	glMatrixMode(GL_PROJECTION);
    1.78 +	glLoadIdentity();
    1.79 +	glScalef(1.0 / aspect, 1.0, 1.0);
    1.80 +}
    1.81 +
    1.82 +void keyboard(unsigned char key, int x, int y)
    1.83 +{
    1.84 +	switch(key) {
    1.85 +	case 27:
    1.86 +		exit(0);
    1.87 +
    1.88 +	case 'f':
    1.89 +	case 'F':
    1.90 +		fullscreen = !fullscreen;
    1.91 +		if(fullscreen) {
    1.92 +			glutFullScreen();
    1.93 +		} else {
    1.94 +			glutReshapeWindow(win_width, win_height);
    1.95 +		}
    1.96 +		break;
    1.97 +
    1.98 +	case ' ':
    1.99 +		tbomb_dbg();
   1.100 +		break;
   1.101 +	}
   1.102 +}
   1.103 +
   1.104 +bool parse_args(int argc, char **argv)
   1.105 +{
   1.106 +	for(int i=1; i<argc; i++) {
   1.107 +		if(argv[i][0] == '-' && argv[i][2] == 0) {
   1.108 +			switch(argv[i][1]) {
   1.109 +			case 'f':
   1.110 +				fullscreen = true;
   1.111 +				break;
   1.112 +
   1.113 +			case 's':
   1.114 +				if(sscanf(argv[++i], "%dx%d", &win_width, &win_height) != 2) {
   1.115 +					fprintf(stderr, "-s must be followed by a resolution WxH\n");
   1.116 +					return false;
   1.117 +				}
   1.118 +				break;
   1.119 +
   1.120 +			case 'h':
   1.121 +				printf("options:\n");
   1.122 +				printf(" -s WxH   set window size\n");
   1.123 +				printf(" -f       fullscreen\n");
   1.124 +				printf(" -h       print this usage info and exit\n");
   1.125 +				exit(0);
   1.126 +
   1.127 +			default:
   1.128 +				fprintf(stderr, "invalid option: %s\n", argv[i]);
   1.129 +				return false;
   1.130 +			}
   1.131 +		} else {
   1.132 +			fprintf(stderr, "unexpected argument: %s\n", argv[i]);
   1.133 +			return false;
   1.134 +		}
   1.135 +	}
   1.136 +	return true;
   1.137 +}