-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
438 lines (349 loc) · 9.85 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <sstream>
#include "ThreadPool.h"
//The timer for provide FPS Count
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
//Screen dimension constants
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//SDL Renderer
SDL_Renderer* gRenderer = NULL;
//SDL_Texture to Blit Mandelbrot image
SDL_Texture* texture = NULL;
//Thread poo interface (extern)
ThreadPool poolThread;
//Need Exit?
int quit = 0;
//Catch Events
SDL_Event e;
//Array for put each pixel of set
unsigned char pixelArray[SCREEN_WIDTH*SCREEN_HEIGHT*4];
//Keep track of the frame count
int frame = 0;
int CPUCount = 0;
//Timer used to calculate the frames per second
Timer fps;
//Timer used to update the caption
Timer update;
//Mandelbrot set Setup
double MinRe = -2.0;
double MaxRe = 1.0;
double MinIm = -1.2;
double MaxIm = MinIm+(MaxRe-MinRe)*SCREEN_HEIGHT/SCREEN_WIDTH;
double Re_factor = (MaxRe-MinRe)/(SCREEN_WIDTH-1);
double Im_factor = (MaxIm-MinIm)/(SCREEN_HEIGHT-1);
const unsigned MaxIterations = 128;
//Color Palette
unsigned char colorPal[MaxIterations*4];
void draw_madelbrot();
//Task of compute mandelbrot slice
//this is sent to ThreadPool
class SliceComputeTask : Task
{
public:
double step; // is ScreenHeight / NumberOfCores
unsigned index; //Actual "Slice"
int state; //Used for wait all threads done
virtual void *DoWork()
{
state = Running;
//Offset of slice
double y1 = step*index;
for(unsigned y=y1; y<y1+step; ++y)
{
double c_im = MaxIm - y*Im_factor;
for(unsigned x=0; x<SCREEN_WIDTH; ++x)
{
double c_re = MinRe + x*Re_factor;
double Z_re = c_re, Z_im = c_im;
bool isInside = true;
//offset of pixelBuffer
const unsigned offset = ( SCREEN_WIDTH * 4 * y ) + x * 4;
//offset of Color Palette
unsigned pOffset = 0;
unsigned n;
for(n=0; n<MaxIterations; ++n)
{
double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
if(Z_re2 + Z_im2 > 4)
{
isInside = false;
break;
}
else
{
//Draw Pixel According with color table
pixelArray[offset] = colorPal[n];
pixelArray[offset + 1] = colorPal[n+1];
pixelArray[offset + 2] = colorPal[n+2];
pixelArray[offset + 3] = SDL_ALPHA_OPAQUE;
}
Z_im = 2*Z_re*Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
}
if(isInside)
{
//Draw pixel
pixelArray[offset] = 0;
pixelArray[offset + 1] = 255;
pixelArray[offset + 2] = 255;
pixelArray[offset + 3] = SDL_ALPHA_OPAQUE;
}
}
}
//Task finished
state = Finished;
};
};
//Pointer to all task
//don't alloc task on each thread
SliceComputeTask *sliceTasks = NULL;
int main( int argc, char* args[] )
{
//Initialize thread Pool
//Get CPU Count
CPUCount = SDL_GetCPUCount();
//initialize Threads
poolThread.init(CPUCount);
//allocate task's in memory
sliceTasks = new SliceComputeTask[64];
//generate color Palette
for (int k = 0; k < MaxIterations; k++)
{
double s = 6.2831 * k / MaxIterations;
double r = 0.6 + 0.4 * cos(s + 0.9);
double g = 0.6 + 0.4 * cos(s + 0.3);
double b = 0.6 + 0.4 * cos(s + 0.2);
double a = SDL_ALPHA_OPAQUE;
if (r > 1)
r = 1;
if (g > 1)
g = 1;
if (b > 1)
b = 1;
colorPal[k + 0] = k*20;
colorPal[k + 1] = k;
colorPal[k + 2] = 0;
colorPal[k + 3] = SDL_ALPHA_OPAQUE;
}
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "Gravidade", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
gRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED );
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0x00, 0x00, 0x00, 0xFF );
}
//Create texture for blit Mandelbrot to screen
SDL_Texture* texture = SDL_CreateTexture
(
gRenderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT
);
//Start the update timer
update.start();
//Start the frame timer
fps.start();
while(quit == 0)
{
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = 1;
}
}
//Draw Mandelbrod
draw_madelbrot();
//Update texture
SDL_UpdateTexture
(
texture,
NULL,
&pixelArray[0],
SCREEN_WIDTH * 4
);
//Copy to screen
SDL_RenderCopy( gRenderer, texture, NULL, NULL );
//Update screen
SDL_RenderPresent( gRenderer );
frame++;
if( fps.get_ticks() > 1000 )
{
//Sleep the remaining frame time
std::stringstream caption;
//Calculate the frames per second and create the string
caption << "FPS: " << frame / ( fps.get_ticks() / 1000.f );
//Reset the caption
SDL_SetWindowTitle(window,caption.str().c_str());
//Restart the update timer
update.start();
}
}
}
}
//Destroy window
SDL_DestroyWindow( window );
SDL_DestroyTexture(texture);
poolThread.destroy();
//delete sliceTasks;
//Quit SDL subsystems
SDL_Quit();
return 0;
}
//mandelbrot main function
void draw_madelbrot()
{
//calculeta slice size
double step = SCREEN_HEIGHT / (CPUCount*2);
//foreach on CPU
for (int i = 0; i < (CPUCount*2); i++)
{
//Setup task
sliceTasks[i].step = step;
sliceTasks[i].index = i;
sliceTasks[i].state = Initialized;
//Queque task
poolThread.addWork((Task*)&sliceTasks[i]);
}
//wait all task finish
int k = 0;
bool frameDone = false;
while(frameDone == false)
{
if (k+1 == (CPUCount*2))
frameDone = true;
if (sliceTasks[k].state == Finished)
k++;
}
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}