ld33_umonster

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #ifdef __APPLE__
5 #include <GLUT/glut.h>
6 #else
7 #include <GL/glut.h>
8 #endif
9 #include "game.h"
10 #include "opt.h"
12 static void display();
13 static void idle();
14 static void reshape(int x, int y);
15 static void keydown(unsigned char key, int x, int y);
16 static void keyup(unsigned char key, int x, int y);
17 static void mouse(int bn, int st, int x, int y);
18 static void motion(int x, int y);
19 static void update_modifiers();
21 static unsigned int start_time;
23 int main(int argc, char **argv)
24 {
25 glutInit(&argc, argv);
26 if(!init_options(argc, argv)) {
27 return 1;
28 }
29 glutInitWindowSize(opt.xres, opt.yres);
30 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
31 glutCreateWindow("umonster");
33 if(opt.fullscreen) {
34 set_fullscreen(true);
35 }
37 glutDisplayFunc(display);
38 glutIdleFunc(idle);
39 glutReshapeFunc(reshape);
40 glutKeyboardFunc(keydown);
41 glutKeyboardUpFunc(keyup);
42 glutMouseFunc(mouse);
43 glutMotionFunc(motion);
44 glutPassiveMotionFunc(motion);
46 if(!game_init()) {
47 return 1;
48 }
49 atexit(game_cleanup);
51 start_time = glutGet(GLUT_ELAPSED_TIME);
52 glutMainLoop();
53 return 0;
54 }
56 void set_fullscreen(bool fs)
57 {
58 if(fs) {
59 glutFullScreen();
60 } else {
61 glutReshapeWindow(opt.xres, opt.yres);
62 }
63 }
65 void redisplay()
66 {
67 glutPostRedisplay();
68 }
70 void quit()
71 {
72 exit(0);
73 }
75 void draw_teapot()
76 {
77 glFrontFace(GL_CW);
78 glutSolidTeapot(1.0);
79 glFrontFace(GL_CCW);
80 }
82 static void display()
83 {
84 unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
85 game_update(msec);
87 game_display();
89 glutSwapBuffers();
90 assert(glGetError() == GL_NO_ERROR);
91 }
93 static void idle()
94 {
95 glutPostRedisplay();
96 }
98 static void reshape(int x, int y)
99 {
100 win_width = x;
101 win_height = y;
102 game_reshape(x, y);
103 }
105 static void keydown(unsigned char key, int x, int y)
106 {
107 update_modifiers();
108 game_keyboard(key, true);
109 }
111 static void keyup(unsigned char key, int x, int y)
112 {
113 update_modifiers();
114 game_keyboard(key, false);
115 }
117 static void mouse(int bn, int st, int x, int y)
118 {
119 update_modifiers();
120 game_mbutton(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
121 }
123 static void motion(int x, int y)
124 {
125 game_mmotion(x, y);
126 }
128 static void update_modifiers()
129 {
130 static unsigned int prev_mod;
131 unsigned int mod = glutGetModifiers();
132 unsigned int delta = mod ^ prev_mod;
134 if(delta & GLUT_ACTIVE_SHIFT) {
135 bool press = (mod & GLUT_ACTIVE_SHIFT) != 0;
136 game_modifier_key(MOD_SHIFT, press);
137 }
138 if(delta & GLUT_ACTIVE_CTRL) {
139 bool press = (mod & GLUT_ACTIVE_CTRL) != 0;
140 game_modifier_key(MOD_CTL, press);
141 }
142 if(delta & GLUT_ACTIVE_ALT) {
143 bool press = (mod & GLUT_ACTIVE_ALT) != 0;
144 game_modifier_key(MOD_ALT, press);
145 }
147 prev_mod = mod;
148 }