stratgame

view src/main.cc @ 5:2e38715de41b

terrain
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 May 2012 07:00:48 +0300
parents 369b51c9e4a8
children
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 //menu_part->start();
66 game_part->start();
67 return true;
68 }
70 static void cleanup()
71 {
72 delete menu_part;
73 delete game_part;
74 }
76 static void disp()
77 {
78 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
79 cur_part->update(msec);
80 cur_part->draw();
82 glutSwapBuffers();
83 usleep(1000);
84 }
86 static void idle()
87 {
88 glutPostRedisplay();
89 }
91 static void reshape(int x, int y)
92 {
93 glViewport(0, 0, x, y);
95 cur_part->reshape(x, y);
96 }
98 static void key_press(unsigned char key, int x, int y)
99 {
100 cur_part->key(key, true);
101 }
103 static void key_release(unsigned char key, int x, int y)
104 {
105 cur_part->key(key, false);
106 }
108 static void mouse(int bn, int state, int x, int y)
109 {
110 cur_part->mouse_button(bn - GLUT_LEFT_BUTTON, state == GLUT_DOWN);
111 }
113 static void motion(int x, int y)
114 {
115 cur_part->mouse_motion(x, y);
116 }
118 static int sbmotion[6];
120 static void sball_motion(int x, int y, int z)
121 {
122 sbmotion[0] = x;
123 sbmotion[1] = y;
124 sbmotion[2] = z;
125 cur_part->spaceball_motion(sbmotion[0], sbmotion[1], sbmotion[2],
126 sbmotion[3], sbmotion[4], sbmotion[5]);
127 }
129 static void sball_rotate(int x, int y, int z)
130 {
131 sbmotion[3] = x;
132 sbmotion[4] = y;
133 sbmotion[5] = z;
134 cur_part->spaceball_motion(sbmotion[0], sbmotion[1], sbmotion[2],
135 sbmotion[3], sbmotion[4], sbmotion[5]);
136 }
138 static void sball_button(int bn, int state)
139 {
140 cur_part->spaceball_button(bn, state == GLUT_DOWN);
141 }