-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleNoise.cpp
More file actions
108 lines (76 loc) · 2.65 KB
/
ExampleNoise.cpp
File metadata and controls
108 lines (76 loc) · 2.65 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
/*
* ExampleNoise.cpp
*
* Created on: Apr 10, 2020
* Author: ans
*/
#include "ExampleNoise.h"
ExampleNoise::ExampleNoise() : pixelSize(2) {}
ExampleNoise::~ExampleNoise() {}
// run the application
int ExampleNoise::run(int argc, char * argv[]) {
UNUSED(argc);
UNUSED(argv);
const std::string name("noise");
constexpr int width = 800;
constexpr int height = 600;
this->setPixelSize(this->pixelSize);
this->setDebugText(this->randGenerator.str());
this->createMainWindow(width, height, name);
this->Engine::run();
return EXIT_SUCCESS;
}
// create resources
void ExampleNoise::onCreate() {}
// update frame
void ExampleNoise::onUpdate(double elapsedTime) {
UNUSED(elapsedTime);
// render noise
const int w = this->getWindowWidth();
const int h = this->getWindowHeight();
if(w > 0 && h > 0) {
for(int x = 0; x < w; ++x) {
for(int y = 0; y < h; ++y) {
const unsigned char r = this->randGenerator.generateByte();
const unsigned char g = this->randGenerator.generateByte();
const unsigned char b = this->randGenerator.generateByte();
this->draw(x, y, r, g, b);
}
}
}
// handle SPACE key for changing the algorithm used for pseudo-random number generation
const unsigned char oldRandAlgo = this->randGenerator.getAlgo();
unsigned char newRandAlgo = oldRandAlgo;
if(this->isKeyPressed(GLFW_KEY_SPACE))
++newRandAlgo;
if(this->isKeyRepeated(GLFW_KEY_SPACE))
++newRandAlgo;
newRandAlgo %= RAND_ALGO_NUM;
if(newRandAlgo != oldRandAlgo) {
this->randGenerator.setAlgo(static_cast<Rand::Algo>(newRandAlgo));
this->setDebugText(this->randGenerator.str());
}
// handle UP/DOWN arrow keys for changing the 'pixel' size
const unsigned short oldPixelSize = this->pixelSize;
if(this->isKeyPressed(GLFW_KEY_UP) && this->pixelSize < 100)
++(this->pixelSize);
if(this->isKeyRepeated(GLFW_KEY_UP) && this->pixelSize < 100)
++(this->pixelSize);
if(this->isKeyPressed(GLFW_KEY_DOWN) && this->pixelSize > 1)
--(this->pixelSize);
if(this->isKeyRepeated(GLFW_KEY_DOWN) && this->pixelSize > 1)
--(this->pixelSize);
if(this->pixelSize != oldPixelSize)
this->setPixelSize(this->pixelSize);
// handle F10-F12 keys for changing the rendering mode
const auto currentRenderingMode = this->getRenderingMode();
auto newRenderingMode = currentRenderingMode;
if(this->isKeyPressed(GLFW_KEY_F10))
newRenderingMode = MainWindow::RENDERING_MODE_PBO;
if(this->isKeyPressed(GLFW_KEY_F11))
newRenderingMode = MainWindow::RENDERING_MODE_POINTS;
if(this->isKeyPressed(GLFW_KEY_F12))
newRenderingMode = MainWindow::RENDERING_MODE_TEXTURE;
if(newRenderingMode != currentRenderingMode)
this->setRenderingMode(newRenderingMode);
}