stratgame

view src/main.cc @ 2:369b51c9e4a8

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 23 May 2012 07:25:43 +0300
parents 55a43e27339a
children 8d95187cb3ee
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include "opengl.h"
5 #include "part.h"
6 #include "menu_part.h"
7 #include "game_part.h"
9 static bool init();
10 static void cleanup();
11 static void disp();
12 static void idle();
13 static void reshape(int x, int y);
14 static void key_press(unsigned char key, int x, int y);
15 static void key_release(unsigned char key, int x, int y);
16 static void mouse(int bn, int state, int x, int y);
17 static void motion(int x, int y);
18 static void sball_motion(int x, int y, int z);
19 static void sball_rotate(int x, int y, int z);
20 static void sball_button(int bn, int state);
22 int main(int argc, char **argv)
23 {
24 glutInit(&argc, argv);
25 glutInitWindowSize(800, 450);
26 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STENCIL);
27 glutCreateWindow("Foo");
29 glutDisplayFunc(disp);
30 glutIdleFunc(idle);
31 glutReshapeFunc(reshape);
32 glutKeyboardFunc(key_press);
33 glutKeyboardUpFunc(key_release);
34 glutMouseFunc(mouse);
35 glutMotionFunc(motion);
36 glutPassiveMotionFunc(motion);
37 glutSpaceballMotionFunc(sball_motion);
38 glutSpaceballRotateFunc(sball_rotate);
39 glutSpaceballButtonFunc(sball_button);
41 if(!init()) {
42 return 1;
43 }
44 atexit(cleanup);
46 glutMainLoop();
47 return 0;
48 }
51 static bool init()
52 {
53 glewInit();
55 menu_part = new MainMenu;
56 if(!menu_part->init()) {
57 return false;
58 }
60 game_part = new Game;
61 if(!game_part->init()) {
62 return false;
63 }
65 cur_part = menu_part;
66 return true;
67 }
69 static void cleanup()
70 {
71 delete menu_part;
72 delete game_part;
73 }
75 static void disp()
76 {
77 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
78 cur_part->update(msec);
79 cur_part->draw();
81 glutSwapBuffers();
82 usleep(1000);
83 }
85 static void idle()
86 {
87 glutPostRedisplay();
88 }
90 static void reshape(int x, int y)
91 {
92 glViewport(0, 0, x, y);
94 cur_part->reshape(x, y);
95 }
97 static void key_press(unsigned char key, int x, int y)
98 {
99 cur_part->key(key, true);
100 }
102 static void key_release(unsigned char key, int x, int y)
103 {
104 cur_part->key(key, false);
105 }
107 static void mouse(int bn, int state, int x, int y)
108 {
109 cur_part->mouse_button(bn - GLUT_LEFT_BUTTON, state == GLUT_DOWN);
110 }
112 static void motion(int x, int y)
113 {
114 cur_part->mouse_motion(x, y);
115 }
117 static int sbmotion[6];
119 static void sball_motion(int x, int y, int z)
120 {
121 sbmotion[0] = x;
122 sbmotion[1] = y;
123 sbmotion[2] = z;
124 cur_part->spaceball_motion(sbmotion[0], sbmotion[1], sbmotion[2],
125 sbmotion[3], sbmotion[4], sbmotion[5]);
126 }
128 static void sball_rotate(int x, int y, int z)
129 {
130 sbmotion[3] = x;
131 sbmotion[4] = y;
132 sbmotion[5] = z;
133 cur_part->spaceball_motion(sbmotion[0], sbmotion[1], sbmotion[2],
134 sbmotion[3], sbmotion[4], sbmotion[5]);
135 }
137 static void sball_button(int bn, int state)
138 {
139 cur_part->spaceball_button(bn, state == GLUT_DOWN);
140 }