Debian Codegraph @

src/platform/win32/agg_platform_support.cpp

raw
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: [email protected]
12 // [email protected]
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // class platform_support
17 //
18 //----------------------------------------------------------------------------
19
20 #include <windows.h>
21 #include <cstring>
22 #include "platform/agg_platform_support.h"
23 #include "platform/win32/agg_win32_bmp.h"
24 #include "util/agg_color_conv.h"
25 #include "util/agg_color_conv_rgb8.h"
26 #include "util/agg_color_conv_rgb16.h"
27 #include "agg_pixfmt_gray.h"
28 #include "agg_pixfmt_rgb.h"
29 #include "agg_pixfmt_rgba.h"
30
31
32 namespace agg
33 {
34
35 //------------------------------------------------------------------------
36 HINSTANCE g_windows_instance = 0;
37 int g_windows_cmd_show = 0;
38
39
40 //------------------------------------------------------------------------
41 class platform_specific
42 {
43 public:
44 platform_specific(pix_format_e format, bool flip_y);
45
46 void create_pmap(unsigned width, unsigned height,
47 rendering_buffer* wnd);
48
49 void display_pmap(HDC dc, const rendering_buffer* src);
50 bool load_pmap(const char* fn, unsigned idx,
51 rendering_buffer* dst);
52
53 bool save_pmap(const char* fn, unsigned idx,
54 const rendering_buffer* src);
55
56 unsigned translate(unsigned keycode);
57
58 pix_format_e m_format;
59 pix_format_e m_sys_format;
60 bool m_flip_y;
61 unsigned m_bpp;
62 unsigned m_sys_bpp;
63 HWND m_hwnd;
64 pixel_map m_pmap_window;
65 pixel_map m_pmap_img[platform_support::max_images];
66 unsigned m_keymap[256];
67 unsigned m_last_translated_key;
68 int m_cur_x;
69 int m_cur_y;
70 unsigned m_input_flags;
71 bool m_redraw_flag;
72 HDC m_current_dc;
73 LARGE_INTEGER m_sw_freq;
74 LARGE_INTEGER m_sw_start;
75 };
76
77
78 //------------------------------------------------------------------------
79 platform_specific::platform_specific(pix_format_e format, bool flip_y) :
80 m_format(format),
81 m_sys_format(pix_format_undefined),
82 m_flip_y(flip_y),
83 m_bpp(0),
84 m_sys_bpp(0),
85 m_hwnd(0),
86 m_last_translated_key(0),
87 m_cur_x(0),
88 m_cur_y(0),
89 m_input_flags(0),
90 m_redraw_flag(true),
91 m_current_dc(0)
92 {
93 memset(m_keymap, 0, sizeof(m_keymap));
94
95 m_keymap[VK_PAUSE] = key_pause;
96 m_keymap[VK_CLEAR] = key_clear;
97
98 m_keymap[VK_NUMPAD0] = key_kp0;
99 m_keymap[VK_NUMPAD1] = key_kp1;
100 m_keymap[VK_NUMPAD2] = key_kp2;
101 m_keymap[VK_NUMPAD3] = key_kp3;
102 m_keymap[VK_NUMPAD4] = key_kp4;
103 m_keymap[VK_NUMPAD5] = key_kp5;
104 m_keymap[VK_NUMPAD6] = key_kp6;
105 m_keymap[VK_NUMPAD7] = key_kp7;
106 m_keymap[VK_NUMPAD8] = key_kp8;
107 m_keymap[VK_NUMPAD9] = key_kp9;
108 m_keymap[VK_DECIMAL] = key_kp_period;
109 m_keymap[VK_DIVIDE] = key_kp_divide;
110 m_keymap[VK_MULTIPLY] = key_kp_multiply;
111 m_keymap[VK_SUBTRACT] = key_kp_minus;
112 m_keymap[VK_ADD] = key_kp_plus;
113
114 m_keymap[VK_UP] = key_up;
115 m_keymap[VK_DOWN] = key_down;
116 m_keymap[VK_RIGHT] = key_right;
117 m_keymap[VK_LEFT] = key_left;
118 m_keymap[VK_INSERT] = key_insert;
119 m_keymap[VK_DELETE] = key_delete;
120 m_keymap[VK_HOME] = key_home;
121 m_keymap[VK_END] = key_end;
122 m_keymap[VK_PRIOR] = key_page_up;
123 m_keymap[VK_NEXT] = key_page_down;
124
125 m_keymap[VK_F1] = key_f1;
126 m_keymap[VK_F2] = key_f2;
127 m_keymap[VK_F3] = key_f3;
128 m_keymap[VK_F4] = key_f4;
129 m_keymap[VK_F5] = key_f5;
130 m_keymap[VK_F6] = key_f6;
131 m_keymap[VK_F7] = key_f7;
132 m_keymap[VK_F8] = key_f8;
133 m_keymap[VK_F9] = key_f9;
134 m_keymap[VK_F10] = key_f10;
135 m_keymap[VK_F11] = key_f11;
136 m_keymap[VK_F12] = key_f12;
137 m_keymap[VK_F13] = key_f13;
138 m_keymap[VK_F14] = key_f14;
139 m_keymap[VK_F15] = key_f15;
140
141 m_keymap[VK_NUMLOCK] = key_numlock;
142 m_keymap[VK_CAPITAL] = key_capslock;
143 m_keymap[VK_SCROLL] = key_scrollock;
144
145
146 switch(m_format)
147 {
148 case pix_format_bw:
149 m_sys_format = pix_format_bw;
150 m_bpp = 1;
151 m_sys_bpp = 1;
152 break;
153
154 case pix_format_gray8:
155 case pix_format_sgray8:
156 m_sys_format = pix_format_sgray8;
157 m_bpp = 8;
158 m_sys_bpp = 8;
159 break;
160
161 case pix_format_gray16:
162 m_sys_format = pix_format_sgray8;
163 m_bpp = 16;
164 m_sys_bpp = 8;
165 break;
166
167 case pix_format_gray32:
168 m_sys_format = pix_format_sgray8;
169 m_bpp = 32;
170 m_sys_bpp = 8;
171 break;
172
173 case pix_format_rgb565:
174 case pix_format_rgb555:
175 m_sys_format = pix_format_rgb555;
176 m_bpp = 16;
177 m_sys_bpp = 16;
178 break;
179
180 case pix_format_rgbAAA:
181 case pix_format_bgrAAA:
182 case pix_format_rgbBBA:
183 case pix_format_bgrABB:
184 m_sys_format = pix_format_bgr24;
185 m_bpp = 32;
186 m_sys_bpp = 24;
187 break;
188
189 case pix_format_rgb24:
190 case pix_format_bgr24:
191 case pix_format_srgb24:
192 case pix_format_sbgr24:
193 m_sys_format = pix_format_sbgr24;
194 m_bpp = 24;
195 m_sys_bpp = 24;
196 break;
197
198 case pix_format_rgb48:
199 case pix_format_bgr48:
200 m_sys_format = pix_format_sbgr24;
201 m_bpp = 48;
202 m_sys_bpp = 24;
203 break;
204
205 case pix_format_rgb96:
206 case pix_format_bgr96:
207 m_sys_format = pix_format_sbgr24;
208 m_bpp = 96;
209 m_sys_bpp = 24;
210 break;
211
212 case pix_format_bgra32:
213 case pix_format_abgr32:
214 case pix_format_argb32:
215 case pix_format_rgba32:
216 case pix_format_sbgra32:
217 case pix_format_sabgr32:
218 case pix_format_sargb32:
219 case pix_format_srgba32:
220 m_sys_format = pix_format_sbgr24;
221 m_bpp = 32;
222 m_sys_bpp = 24;
223 break;
224
225 case pix_format_bgra64:
226 case pix_format_abgr64:
227 case pix_format_argb64:
228 case pix_format_rgba64:
229 m_sys_format = pix_format_sbgr24;
230 m_bpp = 64;
231 m_sys_bpp = 24;
232 break;
233
234 case pix_format_bgra128:
235 case pix_format_abgr128:
236 case pix_format_argb128:
237 case pix_format_rgba128:
238 m_sys_format = pix_format_sbgr24;
239 m_bpp = 128;
240 m_sys_bpp = 24;
241 break;
242
243 }
244 ::QueryPerformanceFrequency(&m_sw_freq);
245 ::QueryPerformanceCounter(&m_sw_start);
246 }
247
248
249 //------------------------------------------------------------------------
250 void platform_specific::create_pmap(unsigned width,
251 unsigned height,
252 rendering_buffer* wnd)
253 {
254 m_pmap_window.create(width, height, org_e(m_bpp));
255 wnd->attach(m_pmap_window.buf(),
256 m_pmap_window.width(),
257 m_pmap_window.height(),
258 m_flip_y ?
259 m_pmap_window.stride() :
260 -m_pmap_window.stride());
261 }
262
263
264 //------------------------------------------------------------------------
265 static void convert_pmap(rendering_buffer* dst,
266 const rendering_buffer* src,
267 pix_format_e format)
268 {
269 switch(format)
270 {
271 case pix_format_gray8:
272 convert<pixfmt_sgray8, pixfmt_gray8>(dst, src);
273 break;
274
275 case pix_format_gray16:
276 convert<pixfmt_sgray8, pixfmt_gray16>(dst, src);
277 break;
278
279 case pix_format_gray32:
280 convert<pixfmt_sgray8, pixfmt_gray32>(dst, src);
281 break;
282
283 case pix_format_rgb565:
284 color_conv(dst, src, color_conv_rgb565_to_rgb555());
285 break;
286
287 case pix_format_rgbAAA:
288 color_conv(dst, src, color_conv_rgbAAA_to_bgr24());
289 break;
290
291 case pix_format_bgrAAA:
292 color_conv(dst, src, color_conv_bgrAAA_to_bgr24());
293 break;
294
295 case pix_format_rgbBBA:
296 color_conv(dst, src, color_conv_rgbBBA_to_bgr24());
297 break;
298
299 case pix_format_bgrABB:
300 color_conv(dst, src, color_conv_bgrABB_to_bgr24());
301 break;
302
303 case pix_format_srgb24:
304 color_conv(dst, src, color_conv_rgb24_to_bgr24());
305 break;
306
307 case pix_format_rgb24:
308 convert<pixfmt_sbgr24, pixfmt_rgb24>(dst, src);
309 break;
310
311 case pix_format_bgr24:
312 convert<pixfmt_sbgr24, pixfmt_bgr24>(dst, src);
313 break;
314
315 case pix_format_rgb48:
316 convert<pixfmt_sbgr24, pixfmt_rgb48>(dst, src);
317 break;
318
319 case pix_format_bgr48:
320 convert<pixfmt_sbgr24, pixfmt_bgr48>(dst, src);
321 break;
322
323 case pix_format_bgra32:
324 convert<pixfmt_sbgr24, pixfmt_bgrx32>(dst, src);
325 break;
326
327 case pix_format_abgr32:
328 convert<pixfmt_sbgr24, pixfmt_xbgr32>(dst, src);
329 break;
330
331 case pix_format_argb32:
332 convert<pixfmt_sbgr24, pixfmt_xrgb32>(dst, src);
333 break;
334
335 case pix_format_rgba32:
336 convert<pixfmt_sbgr24, pixfmt_rgbx32>(dst, src);
337 break;
338
339 case pix_format_sbgra32:
340 convert<pixfmt_sbgr24, pixfmt_sbgrx32>(dst, src);
341 break;
342
343 case pix_format_sabgr32:
344 convert<pixfmt_sbgr24, pixfmt_sxbgr32>(dst, src);
345 break;
346
347 case pix_format_sargb32:
348 convert<pixfmt_sbgr24, pixfmt_sxrgb32>(dst, src);
349 break;
350
351 case pix_format_srgba32:
352 convert<pixfmt_sbgr24, pixfmt_srgbx32>(dst, src);
353 break;
354
355 case pix_format_bgra64:
356 convert<pixfmt_sbgr24, pixfmt_bgrx64>(dst, src);
357 break;
358
359 case pix_format_abgr64:
360 convert<pixfmt_sbgr24, pixfmt_xbgr64>(dst, src);
361 break;
362
363 case pix_format_argb64:
364 convert<pixfmt_sbgr24, pixfmt_xrgb64>(dst, src);
365 break;
366
367 case pix_format_rgba64:
368 convert<pixfmt_sbgr24, pixfmt_rgbx64>(dst, src);
369 break;
370
371 case pix_format_rgb96:
372 convert<pixfmt_sbgr24, pixfmt_rgb96>(dst, src);
373 break;
374
375 case pix_format_bgr96:
376 convert<pixfmt_sbgr24, pixfmt_bgr96>(dst, src);
377 break;
378
379 case pix_format_bgra128:
380 convert<pixfmt_sbgr24, pixfmt_bgrx128>(dst, src);
381 break;
382
383 case pix_format_abgr128:
384 convert<pixfmt_sbgr24, pixfmt_xbgr128>(dst, src);
385 break;
386
387 case pix_format_argb128:
388 convert<pixfmt_sbgr24, pixfmt_xrgb128>(dst, src);
389 break;
390
391 case pix_format_rgba128:
392 convert<pixfmt_sbgr24, pixfmt_rgbx128>(dst, src);
393 break;
394 }
395 }
396
397
398 //------------------------------------------------------------------------
399 void platform_specific::display_pmap(HDC dc, const rendering_buffer* src)
400 {
401 if(m_sys_format == m_format)
402 {
403 m_pmap_window.draw(dc);
404 }
405 else
406 {
407 pixel_map pmap_tmp;
408 pmap_tmp.create(m_pmap_window.width(),
409 m_pmap_window.height(),
410 org_e(m_sys_bpp));
411
412 rendering_buffer rbuf_tmp;
413 rbuf_tmp.attach(pmap_tmp.buf(),
414 pmap_tmp.width(),
415 pmap_tmp.height(),
416 m_flip_y ?
417 pmap_tmp.stride() :
418 -pmap_tmp.stride());
419
420 convert_pmap(&rbuf_tmp, src, m_format);
421 pmap_tmp.draw(dc);
422 }
423 }
424
425
426
427 //------------------------------------------------------------------------
428 bool platform_specific::save_pmap(const char* fn, unsigned idx,
429 const rendering_buffer* src)
430 {
431 if(m_sys_format == m_format)
432 {
433 return m_pmap_img[idx].save_as_bmp(fn);
434 }
435
436 pixel_map pmap_tmp;
437 pmap_tmp.create(m_pmap_img[idx].width(),
438 m_pmap_img[idx].height(),
439 org_e(m_sys_bpp));
440
441 rendering_buffer rbuf_tmp;
442 rbuf_tmp.attach(pmap_tmp.buf(),
443 pmap_tmp.width(),
444 pmap_tmp.height(),
445 m_flip_y ?
446 pmap_tmp.stride() :
447 -pmap_tmp.stride());
448
449 convert_pmap(&rbuf_tmp, src, m_format);
450 return pmap_tmp.save_as_bmp(fn);
451 }
452
453
454
455 //------------------------------------------------------------------------
456 bool platform_specific::load_pmap(const char* fn, unsigned idx,
457 rendering_buffer* dst)
458 {
459 pixel_map pmap_tmp;
460 if(!pmap_tmp.load_from_bmp(fn)) return false;
461
462 rendering_buffer rbuf_tmp;
463 rbuf_tmp.attach(pmap_tmp.buf(),
464 pmap_tmp.width(),
465 pmap_tmp.height(),
466 m_flip_y ?
467 pmap_tmp.stride() :
468 -pmap_tmp.stride());
469
470 m_pmap_img[idx].create(pmap_tmp.width(),
471 pmap_tmp.height(),
472 org_e(m_bpp),
473 0);
474
475 dst->attach(m_pmap_img[idx].buf(),
476 m_pmap_img[idx].width(),
477 m_pmap_img[idx].height(),
478 m_flip_y ?
479 m_pmap_img[idx].stride() :
480 -m_pmap_img[idx].stride());
481
482 switch(m_format)
483 {
484 case pix_format_sgray8:
485 switch(pmap_tmp.bpp())
486 {
487 //case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_gray8()); break;
488 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_gray8()); break;
489 //case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_gray8()); break;
490 }
491 break;
492
493 case pix_format_gray8:
494 switch(pmap_tmp.bpp())
495 {
496 case 24: convert<pixfmt_gray8, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
497 }
498 break;
499
500 case pix_format_gray16:
501 switch(pmap_tmp.bpp())
502 {
503 //case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_gray16()); break;
504 case 24: convert<pixfmt_gray16, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
505 //case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_gray16()); break;
506 }
507 break;
508
509 case pix_format_gray32:
510 switch(pmap_tmp.bpp())
511 {
512 //case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_gray32()); break;
513 case 24: convert<pixfmt_gray32, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
514 //case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_gray32()); break;
515 }
516 break;
517
518 case pix_format_rgb555:
519 switch(pmap_tmp.bpp())
520 {
521 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgb555()); break;
522 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_rgb555()); break;
523 case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgb555()); break;
524 }
525 break;
526
527 case pix_format_rgb565:
528 switch(pmap_tmp.bpp())
529 {
530 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgb565()); break;
531 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_rgb565()); break;
532 case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgb565()); break;
533 }
534 break;
535
536 case pix_format_srgb24:
537 switch(pmap_tmp.bpp())
538 {
539 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgb24()); break;
540 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_rgb24()); break;
541 case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgb24()); break;
542 }
543 break;
544
545 case pix_format_sbgr24:
546 switch(pmap_tmp.bpp())
547 {
548 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_bgr24()); break;
549 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_bgr24()); break;
550 case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_bgr24()); break;
551 }
552 break;
553
554 case pix_format_rgb24:
555 switch(pmap_tmp.bpp())
556 {
557 case 24: convert<pixfmt_rgb24, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
558 }
559 break;
560
561 case pix_format_bgr24:
562 switch(pmap_tmp.bpp())
563 {
564 case 24: convert<pixfmt_bgr24, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
565 }
566 break;
567
568 case pix_format_rgb48:
569 switch(pmap_tmp.bpp())
570 {
571 //case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgb48()); break;
572 case 24: convert<pixfmt_rgb48, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
573 //case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgb48()); break;
574 }
575 break;
576
577 case pix_format_bgr48:
578 switch(pmap_tmp.bpp())
579 {
580 //case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_bgr48()); break;
581 case 24: convert<pixfmt_bgr48, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
582 //case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_bgr48()); break;
583 }
584 break;
585
586 case pix_format_sabgr32:
587 switch(pmap_tmp.bpp())
588 {
589 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_abgr32()); break;
590 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_abgr32()); break;
591 case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_abgr32()); break;
592 }
593 break;
594
595 case pix_format_sargb32:
596 switch(pmap_tmp.bpp())
597 {
598 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_argb32()); break;
599 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_argb32()); break;
600 case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_argb32()); break;
601 }
602 break;
603
604 case pix_format_sbgra32:
605 switch(pmap_tmp.bpp())
606 {
607 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_bgra32()); break;
608 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_bgra32()); break;
609 case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_bgra32()); break;
610 }
611 break;
612
613 case pix_format_srgba32:
614 switch(pmap_tmp.bpp())
615 {
616 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgba32()); break;
617 case 24: color_conv(dst, &rbuf_tmp, color_conv_bgr24_to_rgba32()); break;
618 case 32: color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgba32()); break;
619 }
620 break;
621
622 case pix_format_abgr32:
623 switch(pmap_tmp.bpp())
624 {
625 case 24: convert<pixfmt_abgr32, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
626 }
627 break;
628
629 case pix_format_argb32:
630 switch(pmap_tmp.bpp())
631 {
632 case 24: convert<pixfmt_argb32, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
633 }
634 break;
635
636 case pix_format_bgra32:
637 switch(pmap_tmp.bpp())
638 {
639 case 24: convert<pixfmt_bgra32, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
640 }
641 break;
642
643 case pix_format_rgba32:
644 switch(pmap_tmp.bpp())
645 {
646 case 24: convert<pixfmt_rgba32, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
647 }
648 break;
649
650 case pix_format_abgr64:
651 switch(pmap_tmp.bpp())
652 {
653 case 24: convert<pixfmt_abgr64, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
654 }
655 break;
656
657 case pix_format_argb64:
658 switch(pmap_tmp.bpp())
659 {
660 case 24: convert<pixfmt_argb64, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
661 }
662 break;
663
664 case pix_format_bgra64:
665 switch(pmap_tmp.bpp())
666 {
667 case 24: convert<pixfmt_bgra64, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
668 }
669 break;
670
671 case pix_format_rgba64:
672 switch(pmap_tmp.bpp())
673 {
674 case 24: convert<pixfmt_rgba64, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
675 }
676 break;
677
678 case pix_format_rgb96:
679 switch(pmap_tmp.bpp())
680 {
681 case 24: convert<pixfmt_rgb96, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
682 }
683 break;
684
685 case pix_format_bgr96:
686 switch(pmap_tmp.bpp())
687 {
688 case 24: convert<pixfmt_bgr96, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
689 }
690 break;
691
692 case pix_format_abgr128:
693 switch(pmap_tmp.bpp())
694 {
695 case 24: convert<pixfmt_abgr128, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
696 }
697 break;
698
699 case pix_format_argb128:
700 switch(pmap_tmp.bpp())
701 {
702 case 24: convert<pixfmt_argb128, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
703 }
704 break;
705
706 case pix_format_bgra128:
707 switch(pmap_tmp.bpp())
708 {
709 case 24: convert<pixfmt_bgra128, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
710 }
711 break;
712
713 case pix_format_rgba128:
714 switch(pmap_tmp.bpp())
715 {
716 case 24: convert<pixfmt_rgba128, pixfmt_sbgr24>(dst, &rbuf_tmp); break;
717 }
718 break;
719 }
720
721 return true;
722 }
723
724
725
726
727
728
729
730
731 //------------------------------------------------------------------------
732 unsigned platform_specific::translate(unsigned keycode)
733 {
734 return m_last_translated_key = (keycode > 255) ? 0 : m_keymap[keycode];
735 }
736
737
738
739 //------------------------------------------------------------------------
740 platform_support::platform_support(pix_format_e format, bool flip_y) :
741 m_specific(new platform_specific(format, flip_y)),
742 m_format(format),
743 m_bpp(m_specific->m_bpp),
744 m_window_flags(0),
745 m_wait_mode(true),
746 m_flip_y(flip_y),
747 m_initial_width(10),
748 m_initial_height(10)
749 {
750 std::strcpy(m_caption, "Anti-Grain Geometry Application");
751 }
752
753
754 //------------------------------------------------------------------------
755 platform_support::~platform_support()
756 {
757 delete m_specific;
758 }
759
760
761
762 //------------------------------------------------------------------------
763 void platform_support::caption(const char* cap)
764 {
765 std::strcpy(m_caption, cap);
766 if(m_specific->m_hwnd)
767 {
768 SetWindowText(m_specific->m_hwnd, m_caption);
769 }
770 }
771
772 //------------------------------------------------------------------------
773 void platform_support::start_timer()
774 {
775 ::QueryPerformanceCounter(&(m_specific->m_sw_start));
776 }
777
778 //------------------------------------------------------------------------
779 double platform_support::elapsed_time() const
780 {
781 LARGE_INTEGER stop;
782 ::QueryPerformanceCounter(&stop);
783 return double(stop.QuadPart -
784 m_specific->m_sw_start.QuadPart) * 1000.0 /
785 double(m_specific->m_sw_freq.QuadPart);
786 }
787
788
789
790 //------------------------------------------------------------------------
791 static unsigned get_key_flags(int wflags)
792 {
793 unsigned flags = 0;
794 if(wflags & MK_LBUTTON) flags |= mouse_left;
795 if(wflags & MK_RBUTTON) flags |= mouse_right;
796 if(wflags & MK_SHIFT) flags |= kbd_shift;
797 if(wflags & MK_CONTROL) flags |= kbd_ctrl;
798 return flags;
799 }
800
801
802 void* platform_support::raw_display_handler()
803 {
804 return m_specific->m_current_dc;
805 }
806
807
808 //------------------------------------------------------------------------
809 LRESULT CALLBACK window_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
810 {
811 PAINTSTRUCT ps;
812 HDC paintDC;
813
814
815 void* user_data = reinterpret_cast<void*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
816 platform_support* app = 0;
817
818 if(user_data)
819 {
820 app = reinterpret_cast<platform_support*>(user_data);
821 }
822
823 if(app == 0)
824 {
825 if(msg == WM_DESTROY)
826 {
827 ::PostQuitMessage(0);
828 return 0;
829 }
830 return ::DefWindowProc(hWnd, msg, wParam, lParam);
831 }
832
833 HDC dc = ::GetDC(app->m_specific->m_hwnd);
834 app->m_specific->m_current_dc = dc;
835 LRESULT ret = 0;
836
837 switch(msg)
838 {
839 //--------------------------------------------------------------------
840 case WM_CREATE:
841 break;
842
843 //--------------------------------------------------------------------
844 case WM_SIZE:
845 app->m_specific->create_pmap(LOWORD(lParam),
846 HIWORD(lParam),
847 &app->rbuf_window());
848
849 app->trans_affine_resizing(LOWORD(lParam), HIWORD(lParam));
850 app->on_resize(LOWORD(lParam), HIWORD(lParam));
851 app->force_redraw();
852 break;
853
854 //--------------------------------------------------------------------
855 case WM_ERASEBKGND:
856 break;
857
858 //--------------------------------------------------------------------
859 case WM_LBUTTONDOWN:
860 ::SetCapture(app->m_specific->m_hwnd);
861 app->m_specific->m_cur_x = int16(LOWORD(lParam));
862 if(app->flip_y())
863 {
864 app->m_specific->m_cur_y = app->rbuf_window().height() - int16(HIWORD(lParam));
865 }
866 else
867 {
868 app->m_specific->m_cur_y = int16(HIWORD(lParam));
869 }
870 app->m_specific->m_input_flags = mouse_left | get_key_flags(wParam);
871
872 app->m_ctrls.set_cur(app->m_specific->m_cur_x,
873 app->m_specific->m_cur_y);
874 if(app->m_ctrls.on_mouse_button_down(app->m_specific->m_cur_x,
875 app->m_specific->m_cur_y))
876 {
877 app->on_ctrl_change();
878 app->force_redraw();
879 }
880 else
881 {
882 if(app->m_ctrls.in_rect(app->m_specific->m_cur_x,
883 app->m_specific->m_cur_y))
884 {
885 if(app->m_ctrls.set_cur(app->m_specific->m_cur_x,
886 app->m_specific->m_cur_y))
887 {
888 app->on_ctrl_change();
889 app->force_redraw();
890 }
891 }
892 else
893 {
894 app->on_mouse_button_down(app->m_specific->m_cur_x,
895 app->m_specific->m_cur_y,
896 app->m_specific->m_input_flags);
897 }
898 }
899 /*
900 if(!app->wait_mode())
901 {
902 app->on_idle();
903 }
904 */
905 break;
906
907 //--------------------------------------------------------------------
908 case WM_LBUTTONUP:
909 ::ReleaseCapture();
910 app->m_specific->m_cur_x = int16(LOWORD(lParam));
911 if(app->flip_y())
912 {
913 app->m_specific->m_cur_y = app->rbuf_window().height() - int16(HIWORD(lParam));
914 }
915 else
916 {
917 app->m_specific->m_cur_y = int16(HIWORD(lParam));
918 }
919 app->m_specific->m_input_flags = mouse_left | get_key_flags(wParam);
920
921 if(app->m_ctrls.on_mouse_button_up(app->m_specific->m_cur_x,
922 app->m_specific->m_cur_y))
923 {
924 app->on_ctrl_change();
925 app->force_redraw();
926 }
927 app->on_mouse_button_up(app->m_specific->m_cur_x,
928 app->m_specific->m_cur_y,
929 app->m_specific->m_input_flags);
930 /*
931 if(!app->wait_mode())
932 {
933 app->on_idle();
934 }
935 */
936 break;
937
938
939 //--------------------------------------------------------------------
940 case WM_RBUTTONDOWN:
941 ::SetCapture(app->m_specific->m_hwnd);
942 app->m_specific->m_cur_x = int16(LOWORD(lParam));
943 if(app->flip_y())
944 {
945 app->m_specific->m_cur_y = app->rbuf_window().height() - int16(HIWORD(lParam));
946 }
947 else
948 {
949 app->m_specific->m_cur_y = int16(HIWORD(lParam));
950 }
951 app->m_specific->m_input_flags = mouse_right | get_key_flags(wParam);
952 app->on_mouse_button_down(app->m_specific->m_cur_x,
953 app->m_specific->m_cur_y,
954 app->m_specific->m_input_flags);
955 /*
956 if(!app->wait_mode())
957 {
958 app->on_idle();
959 }
960 */
961 break;
962
963 //--------------------------------------------------------------------
964 case WM_RBUTTONUP:
965 ::ReleaseCapture();
966 app->m_specific->m_cur_x = int16(LOWORD(lParam));
967 if(app->flip_y())
968 {
969 app->m_specific->m_cur_y = app->rbuf_window().height() - int16(HIWORD(lParam));
970 }
971 else
972 {
973 app->m_specific->m_cur_y = int16(HIWORD(lParam));
974 }
975 app->m_specific->m_input_flags = mouse_right | get_key_flags(wParam);
976 app->on_mouse_button_up(app->m_specific->m_cur_x,
977 app->m_specific->m_cur_y,
978 app->m_specific->m_input_flags);
979 /*
980 if(!app->wait_mode())
981 {
982 app->on_idle();
983 }
984 */
985 break;
986
987 //--------------------------------------------------------------------
988 case WM_MOUSEMOVE:
989 app->m_specific->m_cur_x = int16(LOWORD(lParam));
990 if(app->flip_y())
991 {
992 app->m_specific->m_cur_y = app->rbuf_window().height() - int16(HIWORD(lParam));
993 }
994 else
995 {
996 app->m_specific->m_cur_y = int16(HIWORD(lParam));
997 }
998 app->m_specific->m_input_flags = get_key_flags(wParam);
999
1000
1001 if(app->m_ctrls.on_mouse_move(
1002 app->m_specific->m_cur_x,
1003 app->m_specific->m_cur_y,
1004 (app->m_specific->m_input_flags & mouse_left) != 0))
1005 {
1006 app->on_ctrl_change();
1007 app->force_redraw();
1008 }
1009 else
1010 {
1011 if(!app->m_ctrls.in_rect(app->m_specific->m_cur_x,
1012 app->m_specific->m_cur_y))
1013 {
1014 app->on_mouse_move(app->m_specific->m_cur_x,
1015 app->m_specific->m_cur_y,
1016 app->m_specific->m_input_flags);
1017 }
1018 }
1019 /*
1020 if(!app->wait_mode())
1021 {
1022 app->on_idle();
1023 }
1024 */
1025 break;
1026
1027 //--------------------------------------------------------------------
1028 case WM_SYSKEYDOWN:
1029 case WM_KEYDOWN:
1030 app->m_specific->m_last_translated_key = 0;
1031 switch(wParam)
1032 {
1033 case VK_CONTROL:
1034 app->m_specific->m_input_flags |= kbd_ctrl;
1035 break;
1036
1037 case VK_SHIFT:
1038 app->m_specific->m_input_flags |= kbd_shift;
1039 break;
1040
1041 default:
1042 app->m_specific->translate(wParam);
1043 break;
1044 }
1045
1046 if(app->m_specific->m_last_translated_key)
1047 {
1048 bool left = false;
1049 bool up = false;
1050 bool right = false;
1051 bool down = false;
1052
1053 switch(app->m_specific->m_last_translated_key)
1054 {
1055 case key_left:
1056 left = true;
1057 break;
1058
1059 case key_up:
1060 up = true;
1061 break;
1062
1063 case key_right:
1064 right = true;
1065 break;
1066
1067 case key_down:
1068 down = true;
1069 break;
1070
1071 case key_f2:
1072 app->copy_window_to_img(agg::platform_support::max_images - 1);
1073 app->save_img(agg::platform_support::max_images - 1, "screenshot");
1074 break;
1075 }
1076
1077 if(app->window_flags() & window_process_all_keys)
1078 {
1079 app->on_key(app->m_specific->m_cur_x,
1080 app->m_specific->m_cur_y,
1081 app->m_specific->m_last_translated_key,
1082 app->m_specific->m_input_flags);
1083 }
1084 else
1085 {
1086 if(app->m_ctrls.on_arrow_keys(left, right, down, up))
1087 {
1088 app->on_ctrl_change();
1089 app->force_redraw();
1090 }
1091 else
1092 {
1093 app->on_key(app->m_specific->m_cur_x,
1094 app->m_specific->m_cur_y,
1095 app->m_specific->m_last_translated_key,
1096 app->m_specific->m_input_flags);
1097 }
1098 }
1099 }
1100 /*
1101 if(!app->wait_mode())
1102 {
1103 app->on_idle();
1104 }
1105 */
1106 break;
1107
1108 //--------------------------------------------------------------------
1109 case WM_SYSKEYUP:
1110 case WM_KEYUP:
1111 app->m_specific->m_last_translated_key = 0;
1112 switch(wParam)
1113 {
1114 case VK_CONTROL:
1115 app->m_specific->m_input_flags &= ~kbd_ctrl;
1116 break;
1117
1118 case VK_SHIFT:
1119 app->m_specific->m_input_flags &= ~kbd_shift;
1120 break;
1121 }
1122 break;
1123
1124 //--------------------------------------------------------------------
1125 case WM_CHAR:
1126 case WM_SYSCHAR:
1127 if(app->m_specific->m_last_translated_key == 0)
1128 {
1129 app->on_key(app->m_specific->m_cur_x,
1130 app->m_specific->m_cur_y,
1131 wParam,
1132 app->m_specific->m_input_flags);
1133 }
1134 break;
1135
1136 //--------------------------------------------------------------------
1137 case WM_PAINT:
1138 paintDC = ::BeginPaint(hWnd, &ps);
1139 app->m_specific->m_current_dc = paintDC;
1140 if(app->m_specific->m_redraw_flag)
1141 {
1142 app->on_draw();
1143 app->m_specific->m_redraw_flag = false;
1144 }
1145 app->m_specific->display_pmap(paintDC, &app->rbuf_window());
1146 app->on_post_draw(paintDC);
1147 app->m_specific->m_current_dc = 0;
1148 ::EndPaint(hWnd, &ps);
1149 break;
1150
1151 //--------------------------------------------------------------------
1152 case WM_COMMAND:
1153 break;
1154
1155 //--------------------------------------------------------------------
1156 case WM_DESTROY:
1157 ::PostQuitMessage(0);
1158 break;
1159
1160 //--------------------------------------------------------------------
1161 default:
1162 ret = ::DefWindowProc(hWnd, msg, wParam, lParam);
1163 break;
1164 }
1165 app->m_specific->m_current_dc = 0;
1166 ::ReleaseDC(app->m_specific->m_hwnd, dc);
1167 return ret;
1168 }
1169
1170
1171 //------------------------------------------------------------------------
1172 void platform_support::message(const char* msg)
1173 {
1174 ::MessageBox(m_specific->m_hwnd, msg, "AGG Message", MB_OK);
1175 }
1176
1177
1178 //------------------------------------------------------------------------
1179 bool platform_support::init(unsigned width, unsigned height, unsigned flags)
1180 {
1181 if(m_specific->m_sys_format == pix_format_undefined)
1182 {
1183 return false;
1184 }
1185
1186 m_window_flags = flags;
1187
1188 int wflags = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
1189
1190 WNDCLASS wc;
1191 wc.lpszClassName = "AGGAppClass";
1192 wc.lpfnWndProc = window_proc;
1193 wc.style = wflags;
1194 wc.hInstance = g_windows_instance;
1195 wc.hIcon = LoadIcon(0, IDI_APPLICATION);
1196 wc.hCursor = LoadCursor(0, IDC_ARROW);
1197 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1198 wc.lpszMenuName = "AGGAppMenu";
1199 wc.cbClsExtra = 0;
1200 wc.cbWndExtra = 0;
1201 ::RegisterClass(&wc);
1202
1203 wflags = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
1204
1205 if(m_window_flags & window_resize)
1206 {
1207 wflags |= WS_THICKFRAME | WS_MAXIMIZEBOX;
1208 }
1209
1210 m_specific->m_hwnd = ::CreateWindow("AGGAppClass",
1211 m_caption,
1212 wflags,
1213 100,
1214 100,
1215 width,
1216 height,
1217 0,
1218 0,
1219 g_windows_instance,
1220 0);
1221
1222 if(m_specific->m_hwnd == 0)
1223 {
1224 return false;
1225 }
1226
1227
1228 RECT rct;
1229 ::GetClientRect(m_specific->m_hwnd, &rct);
1230
1231 ::MoveWindow(m_specific->m_hwnd, // handle to window
1232 100, // horizontal position
1233 100, // vertical position
1234 width + (width - (rct.right - rct.left)),
1235 height + (height - (rct.bottom - rct.top)),
1236 FALSE);
1237
1238 ::SetWindowLongPtr(m_specific->m_hwnd, GWLP_USERDATA, (LONG)this);
1239 m_specific->create_pmap(width, height, &m_rbuf_window);
1240 m_initial_width = width;
1241 m_initial_height = height;
1242 on_init();
1243 m_specific->m_redraw_flag = true;
1244 ::ShowWindow(m_specific->m_hwnd, g_windows_cmd_show);
1245 return true;
1246 }
1247
1248
1249
1250 //------------------------------------------------------------------------
1251 int platform_support::run()
1252 {
1253 MSG msg;
1254
1255 for(;;)
1256 {
1257 if(m_wait_mode)
1258 {
1259 if(!::GetMessage(&msg, 0, 0, 0))
1260 {
1261 break;
1262 }
1263 ::TranslateMessage(&msg);
1264 ::DispatchMessage(&msg);
1265 }
1266 else
1267 {
1268 if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
1269 {
1270 ::TranslateMessage(&msg);
1271 if(msg.message == WM_QUIT)
1272 {
1273 break;
1274 }
1275 ::DispatchMessage(&msg);
1276 }
1277 else
1278 {
1279 on_idle();
1280 }
1281 }
1282 }
1283 return (int)msg.wParam;
1284 }
1285
1286
1287 //------------------------------------------------------------------------
1288 const char* platform_support::img_ext() const { return ".bmp"; }
1289
1290
1291 //------------------------------------------------------------------------
1292 const char* platform_support::full_file_name(const char* file_name)
1293 {
1294 return file_name;
1295 }
1296
1297 //------------------------------------------------------------------------
1298 bool platform_support::load_img(unsigned idx, const char* file)
1299 {
1300 if(idx < max_images)
1301 {
1302 char fn[1024];
1303 std::strcpy(fn, file);
1304 int len = std::strlen(fn);
1305 if(len < 4 || _stricmp(fn + len - 4, ".BMP") != 0)
1306 {
1307 std::strcat(fn, ".bmp");
1308 }
1309 return m_specific->load_pmap(fn, idx, &m_rbuf_img[idx]);
1310 }
1311 return true;
1312 }
1313
1314
1315
1316 //------------------------------------------------------------------------
1317 bool platform_support::save_img(unsigned idx, const char* file)
1318 {
1319 if(idx < max_images)
1320 {
1321 char fn[1024];
1322 std::strcpy(fn, file);
1323 int len = std::strlen(fn);
1324 if(len < 4 || _stricmp(fn + len - 4, ".BMP") != 0)
1325 {
1326 std::strcat(fn, ".bmp");
1327 }
1328 return m_specific->save_pmap(fn, idx, &m_rbuf_img[idx]);
1329 }
1330 return true;
1331 }
1332
1333
1334
1335 //------------------------------------------------------------------------
1336 bool platform_support::create_img(unsigned idx, unsigned width, unsigned height)
1337 {
1338 if(idx < max_images)
1339 {
1340 if(width == 0) width = m_specific->m_pmap_window.width();
1341 if(height == 0) height = m_specific->m_pmap_window.height();
1342 m_specific->m_pmap_img[idx].create(width, height, org_e(m_specific->m_bpp));
1343 m_rbuf_img[idx].attach(m_specific->m_pmap_img[idx].buf(),
1344 m_specific->m_pmap_img[idx].width(),
1345 m_specific->m_pmap_img[idx].height(),
1346 m_flip_y ?
1347 m_specific->m_pmap_img[idx].stride() :
1348 -m_specific->m_pmap_img[idx].stride());
1349 return true;
1350 }
1351 return false;
1352 }
1353
1354
1355 //------------------------------------------------------------------------
1356 void platform_support::force_redraw()
1357 {
1358 m_specific->m_redraw_flag = true;
1359 ::InvalidateRect(m_specific->m_hwnd, 0, FALSE);
1360 }
1361
1362
1363
1364 //------------------------------------------------------------------------
1365 void platform_support::update_window()
1366 {
1367 HDC dc = ::GetDC(m_specific->m_hwnd);
1368 m_specific->display_pmap(dc, &m_rbuf_window);
1369 ::ReleaseDC(m_specific->m_hwnd, dc);
1370 }
1371
1372
1373 //------------------------------------------------------------------------
1374 void platform_support::on_init() {}
1375 void platform_support::on_resize(int sx, int sy) {}
1376 void platform_support::on_idle() {}
1377 void platform_support::on_mouse_move(int x, int y, unsigned flags) {}
1378 void platform_support::on_mouse_button_down(int x, int y, unsigned flags) {}
1379 void platform_support::on_mouse_button_up(int x, int y, unsigned flags) {}
1380 void platform_support::on_key(int x, int y, unsigned key, unsigned flags) {}
1381 void platform_support::on_ctrl_change() {}
1382 void platform_support::on_draw() {}
1383 void platform_support::on_post_draw(void* raw_handler) {}
1384 }
1385
1386
1387
1388
1389 namespace agg
1390 {
1391 // That's ridiculous. I have to parse the command line by myself
1392 // because Windows doesn't provide a method of getting the command
1393 // line arguments in a form of argc, argv. Of course, there's
1394 // CommandLineToArgv() but first, it returns Unicode that I don't
1395 // need to deal with, but most of all, it's not compatible with Win98.
1396 //-----------------------------------------------------------------------
1397 class tokenizer
1398 {
1399 public:
1400 enum sep_flag
1401 {
1402 single,
1403 multiple,
1404 whole_str
1405 };
1406
1407 struct token
1408 {
1409 const char* ptr;
1410 unsigned len;
1411 };
1412
1413 public:
1414 tokenizer(const char* sep,
1415 const char* trim=0,
1416 const char* quote="\"",
1417 char mask_chr='\\',
1418 sep_flag sf=multiple);
1419
1420 void set_str(const char* str);
1421 token next_token();
1422
1423 private:
1424 int check_chr(const char *str, char chr);
1425
1426 private:
1427 const char* m_src_string;
1428 int m_start;
1429 const char* m_sep;
1430 const char* m_trim;
1431 const char* m_quote;
1432 char m_mask_chr;
1433 unsigned m_sep_len;
1434 sep_flag m_sep_flag;
1435 };
1436
1437
1438
1439 //-----------------------------------------------------------------------
1440 inline void tokenizer::set_str(const char* str)
1441 {
1442 m_src_string = str;
1443 m_start = 0;
1444 }
1445
1446
1447 //-----------------------------------------------------------------------
1448 inline int tokenizer::check_chr(const char *str, char chr)
1449 {
1450 return int(strchr(str, chr));
1451 }
1452
1453
1454 //-----------------------------------------------------------------------
1455 tokenizer::tokenizer(const char* sep,
1456 const char* trim,
1457 const char* quote,
1458 char mask_chr,
1459 sep_flag sf) :
1460 m_src_string(0),
1461 m_start(0),
1462 m_sep(sep),
1463 m_trim(trim),
1464 m_quote(quote),
1465 m_mask_chr(mask_chr),
1466 m_sep_len(sep ? strlen(sep) : 0),
1467 m_sep_flag(sep ? sf : single)
1468 {
1469 }
1470
1471
1472 //-----------------------------------------------------------------------
1473 tokenizer::token tokenizer::next_token()
1474 {
1475 unsigned count = 0;
1476 char quote_chr = 0;
1477 token tok;
1478
1479 tok.ptr = 0;
1480 tok.len = 0;
1481 if(m_src_string == 0 || m_start == -1) return tok;
1482
1483 const char *pstr = m_src_string + m_start;
1484
1485 if(*pstr == 0)
1486 {
1487 m_start = -1;
1488 return tok;
1489 }
1490
1491 int sep_len = 1;
1492 if(m_sep_flag == whole_str) sep_len = m_sep_len;
1493
1494 if(m_sep_flag == multiple)
1495 {
1496 //Pass all the separator symbols at the begin of the string
1497 while(*pstr && check_chr(m_sep, *pstr))
1498 {
1499 ++pstr;
1500 ++m_start;
1501 }
1502 }
1503
1504 if(*pstr == 0)
1505 {
1506 m_start = -1;
1507 return tok;
1508 }
1509
1510 for(count = 0;; ++count)
1511 {
1512 char c = *pstr;
1513 int found = 0;
1514
1515 //We are outside of quotation: find one of separator symbols
1516 if(quote_chr == 0)
1517 {
1518 if(sep_len == 1)
1519 {
1520 found = check_chr(m_sep, c);
1521 }
1522 else
1523 {
1524 found = std::strncmp(m_sep, pstr, m_sep_len) == 0;
1525 }
1526 }
1527
1528 ++pstr;
1529
1530 if(c == 0 || found)
1531 {
1532 if(m_trim)
1533 {
1534 while(count &&
1535 check_chr(m_trim, m_src_string[m_start]))
1536 {
1537 ++m_start;
1538 --count;
1539 }
1540
1541 while(count &&
1542 check_chr(m_trim, m_src_string[m_start + count - 1]))
1543 {
1544 --count;
1545 }
1546 }
1547
1548 tok.ptr = m_src_string + m_start;
1549 tok.len = count;
1550
1551 //Next time it will be the next separator character
1552 //But we must check, whether it is NOT the end of the string.
1553 m_start += count;
1554 if(c)
1555 {
1556 m_start += sep_len;
1557 if(m_sep_flag == multiple)
1558 {
1559 //Pass all the separator symbols
1560 //after the end of the string
1561 while(check_chr(m_sep, m_src_string[m_start]))
1562 {
1563 ++m_start;
1564 }
1565 }
1566 }
1567 break;
1568 }
1569
1570 //Switch quote. If it is not a quote yet, try to check any of
1571 //quote symbols. Otherwise quote must be finished with quote_symb
1572 if(quote_chr == 0)
1573 {
1574 if(check_chr(m_quote, c))
1575 {
1576 quote_chr = c;
1577 continue;
1578 }
1579 }
1580 else
1581 {
1582 //We are inside quote: pass all the mask symbols
1583 if(m_mask_chr && c == m_mask_chr)
1584 {
1585 if(*pstr)
1586 {
1587 ++count;
1588 ++pstr;
1589 }
1590 continue;
1591 }
1592 if(c == quote_chr)
1593 {
1594 quote_chr = 0;
1595 continue;
1596 }
1597 }
1598 }
1599 return tok;
1600 }
1601
1602
1603 }
1604
1605
1606
1607 //----------------------------------------------------------------------------
1608 int agg_main(int argc, char* argv[]);
1609
1610
1611
1612 //----------------------------------------------------------------------------
1613 int PASCAL WinMain(HINSTANCE hInstance,
1614 HINSTANCE hPrevInstance,
1615 LPSTR lpszCmdLine,
1616 int nCmdShow)
1617 {
1618 agg::g_windows_instance = hInstance;
1619 agg::g_windows_cmd_show = nCmdShow;
1620
1621 char* argv_str = new char [std::strlen(lpszCmdLine) + 3];
1622 char* argv_ptr = argv_str;
1623
1624 char* argv[64];
1625 std::memset(argv, 0, sizeof(argv));
1626
1627 agg::tokenizer cmd_line(" ", "\"' ", "\"'", '\\', agg::tokenizer::multiple);
1628 cmd_line.set_str(lpszCmdLine);
1629
1630 int argc = 0;
1631 argv[argc++] = argv_ptr;
1632 *argv_ptr++ = 0;
1633
1634 while(argc < 64)
1635 {
1636 agg::tokenizer::token tok = cmd_line.next_token();
1637 if(tok.ptr == 0) break;
1638 if(tok.len)
1639 {
1640 std::memcpy(argv_ptr, tok.ptr, tok.len);
1641 argv[argc++] = argv_ptr;
1642 argv_ptr += tok.len;
1643 *argv_ptr++ = 0;
1644 }
1645 }
1646
1647 int ret = agg_main(argc, argv);
1648 delete [] argv_str;
1649
1650 return ret;
1651 }
1652
1653
1654
1655