-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
178 lines (148 loc) · 5.37 KB
/
main.cpp
File metadata and controls
178 lines (148 loc) · 5.37 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
#include <iostream>
#include <SDL.h>
#include <thread>
#include "ComplexNumber.h"
#include "ColorConvert.h"
#define WIDTH 1400
#define HEIGHT 800
#define PIXEL_SIZE WIDTH * HEIGHT
#define COLORS 60
void process(int startX, int endX,
int startY, int endY,
float scale,
float re_left, float re_shift,
float im_top, float im_shift,
ComplexNumber* c, ComplexNumber* offset,
int iterations,
unsigned int* pixels,
const unsigned int* colors,
int threshold,
const unsigned int* black) {
for (int x = startX; x < endX; x++) {
float re = scale * (re_left + ((float64_t) x * re_shift));
for (int y = startY; y < endY; y++) {
float im = scale * (im_top + ((float64_t) y * im_shift));
auto base = addComplex(c, ComplexNumber{re, im});
base = addComplex(&base, offset);
auto multiplier = ComplexNumber{re, im};
multiplier = addComplex(&multiplier, offset);
for (int i = 0; i < iterations; i++) {
if (lengthComplex(&base) < threshold) {
pixels[y * WIDTH + x] = *black;
base = addComplex(multiplyComplex(&base, &base), &multiplier);
} else {
pixels[y * WIDTH + x] = colors[i % COLORS];
break;
}
}
}
}
}
int main() {
auto scale = 2.0f;
int iterations = 2;
auto offset = ComplexNumber{-0.4, 0};
auto c = ComplexNumber{0, 0};
float64_t re_left = -1.0f;
float64_t re_shift = 2.0f / (float) WIDTH;
float64_t im_top = -1.0f;
float64_t im_shift = 2.0f / (float) HEIGHT;
auto pixel_size = WIDTH * HEIGHT;
int threshold = 1;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("Hello, World!", 0, 0, WIDTH, HEIGHT, SDL_WINDOW_POPUP_MENU);
SDL_Surface* surface = SDL_GetWindowSurface(window);
auto* pixels = (unsigned int*)surface->pixels;
SDL_Renderer* renderer = SDL_GetRenderer(window);
bool running = true;
const unsigned int BLACK = SDL_MapRGB(surface->format, 0, 0, 0);
unsigned int colors[COLORS];
int color_offset = 0;
while (running) {
bool hasUpdated = false;
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch(e.type) {
case SDL_QUIT:
running = false;
break;
}
}
const Uint8* key_state = SDL_GetKeyboardState(NULL);
if (key_state[SDL_SCANCODE_D]) {
offset = addComplex(&offset, ComplexNumber{0.01f * scale, 0});
hasUpdated = true;
}
if (key_state[SDL_SCANCODE_A]) {
offset = addComplex(&offset, ComplexNumber{-0.01f * scale, 0});
hasUpdated = true;
}
if (key_state[SDL_SCANCODE_W]) {
offset = addComplex(&offset, ComplexNumber{0, -0.01f * scale});
hasUpdated = true;
}
if(key_state[SDL_SCANCODE_S]) {
offset = addComplex(&offset, ComplexNumber{0, 0.01f * scale});
hasUpdated = true;
}
if(key_state[SDL_SCANCODE_E]) {
scale *= 0.9;
hasUpdated = true;
}
if(key_state[SDL_SCANCODE_Q]) {
scale /= 0.9;
hasUpdated = true;
}
if(key_state[SDL_SCANCODE_RIGHT]) {
iterations += 1;
hasUpdated = true;
}
if(key_state[SDL_SCANCODE_LEFT]) {
iterations -= 1;
hasUpdated = true;
}
if(key_state[SDL_SCANCODE_UP]) {
threshold += 1;
hasUpdated = true;
}
if(key_state[SDL_SCANCODE_DOWN]) {
threshold -= 1;
hasUpdated = true;
}
if (key_state[SDL_SCANCODE_Z]) {
color_offset += 1;
for (int i = 0 ; i < COLORS ; i ++) {
RGB col = HSLToRGB(HSL(color_offset + i * 360 / COLORS, 1.0f, 0.5f));
colors[i] = SDL_MapRGB(surface->format, col.R, col.G, col.B);
}
hasUpdated = true;
}
if (key_state[SDL_SCANCODE_X]) {
color_offset -= 1;
for (int i = 0 ; i < COLORS ; i ++) {
RGB col = HSLToRGB(HSL(color_offset + i * 360 / COLORS, 1.0f, 0.5f));
colors[i] = SDL_MapRGB(surface->format, col.R, col.G, col.B);
}
hasUpdated = true;
}
if(key_state[SDL_SCANCODE_ESCAPE]) {
running = false;
}
if (!hasUpdated) { continue; }
SDL_LockSurface(surface);
std::thread draw(SDL_UpdateWindowSurface, window);
int thread_count = HEIGHT;
std::thread THREADS[thread_count];
for (int i = 0 ; i < thread_count ; i ++) {
std::thread t(process, 0, WIDTH, HEIGHT * i/thread_count, HEIGHT * (i + 1)/thread_count, scale, re_left, re_shift, im_top, im_shift, &c, &offset, iterations, pixels, colors, threshold, &BLACK);
THREADS[i].swap(t);
}
for (int i = 0 ; i < thread_count ; i ++) {
THREADS[i].join();
}
draw.join();
SDL_UnlockSurface(surface);
}
// SDL_Event e; bool running = false; while( running == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) running = true; } }
return 0;
}