-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdragon.nt
145 lines (116 loc) · 3.9 KB
/
dragon.nt
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
/**
* Developed primarily by gpt-3.5-turbo/Sage! Thanks, poe.com!
*/
module dragon;
macro import std.macro.cimport;
macro import std.macro.listcomprehension;
import std.math;
import c_header("raylib.h");
pragma(lib, "raylib");
version (windows) {
pragma(lib, "gdi32");
pragma(lib, "glfw3");
pragma(lib, "winmm");
} else {
pragma(lib, "glfw");
}
pragma(lib, "m");
pragma(lib, "pthread");
alias vec2f = Vector(float, 2);
class Button {
private Rectangle rec;
private string label;
private ColorFade color;
this(this.rec, this.label, this.color) {}
bool clicked() => IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && hovered;
bool hovered() => GetMousePosition.CheckCollisionPointRec(this.rec);
void draw() {
this.color.update(hovered);
DrawRectangleRec(rec, this.color.currentValue);
import std.string : toStringz;
auto labelPtr = label.toStringz;
DrawText(labelPtr,
cast(int) (rec.x + rec.width / 2 - MeasureText(labelPtr, 20) / 2),
cast(int) (rec.y + rec.height / 2 - 10),
20,
RAYWHITE,
);
free(labelPtr);
}
}
class ColorFade {
private Color normalColor, hoverColor;
private mut float transition;
this(this.normalColor, this.hoverColor) {
this.transition = 0;
}
Color currentValue() => lerp(normalColor, hoverColor, transition);
void update(bool active) {
float δ = GetFrameTime * 8.0;
if (active) transition = (transition + δ).wrap(0, 1);
else transition = (transition - δ).wrap(0, 1);
}
}
float wrap(float a, float from, float to) {
return from if a < from else to if a > to else a;
}
void draw(vec2f from, vec2f to, float depth) {
auto delta = to - from;
auto perpendicular = vec2f(delta.y, -delta.x);
auto midpoint = from + delta * 0.5f;
auto controlPoint = midpoint + perpendicular * 0.5f;
if (depth < 1) {
auto blendDepth = sin(depth * cast(float) pi / 2);
auto fadeMid = blend(midpoint, controlPoint, blendDepth);
DrawLineEx(from.fromVec, fadeMid.fromVec, 3, BLACK);
DrawLineEx(fadeMid.fromVec, to.fromVec, 3, BLACK);
} else {
draw(from, controlPoint, depth - 1);
draw(to, controlPoint, depth - 1);
}
}
T blend(T)(T a, T b, float by) {
return a + (b - a) * by;
}
void main() {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(width=1200, height=1000, "Dragon Curve");
SetTargetFPS(60);
mut float depth = 0;
mut int targetDepth = 0;
auto plusButton = new Button(Rectangle(10, 10, 40, 40), "+",
new ColorFade(GRAY, BLACK));
auto minusButton = new Button(Rectangle(10 + 40 + 10, 10, 40, 40), "-",
new ColorFade(GRAY, BLACK));
while (!WindowShouldClose) {
if (plusButton.clicked) targetDepth++;
if (minusButton.clicked && targetDepth >= 1) targetDepth--;
auto from = vec2f(GetScreenWidth * 0.25f, GetScreenHeight * 0.65f);
auto to = vec2f(GetScreenWidth * 0.85f, GetScreenHeight * 0.65f);
BeginDrawing;
ClearBackground(RAYWHITE);
draw(from, to, depth);
plusButton.draw;
minusButton.draw;
EndDrawing;
depth += (targetDepth - depth) * GetFrameTime * 6.0;
}
CloseWindow;
}
alias toVec = v => vec2f(v.x, v.y);
alias fromVec = v => Vector2(v.x, v.y);
alias RAYWHITE = color(245, 245, 245, 255);
alias LIGHTGRAY = color(200, 200, 200, 255);
alias GRAY = color(128, 128, 128, 255);
alias BLACK = color(0, 0, 0, 255);
alias color = (r, g, b, a) => Color(
cast(char) r, cast(char) g, cast(char) b, cast(char) a);
Color lerp(Color x, Color y, float t) {
int i(char c) => cast(int) c;
int f(float f) => cast(int) f;
int r = (x.r.i + (y.r.i - x.r) * t).f;
int g = (x.g.i + (y.g.i - x.g) * t).f;
int b = (x.b.i + (y.b.i - x.b) * t).f;
int a = (x.a.i + (y.a.i - x.a) * t).f;
return color(r, g, b, a);
}