GUI library for D

Nick Sabalausky a at a.a
Tue Apr 5 19:05:02 PDT 2011


"Adam D. Ruppe" <destructionator at gmail.com> wrote in message 
news:ing4ir$260f$1 at digitalmars.com...
> Matthias Pleh wrote:
>> Are you talking about the dImage project, with the display-X11/win32
>> backends, on you side?
>
> Yeah, that's the original version. I just finished doing the Windows
> side (wasn't as bad as I thought) and put it all in one module. This
> simplified version has no file format support yet.
>
> http://arsdnet.net/dcode/simpledisplay.d
>
> Usage example:
>
> ====
>
> import simpledisplay;
>
> void main() {
> auto image = new Image(255, 255);
>
> foreach(a; 0..255)
> foreach(b; 0..255)
> image.putPixel(a, b, Color(a, b, 0));
>
> image.display();
> }
>
> ====
>
> To compile:
>
> dmd test.d simpleimage.d
>
> Should work on both Windows and Linux the same way.

I haven't benchmarked or even tested this, and heck, maybe I'm just a 
dinosaur, but for better speed and flexibility I'd recommend something more 
like what I've attached. For reasons that should be apparent in the code, 
this would need to be compiled with -inline, which in turn will most likely 
require DMD issue #5708 to be fixed.

void main() {
    auto image = new Image!(255, 255);

    foreach(a; 0..image.width)
    foreach(b; 0..image.height)
        image.fast[a, b] = Color(a, b, 0);

    foreach(x; 128...999)
        image.crop[x, 128] = image.crop[x, 64];

    image.fast[10, 10] = Color(0xFFFFFF_FF);

    // Set the blue component of a pixel
    // Some API func could be added for this
    auto i = image.fast.indexAt(20, 20) * 4 + 2;
    (cast(ubyte[])image.data)[i] = 0xCC;

    image.display();
}

The main under-the-hood changes:

- The width is statically-known, so the compiler can optimize the index 
calculation from multiplication to shifts when appropriate. The height being 
statically-known might help slightly, too.

- In release mode, using a non-cropped version is faster than cropped (good 
for when you know you won't exceed the bounds).

- Setting/getting a pixel is a one-write (or one-read) 32-bit-aligned 
operation. Ie, faster, of course.

- IIRC, 32-bit framebuffers are a little more typical, so the image data 
should be more likely to be usable without conversion.

- The alpha channel opens the door for, well, mixing images using alpha.

A major improvement, of course, would be to replace the GDI stuff with 
Direct3D 7 or 8 (there's no real need to require anything beyond those 
versions). It's would be a non-trivial change of course, but once done it 
would be a huge improvement.

I really had fun with this :) This is the sort of thing I grew up on. And D 
suits it so much better than any other language I've used.



begin 666 simpleDisplayMod.d
M#0IS=')U8W0 at 0V]L;W(@>PT*"75I;G0 at 9&%T83L-"@T*"2\O($D at 9F]R9V5T
M+"!I<R!T:&ES('-T:6QL(&EM<&QI8VET;'D at 9&5F:6YE9"!W:&5N('1H92!O
M=&AE<B!T=V\@8W1O<G, at 97AI<W0_#0H)=&AI<RAU:6YT(&1A=&$I('L-"@D)
M=&AI<RYD871A(#T at 9&%T83L-"@E]#0H)=&AI<RAU8GET92!R+"!U8GET92!G
M+"!U8GET92!B*2![#0H)"71H:7,N9&%T82 ]("AR(#P\(#(T*2!\("AG(#P\
M(#$V*2!\("AB(#P\(#@I.PT*"7T-"@ET:&ES*'5B>71E('(L('5B>71E(&<L
M('5B>71E(&(L('5B>71E(&$I('L-"@D)=&AI<RYD871A(#T@*'(@/#P@,C0I
M('P@*&<@/#P@,38I('P@*&(@/#P at ."D@?"!A.PT*"7T-"@D-"@E <')O<&5R
M='D@<F5F('5B>71E('(H*2![#0H)"7)E='5R;B H8V%S="AU8GET95LT72ED
M871A*5LP73L-"@E]#0H)0'!R;W!E<G1Y(')E9B!U8GET92!G*"D@>PT*"0ER
M971U<FX@*&-A<W0H=6)Y=&5;-%TI9&%T82E;,5T[#0H)?0T*"4!P<F]P97)T
M>2!R968@=6)Y=&4 at 8B@I('L-"@D)<F5T=7)N("AC87-T*'5B>71E6S1=*61A
M=&$I6S)=.PT*"7T-"@E <')O<&5R='D@<F5F('5B>71E(&$H*2![#0H)"7)E
M='5R;B H8V%S="AU8GET95LT72ED871A*5LS73L-"@E]#0H)#0H)<W1R:6YG
M('1O4W1R:6YG*"D@>PT*"0ER971U<FX@(D-O;&]R*#!X)3 X6"DB+F9O<FUA
M="AD871A*2D[#0H)?0T*?0T*#0IF:6YA;"!C;&%S<R!);6%G92AI;G0 at 7W=I
M9'1H+"!I;G0 at 7VA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@E]#0I]#0H-"G9O:60@;6%I;B at I('L-"@EA=71O(&EM86=E(#T@;F5W($EM
M86=E(2 at R-34L(#(U-2D[#0H-"@EF;W)E86-H*&$[(# N+FEM86=E+G=I9'1H
M*0T*"69O<F5A8V at H8CL@,"XN:6UA9V4N:&5I9VAT*0T*"0EI;6%G92YF87-T
M6V$L(&)=(#T at 0V]L;W(H82P at 8BP@,"D[#0H-"@EF;W)E86-H*'@[(#$R."XN
M+CDY.2D-"@D):6UA9V4N8W)O<%MX+" Q,CA=(#T@:6UA9V4N8W)O<%MX+" V
M-%T[#0H-"@EI;6%G92YF87-T6S$P+" Q,%T@/2!#;VQO<B at P>$9&1D9&1E]&
M1BD[#0H)#0H)+R\@4V5T('1H92!B;'5E(&-O;7!O;F5N="!O9B!A('!I>&5L
M#0H)+R\@4V]M92!!4$D at 9G5N8R!C;W5L9"!B92!A9&1E9"!F;W(@=&AI<PT*
M"6%U=&\@:2 ](&EM86=E+F9A<W0N:6YD97A!="@R,"P@,C I("H at -" K(#([
M#0H)*&-A<W0H=6)Y=&5;72EI;6%G92YD871A*5MI72 ](#!X0T,[#0H)#0H)
7:6UA9V4N9&ES<&QA>2 at I.PT*?0T*#0H`
`
end



More information about the Digitalmars-d mailing list