/win32_gl_mask_test.d
http://github.com/AndrejMitrovic/cairoDSamples · D · 273 lines · 214 code · 50 blank · 9 comment · 3 complexity · 80a410061fecdada87e0394089d6c246 MD5 · raw file
- module win32_gl_mask_test;
- /+
- + 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)
- +/
- import core.memory;
- import core.runtime;
- import core.thread;
- import std.conv;
- import std.math;
- import std.algorithm;
- import std.datetime;
- import std.array;
- import std.stdio;
- import std.range;
- import std.string;
- import std.utf;
- pragma(lib, "gdi32.lib");
- pragma(lib, "comdlg32.lib");
- pragma(lib, "winmm.lib");
- import win32.windef;
- import win32.winuser;
- import win32.wingdi;
- import win32.winbase;
- import win32.commdlg;
- import win32.mmsystem;
- import cairo.cairo;
- import cairo.win32;
- import cairo.gl;
- alias cairo.cairo.RGB RGB;
- alias std.algorithm.max max;
- alias std.algorithm.min min;
- string appName = "CairoGL";
- string description = "CairoGL Mask Test";
- HINSTANCE hinst;
- 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;
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- 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 = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
- 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;
- }
- extern (Windows)
- LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_CREATE:
- self = new Window(hwnd); break;
- case WM_DESTROY:
- return self.OnDestroy(hwnd, message, wParam, lParam);
- case WM_PAINT:
- return self.OnPaint(hwnd, message, wParam, lParam);
- case WM_ERASEBKGND:
- return self.OnBackgroundErase(hwnd, message, wParam, lParam);
- case WM_SIZE:
- {
- auto cxClient = LOWORD(lParam);
- auto cyClient = HIWORD(lParam);
- return self.newSize(cxClient, cyClient);
- }
-
- default:
- }
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
- Window self;
- class Window
- {
- HWND hwnd;
- PAINTSTRUCT ps;
- RECT rc;
- HDC hdc;
- HGLRC hglrc;
- int cxClient, cyClient;
- GLSurface surf;
- GLDevice device;
-
- bool newSize(ushort newX, ushort newY)
- {
- cxClient = newX;
- cyClient = newY;
-
- Render();
- return 0;
- }
-
- this(HWND hwnd)
- {
- this.hwnd = hwnd;
- hdc = GetDC(hwnd);
- PIXELFORMATDESCRIPTOR pfd = PIXELFORMATDESCRIPTOR(
- PIXELFORMATDESCRIPTOR.sizeof,
- 1, // version number
- PFD_DRAW_TO_WINDOW | // support window
- PFD_SUPPORT_OPENGL | // support OpenGL
- PFD_DOUBLEBUFFER, // double buffered
- PFD_TYPE_RGBA, // RGBA type
- 24, // 24-bit color depth
- 0, 0, 0, 0, 0, 0, // color bits ignored
- 0, // no alpha buffer
- 0, // shift bit ignored
- 0, // no accumulation buffer
- 0, 0, 0, 0, // accum bits ignored
- 32, // 32-bit z-buffer
- 0, // no stencil buffer
- 0, // no auxiliary buffer
- PFD_TYPE_RGBA, // main layer
- 0, // reserved
- 0, 0, 0 // layer masks ignored
- );
-
- // get the best available match of the pixel format for the device context
- int iPixelFormat = ChoosePixelFormat(hdc, &pfd);
-
- // use it
- SetPixelFormat(hdc, iPixelFormat, &pfd);
-
- // create a rendering context
- hglrc = wglCreateContext(hdc);
- device = new GLDevice(hglrc);
-
- ReleaseDC(hwnd, hdc);
- }
- void Render()
- {
- InvalidateRect(this.hwnd, NULL, TRUE);
- }
-
- int OnPaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- hdc = BeginPaint(hwnd, &ps);
-
- surf = new GLSurface(device, hdc, cxClient, cyClient);
- auto ctx = Context(surf);
-
- ctx.setSourceRGB(1, 1, 1);
- ctx.paint();
- ctx.scale(cxClient, cyClient);
- ctx.moveTo(0, 0);
- ctx.rectangle(0, 0, 1, 1);
- ctx.setSourceRGBA(1, 1, 1, 0);
- ctx.setOperator(Operator.CAIRO_OPERATOR_CLEAR);
- ctx.fill();
- ctx.setSourceRGB(0, 0, 0);
- ctx.setOperator(Operator.CAIRO_OPERATOR_OVER);
- auto linpat = new LinearGradient(0, 0, 1, 1);
- linpat.addColorStopRGB(0, RGB(0, 0.3, 0.8));
- linpat.addColorStopRGB(1, RGB(0, 0.8, 0.3));
- auto radpat = new RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.75);
- radpat.addColorStopRGBA(0, RGBA(0, 0, 0, 1));
- radpat.addColorStopRGBA(0.5, RGBA(0, 0, 0, 0));
- ctx.setSource(linpat);
-
- {
- StopWatch sw;
- sw.start();
- scope(exit)
- {
- sw.stop();
- writefln("Done in usecs: %s.", sw.peek().usecs);
- sw.reset();
- }
-
- ctx.mask(radpat);
- }
- surf.swapBuffers();
- surf.finish();
- EndPaint(hwnd, &ps);
- return 0;
- }
- auto OnBackgroundErase(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- return 0;
- }
- auto OnDestroy(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PostQuitMessage(0);
- return 0;
- }
- }