Changeset 373
- Timestamp:
- 07/03/08 00:54:53 (5 months ago)
- Files:
-
- trunk/qd/ProggyClean.ttf (added)
- trunk/qd/dsss.conf (modified) (2 diffs)
- trunk/qd/qd.d (modified) (7 diffs)
- trunk/qd/test.d (modified) (1 diff)
- trunk/qd/test15.d (added)
- trunk/redimple (added)
- trunk/redimple/dsss.conf (added)
- trunk/redimple/redimple.d (added)
- trunk/tools/tools/downloader.d (modified) (1 diff)
- trunk/tools/tools/page_queue.d (modified) (1 diff)
- trunk/tools/tools/threads.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/qd/dsss.conf
r369 r373 4 4 # [dt/main.d] 5 5 # buildflags=-L-lSDL -L-lSDL_ttf -g -O 6 #[test.d]7 # buildflags=-L-lSDL 6 [test.d] 7 buildflags=-L-lSDL -g 8 8 [test2.d] 9 9 buildflags=-L-lSDL -L-lSDL_ttf -g … … 33 33 [snake.d] 34 34 buildflags=-L-lSDL 35 [test15.d] 36 buildflags=-L-lSDL -L-lSDL_ttf trunk/qd/qd.d
r369 r373 1 1 module qd; 2 2 import std.c.string, std.math, std.traits: ParameterTypeTuple; 3 4 uint delegate() qd_rand; 5 import std.random; 6 static this() { qd_rand = { return rand(); }; } 3 7 4 8 import tools.base; … … 182 186 return res; 183 187 } 188 static rgb rand() { return rgb(qd_rand() % 255, qd_rand() % 255, qd_rand() % 255); } 184 189 bool opEquals(rgb r) { 185 190 return values == r.values; … … 227 232 putpixel32(display.surface, transformed.tupleof, col); 228 233 } 234 235 void blend(ubyte* rgba, ubyte nr, ubyte ng, ubyte nb, ubyte alpha) { 236 rgba[0] = cast(ubyte) (rgba[0] + ((nr - rgba[0]) * alpha) / 255); 237 rgba[1] = cast(ubyte) (rgba[1] + ((ng - rgba[1]) * alpha) / 255); 238 rgba[2] = cast(ubyte) (rgba[2] + ((nb - rgba[2]) * alpha) / 255); 239 } 240 229 241 void putpixel(int x, int y, ubyte r, ubyte g, ubyte b) { 230 242 putpixel(x, y, SDL_MapRGBA(display.surface.format, r, g, b, 0)); 243 } 244 void putpixel(int x, int y, ubyte[3] rgb, ubyte a) { 245 ubyte[4] old_rgba; 246 getpixel32(display.surface, display.tl.x + x, display.tl.y + y, &old_rgba); 247 blend(old_rgba.ptr, rgb[0], rgb[1], rgb[2], a); 248 putpixel(x, y, SDL_MapRGBA(display.surface.format, old_rgba[0], old_rgba[1], old_rgba[2], 0)); 231 249 } 232 250 … … 355 373 } 356 374 375 void bresenham_aa(bool countUp=true, bool steep=false)(int x0, int y0, int x1, int y1) { 376 auto deltax = x1 - x0, deltay = y1 - y0; 377 static if (steep) { 378 auto Îerror = cast(float)deltax / cast(float)deltay; 379 auto var2 = x0; 380 const string name="y"; 381 } else { 382 auto Îerror = cast(float)deltay / cast(float)deltax; 383 auto var2 = y0; 384 const string name="x"; 385 } 386 auto error = 0f; 387 for (auto var1 = mixin(name~'0'); var1 <= mixin(name~'1'); ++var1) { 388 auto alpha = cast(ubyte) (error*255); 389 static if (steep) { 390 if (countUp) { 391 putpixel(var2, var1, color.values, 255-alpha); 392 if (var2 < display.width) putpixel(var2+1, var1, color.values, alpha); 393 } else { 394 putpixel(var2, var1, color.values, alpha); 395 if (var2 > 0) putpixel(var2-1, var1, color.values, 255-alpha); 396 } 397 } else { 398 if (countUp) { 399 putpixel(var1, var2, color.values, 255-alpha); 400 if (var2 < display.height) putpixel(var1, var2+1, color.values, alpha); 401 } else { 402 putpixel(var1, var2, color.values, alpha); 403 if (var2 > 0) putpixel(var1, var2-1, color.values, 255-alpha); 404 } 405 } 406 error += Îerror; 407 if (abs(error) >= 1f) { static if (countUp) { var2++; error -= 1f; } else { var2--; error += 1f; }} 408 } 409 } 410 357 411 template _UnPoint(int curOffs, T...) { 358 412 static if (T.length) { … … 395 449 } 396 450 451 bool aa = true; 452 397 453 void _line(T...)(int x0, int y0, int x1, int y1, T p) { 398 454 static int max(int a, int b) { return a>b?a:b; } … … 428 484 else { if (x1 < x0) turn; } 429 485 bool stepUp=steep ? (x0 < x1) : (y0 < y1); 430 if (steep) { 431 if (stepUp) bresenham!(true, true)(x0, y0, x1, y1); 432 else bresenham!(false, true)(x0, y0, x1, y1); 433 } else { 434 if (stepUp) bresenham!(true, false)(x0, y0, x1, y1); 435 else bresenham!(false, false)(x0, y0, x1, y1); 436 } 486 mixin(IfBranch!( 487 "aa", "$1", "_aa", "", "steep", "$2", "true", "false", "stepUp", "$3", "true", "false", "bresenham$1!($3, $2)(x0, y0, x1, y1); " 488 )); 437 489 } 438 490 } … … 488 540 import std.stdio; 489 541 void circle(T...)(T t) { 542 static assert(T.length!<3, "Circle: Needs x, y and radius"); 543 int cx=cast(int)t[0], cy=cast(int)t[1]; float radius=cast(float)t[2]; 544 SDL_LockSurface(display.surface); 545 scope(exit) { SDL_UnlockSurface(display.surface); if (doFlip) flip; } 546 execParams(t[3..$]); 547 if (radius!>0) return; 548 static if (T.length>3 && is(T[3]: int)) yradius=t[3]; 549 static if (Select!(back_rgb, T) != -1) { 550 auto back_sdl=SDL_MapRGBA(display.surface.format, back.values[0], back.values[1], back.values[2], 0); 551 for (int i=0; i<=radius; ++i) { 552 ushort j=cast(ushort)(sqrt(cast(real)(radius*radius-i*i))); 553 hline(cx-j, cy+i, 2*j, back_sdl); 554 if (i) hline(cx-j, cy-i, 2*j, back_sdl); 555 } 556 } 557 static if (Select!(back_rgb, T) == -1 || Select!(rgb, T) != -1) { 558 auto x2square=2*radius*radius, y2square=x2square; 559 uint col=SDL_MapRGBA(display.surface.format, color.values[0], color.values[1], color.values[2], 0); 560 { mixin(circle_bresenham_pass!(true).str); } 561 { mixin(circle_bresenham_pass!(false).str); } 562 } 563 } 564 565 void circle_aa(T...)(T t) { 490 566 static assert(T.length!<3, "Circle: Needs x, y and radius"); 491 567 int cx=cast(int)t[0], cy=cast(int)t[1]; float radius=cast(float)t[2]; trunk/qd/test.d
r97 r373 1 1 module test; 2 import qd, std.random; 2 3 3 import qd; 4 5 import std.stdio, std.perf, std.c.string; 6 7 ulong time(void delegate() dg) { 8 auto timer=new PerformanceCounter; 9 timer.start; 10 dg(); 11 timer.stop; 12 return cast(ulong)(timer.milliseconds); 4 import tools.base; 5 void delegate(proc) every(int i) { 6 return stuple(i, 0) /apply/ (int i, ref int count, proc p) { 7 count++; 8 if (count == i) { count = 0; p(); } 9 }; 13 10 } 14 11 15 template New(T) { 16 static if(is(T==struct)) { 17 T *New(U...)(U tuple) { 18 auto res=new T; 19 foreach (id, value; tuple) res.tupleof[id]=cast(typeof(res.tupleof[id]))value; 20 return res; 21 } 12 void main() { 13 screen(640, 480); 14 auto e = every(1024); 15 while (true) { 16 line(rand() % 640, rand() % 480, rand() % 640, rand() % 480, rgb.rand); 17 e({ 18 flip; 19 events; 20 }); 22 21 } 23 22 } 24 25 void times(int count, void delegate() dg) {26 while (count--) dg();27 }28 29 void myblit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect) {30 if (!srcrect)31 if ((src.w != dstrect.w) || (src.h != dstrect.h))32 throw new Exception("Rectangles do not match!");33 if ((srcrect.w != dstrect.w) || (srcrect.h != dstrect.h))34 throw new Exception("Rectangles do not match!");35 int count;36 int srcoffs=src.pitch/4;37 int dstoffs=dst.pitch/4;38 auto ymax=srcrect?(srcrect.h):(src.h);39 auto xmax=srcrect?(srcrect.w):(src.w);40 for (int y=0; y<ymax; ++y) {41 auto readp = cast(ubyte *)(cast(uint *)src.pixels + (y+srcrect.y)*srcoffs + srcrect.x);42 auto writep = cast(ubyte *)(cast(uint *)dst.pixels + (y+dstrect.y)*dstoffs + dstrect.x);43 auto end=readp+4*xmax;44 for (; readp!=end; writep+=4, readp+=4) {45 for (int e=0; e<3; ++e) {46 writep[e] += (((readp[e]-writep[e])*readp[3])>>8);47 }48 }49 }50 }51 52 import std.c.time;53 void main() {54 screen(640, 480);55 int count=10_000;56 auto from=New!(SDL_Rect)(0, 0, 100, 100);57 auto to=New!(SDL_Rect)(100, 100, 100, 100);58 SDL_SetAlpha(display, SDL_SRCALPHA, 0);59 writefln("SDL_Blit: ", time({60 times(count, {61 SDL_FillRect(display, to, 0);62 SDL_FillRect(display, from, 0xa0_00_ff_00);63 SDL_BlitSurface(display, from, display, to);64 });65 }), " ms!");66 writefln("MyBlit: ", time({67 times(count, {68 SDL_FillRect(display, to, 0);69 SDL_FillRect(display, from, 0xa0_00_ff_00);70 myblit(display, from, display, to);71 });72 }), " ms!");73 flip;74 sleep(5);75 }trunk/tools/tools/downloader.d
r372 r373 2 2 3 3 import tools.base, std.string, tools.fixed_socket, std.utf; 4 5 string htmlEscape(string s) { 6 //\todo: complete this 7 return s.replace(" ", "%20"); 8 } 4 9 5 10 bool validAddress(string foob) { trunk/tools/tools/page_queue.d
r331 r373 11 11 T[4000/T.sizeof] data; // almost one page .. leave some space for GC structures 12 12 ushort start; ushort end; 13 invariant { assert (start <= end ); }13 invariant { assert (start <= end, Format("Start ", start, " higher than end ", end, "! Desperation!")); } 14 14 Page* next; 15 15 bool push(T what) { trunk/tools/tools/threads.d
r368 r373 172 172 bool try_acquire() { 173 173 mixin(sem_fn!("sem_trywait(&handle)")); 174 if (!res) return true; 175 static if (!is(typeof(EAGAIN))) const EAGAIN = 11; 176 return errno != EAGAIN; 174 return !res; 177 175 } 178 176 } … … 301 299 New(mesgs); 302 300 } 301 int messages() { return mesgs.length; } 303 302 bool active() { return mesgs.has; } 304 303 void put(T t) {
