Difference between revisions of "Simpledisplay.d"

From D Wiki
Jump to: navigation, search
m (Wrap code example in a code block)
m (event handlers must all be delegates)
Line 31: Line 31:
 
   // do an event loop to show it and collect input
 
   // do an event loop to show it and collect input
 
   window.eventLoop(0, // the 0 is a timer if you want approximate frames
 
   window.eventLoop(0, // the 0 is a timer if you want approximate frames
     (KeyEvent ke) {
+
     delegate(KeyEvent ke) {
 
         // key pressed or released
 
         // key pressed or released
 
     },
 
     },
     (dchar character) {
+
     delegate(dchar character) {
 
         // character pressed
 
         // character pressed
 
     },
 
     },
     (MouseEvent me) {
+
     delegate(MouseEvent me) {
 
         // mouse moved or clicked
 
         // mouse moved or clicked
 
     }
 
     }

Revision as of 20:16, 21 October 2013

About my simpledisplay.d

It is a little library that gives access to simple windows on MS Windows and on X11 with minimal dependencies. (There's some Mac OS Cocoa code in there, but it is unmaintained because I don't have a Mac.)

code: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/simpledisplay.d

depends on: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/color.d

Features:

  * you can create images by writing pixels in a regular format (MemoryImage) or using some operating system APIs (class Image and class Sprite)
  * easily create a window and draw on them or get events from them
  * build more on top of the window


Example program:

import simpledisplay;
void main() {
   // create a window easily
   auto window = new SimpleWindow(500 /* width */, 500 /* height */);
   {
       // draw on the window. it double buffers automatically, so you want it to go out of scope to show changes
       auto painter = window.draw();
       painter.outlineColor = Color.green;
       painter.drawLine(Point(0, 0), Point(500, 500));
   }

   // do an event loop to show it and collect input
   window.eventLoop(0, // the 0 is a timer if you want approximate frames
     delegate(KeyEvent ke) {
         // key pressed or released
     },
     delegate(dchar character) {
        // character pressed
     },
     delegate(MouseEvent me) {
        // mouse moved or clicked
     }
   );
}

Drawing functions: see struct ScreenPainter

MemoryImages are in color.d and my other modules like png.d and bmp.d can load files into them.


More advanced features:

  • clipboard text copy/paste, and PRIMARY selection on X
  • window resize handling: pass Resizability.allowResizing to the constructor, set a handler on window.windowResized
  • some OpenGL: pass OpenGlOptions.yes to the constructor. Compile with version=with_opengl on windows and have glu32.lib ready.
  • hook native events: set delegate to window.handleNativeEvent
  • open multiple windows at once
  • window.title = "something"; to rename window
  • window.icon = some_memory_image; to change window icon
  • integration with generic arsd.eventloop on Linux
  • a bunch of Windows and Xlib functions are prototyped as well
  • the event loop on Windows also calls SleepEx to clear pending async I/O for you
  • class Sprite is a pixmap for more efficient usage on X

Might (might!) be coming:

  • joystick input
  • decorationless windows
  • notification area icons