public
Description:
Homepage:
Clone URL: git://github.com/scoopr/ld11.git
ld11 / main.c
100644 187 lines (130 sloc) 2.829 kb
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifdef _WIN32
#include <windows.h>
#endif
 
#include <stdio.h>
 
// TODO: apple ifdef
 
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
 
#include <stdlib.h>
 
#include <sys/time.h>
#include <math.h>
 
#include <stdarg.h>
 
struct timeval start_time;
 
float the_time;
int level;
int score;
int failed;
 
float goal;
 
#define LEEWAY 0.05f
 
 
 
void timer_reset() {
    the_time=0;
    gettimeofday(&start_time, NULL);
}
 
 
 
float position() {
    float speed = 0.2f + logf(level * 0.05f + 1);
    return the_time * speed;
}
 
 
 
void init_level() {
    timer_reset();
    level+=1;
    failed=0;
    goal=(40+rand()%50)/100.0f;
}
 
 
 
void finish_level() {
float pos=position();
    if( fabs(goal-pos) < LEEWAY && pos < 1 ) {
        score += 1;
    } else {
        failed=1;
    }
}
 
 
 
void timer_update() {
    struct timeval current_time;
    gettimeofday(&current_time, NULL);
    the_time = ( (current_time.tv_sec-start_time.tv_sec) + (current_time.tv_usec-start_time.tv_usec)/1000000.0f);
}
 
 
 
void quad(float scale) {
glLoadIdentity();
glTranslatef(0,0,-1);
glScalef(scale, scale, scale);
    glBegin(GL_QUADS);
    glVertex2f(-1,-1);
    glVertex2f( 1,-1);
    glVertex2f( 1, 1);
    glVertex2f(-1, 1);
    glEnd();
}
 
 
 
void print(char *str, ...) {
va_list args;
int i, len;
char temp[1024];
int width;
va_start(args, str);
len=vsnprintf(temp, 1024, str, args);
glLoadIdentity();
 
width=0;
for(i=0; i < len; i++) {
width+=glutBitmapWidth( GLUT_BITMAP_HELVETICA_18 , temp[i]);
}
 
glRasterPos2f( -width/(float)glutGet(GLUT_WINDOW_WIDTH) , 0.8f);
for(i=0; i < len; i++) {
glutBitmapCharacter( GLUT_BITMAP_9_BY_15 , temp[i]);
}
va_end(args);
}
 
 
 
void display() {
    timer_update();
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);
 
glColor3f(1,1,1);
 
    float pos = position();
 
if( pos > 1) failed=1;
 
    if(failed) {
print("%d", score);
     glutSwapBuffers();
        return;
    }
    
    glColor3f(1,0,0);
    quad(goal+LEEWAY);
    glColor3f(0,0,0);
    quad(goal-LEEWAY);
    
    glColor3f(1,1,1);
    quad(pos);
    
print("%d", score);
    glutSwapBuffers();
    
}
 
 
 
void keyboard(unsigned char key, int x, int y) {
    switch(key) {
    case 27:
        exit(0);
        break;
    case ' ':
     if(failed) {
failed=0;
     level=0;
     score=0;
     init_level();
     } else {
finish_level();
if(!failed) init_level();
     }
        break;
    }
}
 
 
 
void idle() {
    glutPostRedisplay();
}
 
 
 
int main(int argc, char **argv) {
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
    glutInit(&argc, argv);
    glutCreateWindow("redgate");
    glutKeyboardFunc(&keyboard);
    glutDisplayFunc(&display);
    glutIdleFunc(&idle);
    init_level();
    level=0;
    score=0;
    failed=0;
    
    glutMainLoop();
}