-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
111 lines (84 loc) · 1.88 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdlib.h>
#include <GL/glew.h>
#include <GLUT/glut.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "world.h"
// Globals
int previous_frame;
int fps_counter;
int fps_last_update;
// Functions
void init(int argc, char **argv) {
// Seed random number generator
srandomdev();
// Initialize timers
previous_frame = glutGet(GLUT_ELAPSED_TIME);
fps_counter = 0;
fps_last_update = 0;
// Initialize projection matrix
glMatrixMode(GL_PROJECTION);
gluPerspective(60.0f, (1024.0 / 768.0), 0.1f, 1024.0f);
// Depth test
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
// Shading
glShadeModel(GL_SMOOTH);
// Initialize world
world_init(argc, argv);
}
void idle() {
// Get milliseconds since last frame
int elapsed = glutGet(GLUT_ELAPSED_TIME);
int delta = elapsed - previous_frame;
previous_frame = elapsed;
// Calculate FPS
fps_counter++;
if((elapsed - fps_last_update) > 1000) {
// Update window title with current FPS
char *title;
asprintf(&title, "Stone | %d FPS", fps_counter);
glutSetWindowTitle(title);
free(title);
// Reset counter
fps_counter = 0;
fps_last_update = elapsed;
}
world_tick(delta);
glutPostRedisplay();
}
void display() {
world_display();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 'q': // Quit
exit(0);
default:
world_keyboard(key, x, y);
break;
}
}
void mouse(int button, int state, int x, int y) {
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(1024, 768);
glutCreateWindow("Stone");
glutIdleFunc(idle);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glewInit();
if(!GLEW_VERSION_2_0) {
fprintf(stderr, "OpenGL 2.0 not available\n");
return 1;
}
init(argc, argv);
glutMainLoop();
return 0;
}