forked from pcbaecker/example-emscripten-webgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
92 lines (78 loc) · 1.89 KB
/
main.cpp
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
#include <iostream>
#include <GLFW/glfw3.h>
// Include the EMSCRIPTEN specific headers
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
// For example purpose we use global variables
#define SPEED 0.005f
GLFWwindow* window = nullptr;
float red = 0.0f;
bool increase = true;
/**
* @brief This function just increases and decreases the red value.
*
* @return The red value.
*/
float getRed() {
if (increase) {
red += SPEED;
if (red > 1.0f) {
red = 1.0f;
increase = false;
}
} else {
red -= SPEED;
if (red < 0.0f) {
red = 0.0f;
increase = true;
}
}
return red;
}
/**
* @brief This function is called every frame.
*/
void mainLoop() {
// Clear the screen with a color
glClearColor(getRed(), 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the buffers of the window
glfwSwapBuffers(window);
// Poll for the events
glfwPollEvents();
}
/**
* @brief The normal main() function.
*
* @return Exit code.
*/
int main() {
std::cout << "Starting" << std::endl;
// Initialize glfw
if (!glfwInit())
exit(EXIT_FAILURE);
// Create the window
window = glfwCreateWindow(640, 480, "Emscripten webgl example", nullptr, nullptr);
if (!window) {
glfwTerminate();
return EXIT_FAILURE;
}
// Make this window the current context
glfwMakeContextCurrent(window);
std::cout << "Going into loop" << std::endl;
#ifdef EMSCRIPTEN
// Define a mail loop function, that will be called as fast as possible
emscripten_set_main_loop(&mainLoop, 0, 1);
#else
// This is the normal C/C++ main loop
while (!glfwWindowShouldClose(window)) {
mainLoop();
}
#endif
// Tear down the system
std::cout << "Loop ended" << std::endl;
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS;
}