-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
125 lines (100 loc) · 2.65 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
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "src/exports.h"
#define WIDTH 960
#define HEIGHT 720
#define TITLE "Example"
float frand(void) { return (float)rand() / (float)RAND_MAX; }
float fsin(float x) { return sin(x); }
float fcos(float x) { return cos(x); }
void resize_callback(GLFWwindow *window, int new_w, int new_h) {
(void)window;
resize(new_w, new_h);
}
void cursor_callback(GLFWwindow *window, double xpos, double ypos) {
(void)window;
update_mouse(xpos, ypos);
}
int glCreateBuffer(void) {
GLuint buf;
glCreateBuffers(1, &buf);
return buf;
}
int glCreateVertexArray(void) {
GLuint vao;
glCreateVertexArrays(1, &vao);
return vao;
}
void glSetShaderSource(int shader, const char *src) {
glShaderSource(shader, 1, &src, NULL);
}
int glGetShaderParameter(int shader, int pname) {
GLint param;
glGetShaderiv(shader, pname, ¶m);
return param;
}
void message_callback(
GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
const GLchar *message, const void *userParam
) {
(void)source;
(void)id;
(void)length;
(void)userParam;
fprintf(
stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x\n%s\n",
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), type, severity,
message
);
}
int main(void) {
if (!glfwInit()) return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
glfwTerminate();
return -1;
}
glfwSwapInterval(0);
printf("Created window with OpenGL version: %s\n", glGetString(GL_VERSION));
glfwSetFramebufferSizeCallback(window, resize_callback);
glfwSetCursorPosCallback(window, cursor_callback);
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(message_callback, 0);
double last = glfwGetTime();
double last_display = last;
double dt = 0;
unsigned frames = 0;
resize(WIDTH, HEIGHT);
srand(time(0));
game_init();
while (!glfwWindowShouldClose(window)) {
game_update(dt);
glfwSwapBuffers(window);
glfwPollEvents();
frames += 1;
double now = glfwGetTime();
if (now - last_display >= 1) {
char title[64];
sprintf(title, "%s - FPS: %d", TITLE, frames);
glfwSetWindowTitle(window, title);
frames = 0;
last_display = now;
}
dt = now - last;
last = now;
}
glfwTerminate();
return 0;
}