summaryrefslogtreecommitdiff
path: root/Build/source/libs/gd/libgd-src/examples/windows.c
blob: 55976386f4e68f3f9eb9d48f673fc98f376d0c66 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Sample usage of GD on windows. This little program opens a window, fetch its DIB
and assigns to a GD truecolor image.

Thanks to Mateusz Loskot (http://mateusz.loskot.net) for the AttachBuffer function!
$Id$
*/
#include <windows.h>
#include <gd.h>
#include <gdfontg.h>
#include <gdfontl.h>


gdImagePtr gdImageTrueColorAttachBuffer(int* buffer, int sx, int sy, int stride)
{
	int i;
	int height;
	int* rowptr;
	gdImagePtr im;
	im = (gdImage *) malloc (sizeof (gdImage));
	if (!im) {
		return 0;
	}
	memset (im, 0, sizeof (gdImage));

#if 0
	if (overflow2(sizeof (int *), sy)) {
		return 0;
	}
#endif

	im->tpixels = (int **) malloc (sizeof (int *) * sy);
	if (!im->tpixels) {
		free(im);
		return 0;
	}

	im->polyInts = 0;
	im->polyAllocated = 0;
	im->brush = 0;
	im->tile = 0;
	im->style = 0;

	height = sy;
	rowptr = buffer;
	if (stride < 0) {
		int startoff = (height - 1) * stride;
		rowptr = buffer - startoff;
	}

	i = 0;
	while (height--) {
		im->tpixels[i] = rowptr;
		rowptr += stride;
		i++;
	}

	im->sx = sx;
	im->sy = sy;
	im->transparent = (-1);
	im->interlace = 0;
	im->trueColor = 1;
	im->saveAlphaFlag = 0;
	im->alphaBlendingFlag = 1;
	im->thick = 1;
	im->AA = 0;
	im->cx1 = 0;
	im->cy1 = 0;
	im->cx2 = im->sx - 1;
	im->cy2 = im->sy - 1;
	return im;
}

void gdImageDetachBuffer(gdImagePtr im)
{
	free(im->tpixels);
	free(im);
}


BITMAPINFO gdCreateBmp(int width, int height)
{
	BITMAPINFO bmp_info;

	// Configure bitmap properties

	ZeroMemory(&bmp_info, sizeof(BITMAPINFO));
	bmp_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmp_info.bmiHeader.biWidth = width;
	bmp_info.bmiHeader.biHeight = height;
	bmp_info.bmiHeader.biPlanes = 1;
	bmp_info.bmiHeader.biBitCount = 32;
	bmp_info.bmiHeader.biCompression = BI_RGB;
	bmp_info.bmiHeader.biSizeImage = 0;
	bmp_info.bmiHeader.biXPelsPerMeter = 0;
	bmp_info.bmiHeader.biYPelsPerMeter = 0;
	bmp_info.bmiHeader.biClrUsed = 0;
	bmp_info.bmiHeader.biClrImportant = 0;
	return bmp_info;
}

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT ("Bezier") ;
	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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
	wndclass.lpszMenuName  = NULL ;
	wndclass.lpszClassName = szAppName ;

	if (!RegisterClass (&wndclass)) {
		// UNICODE-Compilierung ist die einzige realistische Fehlermöglichkeit
		MessageBox (NULL, TEXT ("Programm arbeitet mit Unicode und setzt Windows NT voraus!"),
		            szAppName, MB_ICONERROR) ;
		return 0 ;
	}

	hwnd = CreateWindow (szAppName, TEXT ("Bezierkurven"),
	                     WS_OVERLAPPEDWINDOW,
	                     CW_USEDEFAULT, CW_USEDEFAULT,
	                     CW_USEDEFAULT, CW_USEDEFAULT,
	                     NULL, NULL, hInstance, NULL) ;

	ShowWindow (hwnd, iCmdShow) ;
	UpdateWindow (hwnd) ;

	while (GetMessage (&msg, NULL, 0, 0)) {
		TranslateMessage (&msg) ;
		DispatchMessage (&msg) ;
	}
	return msg.wParam ;
}

void DrawBezier (HDC hdc, POINT apt[])
{
	PolyBezier (hdc, apt, 4) ;

	MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ;
	LineTo   (hdc, apt[1].x, apt[1].y) ;

	MoveToEx (hdc, apt[2].x, apt[2].y, NULL) ;
	LineTo   (hdc, apt[3].x, apt[3].y) ;
}


void gdDrawImage(HDC hdc, RECT *rc)
{
	HDC  mem_dc;
	BITMAPINFO bmp_info;
	void* bits;
	HBITMAP bmp, temp;
	gdImagePtr im;
	int width, height, stride;
	int white, black, blue, red;
	char *s = "Hello world!";
	gdFontPtr lfont, gfont;

	width = rc->right - rc->left;
	height = rc->bottom - rc->top;

	bmp_info = gdCreateBmp(width, height);

	// Create memory device context
	mem_dc = CreateCompatibleDC(hdc);
	if (!mem_dc) {
		MessageBox(NULL, "Can't create a compatible DC!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return;
	}

	// bits points to a shared buffer of pixels
	bits = NULL;
	bmp = CreateDIBSection(mem_dc, &bmp_info, DIB_RGB_COLORS, (void**)&bits, 0, 0);

	// Selecting the object before doing anything allows you to use libgd
	// together with native Windows GDI.
	temp = (HBITMAP)SelectObject(mem_dc, bmp);

	/*stride = ((width * 1 + 3) >> 2) << 2;*/
	// always uses 32bit in BMPINFO
	stride = width;
	im = NULL;

	// Attach shared buffer of pixels to GD image
	// Negative stride places 0,0 in upper-left corner
	im = gdImageTrueColorAttachBuffer((int*)bits, width, height, -stride);
	if (!im) {
		MessageBox(NULL, "GD image creation failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return;
	}

	// Start of GD drawing
	white = gdImageColorAllocate(im, 255, 255, 255);
	black = gdImageColorAllocate(im, 0, 0, 0);
	blue = gdImageColorAllocate(im, 0, 0, 255);

	// Allocate the color red, 50% transparent.
	red = gdImageColorAllocateAlpha(im, 255, 0, 0, 64);

	// Erase background with white color
	gdImageFilledRectangle(im, 0, 0, width, height, 0xFF0000);

	lfont = gdFontGetLarge();
	gfont = gdFontGetGiant();

	// Draw a dashed line from the upper left corner to the lower right corner.
	gdImageFilledRectangle(im, 25, 25, 100, 100, blue);

	gdImageChar(im, gfont, 35, 35, 'Q', white);
	gdImageFilledRectangle(im, 50, 50, 75, 175, red);
	gdImageLine(im, 0, 0, 150, 150, black);

	gdImageString(im, gdFontGetLarge(),
	              im->sx / 2 - (strlen(s) * lfont->w / 2),
	              im->sy / 2 - lfont->h / 2,
	              (unsigned char*)s, black);

	// Copy drawing from memory context (shared bitmap buffer) to screen DC.
	BitBlt(hdc, rc->left, rc->top, width, height, mem_dc, 0, 0, SRCCOPY);

	// Free
	gdImageDetachBuffer(im);
	SelectObject(mem_dc, temp);
	DeleteObject(bmp);
	DeleteObject(mem_dc);
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static POINT apt[4] ;
	HDC          hdc ;
	int          cxClient, cyClient ;
	PAINTSTRUCT  ps ;
	RECT rc;

	GetClientRect(hwnd, &rc);

	switch (message) {
	case WM_SIZE:
		cxClient = LOWORD (lParam) ;
		cyClient = HIWORD (lParam) ;

		apt[0].x = cxClient / 4 ;
		apt[0].y = cyClient / 2 ;

		apt[1].x = cxClient / 2 ;
		apt[1].y = cyClient / 4 ;

		apt[2].x =     cxClient / 2 ;
		apt[2].y = 3 * cyClient / 4 ;

		apt[3].x = 3 * cxClient / 4 ;
		apt[3].y =     cyClient / 2 ;
		return 0 ;

	case WM_LBUTTONDOWN:
	case WM_RBUTTONDOWN:
	case WM_MOUSEMOVE:
		if (wParam & MK_LBUTTON || wParam & MK_RBUTTON) {
			hdc = GetDC (hwnd) ;

			// alte Kurve löschen (mit Weiß übermalen)
			SelectObject (hdc, GetStockObject (WHITE_PEN)) ;
			DrawBezier (hdc, apt) ;

			if (wParam & MK_LBUTTON) {
				apt[1].x = LOWORD (lParam) ;
				apt[1].y = HIWORD (lParam) ;
			}

			if (wParam & MK_RBUTTON) {
				apt[2].x = LOWORD (lParam) ;
				apt[2].y = HIWORD (lParam) ;
			}

			// neue Kurve (mit Schwarz) zeichnen
			SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
			gdDrawImage(hdc, &rc);
			DrawBezier (hdc, apt) ;
			ReleaseDC (hwnd, hdc) ;
		}
		return 0 ;


	case WM_PAINT:
		hdc = BeginPaint (hwnd, &ps) ;

		GetClientRect(hwnd, &rc);
		gdDrawImage(hdc, &rc);
		DrawBezier (hdc, apt) ;

		EndPaint (hwnd, &ps) ;
		return 0 ;

	case WM_DESTROY:
		PostQuitMessage (0) ;
		return 0 ;
	}
	return DefWindowProc (hwnd, message, wParam, lParam) ;
}