Changeset 373

Show
Ignore:
Timestamp:
07/03/08 00:54:53 (5 months ago)
Author:
FeepingCreature
Message:
  • Moved redimple to tools
  • Stuff
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/qd/dsss.conf

    r369 r373  
    44# [dt/main.d] 
    55# buildflags=-L-lSDL -L-lSDL_ttf -g -O 
    6 # [test.d] 
    7 # buildflags=-L-lSDL 
     6[test.d] 
     7buildflags=-L-lSDL -g 
    88[test2.d] 
    99buildflags=-L-lSDL -L-lSDL_ttf -g 
     
    3333[snake.d] 
    3434buildflags=-L-lSDL 
     35[test15.d] 
     36buildflags=-L-lSDL -L-lSDL_ttf 
  • trunk/qd/qd.d

    r369 r373  
    11module qd; 
    22import std.c.string, std.math, std.traits: ParameterTypeTuple; 
     3 
     4uint delegate() qd_rand; 
     5import std.random; 
     6static this() { qd_rand = { return rand(); }; } 
    37 
    48import tools.base; 
     
    182186    return res; 
    183187  } 
     188  static rgb rand() { return rgb(qd_rand() % 255, qd_rand() % 255, qd_rand() % 255); } 
    184189  bool opEquals(rgb r) { 
    185190    return values == r.values; 
     
    227232  putpixel32(display.surface, transformed.tupleof, col); 
    228233} 
     234 
     235void 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 
    229241void putpixel(int x, int y, ubyte r, ubyte g, ubyte b) { 
    230242  putpixel(x, y, SDL_MapRGBA(display.surface.format, r, g, b, 0)); 
     243} 
     244void 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)); 
    231249} 
    232250 
     
    355373} 
    356374 
     375void 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 
    357411template _UnPoint(int curOffs, T...) { 
    358412  static if (T.length) { 
     
    395449} 
    396450 
     451bool aa = true; 
     452 
    397453void _line(T...)(int x0, int y0, int x1, int y1, T p) { 
    398454  static int max(int a, int b) { return a>b?a:b; } 
     
    428484    else { if (x1 < x0) turn; } 
    429485    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    )); 
    437489  } 
    438490} 
     
    488540import std.stdio; 
    489541void 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 
     565void circle_aa(T...)(T t) { 
    490566  static assert(T.length!<3, "Circle: Needs x, y and radius"); 
    491567  int cx=cast(int)t[0], cy=cast(int)t[1]; float radius=cast(float)t[2]; 
  • trunk/qd/test.d

    r97 r373  
    11module test; 
     2import qd, std.random; 
    23 
    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); 
     4import tools.base; 
     5void 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  }; 
    1310} 
    1411 
    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     } 
     12void 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    }); 
    2221  } 
    2322} 
    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  
    22 
    33import tools.base, std.string, tools.fixed_socket, std.utf; 
     4 
     5string htmlEscape(string s) { 
     6  //\todo: complete this 
     7  return s.replace(" ", "%20"); 
     8} 
    49 
    510bool validAddress(string foob) { 
  • trunk/tools/tools/page_queue.d

    r331 r373  
    1111    T[4000/T.sizeof] data; // almost one page .. leave some space for GC structures 
    1212    ushort start; ushort end; 
    13     invariant { assert (start <= end); }  
     13    invariant { assert (start <= end, Format("Start ", start, " higher than end ", end, "! Desperation!")); }  
    1414    Page* next; 
    1515    bool push(T what) { 
  • trunk/tools/tools/threads.d

    r368 r373  
    172172    bool try_acquire() { 
    173173      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; 
    177175    } 
    178176  } 
     
    301299    New(mesgs); 
    302300  } 
     301  int messages() { return mesgs.length; } 
    303302  bool active() { return mesgs.has; } 
    304303  void put(T t) {