/win32_cairo_doublebuffer.d
http://github.com/AndrejMitrovic/cairoDSamples · D · 331 lines · 223 code · 60 blank · 48 comment · 9 complexity · 89869bf7d7e30653a519a1df2ee29cc5 MD5 · raw file
- module win32_cairo_doublebuffer;
- /+
- + Copyright Andrej Mitrovic 2011.
- + Distributed under the Boost Software License, Version 1.0.
- + (See accompanying file LICENSE_1_0.txt or copy at
- + http://www.boost.org/LICENSE_1_0.txt)
- +/
- /+
- + Demonstrates the usage of a double-buffer using Cairo and win32.
- + Also shows how to avoid re-blitting an area by simply using the
- + ps.rcPaint bounding rectangle field, and removing redrawing of
- + the entire window when it is resized.
- +
- + For more info on double-buffering and avoiding screen flicker, see:
- + http://wiki.osdev.org/Double_Buffering
- + http://www.catch22.net/tuts/flicker
- +/
- import core.runtime;
- import std.process;
- import std.stdio;
- import std.utf;
- pragma(lib, "gdi32.lib");
- import win32.windef;
- import win32.winuser;
- import win32.wingdi;
- string appName = "CairoWindow";
- string description = "A simple win32 window with Cairo drawing";
- HINSTANCE hinst;
- import cairo.cairo;
- import cairo.win32;
- alias cairo.cairo.RGB RGB; // conflicts with win32.wingdi.RGB
- extern (Windows)
- int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
- {
- int result;
- void exceptionHandler(Throwable e) { throw e; }
- try
- {
- Runtime.initialize(&exceptionHandler);
- result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
- Runtime.terminate(&exceptionHandler);
- }
- catch (Throwable o)
- {
- MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
- result = 0;
- }
- return result;
- }
- int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
- {
- hinst = hInstance;
- HACCEL hAccel;
- HWND hwnd;
- MSG msg;
- WNDCLASS wndclass;
- // commented out so we do not redraw the entire screen on resize
- //~ wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.style = WS_CLIPCHILDREN;
- wndclass.lpfnWndProc = &WndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndclass.hbrBackground = null;
- wndclass.lpszMenuName = appName.toUTF16z;
- wndclass.lpszClassName = appName.toUTF16z;
- if (!RegisterClass(&wndclass))
- {
- MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
- return 0;
- }
- hwnd = CreateWindow(appName.toUTF16z, // window class name
- description.toUTF16z, // window caption
- WS_OVERLAPPEDWINDOW, // window style
- CW_USEDEFAULT, // initial x position
- CW_USEDEFAULT, // initial y position
- CW_USEDEFAULT, // initial x size
- CW_USEDEFAULT, // initial y size
- NULL, // parent window handle
- NULL, // window menu handle
- hInstance, // program instance handle
- NULL); // creation parameters
- ShowWindow(hwnd, iCmdShow);
- UpdateWindow(hwnd);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- void roundedRectangle(Context ctx, int x, int y, int w, int h, int radius_x = 5, int radius_y = 5)
- {
- enum ARC_TO_BEZIER = 0.55228475;
- if (radius_x > w - radius_x)
- radius_x = w / 2;
- if (radius_y > h - radius_y)
- radius_y = h / 2;
- // approximate (quite close) the arc using a bezier curve
- auto c1 = ARC_TO_BEZIER * radius_x;
- auto c2 = ARC_TO_BEZIER * radius_y;
- ctx.newPath();
- ctx.moveTo(x + radius_x, y);
- ctx.relLineTo(w - 2 * radius_x, 0.0);
- ctx.relCurveTo(c1, 0.0, radius_x, c2, radius_x, radius_y);
- ctx.relLineTo(0, h - 2 * radius_y);
- ctx.relCurveTo(0.0, c2, c1 - radius_x, radius_y, -radius_x, radius_y);
- ctx.relLineTo(-w + 2 * radius_x, 0);
- ctx.relCurveTo(-c1, 0, -radius_x, -c2, -radius_x, -radius_y);
- ctx.relLineTo(0, -h + 2 * radius_y);
- ctx.relCurveTo(0.0, -c2, radius_x - c1, -radius_y, radius_x, -radius_y);
- ctx.closePath();
- }
- extern (Windows)
- LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_CREATE:
- {
- window = new Window(hwnd);
- return 0;
- }
- default:
- }
- if (window)
- return window.process(hwnd, message, wParam, lParam);
- else
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
- Window window;
- struct PaintBuffer
- {
- int width, height;
- this(HDC localHdc, int cxClient, int cyClient)
- {
- width = cxClient;
- height = cyClient;
- hBuffer = CreateCompatibleDC(localHdc);
- hBitmap = CreateCompatibleBitmap(localHdc, cxClient, cyClient);
- hOldBitmap = SelectObject(hBuffer, hBitmap);
- surf = new Win32Surface(hBuffer);
- ctx = Context(surf);
- initialized = true;
- }
- ~this()
- {
- if (initialized) // struct dtors are still buggy sometimes
- {
- ctx.dispose();
- surf.finish();
- surf.dispose();
- SelectObject(hBuffer, hOldBitmap);
- DeleteObject(hBitmap);
- DeleteDC(hBuffer);
- initialized = false;
- }
- }
- bool initialized;
- HDC hBuffer;
- HBITMAP hBitmap;
- HBITMAP hOldBitmap;
- Context ctx;
- Surface surf;
- }
- class Window
- {
- int width, height;
- HWND hwnd;
- PAINTSTRUCT ps;
- PaintBuffer paintBuffer;
- bool needsRedraw;
- this(HWND hwnd)
- {
- this.hwnd = hwnd;
- auto hDesk = GetDesktopWindow();
- RECT rc;
- GetClientRect(hDesk, &rc);
- auto localHdc = GetDC(hwnd);
- paintBuffer = PaintBuffer(localHdc, rc.right, rc.bottom);
- needsRedraw = true;
- }
- LRESULT process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- case WM_PAINT:
- OnPaint(hwnd, message, wParam, lParam);
- return 0;
- case WM_ERASEBKGND:
- return 1;
- case WM_SIZE:
- {
- width = LOWORD(lParam);
- height = HIWORD(lParam);
- return 0;
- }
- default:
- }
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
- void OnPaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static int blitCount;
- auto hdc = BeginPaint(hwnd, &ps);
- auto ctx = paintBuffer.ctx;
- auto hBuffer = paintBuffer.hBuffer;
- auto boundRect = ps.rcPaint;
- with (boundRect)
- {
- system("cls");
- writefln("left: %.3s, top: %.3s, bottom: %.3s, right: %.3s", left, top, bottom, right);
- }
- if (needsRedraw) // cairo needs to redraw
- {
- draw(ctx);
- needsRedraw = false;
- }
- writefln("blit: %.3s", ++blitCount);
- with (boundRect) // blit only required areas
- {
- BitBlt(hdc, left, top, right, bottom, hBuffer, left, top, SRCCOPY);
- }
- EndPaint(hwnd, &ps);
- }
- void draw(Context ctx)
- {
- ctx.setSourceRGB(1, 1, 1);
- ctx.paint();
-
- ctx.rectangle(0, 0, 120, 90);
- ctx.setSourceRGBA(0.7, 0, 0, 0.8);
- ctx.fill();
-
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_CLEAR);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_SOURCE);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_OVER);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_IN);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_OUT);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_ATOP);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_DEST);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_DEST_OVER);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_DEST_IN);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_DEST_OUT);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_DEST_ATOP);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_DEST_XOR);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_DEST_ADD);
- //~ ctx.setOperator(Operator.CAIRO_OPERATOR_DEST_SATURATE);
-
- //~ // blue
- ctx.rectangle(40, 30, 120, 90);
- ctx.setSourceRGBA(0, 0, 0.9, 0.4);
- ctx.fill();
-
- //~ auto color = RGB(0.9, 0.9, 0.9);
- //~ ctx.setSourceRGB(color);
- //~ ctx.translate(100, 100);
- //~ ctx.rectangle(0, 0, 200, 200);
- //~ ctx.strokePreserve();
- //~ ctx.clip();
- //~ ctx.rectangle(0, 50, 100, 100);
- //~ ctx.fill();
- //~ ctx.setSourceRGB(color);
- //~ roundedRectangle(ctx, 50, 50, 250, 250, 10, 10);
- //~ ctx.fillPreserve();
- //~ color = RGB(0.9, 0.9, 0.9);
- //~ ctx.setSourceRGB(color);
- //~ ctx.stroke();
- }
- }