-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
318 lines (275 loc) · 12.8 KB
/
main.cpp
File metadata and controls
318 lines (275 loc) · 12.8 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <SDL2/SDL.h>
#include <math.h>
#include <stdio.h>
#include <climits>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstring>
#include "FastNoiseLite.h"
#include <chrono>
#include <mutex>
#include <thread>
#define SCREEN_WIDTH 500
#define SCREEN_HEIGHT 300
#define NOISE_FREQ 1
#define RED_FREQ 10
#define GREEN_FREQ 50
#define BLUE_FREQ 1
#define OCTAVES 16
static std::mutex mtx_cout;
struct acout
{
std::unique_lock<std::mutex> lk;
acout()
:
lk(std::unique_lock<std::mutex>(mtx_cout))
{
}
template<typename T>
acout& operator<<(const T& _t)
{
std::cout << _t;
return *this;
}
acout& operator<<(std::ostream& (*fp)(std::ostream&))
{
std::cout << fp;
return *this;
}
};
void updateNoiseField(uint* i, uint* j, float *xred_off, float *xgreen_off, float *yred_off, float *ygreen_off, std::vector<unsigned char>* pixels, std::vector<unsigned char>* pixels2, FastNoiseLite* noise, size_t threads) {
for (int repeat = 0; repeat < (SCREEN_WIDTH-1)*((SCREEN_HEIGHT-2)/(int)(threads-1)); repeat++) {
*i += 1;
if (*i < SCREEN_WIDTH-1) {
if (*j > 0) {
float xfactor = (float)*i/(float)SCREEN_WIDTH;
float yfactor = (float)*j/(float)SCREEN_HEIGHT;
const unsigned int offset = ( SCREEN_WIDTH * 4 * *j ) + *i * 4;
const unsigned int leftneighbour = ( SCREEN_WIDTH * 4 * (*j+1) ) + (*i-1) * 4;
const unsigned int rightneighbour = ( SCREEN_WIDTH * 4 * (*j+1) ) + (*i+1) * 4;
const unsigned int topneighbour = ( SCREEN_WIDTH * 4 * (*j-1) ) + *i * 4;
const unsigned int bottomneighbour = ( SCREEN_WIDTH * 4 * (*j+1) ) + *i * 4;
uint8_t tmp_noiseR = (uint8_t)(floor(255 * noise->GetNoise((xfactor + *xred_off)*RED_FREQ, (yfactor + *yred_off)*RED_FREQ)));
uint8_t tmp_noiseG = (uint8_t)(floor(128 * noise->GetNoise((xfactor + *xgreen_off)*GREEN_FREQ, (yfactor + *ygreen_off)*GREEN_FREQ)));
tmp_noiseR /= floor(100+(rand()%100)+(*j/SCREEN_HEIGHT));
tmp_noiseG /= floor(25+rand()%230);
if (*i > 1 && *i < SCREEN_WIDTH-2 && *j > 1 && *j < SCREEN_HEIGHT-2) {
int tmp_pixelB = 0;
int tmp_pixelR = ((*pixels2)[leftneighbour+2] + (*pixels2)[rightneighbour+2] + (*pixels2)[topneighbour+2] + (*pixels2)[bottomneighbour+2])/4 - tmp_noiseR;
int tmp_pixelG = ((*pixels2)[leftneighbour+1] + (*pixels2)[rightneighbour+1] + (*pixels2)[topneighbour+1] + (*pixels2)[bottomneighbour+1])/4 - tmp_noiseG;
if (tmp_pixelR <= 0)
tmp_pixelR = 0;
if (tmp_pixelR >= 255)
tmp_pixelR = 255;
if (tmp_pixelG <= 0)
tmp_pixelG = 0;
if (tmp_pixelG >= 255)
tmp_pixelG = 255;
(*pixels)[ offset + 0 ] = (uint8_t)tmp_pixelB;
(*pixels)[ offset + 1 ] = (uint8_t)tmp_pixelG;
(*pixels)[ offset + 2 ] = (uint8_t)tmp_pixelR;
(*pixels)[ offset + 3 ] = SDL_ALPHA_OPAQUE;
}
}
else
{
*j = SCREEN_HEIGHT-3;
//*yblue_off += 0.02;
*ygreen_off += 0.15;
*yred_off += 0.20;
}
}
else {
*i = 0;
*j -= 1;
}
}
}
int main( int argc, char** argv )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Window* window = SDL_CreateWindow
(
"Noise",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_FULLSCREEN_DESKTOP
);
SDL_Renderer* renderer = SDL_CreateRenderer
(
window,
-1,
SDL_RENDERER_ACCELERATED
);
SDL_RendererInfo info;
SDL_GetRendererInfo( renderer, &info );
std::cout << "Renderer name: " << info.name << std::endl;
std::cout << "Texture formats: " << std::endl;
for( Uint32 i = 0; i < info.num_texture_formats; i++ )
{
std::cout << SDL_GetPixelFormatName( info.texture_formats[i] ) << std::endl;
}
const unsigned int texWidth = SCREEN_WIDTH;
const unsigned int texHeight = SCREEN_HEIGHT;
SDL_Texture* texture = SDL_CreateTexture
(
renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
texWidth, texHeight
);
std::vector< unsigned char > pixels( texWidth * texHeight * 4, 0 );
std::vector< unsigned char > pixels2( texWidth * texHeight * 4, 0 );
SDL_Event event;
bool running = true;
bool useLocktexture = false;
FastNoiseLite noise(1337);
noise.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
noise.SetFractalType(FastNoiseLite::FractalType_DomainWarpIndependent);
noise.SetFractalOctaves(OCTAVES);
noise.SetFrequency(NOISE_FREQ);
unsigned int frames = 0;
uint i = 0, j = 0;
float xred_off = 0, xgreen_off = 100, prev_xred_off = 256, prev_xblue_off = 256, prev_xgreen_off = 256, yred_off = 0, yblue_off = 0, ygreen_off = 0, prev_yred_off = 256, prev_yblue_off = 256, prev_ygreen_off = 256;
size_t thread_counter = 0;
/*for (i = 0; i < SCREEN_WIDTH; i++) {
for (j = 0; j < SCREEN_HEIGHT; j++) {
const unsigned int offset = ( texWidth * 4 * j ) + i * 4;//= i * 4 * SCREEN_HEIGHT + j * 4;
pixels[ offset + 0 ] = 0;//(uint8_t)(floor(255 * noise. GetNoise((x)* BLUE_FREQ, (y)*BLUE_FREQ))); // b
pixels[ offset + 1 ] = 0;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xgreen_off)*GREEN_FREQ , (yfactor + xgreen_off)*GREEN_FREQ))); // g
pixels[ offset + 2 ] = 0;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xred_off)*RED_FREQ, (yfactor + xred_off)*RED_FREQ))); // r
pixels[ offset + 3 ] = SDL_ALPHA_OPAQUE; // a
pixels2[ offset + 0 ] = 0;//(uint8_t)(floor(255 * noise. GetNoise((x)* BLUE_FREQ, (y)*BLUE_FREQ))); // b
pixels2[ offset + 1 ] = 0;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xgreen_off)*GREEN_FREQ , (yfactor + xgreen_off)*GREEN_FREQ))); // g
pixels2[ offset + 2 ] = 0;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xred_off)*RED_FREQ, (yfactor + xred_off)*RED_FREQ))); // r
pixels2[ offset + 3 ] = SDL_ALPHA_OPAQUE; // a
}
}*/
j = SCREEN_HEIGHT-2;
for (i = 0; i < SCREEN_WIDTH; i++) {
const unsigned int offset = ( texWidth * 4 * j ) + i * 4;//= i * 4 * SCREEN_HEIGHT + j * 4;
pixels[ offset + 0 ] = 0;//(uint8_t)(floor(255 * noise. GetNoise((x)* BLUE_FREQ, (y)*BLUE_FREQ))); // b
pixels[ offset + 1 ] = 128;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xgreen_off)*GREEN_FREQ , (yfactor + xgreen_off)*GREEN_FREQ))); // g
pixels[ offset + 2 ] = 255;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xred_off)*RED_FREQ, (yfactor + xred_off)*RED_FREQ))); // r
pixels[ offset + 3 ] = SDL_ALPHA_OPAQUE; // a
}
j = SCREEN_HEIGHT-1;
for (i = 0; i < SCREEN_WIDTH; i++) {
const unsigned int offset = ( texWidth * 4 * j ) + i * 4;//= i * 4 * SCREEN_HEIGHT + j * 4;
pixels[ offset + 0 ] = 0;//(uint8_t)(floor(255 * noise. GetNoise((x)* BLUE_FREQ, (y)*BLUE_FREQ))); // b
pixels[ offset + 1 ] = 128;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xgreen_off)*GREEN_FREQ , (yfactor + xgreen_off)*GREEN_FREQ))); // g
pixels[ offset + 2 ] = 255;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xred_off)*RED_FREQ, (yfactor + xred_off)*RED_FREQ))); // r
pixels[ offset + 3 ] = SDL_ALPHA_OPAQUE; // a
}
j = SCREEN_HEIGHT;
for (i = 0; i < SCREEN_WIDTH; i++) {
const unsigned int offset = ( texWidth * 4 * j ) + i * 4;//= i * 4 * SCREEN_HEIGHT + j * 4;
pixels[ offset + 0 ] = 0;//(uint8_t)(floor(255 * noise. GetNoise((x)* BLUE_FREQ, (y)*BLUE_FREQ))); // b
pixels[ offset + 1 ] = 128;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xgreen_off)*GREEN_FREQ , (yfactor + xgreen_off)*GREEN_FREQ))); // g
pixels[ offset + 2 ] = 255;//(uint8_t)(floor(255 * noise.GetNoise((xfactor + xred_off)*RED_FREQ, (yfactor + xred_off)*RED_FREQ))); // r
pixels[ offset + 3 ] = SDL_ALPHA_OPAQUE; // a
}
//j = SCREEN_HEIGHT-1;
Uint64 start = SDL_GetPerformanceCounter();
while( running )
{
SDL_SetRenderDrawColor( renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
SDL_RenderClear( renderer );
while( SDL_PollEvent( &event ) )
{
if( ( SDL_QUIT == event.type ) ||
( SDL_KEYDOWN == event.type && SDL_SCANCODE_ESCAPE == event.key.keysym.scancode ) )
{
running = false;
break;
}
if( SDL_KEYDOWN == event.type && SDL_SCANCODE_L == event.key.keysym.scancode )
{
useLocktexture = !useLocktexture;
std::cout << "Using " << ( useLocktexture ? "SDL_LockTexture() + memcpy()" : "SDL_UpdateTexture()" ) << std::endl;
}
}
//std::vector<std::thread> workers_cout;
std::vector<std::thread> workers_acout;
size_t worker(0);
size_t threads(10);
//std::cout << "\nWith acout():" << std::endl;
//size_t tmp = thread_counter;
for (int tmpint = 0; tmpint < 2; tmpint++) {
if (thread_counter < threads) {
//size_t tmp = thread_counter;
//for (thread_counter; thread_counter < tmp+3; thread_counter++) {
i = 0;
j = (uint)(SCREEN_HEIGHT/(uint)(threads)*(uint)thread_counter);
//std::cout << j << std::endl;
workers_acout.emplace_back([&]
{
updateNoiseField(&i, &j, &xred_off, &xgreen_off, &yred_off, &ygreen_off, &pixels, &pixels2, &noise, threads);
});
//worker++;
//}
thread_counter += 1;
//++worker;
} else {
thread_counter = 0;
//acout() << "\tThis is worker " << ++worker << " in thread " << std::this_thread::get_id() << std::endl;
}
}
/*for (auto& w : workers_acout) {
w.join();
}*/
//if (worker >= threads) {
for (auto& w : workers_acout) {
w.join();
}
worker = 0;
//}
if( useLocktexture )
{
unsigned char* lockedPixels = nullptr;
int pitch = 0;
SDL_LockTexture
(
texture,
NULL,
reinterpret_cast< void** >( &lockedPixels ),
&pitch
);
std::memcpy(lockedPixels, pixels.data(), pixels.size());
SDL_UnlockTexture( texture );
}
else
{
SDL_UpdateTexture
(
texture,
NULL,
pixels.data(),
texWidth * 4
);
}
pixels2 = pixels;
SDL_RenderCopy( renderer, texture, NULL, NULL );
SDL_RenderPresent( renderer );
frames++;
const Uint64 end = SDL_GetPerformanceCounter();
const static Uint64 freq = SDL_GetPerformanceFrequency();
const double seconds = ( end - start ) / static_cast< double >( freq );
if( seconds > 2.0 )
{
std::cout
<< frames << " frames in "
<< std::setprecision(1) << std::fixed << seconds << " seconds = "
<< std::setprecision(1) << std::fixed << frames / seconds << " FPS ("
<< std::setprecision(3) << std::fixed << ( seconds * 1000.0 ) / frames << " ms/frame)"
<< std::endl;
start = end;
frames = 0;
}
}
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}