Changeset 369

Show
Ignore:
Timestamp:
06/26/08 06:20:50 (5 months ago)
Author:
FeepingCreature
Message:
  • Added a Snakes impl
Files:

Legend:

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

    r350 r369  
    101101  } body { 
    102102  auto surfs = new Area[strings.length]; 
    103   scope(exit) foreach (surf; surfs) SDL_FreeSurface(surf.surface); 
     103  scope(exit) foreach (surf; surfs) if (surf) SDL_FreeSurface(surf.surface); 
    104104  int width, height; 
    105105  foreach (i, str; strings) { 
  • trunk/qd/dsss.conf

    r358 r369  
    3131# [test13.d] 
    3232# buildflags=-L-lSDL -L-lSDL_ttf -O -release -inline 
     33[snake.d] 
     34buildflags=-L-lSDL 
  • trunk/qd/qd.d

    r358 r369  
    7373    Escape = 27, 
    7474    LCtrl = 306, 
     75    Up = 273, Down = 274, Right = 275, Left = 276 
    7576  } 
    7677  enum SDLMod { 
     
    112113  void SDL_QuitSubSystem(uint flags); 
    113114  const uint SDL_INIT_VIDEO = 0x00000020; 
    114 
     115  int SDL_Init(uint flags); 
     116
     117static this() { SDL_Init(SDL_INIT_VIDEO); } 
    115118 
    116119import std.string: toStringz; 
     
    486489void circle(T...)(T t) { 
    487490  static assert(T.length!<3, "Circle: Needs x, y and radius"); 
    488   int cx=cast(int)t[0], cy=cast(int)t[1], radius=cast(int)t[2]; 
     491  int cx=cast(int)t[0], cy=cast(int)t[1]; float radius=cast(float)t[2]; 
    489492  SDL_LockSurface(display.surface); 
    490493  scope(exit) { SDL_UnlockSurface(display.surface); if (doFlip) flip; } 
     
    606609 
    607610void updateMouse(int x, int y, ubyte button, int pushed) { 
     611  auto b = cast(Button) button; 
    608612  mouse.pos = pt(x, y); 
    609613  if (pushed == 1) { 
    610     mouse.pressed = true; 
    611     if (button == 1) mouse._leftclicked = true; 
    612     if (button == 3) mouse._rightclicked = true; 
    613   } 
    614   if (pushed == -1) mouse.pressed = false; 
    615 
     614    logln("pushed ", b); 
     615    mouse._pressed[b] = true; 
     616  } 
     617  if (pushed == -1) mouse._pressed[b] = false; 
     618
     619 
     620enum Button : ubyte { Left=1, Middle, Right, Extra1, Extra2 } 
    616621 
    617622struct _mouse { 
    618623  pt pos; 
    619   int x() { return pos.x; } int y() { return pos.y; } 
    620   bool _leftclicked, _rightclicked; 
    621   bool pressed; 
    622   bool clicked() { if (!_leftclicked) return false; _leftclicked = false; return true; } 
    623   bool rightclicked() { if (!_rightclicked) return false; _rightclicked = false; return true; } 
     624  int x() { return pos.x; } 
     625  int y() { return pos.y; } 
     626  bool pressed() { return pressed(Button.Left); } 
     627  bool pressed(Button b) { return _pressed[b]; } 
     628  bool[256] _pressed; 
     629  bool[256] was_pressed; 
     630  bool clicked() { return clicked(Button.Left); } 
     631  bool clicked(Button which) { if (!was_pressed[which]) return false; was_pressed[which] = false; return true; } 
     632  void down(Button b) { _pressed[b] = true; was_pressed[b] = true; } 
     633  void up(Button b) { _pressed[b] = false; } 
    624634} 
    625635 
     
    627637 
    628638bool[int] keystate; 
     639 
     640struct _key { 
     641  bool[int] key_pressed; 
     642  bool pressed(SDLKey which) { 
     643    int i = cast(int) which; 
     644    if (auto kp = i in key_pressed) { 
     645      auto res = *kp; 
     646      *kp = false; 
     647      return res; 
     648    } else return false; 
     649  } 
     650} 
     651 
     652_key key; 
    629653 
    630654void delegate() onResize; 
     
    656680      case SDL_EventType.KeyDown: 
    657681        keystate[evt.key.keysym.sym] = true; 
     682        .key.key_pressed[evt.key.keysym.sym] = true; 
    658683        if (key) key(evt.key.keysym.sym, true); 
    659684        break; 
     
    746771  bool opIn_r(T)(T vt) { return (vt.x>=tl.x && vt.x<=(tl.x+size.x)) && (vt.y>=tl.y && vt.y<=(tl.y+size.y)); } 
    747772  Area select(int x, int y, int w=int.max, int h=int.max) { 
    748     if (w==int.max) w=size.x
    749     if (h==int.max) h=size.y
     773    if (w==int.max) w=size.x - x
     774    if (h==int.max) h=size.y - x
    750775    return Area(tl+pt(x, y), pt(w, h), surface); 
    751776  } 
  • trunk/qd/test2.d

    r354 r369  
    5757  bool runsMe() { foreach (thread; threads) if (thread.runsMe) return true; return false; } 
    5858} 
     59 
     60StackThreadSched mouse_sched; 
     61static this() { New(mouse_sched); } 
    5962 
    6063import std.thread, tools.threads; 
     
    8790    pt start, end; 
    8891    void delegate(ref State)[State] tf=[ 
    89       State.inside: (ref State s) { if (mouse.button) start=mouse.pos; }, 
    90       State.drag_zoomin: (ref State s) { if (!mouse.button) end=mouse.pos; }, 
    91       State.drag_zoomout: (ref State s) { if (!mouse.button) end=mouse.pos; }, 
     92      State.inside: (ref State s) { if (mouse.pressed) start=mouse.pos; }, 
     93      State.drag_zoomin: (ref State s) { if (!mouse.pressed) end=mouse.pos; }, 
     94      State.drag_zoomout: (ref State s) { if (!mouse.pressed) end=mouse.pos; }, 
    9295      State.doZoomin: (ref State s) { 
    9396        T nfrom=myArea.locate(start, from, to), nto=myArea.locate(end, from, to); 
     
    109112      } 
    110113    ]; 
    111     tf.addBidirectionalEdge({ return (mouse in area) && !mouse.button; }, { return !(mouse in area); }, State.start, State.inside); 
     114    tf.addBidirectionalEdge({ return (mouse in area) && !mouse.pressed; }, { return !(mouse in area); }, State.start, State.inside); 
    112115    tf.addEdges( 
    113       { return mouse.button && mouse.which==1; }, State.inside, State.drag_zoomin, 
    114       { return mouse.button && mouse.which==3; }, State.inside, State.drag_zoomout, 
    115       { return !mouse.button; }, State.drag_zoomin, State.doZoomin, 
    116       { return !mouse.button; }, State.drag_zoomout, State.doZoomout 
     116      { return mouse.pressed(Button.Left); }, State.inside, State.drag_zoomin, 
     117      { return mouse.pressed(Button.Right); }, State.inside, State.drag_zoomout, 
     118      { return !mouse.pressed; }, State.drag_zoomin, State.doZoomin, 
     119      { return !mouse.pressed; }, State.drag_zoomout, State.doZoomout 
    117120    ); 
    118121    tf.runState(yield); 
     
    441444   
    442445  list!(Window) windows; 
     446<<<<<<< .mine 
     447  this() { mouse_sched~=stackthread=&mousethr; } 
     448======= 
    443449  // this() { mouse.sched~=stackthread=&mousethr; } 
     450>>>>>>> .r368 
    444451  void draw() { windows.each((Window w) { w.draw(); }); } 
     452<<<<<<< .mine 
     453  void mousethr(proc yield) { 
     454    assert(mouse_sched.runsMe); 
     455======= 
    445456  /*void mousethr(proc yield) { 
    446457    // assert(mouse.sched.runsMe); 
     458>>>>>>> .r368 
    447459    enum State { start, title_inside, title_dragging, resize_inside, resize_dragging } 
    448460    pt lastpos; 
     
    454466        windows.each_reverse((size_t id, ref Window w, proc brk) { 
    455467          if (mouse.pos in w.area) { 
    456             if (mouse.button) { 
     468            if (mouse.pressed) { 
    457469              newActive=id; // make current 
    458470              if (s==State.start && (w is windows.top)) w.motion(); 
     
    478490    ]; 
    479491    transitions.addBidirectionalEdges( 
    480       { return mouseInTitle && !mouse.button; }, { return !mouseInTitle; }, State.start, State.title_inside, 
    481       { return mouseInBox && !mouse.button; }, { return !mouseInBox; }, State.start, State.resize_inside, 
    482       { return mouse.button; }, State.title_inside, State.title_dragging, 
    483       { return mouse.button; }, State.resize_inside, State.resize_dragging 
     492      { return mouseInTitle && !mouse.pressed; }, { return !mouseInTitle; }, State.start, State.title_inside, 
     493      { return mouseInBox && !mouse.pressed; }, { return !mouseInBox; }, State.start, State.resize_inside, 
     494      { return mouse.pressed; }, State.title_inside, State.title_dragging, 
     495      { return mouse.pressed; }, State.resize_inside, State.resize_dragging 
    484496    ); 
    485497    transitions.runState(yield); 
     
    568580    events; 
    569581    countFPS(); 
    570   } 
    571 
     582    mouse_sched (); 
     583  } 
     584