5d6cc821e4d53ddaa2e6ab1b5049ad9702c3b4fc
[raydungeon] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "miniglut.h"
5 #include "game.h"
6
7 static void idle(void);
8 static void keydown(unsigned char key, int x, int y);
9 static void keyup(unsigned char key, int x, int y);
10 static void skeydown(int key, int x, int y);
11 static void skeyup(int key, int x, int y);
12 static void mouse(int bn, int st, int x, int y);
13 static int translate_skey(int key);
14
15
16 int main(int argc, char **argv)
17 {
18         glutInit(&argc, argv);
19         glutInitWindowSize(1280, 800);
20         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
21         glutCreateWindow("raydungeon");
22
23         glutDisplayFunc(game_display);
24         glutIdleFunc(idle);
25         glutReshapeFunc(game_reshape);
26         glutKeyboardFunc(keydown);
27         glutKeyboardUpFunc(keyup);
28         glutSpecialFunc(skeydown);
29         glutSpecialUpFunc(skeyup);
30         glutMouseFunc(mouse);
31         glutMotionFunc(game_motion);
32
33         if(game_init() == -1) {
34                 return 1;
35         }
36         atexit(game_shutdown);
37         glutMainLoop();
38         return 0;
39 }
40
41 void game_swap_buffers(void)
42 {
43         glutSwapBuffers();
44         assert(glGetError() == GL_NO_ERROR);
45 }
46
47 void game_quit(void)
48 {
49         exit(0);
50 }
51
52
53 static void idle(void)
54 {
55         glutPostRedisplay();
56 }
57
58 static void keydown(unsigned char key, int x, int y)
59 {
60         game_keyboard(key, 1);
61 }
62
63 static void keyup(unsigned char key, int x, int y)
64 {
65         game_keyboard(key, 0);
66 }
67
68 static void skeydown(int key, int x, int y)
69 {
70         int k = translate_skey(key);
71         if(k >= 0) {
72                 game_keyboard(k, 1);
73         }
74 }
75
76 static void skeyup(int key, int x, int y)
77 {
78         int k = translate_skey(key);
79         if(k >= 0) {
80                 game_keyboard(k, 0);
81         }
82 }
83
84 static void mouse(int bn, int st, int x, int y)
85 {
86         game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
87 }
88
89 static int translate_skey(int key)
90 {
91         switch(key) {
92         case GLUT_KEY_LEFT:
93                 return GKEY_LEFT;
94         case GLUT_KEY_UP:
95                 return GKEY_UP;
96         case GLUT_KEY_RIGHT:
97                 return GKEY_RIGHT;
98         case GLUT_KEY_DOWN:
99                 return GKEY_DOWN;
100         case GLUT_KEY_PAGE_UP:
101                 return GKEY_PGUP;
102         case GLUT_KEY_PAGE_DOWN:
103                 return GKEY_PGDOWN;
104         case GLUT_KEY_HOME:
105                 return GKEY_HOME;
106         case GLUT_KEY_END:
107                 return GKEY_END;
108         case GLUT_KEY_INSERT:
109                 return GKEY_INS;
110         default:
111                 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
112                         return key - (GLUT_KEY_F1 + GKEY_F1);
113                 }
114         }
115
116         return -1;
117 }