-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
169 lines (142 loc) · 3.92 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
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
// BPM tapper: made by EHQ
#include <stdio.h>
#include <time.h>
#include <GLFW/glfw3.h>
#include <GL/glut.h>
#include <string.h>
#include <math.h>
#include "src/draw.c"
#include "src/font.h"
#define size 25 //size of the "blips"
#define WINDOW_WIDTH 200
#define WINDOW_HEIGHT 200
int clicks = 0;
int sig = 4; // time signature
float bpm;
void makeRasterFont(void) {
GLuint i, j;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
fontOffset = glGenLists (128);
for (i = 0,j = 'A'; i < 26; i++,j++) { // letters
glNewList(fontOffset + j, GL_COMPILE);
glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]); // width, height, xorig, yorig, kerning(?), angle(?)
glEndList();
}
for (i = 0, j = '0'; i < 10; i++, j++) { // numbers
glNewList(fontOffset + j, GL_COMPILE);
glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, numbers[i]);
glEndList();
}
glNewList(fontOffset + ' ', GL_COMPILE); // space
glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space);
glEndList();
glNewList(fontOffset + '.', GL_COMPILE); // period
glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, period);
glEndList();
}
void printString(char *s, int x, int y) {
glRasterPos2i(x, y);
glPushAttrib (GL_LIST_BIT);
glListBase(fontOffset);
glCallLists((GLsizei) strlen(s), GL_UNSIGNED_BYTE, (GLubyte *) s);
glPopAttrib();
}
void render() {
glClearColor(0.8f, 0.79f, 0.54f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.22f, 0.58f, 0.22f);
// outlines
for (int i = 0; i < sig; i++) {
rect((i * size) + 5 + (i * 5), 5, (i * size) + 5 + (i * 5) + size, 5 + size);
}
// fills
for (int i = 0; i < clicks; i++) {
square((i * size) + 5 + (i * 5), 5, size);
}
float black[3] = {0, 0, 0}; // text color def.
glColor3fv(black); // text color call
char buf[100] = "...";
if (clicks == 0) {
char buf[100] = "...";
bpm = 0;
} else {
if (!isinf(bpm)) {
gcvt(bpm, 5, buf);
}
}
printString(buf, (WINDOW_WIDTH / 2) - (4 * 8), WINDOW_HEIGHT / 2); // text, X, Y
// scary hardcoded vars here ^^^
printString("BPM", (WINDOW_WIDTH / 2) - (4 * 8), WINDOW_HEIGHT / 2 - 20);
glFlush();
}
int main() {
// Inititalize GLFW
if (!glfwInit()) {
return -1;
}
// Define the window
GLFWwindow* window;
// Create a windowed mode window & its OpenGL context
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "BPM tapper", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// Set up the "camera" view
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Create the canvas to draw on
glOrtho(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT, 0.0, 1.0);
if (!window) {
glfwTerminate();
return -1;
}
// "focuses" the window
glfwMakeContextCurrent(window);
// Font rendering setup
makeRasterFont();
float *times;
times = malloc(sig * sizeof(float));
// MAIN LOOP
while (!glfwWindowShouldClose(window)) {
float now = glfwGetTime();
if (clicks == sig || clicks == 0) {
glfwSetTime(0);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
if (clicks == sig) {
clicks = 1;
} else {
clicks++;
}
times[clicks] = now;
// :^)
//bpm = 60 / times[sig] * sig; //original
bpm = (60 / times[sig] * sig) - 25; //??? corrects the BPM for some reason
//printf("%f\n", times[clicks]);
}
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
clicks = 0;
if (times[clicks]) {
times = malloc(sig * sizeof(float));
}
}
}
glfwSetMouseButtonCallback(window, mouse_button_callback);
// ESC -> EXIT
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
//printf("\nBPM:\t%f\n", bpm);
glfwSetWindowShouldClose(window, 1);
}
// Render
render();
// Swap front & back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
//glfwWaitEvents();
}
glfwTerminate();
return 0;
}