comparison demos/qd.d @ 41:835320b88ad6 trunk

[svn r45] Added a modified version of scrapple.qd (by downs) to the demos dir. It doesn't compile yet though :(
author lindquist
date Fri, 19 Oct 2007 07:45:35 +0200
parents
children b706170e24a9
comparison
equal deleted inserted replaced
40:8b0e809563df 41:835320b88ad6
1 // modified version of scrapple.qd to work with llvmdc
2 import std.stdio;
3
4 //version(none)
5 void main() {
6 screen(640, 480);
7 pset(10, 10);
8 line(0, 0, 100, 100, Box, Back(Red~Black));
9 for (int i=0; i<=100; i+=10) {
10 line(i, 0, 100-i, 100);
11 line(0, i, 100, 100-i);
12 }
13 circle(100, 100, 50, 15, White~Black, Fill=White~Black);
14 paint(200, 200, Red, Back=White);
15 circle(100, 100, 50, 15, White);
16 paint(200, 200, Black);
17 pset(10, 11); pset(10, 11, Black);
18 pset(10, 10);
19 SDL_Delay(5000);
20 }
21
22 extern(C) {
23 struct SDL_Rect {
24 short x, y;
25 ushort w, h;
26 }
27 struct SDL_PixelFormat {
28 //SDL_Palette *palette;
29 void *palette;
30 ubyte BitsPerPixel, BytesPerPixel, Rloss, Gloss, Bloss, Aloss, Rshift, Gshift, Bshift, Ashift;
31 uint Rmask, Gmask, Bmask, Amask, colorkey; ubyte alpha;
32 }
33 struct SDL_Surface {
34 uint flags;
35 SDL_PixelFormat *format;
36 int w, h;
37 ushort pitch;
38 void *pixels;
39 int offset;
40 void *hwdata;
41 SDL_Rect clip_rect;
42 uint unused;
43 uint locked;
44 void *map;
45 uint format_version;
46 int refcount;
47 }
48 uint SDL_MapRGBA(SDL_PixelFormat *format, ubyte r, ubyte g, ubyte b, ubyte a);
49 void SDL_GetRGBA(uint pixel, SDL_PixelFormat *fmt, ubyte *r, ubyte *g, ubyte *b, ubyte *a);
50 int SDL_LockSurface(SDL_Surface *);
51 void SDL_UnlockSurface(SDL_Surface *);
52 SDL_Surface * SDL_SetVideoMode(int width, int height, int bpp, uint flags);
53 int SDL_Flip(SDL_Surface *);
54 void SDL_Delay(uint);
55 const uint SDL_SWSURFACE=0;
56 const uint SDL_HWSURFACE=1;
57 const uint SDL_DOUBLEBUF=0x40000000;
58 const uint SDL_FULLSCREEN=0x80000000;
59 }
60
61 SDL_Surface *display;
62
63 void putpixel32(SDL_Surface *surf, int x, int y, ubyte[4] col) {
64 uint *bufp = cast(uint *)surf.pixels + y*surf.pitch/4 + x;
65 *bufp = SDL_MapRGBA(surf.format, col[0], col[1], col[2], col[3]);
66 }
67
68 void getpixel32(SDL_Surface *surf, int x, int y, ubyte[4] *col) {
69 uint *bufp = cast(uint *)surf.pixels + y*surf.pitch/4 + x;
70 SDL_GetRGBA(*bufp, surf.format, &(*col)[0], &(*col)[1], &(*col)[2], &(*col)[3]);
71 }
72
73 struct rgb {
74 ubyte[3] values;
75 rgb opCat(rgb other) {
76 rgb res;
77 foreach (id, ref v; res.values) v=(values[id]+other.values[id])/2;
78 return res;
79 }
80 bool opEquals(rgb r) {
81 return values == r.values;
82 }
83 }
84
85 void putpixel(SDL_Surface *surf, int x, int y, rgb c) {
86 if ( (x<0) || (y<0) || (x!<surf.w) || (y!<surf.h) ) return;
87 putpixel32(surf, x, y, [c.values[0], c.values[1], c.values[2], 0]);
88 }
89
90 const rgb White={[255, 255, 255]};
91 const rgb Black={[0, 0, 0]};
92 const rgb Red={[255, 0, 0]};
93 const rgb Green={[0, 255, 0]};
94 const rgb Blue={[0, 0, 255]};
95 rgb color=White;
96 rgb back=Black;
97
98 template failfind(U, T...) {
99 static if (T.length)
100 static if (is(T[0] == U)) static assert(false, "Duplicate "~U.stringof~" found!");
101 else const bool failfind=failfind!(U, T[1..$]);
102 else
103 const bool failfind=true;
104 }
105
106 template select(U, T...) {
107 static if(T.length)
108 static if (is(U == T[0])) { static if (failfind!(U, T[1..$])) { }; const int select = 0; }
109 else
110 static if (select!(U, T[1..$]) != -1)
111 const int select = 1 + select!(U, T[1..$]);
112 else
113 const int select = -1;
114 else
115 const int select = -1;
116 }
117
118 typedef rgb back_rgb;
119 back_rgb Back(rgb r) { return cast(back_rgb) r; }
120 back_rgb Back() { return cast(back_rgb) back; }
121 typedef rgb box_rgb;
122 box_rgb Box(rgb r) { return cast(box_rgb) r; }
123 box_rgb Box() { return cast(box_rgb) color; }
124 alias Back Fill;
125
126 void execParams(T...)(T params) {
127 const int col=select!(rgb, T);
128 static if (col != -1) color=params[col];
129 const int bcol=select!(back_rgb, T);
130 static if (bcol != -1) back=cast(rgb) params[bcol];
131 const int boxcol=select!(box_rgb, T);
132 static if (boxcol != -1) color=cast(rgb) params[boxcol];
133 }
134
135 void pset(T...)(int x, int y, T params) {
136 SDL_LockSurface(display);
137 scope(exit) { SDL_UnlockSurface(display); SDL_Flip(display); }
138 execParams(params);
139 putpixel(display, x, y, color);
140 }
141
142 rgb pget(int x, int y) {
143 SDL_LockSurface(display);
144 scope(exit) SDL_UnlockSurface(display);
145 ubyte[4] c;
146 getpixel32(display, x, y, &c);
147 rgb res; res.values[]=c[0..3]; return res;
148 }
149
150 void swap(T)(ref T a, ref T b) { T c=a; a=b; b=c; }
151
152 T abs(T)(T f) { return f < 0 ? -f : f; }
153
154 void bresenham(bool countUp=true, bool steep=false)(int x0, int y0, int x1, int y1) {
155 auto deltax = x1 - x0, deltay = y1 - y0;
156 static if (steep) {
157 auto Δerror = cast(float)deltax / cast(float)deltay;
158 auto var2 = x0;
159 const string name="y";
160 } else {
161 auto Δerror = cast(float)deltay / cast(float)deltax;
162 auto var2 = y0;
163 const string name="x";
164 }
165 auto error = 0f;
166 for (auto var1 = mixin(name~'0'); var1 <= mixin(name~'1'); ++var1) {
167 static if (steep) putpixel(display, var2, var1, color);
168 else putpixel(display, var1, var2, color);
169 error += Δerror;
170 if (abs(error) >= 1f) { static if (countUp) { var2++; error -= 1f; } else { var2--; error += 1f; }}
171 }
172 }
173
174 T max(T)(T a, T b) { return a>b?a:b; }
175 T min(T)(T a, T b) { return a<b?a:b; }
176
177 void line(T...)(int x0, int y0, int x1, int y1, T p) {
178 execParams(p);
179 static if (select!(back_rgb, T)!=-1) {
180 SDL_LockSurface(display);
181 scope(exit) { SDL_UnlockSurface(display); SDL_Flip(display); }
182 auto xend=max(x0, x1);
183 for (int x=min(x0, x1); x<=xend; ++x) {
184 auto yend=max(y0, y1);
185 for (int y=min(y0, y1); y<=yend; ++y) {
186 putpixel(display, x, y, back);
187 }
188 }
189 }
190 static if (select!(box_rgb, T)!=-1) {
191 line(x0, y0, x1, y0);
192 line(x1, y0, x1, y1);
193 line(x1, y1, x0, y1);
194 line(x0, y1, x0, y0);
195 }
196 static if (select!(box_rgb, T)+select!(back_rgb, T)==-2) {
197 SDL_LockSurface(display);
198 scope(exit) { SDL_UnlockSurface(display); SDL_Flip(display); }
199 bool steep = abs(y1 - y0) > abs(x1 - x0);
200 void turn() { swap(x0, x1); swap(y0, y1); }
201 if (steep) { if (y1 < y0) turn; }
202 else { if (x1 < x0) turn; }
203 bool stepUp=steep ? (x0 < x1) : (y0 < y1);
204 if (steep) {
205 if (stepUp) bresenham!(true, true)(x0, y0, x1, y1);
206 else bresenham!(false, true)(x0, y0, x1, y1);
207 } else {
208 if (stepUp) bresenham!(true, false)(x0, y0, x1, y1);
209 else bresenham!(false, false)(x0, y0, x1, y1);
210 }
211 }
212 }
213
214 import llvm.intrinsic;
215 alias llvm_sqrt sqrt;
216
217 template circle_bresenham_pass(bool first) {
218 const string xy=(first?"x":"y");
219 const string yx=(first?"y":"x");
220 const string str="
221 auto x="~(first?"xradius":"0")~";
222 auto y="~(first?"0":"yradius")~";
223 auto xchange=yradius*yradius*"~(first?"(1-2*xradius)":"1")~";
224 auto ychange=xradius*xradius*"~(first?"1":"(1-2*yradius)")~";
225 auto error=0;
226 auto stopx="~(first?"y2square*xradius":"0")~";
227 auto stopy="~(first?"0":"x2square*yradius")~";
228 while (stopx"~(first?">=":"<=")~"stopy) {
229 putpixel(display, cx+x, cy+y, color);
230 putpixel(display, cx+x, cy-y, color);
231 putpixel(display, cx-x, cy+y, color);
232 putpixel(display, cx-x, cy-y, color);
233 "~yx~"++;
234 stop"~yx~"+="~xy~"2square;
235 error+="~yx~"change;
236 "~yx~"change+="~xy~"2square;
237 if ((2*error+"~xy~"change)>0) {
238 --"~xy~";
239 stop"~xy~"-="~yx~"2square;
240 error+="~xy~"change;
241 "~xy~"change+="~yx~"2square;
242 }
243 }
244 ";
245 }
246
247 void circle(T...)(int cx, int cy, int xradius, T t) {
248 SDL_LockSurface(display);
249 scope(exit) { SDL_UnlockSurface(display); SDL_Flip(display); }
250 execParams(t);
251 auto yradius=xradius;
252 static if (T.length && is(T[0]: int)) yradius=t[0];
253 static if (select!(back_rgb, T) != -1) {
254 auto ratio=xradius*1f/yradius;
255 for (int i=0; i<=yradius; ++i) {
256 ushort j=cast(ushort)(sqrt(cast(real)(yradius*yradius-i*i))*ratio);
257 for (int lx=cx-j; lx<=cx+j; ++lx) putpixel(display, lx, cy+i, back);
258 for (int lx=cx-j; lx<=cx+j; ++lx) putpixel(display, lx, cy-i, back);
259 }
260 }
261 auto x2square=2*xradius*xradius;
262 auto y2square=2*yradius*yradius;
263 { mixin(circle_bresenham_pass!(true).str); }
264 { mixin(circle_bresenham_pass!(false).str); }
265 }
266
267 struct floodfill_node {
268 int x, y;
269 static floodfill_node opCall(int x, int y) {
270 floodfill_node res;
271 res.x=x; res.y=y;
272 return res;
273 }
274 }
275
276 void paint(T...)(int x, int y, T t) {
277 SDL_LockSurface(display);
278 scope(exit) { SDL_UnlockSurface(display); SDL_Flip(display); }
279 execParams(t);
280 bool border=true;
281 if (select!(back_rgb, T) == -1) {
282 back=pget(x, y);
283 border=false;
284 }
285 bool check(rgb r) {
286 if (border) return (r != back) && (r != color);
287 else return r == back;
288 }
289 if (back == color) throw new Exception("Having identical backgrounds and foregrounds will severely mess up floodfill.");
290 alias floodfill_node node;
291 node[] queue;
292 queue ~= node(x, y);
293 size_t count=0;
294 while (count<queue.length) {
295 scope(exit) count++;
296 with (queue[count]) {
297 if (check(pget(x, y))) {
298 int w=x, e=x;
299 if (w<display.w) do w++; while ((w<display.w) && check(pget(w, y)));
300 if (e>=0) do e--; while (e>=0 && check(pget(e, y)));
301 //SDL_Flip(display);
302 for (int i=e+1; i<w; ++i) {
303 putpixel(display, i, y, color);
304 if (y && check(pget(i, y-1)) && ((i==w-1)||!check(pget(i+1, y-1)))) queue ~= node(i, y-1);
305 if ((y < display.h-1) && check(pget(i, y+1)) && ((i==w-1)||!check(pget(i+1, y+1)))) queue ~= node(i, y+1);
306 }
307 }
308 }
309 }
310 }
311
312 void screen(size_t w, size_t h) {
313 display = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE);
314 }
315
316 void cls() { line(0, 0, display.w-1, display.h-1, Fill=Black); }