-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.d
150 lines (131 loc) · 3.76 KB
/
main.d
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
/**
* ae.demo.canvas.main
*
* License:
* This Source Code Form is subject to the terms of
* the Mozilla Public License, v. 2.0. If a copy of
* the MPL was not distributed with this file, You
* can obtain one at http://mozilla.org/MPL/2.0/.
*
* Authors:
* Vladimir Panteleev <ae@cy.md>
*/
module ae.demo.canvas.main;
import std.random;
import std.algorithm : min, max;
import std.datetime, std.conv;
import ae.ui.app.application;
import ae.ui.app.posix.main;
import ae.ui.shell.shell;
import ae.ui.shell.sdl2.shell;
import ae.ui.video.video;
import ae.ui.video.sdl2.video;
import ae.ui.video.renderer;
import ae.utils.math;
import ae.utils.geometry : TAU;
import ae.utils.graphics.draw;
import ae.utils.fps;
final class MyApplication : Application
{
override string getName() { return "Demo/Canvas"; }
override string getCompanyName() { return "CyberShadow"; }
Shell shell;
FPSCounter fps;
bool first = true;
override void render(Renderer s)
{
fps.tick(&shell.setCaption);
auto canvas = s.lock();
scope(exit) s.unlock();
ubyte randByte() { return cast(ubyte)uniform(0, 256); }
BGRX randColor() { return BGRX(randByte(), randByte(), randByte()); }
xy_t randX() { return uniform(0, canvas.w); }
xy_t randY() { return uniform(0, canvas.h); }
enum Shape
{
pixel, hline, vline, line, rect, fillRect, fillRect2, circle, sector, poly,
softCircle, softRing, aaLine,
}
static Shape shape;
if (first)
canvas.whiteNoise(),
shape = cast(Shape) uniform!"(]"(0, Shape.max),
first = false;
final switch (shape)
{
case Shape.pixel:
canvas[randX(), randY()] = randColor();
return;
case Shape.hline:
return canvas.hline(randX(), randX(), randY(), randColor());
case Shape.vline:
return canvas.vline(randX(), randY(), randY(), randColor());
case Shape.line:
return canvas.line(randX(), randY(), randX(), randY(), randColor());
case Shape.rect:
return canvas.rect (randX(), randY(), randX(), randY(), randColor());
case Shape.fillRect:
return canvas.fillRect(randX(), randY(), randX(), randY(), randColor());
case Shape.fillRect2:
return canvas.fillRect(randX(), randY(), randX(), randY(), randColor(), randColor());
case Shape.circle:
{
int r = uniform(10, 100);
return canvas.fillCircle(uniform(r, canvas.w-r), uniform(r, canvas.h-r), r, randColor());
}
case Shape.sector:
{
int r1 = uniform(10, 100);
int r0 = uniform(0, r1);
return canvas.fillSector(uniform(r1, canvas.w-r1), uniform(r1, canvas.h-r1), r0, r1, uniform(0, TAU), uniform(0, TAU), randColor());
}
case Shape.poly:
{
auto coords = new Coord[uniform(3, 10)];
foreach (ref coord; coords)
coord = Coord(randX(), randY());
return canvas.fillPoly(coords, randColor());
}
case Shape.softCircle:
{
int r1 = uniform(10, 100);
int r0 = uniform(0, r1-5);
return canvas.softCircle(uniform(r1, canvas.w-r1), uniform(r1, canvas.h-r1), r0, r1, randColor());
}
case Shape.softRing:
{
int r2 = uniform(15, 100);
int r0 = uniform(0, r2-10);
int r1 = uniform(r0+5, r2-5);
return canvas.softRing(uniform(r2, canvas.w-r2), uniform(r2, canvas.h-r2), r0, r1, r2, randColor());
}
case Shape.aaLine:
return canvas.aaLine(randX(), randY(), randX(), randY(), randColor());
}
}
override void handleMouseDown(uint x, uint y, MouseButton button)
{
first = true;
}
override void setShellSettings(ShellSettings settings)
{
first = true;
return super.setShellSettings(settings);
}
override int run(string[] args)
{
shell = new SDL2Shell(this);
shell.video = new SDL2SoftwareVideo();
shell.run();
shell.video.shutdown();
return 0;
}
override void handleQuit()
{
shell.quit();
}
}
shared static this()
{
createApplication!MyApplication();
}