ld33_umonster

diff src/main.cc @ 0:4a6683050e29

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Aug 2015 07:15:00 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.cc	Sat Aug 22 07:15:00 2015 +0300
     1.3 @@ -0,0 +1,148 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <assert.h>
     1.7 +#ifdef __APPLE__
     1.8 +#include <GLUT/glut.h>
     1.9 +#else
    1.10 +#include <GL/glut.h>
    1.11 +#endif
    1.12 +#include "game.h"
    1.13 +#include "opt.h"
    1.14 +
    1.15 +static void display();
    1.16 +static void idle();
    1.17 +static void reshape(int x, int y);
    1.18 +static void keydown(unsigned char key, int x, int y);
    1.19 +static void keyup(unsigned char key, int x, int y);
    1.20 +static void mouse(int bn, int st, int x, int y);
    1.21 +static void motion(int x, int y);
    1.22 +static void update_modifiers();
    1.23 +
    1.24 +static unsigned int start_time;
    1.25 +
    1.26 +int main(int argc, char **argv)
    1.27 +{
    1.28 +	glutInit(&argc, argv);
    1.29 +	if(!init_options(argc, argv)) {
    1.30 +		return 1;
    1.31 +	}
    1.32 +	glutInitWindowSize(opt.xres, opt.yres);
    1.33 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    1.34 +	glutCreateWindow("umonster");
    1.35 +
    1.36 +	if(opt.fullscreen) {
    1.37 +		set_fullscreen(true);
    1.38 +	}
    1.39 +
    1.40 +	glutDisplayFunc(display);
    1.41 +	glutIdleFunc(idle);
    1.42 +	glutReshapeFunc(reshape);
    1.43 +	glutKeyboardFunc(keydown);
    1.44 +	glutKeyboardUpFunc(keyup);
    1.45 +	glutMouseFunc(mouse);
    1.46 +	glutMotionFunc(motion);
    1.47 +	glutPassiveMotionFunc(motion);
    1.48 +
    1.49 +	if(!game_init()) {
    1.50 +		return 1;
    1.51 +	}
    1.52 +	atexit(game_cleanup);
    1.53 +
    1.54 +	start_time = glutGet(GLUT_ELAPSED_TIME);
    1.55 +	glutMainLoop();
    1.56 +	return 0;
    1.57 +}
    1.58 +
    1.59 +void set_fullscreen(bool fs)
    1.60 +{
    1.61 +	if(fs) {
    1.62 +		glutFullScreen();
    1.63 +	} else {
    1.64 +		glutReshapeWindow(opt.xres, opt.yres);
    1.65 +	}
    1.66 +}
    1.67 +
    1.68 +void redisplay()
    1.69 +{
    1.70 +	glutPostRedisplay();
    1.71 +}
    1.72 +
    1.73 +void quit()
    1.74 +{
    1.75 +	exit(0);
    1.76 +}
    1.77 +
    1.78 +void draw_teapot()
    1.79 +{
    1.80 +	glFrontFace(GL_CW);
    1.81 +	glutSolidTeapot(1.0);
    1.82 +	glFrontFace(GL_CCW);
    1.83 +}
    1.84 +
    1.85 +static void display()
    1.86 +{
    1.87 +	unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
    1.88 +	game_update(msec);
    1.89 +
    1.90 +	game_display();
    1.91 +
    1.92 +	glutSwapBuffers();
    1.93 +	assert(glGetError() == GL_NO_ERROR);
    1.94 +}
    1.95 +
    1.96 +static void idle()
    1.97 +{
    1.98 +	glutPostRedisplay();
    1.99 +}
   1.100 +
   1.101 +static void reshape(int x, int y)
   1.102 +{
   1.103 +	win_width = x;
   1.104 +	win_height = y;
   1.105 +	game_reshape(x, y);
   1.106 +}
   1.107 +
   1.108 +static void keydown(unsigned char key, int x, int y)
   1.109 +{
   1.110 +	update_modifiers();
   1.111 +	game_keyboard(key, true);
   1.112 +}
   1.113 +
   1.114 +static void keyup(unsigned char key, int x, int y)
   1.115 +{
   1.116 +	update_modifiers();
   1.117 +	game_keyboard(key, false);
   1.118 +}
   1.119 +
   1.120 +static void mouse(int bn, int st, int x, int y)
   1.121 +{
   1.122 +	update_modifiers();
   1.123 +	game_mbutton(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
   1.124 +}
   1.125 +
   1.126 +static void motion(int x, int y)
   1.127 +{
   1.128 +	game_mmotion(x, y);
   1.129 +}
   1.130 +
   1.131 +static void update_modifiers()
   1.132 +{
   1.133 +	static unsigned int prev_mod;
   1.134 +	unsigned int mod = glutGetModifiers();
   1.135 +	unsigned int delta = mod ^ prev_mod;
   1.136 +
   1.137 +	if(delta & GLUT_ACTIVE_SHIFT) {
   1.138 +		bool press = (mod & GLUT_ACTIVE_SHIFT) != 0;
   1.139 +		game_modifier_key(MOD_SHIFT, press);
   1.140 +	}
   1.141 +	if(delta & GLUT_ACTIVE_CTRL) {
   1.142 +		bool press = (mod & GLUT_ACTIVE_CTRL) != 0;
   1.143 +		game_modifier_key(MOD_CTL, press);
   1.144 +	}
   1.145 +	if(delta & GLUT_ACTIVE_ALT) {
   1.146 +		bool press = (mod & GLUT_ACTIVE_ALT) != 0;
   1.147 +		game_modifier_key(MOD_ALT, press);
   1.148 +	}
   1.149 +
   1.150 +	prev_mod = mod;
   1.151 +}