Debian Codegraph @

examples/win32_api/pure_api/pure_api.cpp

raw
1 // pure_api.cpp : Defines the entry point for the application.
2 //
3
4 #include "stdafx.h"
5 #include "resource.h"
6
7 #include "agg_scanline_p.h"
8 #include "agg_renderer_scanline.h"
9 #include "agg_pixfmt_rgba.h"
10 #include "agg_rasterizer_scanline_aa.h"
11
12 #define MAX_LOADSTRING 100
13
14 // Global Variables:
15 HINSTANCE hInst; // current instance
16 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
17 TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
18
19 // Foward declarations of functions included in this code module:
20 ATOM MyRegisterClass(HINSTANCE hInstance);
21 BOOL InitInstance(HINSTANCE, int);
22 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
23 LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
24
25 int APIENTRY WinMain(HINSTANCE hInstance,
26 HINSTANCE hPrevInstance,
27 LPSTR lpCmdLine,
28 int nCmdShow)
29 {
30 // TODO: Place code here.
31 MSG msg;
32 HACCEL hAccelTable;
33
34 // Initialize global strings
35 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
36 LoadString(hInstance, IDC_PURE_API, szWindowClass, MAX_LOADSTRING);
37 MyRegisterClass(hInstance);
38
39 // Perform application initialization:
40 if (!InitInstance (hInstance, nCmdShow))
41 {
42 return FALSE;
43 }
44
45 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_PURE_API);
46
47 // Main message loop:
48 while (GetMessage(&msg, NULL, 0, 0))
49 {
50 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
51 {
52 TranslateMessage(&msg);
53 DispatchMessage(&msg);
54 }
55 }
56
57 return msg.wParam;
58 }
59
60
61
62 //
63 // FUNCTION: MyRegisterClass()
64 //
65 // PURPOSE: Registers the window class.
66 //
67 // COMMENTS:
68 //
69 // This function and its usage is only necessary if you want this code
70 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
71 // function that was added to Windows 95. It is important to call this function
72 // so that the application will get 'well formed' small icons associated
73 // with it.
74 //
75 ATOM MyRegisterClass(HINSTANCE hInstance)
76 {
77 WNDCLASSEX wcex;
78
79 wcex.cbSize = sizeof(WNDCLASSEX);
80
81 wcex.style = CS_HREDRAW | CS_VREDRAW;
82 wcex.lpfnWndProc = (WNDPROC)WndProc;
83 wcex.cbClsExtra = 0;
84 wcex.cbWndExtra = 0;
85 wcex.hInstance = hInstance;
86 wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_PURE_API);
87 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
88 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
89 wcex.lpszMenuName = (LPCSTR)IDC_PURE_API;
90 wcex.lpszClassName = szWindowClass;
91 wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
92
93 return RegisterClassEx(&wcex);
94 }
95
96 //
97 // FUNCTION: InitInstance(HANDLE, int)
98 //
99 // PURPOSE: Saves instance handle and creates main window
100 //
101 // COMMENTS:
102 //
103 // In this function, we save the instance handle in a global variable and
104 // create and display the main program window.
105 //
106 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
107 {
108 HWND hWnd;
109
110 hInst = hInstance; // Store instance handle in our global variable
111
112 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
113 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
114
115 if (!hWnd)
116 {
117 return FALSE;
118 }
119
120 ShowWindow(hWnd, nCmdShow);
121 UpdateWindow(hWnd);
122
123 return TRUE;
124 }
125
126 //
127 // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
128 //
129 // PURPOSE: Processes messages for the main window.
130 //
131 // WM_COMMAND - process the application menu
132 // WM_PAINT - Paint the main window
133 // WM_DESTROY - post a quit message and return
134 //
135 //
136 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
137 {
138 int wmId, wmEvent;
139 PAINTSTRUCT ps;
140 HDC hdc;
141 TCHAR szHello[MAX_LOADSTRING];
142 LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
143
144 switch (message)
145 {
146 case WM_COMMAND:
147 wmId = LOWORD(wParam);
148 wmEvent = HIWORD(wParam);
149 // Parse the menu selections:
150 switch (wmId)
151 {
152 case IDM_ABOUT:
153 DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
154 break;
155 case IDM_EXIT:
156 DestroyWindow(hWnd);
157 break;
158 default:
159 return DefWindowProc(hWnd, message, wParam, lParam);
160 }
161 break;
162 case WM_PAINT:
163 {
164 hdc = BeginPaint(hWnd, &ps);
165 RECT rt;
166 GetClientRect(hWnd, &rt);
167
168 int width = rt.right - rt.left;
169 int height = rt.bottom - rt.top;
170
171 //============================================================
172 //Creating compatible DC and a bitmap to render the image
173 BITMAPINFO bmp_info;
174 bmp_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
175 bmp_info.bmiHeader.biWidth = width;
176 bmp_info.bmiHeader.biHeight = height;
177 bmp_info.bmiHeader.biPlanes = 1;
178 bmp_info.bmiHeader.biBitCount = 32;
179 bmp_info.bmiHeader.biCompression = BI_RGB;
180 bmp_info.bmiHeader.biSizeImage = 0;
181 bmp_info.bmiHeader.biXPelsPerMeter = 0;
182 bmp_info.bmiHeader.biYPelsPerMeter = 0;
183 bmp_info.bmiHeader.biClrUsed = 0;
184 bmp_info.bmiHeader.biClrImportant = 0;
185
186 HDC mem_dc = ::CreateCompatibleDC(hdc);
187
188 void* buf = 0;
189
190 HBITMAP bmp = ::CreateDIBSection(
191 mem_dc,
192 &bmp_info,
193 DIB_RGB_COLORS,
194 &buf,
195 0,
196 0
197 );
198
199 // Selecting the object before doing anything allows you
200 // to use AGG together with native Windows GDI.
201 HBITMAP temp = (HBITMAP)::SelectObject(mem_dc, bmp);
202
203
204 //============================================================
205 // AGG lowest level code.
206 agg::rendering_buffer rbuf;
207 rbuf.attach((unsigned char*)buf, width, height, -width*4); // Use negative stride in order
208 // to keep Y-axis consistent with
209 // WinGDI, i.e., going down.
210
211 // Pixel format and basic primitives renderer
212 agg::pixfmt_bgra32 pixf(rbuf);
213 agg::renderer_base<agg::pixfmt_bgra32> renb(pixf);
214
215 renb.clear(agg::rgba8(255, 255, 255, 255));
216
217 // Scanline renderer for solid filling.
218 agg::renderer_scanline_aa_solid<agg::renderer_base<agg::pixfmt_bgra32> > ren(renb);
219
220 // Rasterizer & scanline
221 agg::rasterizer_scanline_aa<> ras;
222 agg::scanline_p8 sl;
223
224 // Polygon (triangle)
225 ras.move_to_d(20.7, 34.15);
226 ras.line_to_d(398.23, 123.43);
227 ras.line_to_d(165.45, 401.87);
228
229 // Setting the attrribute (color) & Rendering
230 ren.color(agg::rgba8(80, 90, 60));
231 agg::render_scanlines(ras, sl, ren);
232 //============================================================
233
234
235
236 //------------------------------------------------------------
237 // Display the image. If the image is B-G-R-A (32-bits per pixel)
238 // one can use AlphaBlend instead of BitBlt. In case of AlphaBlend
239 // one also should clear the image with zero alpha, i.e. rgba8(0,0,0,0)
240
241 ::BitBlt(
242 hdc,
243 rt.left,
244 rt.top,
245 width,
246 height,
247 mem_dc,
248 0,
249 0,
250 SRCCOPY
251 );
252
253 // Free resources
254 ::SelectObject(mem_dc, temp);
255 ::DeleteObject(bmp);
256 ::DeleteObject(mem_dc);
257
258 EndPaint(hWnd, &ps);
259 }
260 break;
261
262
263 case WM_ERASEBKGND: // Don't forget to do nothing on Erase Background event :-)
264 break;
265
266 case WM_DESTROY:
267 PostQuitMessage(0);
268 break;
269 default:
270 return DefWindowProc(hWnd, message, wParam, lParam);
271 }
272 return 0;
273 }
274
275 // Mesage handler for about box.
276 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
277 {
278 switch (message)
279 {
280 case WM_INITDIALOG:
281 return TRUE;
282
283 case WM_COMMAND:
284 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
285 {
286 EndDialog(hDlg, LOWORD(wParam));
287 return TRUE;
288 }
289 break;
290 }
291 return FALSE;
292 }