Changeset 443

Show
Ignore:
Timestamp:
10/13/08 19:05:15 (1 month ago)
Author:
FeepingCreature
Message:
  • Added test19, hashlife+wireworld
  • Added python_hash, python's hash adapted for tango adapted for

phobos adapted for caching :) thanks mwarning

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/qd/SDL_ttf.d

    r400 r443  
    44version (Tango) import std.compat; 
    55 
     6int deflt_size = 15; 
     7 
    68struct fontsettings { 
    79  bool bold, italic, underline; 
    8   rgb color=Black; int size=14
     10  rgb color=Black; int size=-1
    911} 
    1012 
     
    2022  if (name.length) data = name.read(); 
    2123  else data = import("Vera.ttf"); 
     24  deflt_size = size; 
    2225  standard[size] = new TTF_FontClass(data, size); 
    2326} 
     
    3538 
    3639void prettyprint(int x, int y, Align how, string text, fontsettings deflt = Init!(fontsettings)) { 
     40  if (deflt.size == -1) deflt.size = deflt_size; 
    3741  string[] chunks; 
    3842  fontsettings[] sets; 
     
    5761    sets ~= fs_pop(tag); 
    5862  } 
    59   void delegate(string, ref string) close_tag(string _tag) { 
    60     return _tag /apply/ (string tag, string pre, ref string post) { pop(pre, tag); }; 
     63  void delegate(string, ref string) close_tag(string tag) { 
     64    return stuple(&pop, tag) /apply/ (typeof(&pop) p, string tag, string pre, ref string post) { return p(pre, tag); }; 
    6165  } 
    6266  void delegate(string, ref string) [string] tags = [ 
     
    7377      string color; ptuple(color, post) = post.splitAt("]"); 
    7478      //                            color.length != 3 || color.length != 6 -- see, it's shorter :) 
    75       if (!color.startsWith("#") || color.length != 3 /or/ 6) 
    76         throw new Exception("Colors except #xxx and #xxxxxx not supported."); 
    77       color = color [1 .. $]
     79      color = color.startsWith("#"); 
     80      if (!color || color.length != 3 /or/ 6) 
     81        throw new Exception("Colors except #xxx and #xxxxxx not supported: "~color~".")
    7882      if (color.length == 3) color = color[0]~"0"~color[1]~"0"~color[2]~"0"; 
    7983      auto res = rgb(color[0 .. 2].htoi(), color[2 .. 4].htoi(), color[4 .. 6].htoi()); 
     
    127131 
    128132void print(Area target, Align how, rgb col, string what) { 
    129   fontsettings foo; foo.color = col; print((int w, int h) { return target.select(pt(w, h), how); }, [foo], [what]); 
    130 
     133  fontsettings foo; 
     134  foo.color = col; 
     135  foo.size = deflt_size; 
     136  print((int w, int h) { return target.select(pt(w, h), how); }, [foo], [what]); 
     137
     138 
     139void print(Area target, Align how, string what) { print(target, how, White, what); } 
    131140 
    132141void print(Align how, fontsettings[] settings, string[] strings) { 
     
    135144 
    136145void print(int x, int y, Align how, rgb col, string what) { fontsettings foo; foo.color = col; print(x, y, how, [foo], [what]); } 
     146void print(int x, int y, Align how, string what) { print(x, y, how, White, what); } 
     147void print(int x, int y, string what) { print(x, y, Bottom|Right, White, what); } 
    137148 
    138149void print(Align how, rgb col, string what) { fontsettings foo; foo.color = col; print(how, [foo], [what]); } 
     
    178189  } 
    179190  Area render(string text, fontsettings s=Default) { 
    180     if (!text.length) return Area(SDL_CreateRGBSurface(0, 0, 0, 32)); 
     191    if (!text.length) return Area(SDL_CreateRGBSurface(0, 1, 1, 32)); 
    181192    auto res=_render(text, s); 
    182193    if (!res) throw new Exception("Couldn't render "~'"'~text~'"'~": "~.toString(SDL_GetError)); 
  • trunk/qd/dsss.conf

    r400 r443  
    2525[test15.d] 
    2626buildflags=-L-lSDL -L-lSDL_ttf 
     27[test17.d] 
     28buildflags=-L-lSDL -L-lSDL_ttf -J. -O -release -q,-ffast-math 
     29[dt/main.d] 
     30buildflags=-L-lSDL -L-lSDL_ttf 
  • trunk/qd/dt/fatline.d

    r350 r443  
    116116import tools.time; 
    117117void delegate() gen_fps() { 
    118   return stuple(sec(), 0) /apply/ (ref float s, ref int c) { 
     118  return stuple(sec(), 0) /apply/ (ref double s, ref int c) { 
    119119    auto t = sec(); 
    120120    if (t > s + 1f) { 
  • trunk/qd/dt/main.d

    r350 r443  
    99 
    1010import tools.time; 
    11 bool delegate() blink(float _delay) { 
    12   return stuple(_delay, true, sec()) /apply/ (float delay, ref bool state, ref float last) { 
     11bool delegate() blink(double delay) { 
     12  return stuple(delay, true, sec()) /apply/ (double delay, ref bool state, ref double last) { 
    1313    if (sec() < last) { 
    14       last = sec(); // timer wrapped around. Can't help this. Don't switch. Hope the user doesn't notice. 
     14      last = sec(); // timer wrapped around. Can't help this. Don't switch. Hope the user doesn't notice. Should be raaaaare. 
    1515      return state; 
    1616    } 
     
    3333  void selected(bool b); void highlighted(bool b); void broken(bool b); 
    3434  bool isSelected(); bool isHighlighted(); bool isBroken(); 
    35    
    3635} 
    3736 
     
    510509  vec2f mouse_pos, start; 
    511510  bool drag=false; 
    512   bool[int] pressed; 
    513511  auto thingies = new InteractionSet; 
    514512  bool underMouse(WorldThing wt) { 
     
    570568  auto limiter=new FrameLimiter(60, 60); 
    571569  bool delegate(WorldThing) shouldHighlight = &underMouse; 
    572   auto test_turret = new Turret( 
    573     Turret.Settings(115, 12, false), 
    574     Ammo.Settings(0.5, 0.2, 0.9, 0.9, 1, 128, true, false, false), 
    575     spawner.critters, vec2f(0, 0) 
    576   ); 
     570  Turret test_turret; 
     571  void reset_test_turret() { 
     572    test_turret = new Turret( 
     573      Turret.Settings(115, /*12*/-4, false), 
     574      Ammo.Settings(0.5, 0.2, 0.9, 0.9, 1, 1024, true, false, false), 
     575      spawner.critters, vec2f(0, 0) 
     576    ); 
     577  } 
     578  reset_test_turret; 
    577579  do with (group) { 
    578580    update; 
     
    590592    }(); 
    591593    test_turret.draw(); 
     594    if (!test_turret.isBroken && mouse.clicked) { 
     595      group.addThing(test_turret); 
     596      reset_test_turret; 
     597    } 
    592598    auto foo = display.select(pt(100, 40), Bottom|Left); 
    593599    line(foo.tl, foo.br, Box=Black, Fill=Background~White); 
     
    604610    } 
    605611    limiter.limit({ flip; }); 
    606     events((int key, bool p) { 
    607       //writefln("Key ", evt.key.keysym.sym); 
    608       if (p) pressed[key]=true; 
    609       else pressed.remove(key); 
    610       if (key==27) throw new Error("Quit"); 
     612    events((wchar key, bool p) { 
     613      if (key == 27) throw new Error("Quit"); 
    611614    }, (int x, int y, ubyte button, int p) { 
    612615      foreach (thing; group) { 
  • trunk/qd/qd.d

    r400 r443  
    11module qd; 
    22import std.c.string, std.math, std.traits: ParameterTypeTuple; 
     3 
     4template ctype(string S) { 
     5  static if (S == "unsigned long") 
     6    alias uint ctype; 
     7  else static if (S == "long") 
     8    alias int ctype; 
     9  else static assert(false, "Unsupported C type "~S); 
     10} 
    311 
    412import tools.base; 
     
    6876  void SDL_Delay(uint ms); 
    6977  uint SDL_GetTicks(); 
     78  ubyte SDL_EventState(ubyte type, int state); 
    7079 
    7180  enum SDLKey { 
     
    8695 
    8796  struct SDL_keysym { ubyte scancode; SDLKey sym; SDLMod mod; ushort unicode; } 
     97  void SDL_EnableUNICODE(int enable=1); 
    8898  enum SDL_EventType : ubyte { 
    8999    NoEvent=0, Active, KeyDown, KeyUp, 
     
    91101    JoyBallMotion, JoyHatMotion, JoyButtonDown, JoyButtonUp, 
    92102    Quit, SysWMEvent, ReservedA, ReservedB, Resize 
     103  } 
     104  version(win32) { } 
     105  else { 
     106    alias uint Window; alias uint Atom; 
     107    char* XGetAtomName(void* display, Atom atom); 
     108    Atom XInternAtom(void* display, char* name, bool only_if_exists = true); 
     109    int XConvertSelection(void* display, Atom selection, Atom target, Atom property, Window requester, int time); 
     110    bool XCheckTypedWindowEvent(void* display, Window w, int type, XEvent* target); 
     111    int XGetWindowProperty(void* display, Window w, Atom property, 
     112      ctype!("long") offset, ctype!("long") length, bool del, Atom req_type, Atom* actual_type_return, 
     113        int* actual_format_return, ctype!("unsigned long")* nitems_return, ctype!("unsigned long")* bytes_after_return, 
     114          ubyte** prop_return); 
     115    int XFree(void* data); 
     116    void* XOpenDisplay(char* name = null); 
     117    int XCloseDisplay(void* display); 
     118    int XChangeProperty(void* display, Window w, Atom property, Atom type, int format, int mode, ubyte* data, int nelements); 
     119    const XA_ATOM = 4, PropModeReplace = 0; 
     120    void setupDragDrop() { 
     121      Atom dndVersion = 4; 
     122      auto disp = XOpenDisplay; scope(exit) XCloseDisplay(disp); 
     123      XChangeProperty(disp, getWindow, XInternAtom(disp, "XdndAware"), XA_ATOM, 32, PropModeReplace, cast(ubyte*) &dndVersion, 1); 
     124    } 
     125    const SelectionNotify = 31, AnyPropertyType = 0; 
     126    struct XClientMessageEvent { 
     127      ctype!("unsigned long") serial; 
     128      bool send_event; 
     129      void* display; 
     130      Window window; 
     131      Atom type; 
     132      int format; 
     133      union Data { byte[20] b; short[10] s; int[5] l; } 
     134      Data data; 
     135    } 
     136    struct XSelectionEvent { 
     137      ctype!("unsigned long") serial; 
     138      bool send_event; 
     139      void* display; 
     140      Window window; 
     141      Atom selection, target, property; int time; 
     142    } 
     143    struct XEvent { int type; union { XClientMessageEvent xclient; XSelectionEvent xselect; } } 
     144    alias int Status; 
     145    Status XSendEvent(void* display, Window w, bool propagate, ctype!("long") mask, XEvent* send); 
     146    struct SDL_SysWMinfo { 
     147      ubyte major, minor, patch; 
     148      int SDL_SYSWM_TYPE; 
     149      union { 
     150        struct _x11 { 
     151          void* display; 
     152          Window window; 
     153        } 
     154        _x11 x11; 
     155      } 
     156    } 
     157    int SDL_GetWMInfo(SDL_SysWMinfo* info); 
     158    Window getWindow() { 
     159      char[1024] buffer; 
     160      auto info = cast(SDL_SysWMinfo*) buffer.ptr; 
     161      info.major = 1; info.minor = 2; info.patch = 13; 
     162      SDL_GetWMInfo(info); 
     163      return info.x11.window; 
     164    } 
    93165  } 
    94166  union SDL_Event { 
     
    106178    struct Quit { SDL_EventType type; }; Quit quit; 
    107179    struct User { SDL_EventType type; int code; void *data1, data2; }; User user; 
    108     struct Syswm { SDL_EventType type; void *msg; }; Syswm syswm; 
     180    version(win32) alias void SyswmMsg; 
     181    else { 
     182      struct SyswmMsg { 
     183        ubyte major, minor, patch; 
     184        uint subsystem; 
     185        XEvent xevent; 
     186      } 
     187    } 
     188    struct Syswm { SDL_EventType type; SyswmMsg *msg; }; Syswm syswm; 
    109189  } 
    110190 
     
    114194  const uint SDL_INIT_VIDEO = 0x00000020; 
    115195  int SDL_Init(uint flags); 
     196  char* SDL_GetKeyName(SDLKey key); 
    116197} 
    117198uint delegate() qd_rand; 
    118199import std.random; 
    119 static this() { qd_rand = { return rand(); }; SDL_Init(SDL_INIT_VIDEO);
     200static this() { qd_rand = { return rand(); }; SDL_Init(SDL_INIT_VIDEO); SDL_EnableUNICODE();
    120201 
    121202import std.string: toStringz; 
     
    128209  uint *bufp = cast(uint *)which.pixels + y*which.pitch/4 + x; 
    129210  *bufp = c; 
     211} 
     212 
     213void putpixel32(int x, int y, rgb col) { 
     214  putpixel32(display.surface, x, y, SDL_MapRGBA(display.surface.format, col.values[0], col.values[1], col.values[2], 0)); 
    130215} 
    131216 
     
    192277    static assert(is(typeof(R.r)) && is(typeof(R.g)) && is(typeof(R.b)), R.stringof~" cannot be blended with: needs r,g,b!"); 
    193278    return rgb(r+(other.r-r)*f, g+(other.g-g)*f, b+(other.b-b)*f); 
     279  } 
     280  rgb opAdd(rgb other) { 
     281    rgb res = void; 
     282    res.r = min(255, r + other.r); 
     283    res.g = min(255, g + other.g); 
     284    res.b = min(255, b + other.b); 
     285    return res; 
    194286  } 
    195287  rgb invert() { return rgb(255-r, 255-g, 255-b); } 
     
    440532  static if (T.length>=3 && (is(T[2]: long)||is(T[2]: real))) { 
    441533    _line(cast(int)p._0, cast(int)p._1, cast(int)p._2, cast(int)p._3, p.tupleof[4..$]); 
    442     lastx=cast(int)p._0; lasty=cast(int)p._1
     534    lastx=cast(int)p._2; lasty=cast(int)p._3
    443535  } else { 
    444536    _line(lastx, lasty, cast(int)p._0, cast(int)p._1, p.tupleof[2..$]); 
     
    626718      offscreen = osc; 
    627719      if (offscreen) display=Area(SDL_CreateRGBSurface(sdl_flags, w, h, 32)); 
    628       else display=Area(SDL_SetVideoMode(w, h, 32, sdl_flags)); 
     720      else { 
     721        display=Area(SDL_SetVideoMode(w, h, 32, sdl_flags)); 
     722        setupDragDrop(); 
     723      } 
    629724    } 
    630725    void close() { SDL_QuitSubSystem(SDL_INIT_VIDEO); } 
     
    660755void cls(rgb fill=Black) { line(0, 0, display.width-1, display.height-1, Fill=fill); } 
    661756 
     757import tools.log; 
    662758void updateMouse(int x, int y, ubyte button, int pushed) { 
    663759  auto b = cast(Button) button; 
     
    665761  if (pushed == 1) { 
    666762    logln("pushed ", b); 
    667     mouse._pressed[b] = true
    668   } 
    669   if (pushed == -1) mouse._pressed[b] = false
     763    mouse.down(b)
     764  } 
     765  if (pushed == -1) mouse.up(b)
    670766} 
    671767 
     
    678774  bool pressed() { return pressed(Button.Left); } 
    679775  bool pressed(Button b) { return _pressed[b]; } 
     776  bool pressed(Button b, Area a) { return pressed(b) && (pos in a); } 
     777  bool pressed(Area a) { return pressed(Button.Left, a); } 
    680778  bool[256] _pressed; 
    681779  bool[256] was_pressed; 
    682780  bool clicked() { return clicked(Button.Left); } 
    683   bool clicked(Button which) { if (!was_pressed[which]) return false; was_pressed[which] = false; return true; } 
     781  bool clicked(Button which) { return was_pressed[which]; } 
     782  bool clicked(Button which, Area a) { return (pos in a) && clicked(which); } 
     783  bool clicked(Area a) { return clicked(Button.Left, a); } 
    684784  void down(Button b) { _pressed[b] = true; was_pressed[b] = true; } 
    685785  void up(Button b) { _pressed[b] = false; } 
     786  void events_reset() { foreach (ref b; was_pressed) b = false; } 
    686787} 
    687788 
    688789_mouse mouse; 
    689790 
    690 bool[int] keystate; 
     791bool[wchar] keystate; 
    691792 
    692793struct _key { 
    693   bool[int] key_pressed; 
    694   bool pressed(SDLKey which) { 
    695     int i = cast(int) which; 
    696     if (auto kp = i in key_pressed) { 
     794  bool[ushort] key_pressed; 
     795  bool pressed(ushort which) { 
     796    if (auto kp = which in key_pressed) { 
    697797      auto res = *kp; 
    698       *kp = false; 
     798      // *kp = false; pressed, not pushed 
    699799      return res; 
    700800    } else return false; 
     
    706806void delegate() onResize; 
    707807 
     808int drop_type, drop_action; 
     809 
     810string _text_dropped; 
     811string textDropped() { 
     812  auto res = _text_dropped; 
     813  _text_dropped = null; 
     814  return res; 
     815} 
     816 
     817bool accept_drop; 
     818 
     819import tools.time; 
     820static this() { SDL_EventState(SDL_EventType.SysWMEvent, true); } 
    708821/// parameters to key: which key, and if it's pressed or released 
    709822/// parameters to mouse: x, y, button [Left: 1, Mid: 2, Right: 3], change [1: pressed, -1: released] 
    710 void events(void delegate(int, bool) key=null, void delegate(int, int, ubyte, int) mouse=null, void delegate() quit=null) { 
     823void events(void delegate(ushort, bool) key=null, void delegate(int, int, ubyte, int) mouse=null, void delegate() quit=null) { 
     824  .mouse.events_reset; 
    711825  SDL_Event evt; 
    712826  while (SDL_PollEvent(&evt)) { 
     
    731845        break; 
    732846      case SDL_EventType.KeyDown: 
    733         keystate[evt.key.keysym.sym] = true
    734         .key.key_pressed[evt.key.keysym.sym] = true; 
    735         if (key) key(evt.key.keysym.sym, true); 
     847        auto sym = evt.key.keysym.sym
     848        .key.key_pressed[sym] = true; 
     849        if (key) key(sym, true); 
    736850        break; 
    737851      case SDL_EventType.KeyUp: 
    738         keystate.remove(evt.key.keysym.sym); 
    739         if (key) key(evt.key.keysym.sym, false); 
     852        auto sym = evt.key.keysym.sym; 
     853        .key.key_pressed[sym] = false; 
     854        if (key) key(sym, false); 
    740855        break; 
    741856      case SDL_EventType.Resize: 
     
    747862        else throw new Error("Quit"); 
    748863        break; 
    749       default: break; 
    750     } 
    751   } 
    752 
    753  
    754 void events(void delegate(int) key, void delegate(int, int, ubyte, int) mouse=null, void delegate() quit=null) { 
    755   events((int a, bool b) { 
     864      case SDL_EventType.SysWMEvent: 
     865        version(win32) static assert(false, "No system events support for win32 yet"); 
     866        else { // presume X11 
     867          with (evt.syswm.msg.xevent) { 
     868            Atom atom(string name) { return XInternAtom(xclient.display, toStringz(name)); } 
     869            string atomName(Atom atom) { return toString(XGetAtomName(xclient.display, atom)); } 
     870            if (type == 33) { 
     871              // writefln("Type: ", xclient.type); 
     872              auto name = atomName(xclient.type); 
     873              // writefln("Name: ", name); 
     874              if (name == "XdndEnter") { 
     875                auto source = xclient.data.l[0]; 
     876                if (atomName(xclient.data.l[2]).startsWith("text/plain")) { 
     877                  accept_drop = true; 
     878                  // writefln("Drop accepted"); 
     879                } 
     880                drop_type = xclient.data.l[2]; 
     881              } 
     882              if (!accept_drop) break; 
     883              if (name == "XdndPosition") { 
     884                XEvent drop_response; 
     885                drop_response.type = 33; 
     886                with (drop_response.xclient) { 
     887                  display = xclient.display; 
     888                  window = xclient.data.l[0]; // wtf? see wine source code, but why? 
     889                  type = "XdndStatus".atom(); 
     890                  format = 32; 
     891                  data.l[0] = window; 
     892                  data.l[1] = true; 
     893                  data.l[2] = 0; 
     894                  data.l[3] = 0; 
     895                  drop_action = data.l[4] = xclient.data.l[4]; 
     896                } 
     897                auto res = XSendEvent(xclient.display, xclient.data.l[0], false, 0, &drop_response); 
     898              } 
     899              if (name == "XdndDrop") { 
     900                auto drop_timekey = xclient.data.l[2]; 
     901                auto res = XConvertSelection(xclient.display, "XdndSelection".atom(), 
     902                  drop_type, "XdndTarget".atom(), getWindow, drop_timekey); 
     903                auto start = sec(); 
     904                while (sec() - start < 1.0) { 
     905                  XEvent event; 
     906                  auto check_res = XCheckTypedWindowEvent(xclient.display, getWindow, SelectionNotify, &event); 
     907                  if (check_res && event.xselect.selection == "XdndSelection".atom()) goto done; 
     908                } 
     909                return; // timeout 
     910done: 
     911                Atom act_type; uint bytes_left, items; int act_format; ubyte* data; 
     912                XGetWindowProperty(xclient.display, getWindow, "XdndTarget".atom(), 0, -1, true, 
     913                  AnyPropertyType, &act_type, &act_format, &items, &bytes_left, &data); 
     914                items *= (act_format / 8); 
     915                auto my_data = toString(cast(char*) data).dup; 
     916                XFree(data); 
     917                XEvent finished; finished.type = 33; 
     918                with (finished.xclient) { 
     919                  display = xclient.display; 
     920                  window = getWindow(); 
     921                  type = "XdndFinished".atom(); 
     922                  format = 32; 
     923                  data.l[0] = getWindow; 
     924                  data.l[1] = true; 
     925                  data.l[2] = drop_action; 
     926                } 
     927                XSendEvent(xclient.display, xclient.data.l[0], false, 0, &finished); 
     928                // writefln("Sent XdndFinished"); 
     929                _text_dropped = my_data; 
     930                accept_drop = false; 
     931              } 
     932            } 
     933          } 
     934        } 
     935        break; 
     936      case SDL_EventType.Active: break; 
     937      default: 
     938    writefln("Unknown event type ", evt.type); 
     939    break; 
     940    } 
     941  } 
     942
     943 
     944void events(void delegate(ushort) key, void delegate(int, int, ubyte, int) mouse=null, void delegate() quit=null) { 
     945  events((ushort a, bool b) { 
    756946    if (b) key(a); 
    757947  }, mouse, quit); 
    758948} 
    759949 
    760 void events(void delegate(int) key, void delegate(int, int) mouse, void delegate() quit=null) { 
     950void events(void delegate(ushort) key, void delegate(int, int) mouse, void delegate() quit=null) { 
    761951  events(key, (int x, int y, ubyte b, int p) { mouse(x, y); }, quit); 
    762952} 
    763953 
    764 void events(void delegate(int, bool) key, void delegate(int, int) mouse, void delegate() quit=null) { 
     954void events(void delegate(ushort, bool) key, void delegate(int, int) mouse, void delegate() quit=null) { 
    765955  events(key, (int x, int y, ubyte b, int p) { mouse(x, y); }, quit); 
    766956} 
     
    806996  pt tl, dimensions; 
    807997  pt br() { return tl+size; } 
     998  pt bl() { return pt(tl.x, tl.y + size.y); } 
     999  pt tr() { return pt(tl.x + size.x, tl.y); } 
    8081000  static Area opCall(pt _tl, pt _size, SDL_Surface *_surf=null) { 
    8091001    auto res=new Area;