dungeon_crawler

view prototype/src/main.cc @ 5:252a00508411

more stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 12 Aug 2012 07:07:57 +0300
parents 96de911d05d4
children 8fb37db44fd8
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "level.h"
6 #include "camera.h"
7 #include "datapath.h"
8 #include "tileset.h"
10 bool init();
11 void cleanup();
12 void disp();
13 void reshape(int x, int y);
14 void keyb(unsigned char key, int x, int y);
15 void mouse(int bn, int state, int x, int y);
16 void motion(int x, int y);
18 static TileSet *tileset;
19 static Level *level;
20 static OrbitCamera cam;
22 static const char *level_file = "0.level";
24 int main(int argc, char **argv)
25 {
26 glutInit(&argc, argv);
28 if(argc > 1) {
29 level_file = argv[1];
30 }
32 glutInitWindowSize(800, 600);
33 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
34 glutCreateWindow("prototype");
36 glutDisplayFunc(disp);
37 glutReshapeFunc(reshape);
38 glutKeyboardFunc(keyb);
39 glutMouseFunc(mouse);
40 glutMotionFunc(motion);
42 glewInit();
44 if(!init()) {
45 return 1;
46 }
48 glutMainLoop();
49 }
51 bool init()
52 {
53 glEnable(GL_LIGHTING);
54 glEnable(GL_LIGHT0);
55 float ldir[] = {-1, 1, 2, 0};
56 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
57 glEnable(GL_NORMALIZE);
59 glEnable(GL_DEPTH_TEST);
60 glEnable(GL_CULL_FACE);
61 glEnable(GL_MULTISAMPLE);
63 add_data_path("data");
65 // load a tileset
66 tileset = new TileSet;
67 if(!tileset->load(datafile_path("default.tileset"))) {
68 return false;
69 }
70 set_active_tileset(tileset);
72 level = new Level;
73 if(!level->load(datafile_path(level_file))) {
74 return false;
75 }
77 return true;
78 }
80 void disp()
81 {
82 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84 glMatrixMode(GL_MODELVIEW);
85 glLoadIdentity();
86 cam.use();
88 level->draw();
90 glutSwapBuffers();
91 assert(glGetError() == GL_NO_ERROR);
92 }
94 void reshape(int x, int y)
95 {
96 glViewport(0, 0, x, y);
97 glMatrixMode(GL_PROJECTION);
98 glLoadIdentity();
99 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
100 }
102 void keyb(unsigned char key, int x, int y)
103 {
104 switch(key) {
105 case 27:
106 exit(0);
107 }
108 }
110 static int prev_x, prev_y;
111 static bool bnstate[32];
113 void mouse(int bn, int state, int x, int y)
114 {
115 prev_x = x;
116 prev_y = y;
117 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
118 }
120 void motion(int x, int y)
121 {
122 int dx = x - prev_x;
123 int dy = y - prev_y;
124 prev_x = x;
125 prev_y = y;
127 if(bnstate[0]) {
128 cam.input_rotate(dx * 0.01, dy * 0.01, 0);
129 glutPostRedisplay();
130 }
131 if(bnstate[2]) {
132 cam.input_zoom(dy * 0.1);
133 glutPostRedisplay();
134 }
135 }