File: pure_api_win.dpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (254 lines) | stat: -rw-r--r-- 5,749 bytes parent folder | download | duplicates (6)
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
{target:win}
//
// AggPas 2.4 RM3 Demo application
// Note: Press F1 key on run to see more info about this demo
//
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
//
program
 pure_api_win;

uses
 Windows ,Messages ,

 agg_basics ,
 agg_rendering_buffer ,
 agg_color ,
 agg_pixfmt ,
 agg_pixfmt_rgba ,
 agg_renderer_base ,
 agg_renderer_scanline ,
 agg_rasterizer_scanline_aa ,
 agg_scanline_p ,
 agg_render_scanlines ;

{$I agg_mode.inc }

const
 szWindowClass = 'PURE_API';
 szTitle       = 'pure_api';

{ WndProc }
function WndProc(Wnd : HWND; Msg : UINT; WPar : WParam; LPar : LParam ) : LResult; stdcall;
var
// Win32
 dc : HDC;
 ps : TPaintStruct;
 rt : TRect;

 width  ,
 height : integer;

 bmp_info : TBitmapInfo;
 mem_dc   : HDC;

 buf  : pointer;
 bmp  ,
 temp : HBitmap;

// AGG
 rbuf : rendering_buffer;
 pixf : pixel_formats;
 renb : renderer_base;
 rgba : aggclr;

 ren : renderer_scanline_aa_solid;
 ras : rasterizer_scanline_aa;
 sl  : scanline_p8;

begin
 result:=0;

 case Msg of
  WM_PAINT :
   begin
    dc:=BeginPaint(Wnd ,ps );

    if dc <> 0 then
     begin
      GetClientRect(Wnd ,rt );

      width :=rt.Right - rt.Left;
      height:=rt.Bottom - rt.Top;

     //Creating compatible DC and a bitmap to render the image
      mem_dc:=CreateCompatibleDC(dc );

      bmp_info.bmiHeader.biSize  :=sizeof(TBITMAPINFOHEADER );
      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;

      buf:=NIL;
      bmp:=CreateDIBSection(mem_dc ,bmp_info ,DIB_RGB_COLORS ,buf ,0 ,0 );

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

     //============================================================
     // AGG lowest level code
      rbuf.Construct;
      rbuf.attach(buf ,width ,height ,-width * 4 ); // Use negative stride in order
                                                    // to keep Y-axis consistent with
                                                    // WinGDI, i.e., going down.

     // Pixel format and basic primitives renderer
      pixfmt_bgra32(pixf ,@rbuf );

      renb.Construct(@pixf );
      rgba.ConstrInt(255 ,255 ,255 ,127 );

      renb.clear(@rgba );

     // Scanline renderer for solid filling
      ren.Construct(@renb );

     // Rasterizer & scanline
      ras.Construct;
      sl.Construct;

     // Polygon (triangle)
      ras.move_to_d(20.7   ,34.15  );
      ras.line_to_d(398.23 ,123.43 );
      ras.line_to_d(165.45 ,401.87 );

     // Setting the attrribute (color) & Rendering
      rgba.ConstrInt(80 ,90 ,60 );
      ren.color_    (@rgba );

      render_scanlines(@ras ,@sl ,@ren );

     //------------------------------------------------------------
     // Display the image. If the image is B-G-R-A (32-bits per pixel)
     // one can use AlphaBlend instead of BitBlt. In case of AlphaBlend
     // one also should clear the image with zero alpha, i.e. rgba8(0,0,0,0)
      BitBlt(
       dc ,
       rt.left ,rt.top ,width ,height ,
       mem_dc ,0 ,0 ,
       SRCCOPY );

     // Free AGG resources
     // Pascal doesn't call object destructors when leaving the local function range,
     // thus we have to do it by hand
      rbuf.Destruct;
      ras.Destruct;
      sl.Destruct;

     // Free resources
      SelectObject(mem_dc ,temp );
      DeleteObject(bmp );
      DeleteObject(mem_dc );

      EndPaint(Wnd ,ps );

     end;

   end;

  WM_SYSKEYDOWN ,WM_KEYDOWN :
   case WPar of
    VK_F1 :
     MessageBox(
      Wnd ,
      'The AGG library is able to draw to any surface. It is achieved by using an offline'#13 +
      'bitmap (buffer), to which AGG primarily renders. Then that bitmap is blited to the'#13 +
      'GDI device context of the destination device.'#13#13 +
      'This example demonstrates that simple setup for a Windows app that paints to a GDI '#13 +
      'device context. All it needs are the CreateCompatibleBitmap(), CreateDIBSection()'#13 +
      'and BitBlt() WinAPI calls.'#0 ,
      'AGG Message' ,MB_OK );

    else
     result:=DefWindowProc(Wnd ,Msg ,WPar ,LPar );

   end;

  WM_ERASEBKGND :
   NoP;

  WM_DESTROY :
   PostQuitMessage(0 );

  else
   result:=DefWindowProc(Wnd ,Msg ,WPar ,LPar );

 end;

end;

{ MyRegisterClass }
procedure MyRegisterClass;
var
 wcex : TWndClassEx;

begin
 wcex.cbSize:=sizeof(TWndClassEx );

 wcex.style        :=CS_HREDRAW or CS_VREDRAW;
 wcex.lpfnWndProc  :=@WndProc;
 wcex.cbClsExtra   :=0;
 wcex.cbWndExtra   :=0;
 wcex.hInstance    :=HInstance;
 wcex.hIcon        :=LoadIcon(HInstance ,IDI_APPLICATION );
 wcex.hCursor      :=LoadCursor(0 ,IDC_ARROW );
 wcex.hbrBackground:=COLOR_WINDOW + 1;
 wcex.lpszMenuName :=NIL;
 wcex.lpszClassName:=szWindowClass;
 wcex.hIconSm      :=0;

 RegisterClassEx(wcex );

end;

{ InitInstance }
function InitInstance : boolean;
var
 Wnd : HWND;

begin
 Wnd:=
  CreateWindow(
   szWindowClass ,
   szTitle ,
   WS_OVERLAPPEDWINDOW ,
   integer(CW_USEDEFAULT ) ,0 ,integer(CW_USEDEFAULT ) ,0 ,
   0 ,0 ,HInstance ,NIL );

 if Wnd <> 0 then
  begin
   ShowWindow  (Wnd,SW_SHOW );
   UpdateWindow(Wnd );

   result:=true;

  end
 else
  result:=false;

end;

var
 Msg : TMsg;

BEGIN
 MyRegisterClass;

 if InitInstance then
  while GetMessage(Msg ,0 ,0 ,0 ) do
   begin
    TranslateMessage(Msg );
    DispatchMessage (Msg );

   end;

END.