Changeset 443
- Timestamp:
- 10/13/08 19:05:15 (1 month ago)
- Files:
-
- trunk/qd/SDL_ttf.d (modified) (8 diffs)
- trunk/qd/dsss.conf (modified) (1 diff)
- trunk/qd/dt/fatline.d (modified) (1 diff)
- trunk/qd/dt/main.d (modified) (6 diffs)
- trunk/qd/python_hash.d (added)
- trunk/qd/qd.d (modified) (17 diffs)
- trunk/qd/test19.d (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/qd/SDL_ttf.d
r400 r443 4 4 version (Tango) import std.compat; 5 5 6 int deflt_size = 15; 7 6 8 struct fontsettings { 7 9 bool bold, italic, underline; 8 rgb color=Black; int size= 14;10 rgb color=Black; int size=-1; 9 11 } 10 12 … … 20 22 if (name.length) data = name.read(); 21 23 else data = import("Vera.ttf"); 24 deflt_size = size; 22 25 standard[size] = new TTF_FontClass(data, size); 23 26 } … … 35 38 36 39 void prettyprint(int x, int y, Align how, string text, fontsettings deflt = Init!(fontsettings)) { 40 if (deflt.size == -1) deflt.size = deflt_size; 37 41 string[] chunks; 38 42 fontsettings[] sets; … … 57 61 sets ~= fs_pop(tag); 58 62 } 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); }; 61 65 } 62 66 void delegate(string, ref string) [string] tags = [ … … 73 77 string color; ptuple(color, post) = post.splitAt("]"); 74 78 // 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~"."); 78 82 if (color.length == 3) color = color[0]~"0"~color[1]~"0"~color[2]~"0"; 79 83 auto res = rgb(color[0 .. 2].htoi(), color[2 .. 4].htoi(), color[4 .. 6].htoi()); … … 127 131 128 132 void 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 139 void print(Area target, Align how, string what) { print(target, how, White, what); } 131 140 132 141 void print(Align how, fontsettings[] settings, string[] strings) { … … 135 144 136 145 void print(int x, int y, Align how, rgb col, string what) { fontsettings foo; foo.color = col; print(x, y, how, [foo], [what]); } 146 void print(int x, int y, Align how, string what) { print(x, y, how, White, what); } 147 void print(int x, int y, string what) { print(x, y, Bottom|Right, White, what); } 137 148 138 149 void print(Align how, rgb col, string what) { fontsettings foo; foo.color = col; print(how, [foo], [what]); } … … 178 189 } 179 190 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)); 181 192 auto res=_render(text, s); 182 193 if (!res) throw new Exception("Couldn't render "~'"'~text~'"'~": "~.toString(SDL_GetError)); trunk/qd/dsss.conf
r400 r443 25 25 [test15.d] 26 26 buildflags=-L-lSDL -L-lSDL_ttf 27 [test17.d] 28 buildflags=-L-lSDL -L-lSDL_ttf -J. -O -release -q,-ffast-math 29 [dt/main.d] 30 buildflags=-L-lSDL -L-lSDL_ttf trunk/qd/dt/fatline.d
r350 r443 116 116 import tools.time; 117 117 void delegate() gen_fps() { 118 return stuple(sec(), 0) /apply/ (ref floats, ref int c) {118 return stuple(sec(), 0) /apply/ (ref double s, ref int c) { 119 119 auto t = sec(); 120 120 if (t > s + 1f) { trunk/qd/dt/main.d
r350 r443 9 9 10 10 import tools.time; 11 bool delegate() blink( float _delay) {12 return stuple( _delay, true, sec()) /apply/ (float delay, ref bool state, ref floatlast) {11 bool delegate() blink(double delay) { 12 return stuple(delay, true, sec()) /apply/ (double delay, ref bool state, ref double last) { 13 13 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. 15 15 return state; 16 16 } … … 33 33 void selected(bool b); void highlighted(bool b); void broken(bool b); 34 34 bool isSelected(); bool isHighlighted(); bool isBroken(); 35 36 35 } 37 36 … … 510 509 vec2f mouse_pos, start; 511 510 bool drag=false; 512 bool[int] pressed;513 511 auto thingies = new InteractionSet; 514 512 bool underMouse(WorldThing wt) { … … 570 568 auto limiter=new FrameLimiter(60, 60); 571 569 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; 577 579 do with (group) { 578 580 update; … … 590 592 }(); 591 593 test_turret.draw(); 594 if (!test_turret.isBroken && mouse.clicked) { 595 group.addThing(test_turret); 596 reset_test_turret; 597 } 592 598 auto foo = display.select(pt(100, 40), Bottom|Left); 593 599 line(foo.tl, foo.br, Box=Black, Fill=Background~White); … … 604 610 } 605 611 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"); 611 614 }, (int x, int y, ubyte button, int p) { 612 615 foreach (thing; group) { trunk/qd/qd.d
r400 r443 1 1 module qd; 2 2 import std.c.string, std.math, std.traits: ParameterTypeTuple; 3 4 template 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 } 3 11 4 12 import tools.base; … … 68 76 void SDL_Delay(uint ms); 69 77 uint SDL_GetTicks(); 78 ubyte SDL_EventState(ubyte type, int state); 70 79 71 80 enum SDLKey { … … 86 95 87 96 struct SDL_keysym { ubyte scancode; SDLKey sym; SDLMod mod; ushort unicode; } 97 void SDL_EnableUNICODE(int enable=1); 88 98 enum SDL_EventType : ubyte { 89 99 NoEvent=0, Active, KeyDown, KeyUp, … … 91 101 JoyBallMotion, JoyHatMotion, JoyButtonDown, JoyButtonUp, 92 102 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 } 93 165 } 94 166 union SDL_Event { … … 106 178 struct Quit { SDL_EventType type; }; Quit quit; 107 179 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; 109 189 } 110 190 … … 114 194 const uint SDL_INIT_VIDEO = 0x00000020; 115 195 int SDL_Init(uint flags); 196 char* SDL_GetKeyName(SDLKey key); 116 197 } 117 198 uint delegate() qd_rand; 118 199 import std.random; 119 static this() { qd_rand = { return rand(); }; SDL_Init(SDL_INIT_VIDEO); }200 static this() { qd_rand = { return rand(); }; SDL_Init(SDL_INIT_VIDEO); SDL_EnableUNICODE(); } 120 201 121 202 import std.string: toStringz; … … 128 209 uint *bufp = cast(uint *)which.pixels + y*which.pitch/4 + x; 129 210 *bufp = c; 211 } 212 213 void 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)); 130 215 } 131 216 … … 192 277 static assert(is(typeof(R.r)) && is(typeof(R.g)) && is(typeof(R.b)), R.stringof~" cannot be blended with: needs r,g,b!"); 193 278 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; 194 286 } 195 287 rgb invert() { return rgb(255-r, 255-g, 255-b); } … … 440 532 static if (T.length>=3 && (is(T[2]: long)||is(T[2]: real))) { 441 533 _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; 443 535 } else { 444 536 _line(lastx, lasty, cast(int)p._0, cast(int)p._1, p.tupleof[2..$]); … … 626 718 offscreen = osc; 627 719 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 } 629 724 } 630 725 void close() { SDL_QuitSubSystem(SDL_INIT_VIDEO); } … … 660 755 void cls(rgb fill=Black) { line(0, 0, display.width-1, display.height-1, Fill=fill); } 661 756 757 import tools.log; 662 758 void updateMouse(int x, int y, ubyte button, int pushed) { 663 759 auto b = cast(Button) button; … … 665 761 if (pushed == 1) { 666 762 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); 670 766 } 671 767 … … 678 774 bool pressed() { return pressed(Button.Left); } 679 775 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); } 680 778 bool[256] _pressed; 681 779 bool[256] was_pressed; 682 780 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); } 684 784 void down(Button b) { _pressed[b] = true; was_pressed[b] = true; } 685 785 void up(Button b) { _pressed[b] = false; } 786 void events_reset() { foreach (ref b; was_pressed) b = false; } 686 787 } 687 788 688 789 _mouse mouse; 689 790 690 bool[ int] keystate;791 bool[wchar] keystate; 691 792 692 793 struct _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) { 697 797 auto res = *kp; 698 *kp = false;798 // *kp = false; pressed, not pushed 699 799 return res; 700 800 } else return false; … … 706 806 void delegate() onResize; 707 807 808 int drop_type, drop_action; 809 810 string _text_dropped; 811 string textDropped() { 812 auto res = _text_dropped; 813 _text_dropped = null; 814 return res; 815 } 816 817 bool accept_drop; 818 819 import tools.time; 820 static this() { SDL_EventState(SDL_EventType.SysWMEvent, true); } 708 821 /// parameters to key: which key, and if it's pressed or released 709 822 /// 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) { 823 void events(void delegate(ushort, bool) key=null, void delegate(int, int, ubyte, int) mouse=null, void delegate() quit=null) { 824 .mouse.events_reset; 711 825 SDL_Event evt; 712 826 while (SDL_PollEvent(&evt)) { … … 731 845 break; 732 846 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); 736 850 break; 737 851 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); 740 855 break; 741 856 case SDL_EventType.Resize: … … 747 862 else throw new Error("Quit"); 748 863 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 910 done: 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 944 void events(void delegate(ushort) key, void delegate(int, int, ubyte, int) mouse=null, void delegate() quit=null) { 945 events((ushort a, bool b) { 756 946 if (b) key(a); 757 947 }, mouse, quit); 758 948 } 759 949 760 void events(void delegate( int) key, void delegate(int, int) mouse, void delegate() quit=null) {950 void events(void delegate(ushort) key, void delegate(int, int) mouse, void delegate() quit=null) { 761 951 events(key, (int x, int y, ubyte b, int p) { mouse(x, y); }, quit); 762 952 } 763 953 764 void events(void delegate( int, bool) key, void delegate(int, int) mouse, void delegate() quit=null) {954 void events(void delegate(ushort, bool) key, void delegate(int, int) mouse, void delegate() quit=null) { 765 955 events(key, (int x, int y, ubyte b, int p) { mouse(x, y); }, quit); 766 956 } … … 806 996 pt tl, dimensions; 807 997 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); } 808 1000 static Area opCall(pt _tl, pt _size, SDL_Surface *_surf=null) { 809 1001 auto res=new Area;
