summaryrefslogtreecommitdiff
path: root/dmagick/internal/Windows.d
blob: 99a2d9efc956729ff8fbee3b92f0e1e4d4693889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 * Copyright: Mike Wey 2011
 * License:   zlib (See accompanying LICENSE file)
 * Authors:   Mike Wey
 */
 
module dmagick.internal.Windows;

import core.sys.windows.windows;

import dmagick.Image;
import dmagick.Exception;
import dmagick.Geometry;

class Window
{
	Image image;
	Image[] imageList;
	size_t index;
	size_t height;
	size_t width;

	WNDCLASS  wndclass;
	HINSTANCE hInstance;
	HWND      hwnd;
	MSG       msg;

	static Window[HWND] windows;

	this(Image image)
	{
		this.image = image;

		height = image.rows;
		width  = image.columns;

		hInstance = cast(HINSTANCE) GetModuleHandleA(null);

		wndclass.style         = CS_HREDRAW | CS_VREDRAW;
		wndclass.lpfnWndProc   = &WndProc;
		wndclass.cbClsExtra    = 0;
		wndclass.cbWndExtra    = 0;
		wndclass.hInstance     = hInstance;
		wndclass.hIcon         = LoadIconA(null, IDI_APPLICATION);
		wndclass.hCursor       = LoadCursorA(null, IDC_ARROW);
		wndclass.hbrBackground = null;
		wndclass.lpszMenuName  = null;
		wndclass.lpszClassName = "DMagick";

		if (!RegisterClassA(&wndclass))
			throw new DMagickException("This program requires Windows NT!");

		RECT rect = RECT(0,0, width,height);
		AdjustWindowRect(&rect, WS_CAPTION | WS_SYSMENU, false);

		hwnd = CreateWindowA("DMagick", "DMagick", WS_CAPTION | WS_SYSMENU,
			CW_USEDEFAULT, CW_USEDEFAULT, rect.right-rect.left, rect.bottom-rect.top,
			null, null, hInstance, null);

		windows[hwnd] = this;
	}
	
	this(Image[] images)
	{
		this(images[0]);
	
		imageList = images;
		index = 0;
	}

	void display()
	{
		ShowWindow(hwnd, SW_SHOWNORMAL);
		UpdateWindow(hwnd);
		
		if ( imageList !is null )
		{
			UINT delay = cast(UINT)image.animationDelay.total!"msecs"();
			SetTimer(hwnd, 0, delay, null);
		}
		
		while (GetMessageA(&msg, null, 0, 0))
		{
			TranslateMessage(&msg);
			DispatchMessageA(&msg);
		}
	}

	extern(Windows) static LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		switch (message)
		{
			case WM_ERASEBKGND:  // don't redraw bg
				return 1;

			case WM_PAINT:
				windows[hwnd].draw();
				return 0;

			case WM_TIMER:
				windows[hwnd].nextFrame();
				return 0;

			case WM_DESTROY:
				windows[hwnd] = null;
				PostQuitMessage(0);
				return 0;

			default:
		}

		return DefWindowProcA(hwnd, message, wParam, lParam);
	}

	void draw()
	{
		HDC hdc;                              // handle of the DC we will create
		HDC hdcwnd;                           // DC for the window
		HBITMAP hbitmap;                      // bitmap handle
		BITMAPINFO bmi;                       // bitmap header
		PAINTSTRUCT ps;
		VOID*  pvBits;                        // pointer to DIB section
		ULONG  ulWindowWidth, ulWindowHeight; // window width/height
		RECT   rt;                            // used for getting window dimensions

		// get window dimensions
		GetClientRect(hwnd, &rt);

		// calculate window width/height
		ulWindowWidth  = rt.right - rt.left;
		ulWindowHeight = rt.bottom - rt.top;

		// make sure we have at least some window size
		if (ulWindowWidth < 1 || ulWindowHeight < 1)
			return;
			
		// Get DC for window
		hdcwnd = BeginPaint(hwnd, &ps);

		// create a DC for our bitmap -- the source DC for BitBlt
		hdc = CreateCompatibleDC(hdcwnd);

		// setup bitmap info
		bmi.bmiHeader.biSize        = BITMAPINFOHEADER.sizeof;
		bmi.bmiHeader.biWidth       = width;
		bmi.bmiHeader.biHeight      = -height;  // must be inverted so Y axis is at top
		bmi.bmiHeader.biPlanes      = 1;
		bmi.bmiHeader.biBitCount    = 32;      // four 8-bit components
		bmi.bmiHeader.biCompression = BI_RGB;
		bmi.bmiHeader.biSizeImage   = width * height * 4;

		// create our DIB section and select the bitmap into the dc
		hbitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvBits, null, 0x0);
		SelectObject(hdc, hbitmap);

		enum channels = "BGRA";  // win32 uses BGR(A)
		Geometry area = Geometry(width, height);
		byte[] arr = (cast(byte*)pvBits)[0 .. (area.width * area.height) * channels.length];
		image.exportPixels(area, arr, channels);

		BitBlt(hdcwnd, 0, 0, width, height, hdc, 0, 0, SRCCOPY);

		DeleteObject(hbitmap);
		DeleteDC(hdc);
		EndPaint(hwnd, &ps);
	}

	/**
	 * Setup the next frame, and invalidate the window so its repainted.
	 */
	void nextFrame()
	{
		if (++index == imageList.length)
			index = 0;
			
		image = imageList[index];

		UINT delay = cast(UINT)image.animationDelay.total!"msecs"();
		SetTimer(hwnd, 0, delay, null);
		
		InvalidateRect(hwnd,null,false);
	}
}

pragma(lib, "gdi32.lib");

const AC_SRC_OVER  = 0x00;
const AC_SRC_ALPHA = 0x01;

enum DWORD BI_RGB = 0;
enum UINT DIB_RGB_COLORS = 0;

extern(Windows) BOOL BitBlt(HDC, int, int, int, int, HDC, int, int, DWORD);
extern(Windows) HBITMAP CreateCompatibleBitmap(HDC, int, int);
extern(Windows) HBITMAP CreateDIBSection(HDC, const(BITMAPINFO)*, UINT, void**, HANDLE, DWORD);
extern(Windows) UINT_PTR SetTimer(HWND, UINT_PTR, UINT, TIMERPROC);