-
Notifications
You must be signed in to change notification settings - Fork 1
/
twister.cc
76 lines (62 loc) · 1.93 KB
/
twister.cc
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include "buffer.hh"
/*
* Twister effect
* 2019, Giulio Zausa
* gcc twister.cc -o twister -l stdc++ -l SDL2 -std=c++17 -Ofast
*/
#define WIDTH 640
#define HEIGHT 480
#define convCoord(x, y) (y * WIDTH + x)
#define vline vline_AA
inline uint32_t blend(uint32_t color1, uint32_t color2, uint8_t alpha) {
uint32_t rb = color1 & 0xff00ff;
uint32_t g = color1 & 0x00ff00;
rb += ((color2 & 0xff00ff) - rb) * alpha >> 8;
g += ((color2 & 0x00ff00) - g) * alpha >> 8;
return (rb & 0xff00ff) | (g & 0xff00);
}
inline void vline_INT(uint32_t *buf, int x, int y0, int y1, uint32_t color) {
int i = convCoord(x, (int)y0);
for (int y = y0; y < y1; y++) {
double d = (abs(0.5 - pow((y - y0) / (float)(y1 - y0), 3)) / 2 + 0.5);
buf[i] = blend(color, buf[i], d * 255);
i += WIDTH;
}
}
inline void vline_AA(uint32_t *buf, int x, double y0, double y1,
uint32_t color) {
for (double y = y0; y < y1; y += 0.5) {
int i = convCoord(x, (int)y);
double d = (abs(0.5 - pow((y - y0) / (float)(y1 - y0), 3)) / 2 + 0.5);
buf[i] = blend(color, buf[i], d * 255);
}
}
int main() {
auto wnd = BufferWindow<WIDTH, HEIGHT>();
auto time = 0.0;
while (!wnd.checkExit()) {
wnd.startDraw();
memset(wnd.pixels, 0, WIDTH * HEIGHT * 4);
auto p = time / 500;
auto scale = 100;
auto off = HEIGHT / 2;
for (int x = 0; x < WIDTH; x++) {
auto y0 = (sin(p) * scale) + off;
auto y1 = (sin(p + M_PI_2) * scale) + off;
auto y2 = (sin(p + M_PI) * scale) + off;
auto y3 = (sin(p + M_PI_2 * 3) * scale) + off;
vline(wnd.pixels, x, y3, y0, 0xFFFFFF00);
vline(wnd.pixels, x, y2, y3, 0xFFFF0000);
vline(wnd.pixels, x, y1, y2, 0xFF00FF00);
vline(wnd.pixels, x, y0, y1, 0xFF0000FF);
p += 0.005 * sin(time / 500) * 3;
}
wnd.endDraw();
time += wnd.getDeltaTime();
}
return 0;
}