# HG changeset patch # User lindquist # Date 1192772735 -7200 # Node ID 835320b88ad6f688296418d58722743a0ddbf155 # Parent 8b0e809563dfefb2b707da1f6309c733218645c2 [svn r45] Added a modified version of scrapple.qd (by downs) to the demos dir. It doesn't compile yet though :( diff -r 8b0e809563df -r 835320b88ad6 demos/qd.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/qd.d Fri Oct 19 07:45:35 2007 +0200 @@ -0,0 +1,316 @@ +// modified version of scrapple.qd to work with llvmdc +import std.stdio; + +//version(none) +void main() { + screen(640, 480); + pset(10, 10); + line(0, 0, 100, 100, Box, Back(Red~Black)); + for (int i=0; i<=100; i+=10) { + line(i, 0, 100-i, 100); + line(0, i, 100, 100-i); + } + circle(100, 100, 50, 15, White~Black, Fill=White~Black); + paint(200, 200, Red, Back=White); + circle(100, 100, 50, 15, White); + paint(200, 200, Black); + pset(10, 11); pset(10, 11, Black); + pset(10, 10); + SDL_Delay(5000); +} + +extern(C) { + struct SDL_Rect { + short x, y; + ushort w, h; + } + struct SDL_PixelFormat { + //SDL_Palette *palette; + void *palette; + ubyte BitsPerPixel, BytesPerPixel, Rloss, Gloss, Bloss, Aloss, Rshift, Gshift, Bshift, Ashift; + uint Rmask, Gmask, Bmask, Amask, colorkey; ubyte alpha; + } + struct SDL_Surface { + uint flags; + SDL_PixelFormat *format; + int w, h; + ushort pitch; + void *pixels; + int offset; + void *hwdata; + SDL_Rect clip_rect; + uint unused; + uint locked; + void *map; + uint format_version; + int refcount; + } + uint SDL_MapRGBA(SDL_PixelFormat *format, ubyte r, ubyte g, ubyte b, ubyte a); + void SDL_GetRGBA(uint pixel, SDL_PixelFormat *fmt, ubyte *r, ubyte *g, ubyte *b, ubyte *a); + int SDL_LockSurface(SDL_Surface *); + void SDL_UnlockSurface(SDL_Surface *); + SDL_Surface * SDL_SetVideoMode(int width, int height, int bpp, uint flags); + int SDL_Flip(SDL_Surface *); + void SDL_Delay(uint); + const uint SDL_SWSURFACE=0; + const uint SDL_HWSURFACE=1; + const uint SDL_DOUBLEBUF=0x40000000; + const uint SDL_FULLSCREEN=0x80000000; +} + +SDL_Surface *display; + +void putpixel32(SDL_Surface *surf, int x, int y, ubyte[4] col) { + uint *bufp = cast(uint *)surf.pixels + y*surf.pitch/4 + x; + *bufp = SDL_MapRGBA(surf.format, col[0], col[1], col[2], col[3]); +} + +void getpixel32(SDL_Surface *surf, int x, int y, ubyte[4] *col) { + uint *bufp = cast(uint *)surf.pixels + y*surf.pitch/4 + x; + SDL_GetRGBA(*bufp, surf.format, &(*col)[0], &(*col)[1], &(*col)[2], &(*col)[3]); +} + +struct rgb { + ubyte[3] values; + rgb opCat(rgb other) { + rgb res; + foreach (id, ref v; res.values) v=(values[id]+other.values[id])/2; + return res; + } + bool opEquals(rgb r) { + return values == r.values; + } +} + +void putpixel(SDL_Surface *surf, int x, int y, rgb c) { + if ( (x<0) || (y<0) || (x!= 1f) { static if (countUp) { var2++; error -= 1f; } else { var2--; error += 1f; }} + } +} + +T max(T)(T a, T b) { return a>b?a:b; } +T min(T)(T a, T b) { return a abs(x1 - x0); + void turn() { swap(x0, x1); swap(y0, y1); } + if (steep) { if (y1 < y0) turn; } + else { if (x1 < x0) turn; } + bool stepUp=steep ? (x0 < x1) : (y0 < y1); + if (steep) { + if (stepUp) bresenham!(true, true)(x0, y0, x1, y1); + else bresenham!(false, true)(x0, y0, x1, y1); + } else { + if (stepUp) bresenham!(true, false)(x0, y0, x1, y1); + else bresenham!(false, false)(x0, y0, x1, y1); + } + } +} + +import llvm.intrinsic; +alias llvm_sqrt sqrt; + +template circle_bresenham_pass(bool first) { + const string xy=(first?"x":"y"); + const string yx=(first?"y":"x"); + const string str=" + auto x="~(first?"xradius":"0")~"; + auto y="~(first?"0":"yradius")~"; + auto xchange=yradius*yradius*"~(first?"(1-2*xradius)":"1")~"; + auto ychange=xradius*xradius*"~(first?"1":"(1-2*yradius)")~"; + auto error=0; + auto stopx="~(first?"y2square*xradius":"0")~"; + auto stopy="~(first?"0":"x2square*yradius")~"; + while (stopx"~(first?">=":"<=")~"stopy) { + putpixel(display, cx+x, cy+y, color); + putpixel(display, cx+x, cy-y, color); + putpixel(display, cx-x, cy+y, color); + putpixel(display, cx-x, cy-y, color); + "~yx~"++; + stop"~yx~"+="~xy~"2square; + error+="~yx~"change; + "~yx~"change+="~xy~"2square; + if ((2*error+"~xy~"change)>0) { + --"~xy~"; + stop"~xy~"-="~yx~"2square; + error+="~xy~"change; + "~xy~"change+="~yx~"2square; + } + } + "; +} + +void circle(T...)(int cx, int cy, int xradius, T t) { + SDL_LockSurface(display); + scope(exit) { SDL_UnlockSurface(display); SDL_Flip(display); } + execParams(t); + auto yradius=xradius; + static if (T.length && is(T[0]: int)) yradius=t[0]; + static if (select!(back_rgb, T) != -1) { + auto ratio=xradius*1f/yradius; + for (int i=0; i<=yradius; ++i) { + ushort j=cast(ushort)(sqrt(cast(real)(yradius*yradius-i*i))*ratio); + for (int lx=cx-j; lx<=cx+j; ++lx) putpixel(display, lx, cy+i, back); + for (int lx=cx-j; lx<=cx+j; ++lx) putpixel(display, lx, cy-i, back); + } + } + auto x2square=2*xradius*xradius; + auto y2square=2*yradius*yradius; + { mixin(circle_bresenham_pass!(true).str); } + { mixin(circle_bresenham_pass!(false).str); } +} + +struct floodfill_node { + int x, y; + static floodfill_node opCall(int x, int y) { + floodfill_node res; + res.x=x; res.y=y; + return res; + } +} + +void paint(T...)(int x, int y, T t) { + SDL_LockSurface(display); + scope(exit) { SDL_UnlockSurface(display); SDL_Flip(display); } + execParams(t); + bool border=true; + if (select!(back_rgb, T) == -1) { + back=pget(x, y); + border=false; + } + bool check(rgb r) { + if (border) return (r != back) && (r != color); + else return r == back; + } + if (back == color) throw new Exception("Having identical backgrounds and foregrounds will severely mess up floodfill."); + alias floodfill_node node; + node[] queue; + queue ~= node(x, y); + size_t count=0; + while (count=0) do e--; while (e>=0 && check(pget(e, y))); + //SDL_Flip(display); + for (int i=e+1; i