Debian Codegraph @

src/platform/sdl/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. SDL version.
17 //
18 //----------------------------------------------------------------------------
19
20 #include <cstring>
21 #include <cstdio>
22 #include "platform/agg_platform_support.h"
23 #include "SDL.h"
24 #include "SDL_byteorder.h"
25
26
27 namespace agg
28 {
29
30 //------------------------------------------------------------------------
31 class platform_specific
32 {
33 public:
34 platform_specific(pix_format_e format, bool flip_y);
35 ~platform_specific();
36
37 pix_format_e m_format;
38 pix_format_e m_sys_format;
39 bool m_flip_y;
40 unsigned m_bpp;
41 unsigned m_sys_bpp;
42 unsigned m_rmask;
43 unsigned m_gmask;
44 unsigned m_bmask;
45 unsigned m_amask;
46 bool m_update_flag;
47 bool m_resize_flag;
48 bool m_initialized;
49 SDL_Surface* m_surf_screen;
50 SDL_Surface* m_surf_window;
51 SDL_Surface* m_surf_img[platform_support::max_images];
52 int m_cur_x;
53 int m_cur_y;
54 int m_sw_start;
55 };
56
57
58
59 //------------------------------------------------------------------------
60 platform_specific::platform_specific(pix_format_e format, bool flip_y) :
61 m_format(format),
62 m_sys_format(pix_format_undefined),
63 m_flip_y(flip_y),
64 m_bpp(0),
65 m_sys_bpp(0),
66 m_update_flag(true),
67 m_resize_flag(true),
68 m_initialized(false),
69 m_surf_screen(0),
70 m_surf_window(0),
71 m_cur_x(0),
72 m_cur_y(0)
73 {
74 std::memset(m_surf_img, 0, sizeof(m_surf_img));
75
76 switch(m_format)
77 {
78 case pix_format_gray8:
79 m_bpp = 8;
80 break;
81
82 case pix_format_rgb565:
83 m_rmask = 0xF800;
84 m_gmask = 0x7E0;
85 m_bmask = 0x1F;
86 m_amask = 0;
87 m_bpp = 16;
88 break;
89
90 case pix_format_rgb555:
91 m_rmask = 0x7C00;
92 m_gmask = 0x3E0;
93 m_bmask = 0x1F;
94 m_amask = 0;
95 m_bpp = 16;
96 break;
97
98 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
99 case pix_format_rgb24:
100 m_rmask = 0xFF;
101 m_gmask = 0xFF00;
102 m_bmask = 0xFF0000;
103 m_amask = 0;
104 m_bpp = 24;
105 break;
106
107 case pix_format_bgr24:
108 m_rmask = 0xFF0000;
109 m_gmask = 0xFF00;
110 m_bmask = 0xFF;
111 m_amask = 0;
112 m_bpp = 24;
113 break;
114
115 case pix_format_bgra32:
116 m_rmask = 0xFF0000;
117 m_gmask = 0xFF00;
118 m_bmask = 0xFF;
119 m_amask = 0xFF000000;
120 m_bpp = 32;
121 break;
122
123 case pix_format_abgr32:
124 m_rmask = 0xFF000000;
125 m_gmask = 0xFF0000;
126 m_bmask = 0xFF00;
127 m_amask = 0xFF;
128 m_bpp = 32;
129 break;
130
131 case pix_format_argb32:
132 m_rmask = 0xFF00;
133 m_gmask = 0xFF0000;
134 m_bmask = 0xFF000000;
135 m_amask = 0xFF;
136 m_bpp = 32;
137 break;
138
139 case pix_format_rgba32:
140 m_rmask = 0xFF;
141 m_gmask = 0xFF00;
142 m_bmask = 0xFF0000;
143 m_amask = 0xFF000000;
144 m_bpp = 32;
145 break;
146 #else //SDL_BIG_ENDIAN (PPC)
147 case pix_format_rgb24:
148 m_rmask = 0xFF0000;
149 m_gmask = 0xFF00;
150 m_bmask = 0xFF;
151 m_amask = 0;
152 m_bpp = 24;
153 break;
154
155 case pix_format_bgr24:
156 m_rmask = 0xFF;
157 m_gmask = 0xFF00;
158 m_bmask = 0xFF0000;
159 m_amask = 0;
160 m_bpp = 24;
161 break;
162
163 case pix_format_bgra32:
164 m_rmask = 0xFF00;
165 m_gmask = 0xFF0000;
166 m_bmask = 0xFF000000;
167 m_amask = 0xFF;
168 m_bpp = 32;
169 break;
170
171 case pix_format_abgr32:
172 m_rmask = 0xFF;
173 m_gmask = 0xFF00;
174 m_bmask = 0xFF0000;
175 m_amask = 0xFF000000;
176 m_bpp = 32;
177 break;
178
179 case pix_format_argb32:
180 m_rmask = 0xFF0000;
181 m_gmask = 0xFF00;
182 m_bmask = 0xFF;
183 m_amask = 0xFF000000;
184 m_bpp = 32;
185 break;
186
187 case pix_format_rgba32:
188 m_rmask = 0xFF000000;
189 m_gmask = 0xFF0000;
190 m_bmask = 0xFF00;
191 m_amask = 0xFF;
192 m_bpp = 32;
193 break;
194 #endif
195 default:
196 break;
197 }
198 }
199
200 //------------------------------------------------------------------------
201 platform_specific::~platform_specific()
202 {
203 int i;
204 for(i = platform_support::max_images - 1; i >= 0; --i)
205 {
206 if(m_surf_img[i]) SDL_FreeSurface(m_surf_img[i]);
207 }
208 if(m_surf_window) SDL_FreeSurface(m_surf_window);
209 if(m_surf_screen) SDL_FreeSurface(m_surf_screen);
210 }
211
212
213
214 //------------------------------------------------------------------------
215 platform_support::platform_support(pix_format_e format, bool flip_y) :
216 m_specific(new platform_specific(format, flip_y)),
217 m_format(format),
218 m_bpp(m_specific->m_bpp),
219 m_window_flags(0),
220 m_wait_mode(true),
221 m_flip_y(flip_y)
222 {
223 SDL_Init(SDL_INIT_VIDEO);
224 std::strcpy(m_caption, "Anti-Grain Geometry Application");
225 }
226
227
228 //------------------------------------------------------------------------
229 platform_support::~platform_support()
230 {
231 delete m_specific;
232 }
233
234
235
236 //------------------------------------------------------------------------
237 void platform_support::caption(const char* cap)
238 {
239 std::strcpy(m_caption, cap);
240 if(m_specific->m_initialized)
241 {
242 SDL_WM_SetCaption(cap, 0);
243 }
244 }
245
246
247
248
249
250 //------------------------------------------------------------------------
251 bool platform_support::init(unsigned width, unsigned height, unsigned flags)
252 {
253 m_window_flags = flags;
254 unsigned wflags = SDL_SWSURFACE;
255
256 if(m_window_flags & window_hw_buffer)
257 {
258 wflags = SDL_HWSURFACE;
259 }
260
261 if(m_window_flags & window_resize)
262 {
263 wflags |= SDL_RESIZABLE;
264 }
265
266 if(m_specific->m_surf_screen) SDL_FreeSurface(m_specific->m_surf_screen);
267
268 m_specific->m_surf_screen = SDL_SetVideoMode(width, height, m_bpp, wflags);
269 if(m_specific->m_surf_screen == 0)
270 {
271 std::fprintf(stderr,
272 "Unable to set %dx%d %d bpp video: %s\n",
273 width,
274 height,
275 m_bpp,
276 ::SDL_GetError());
277 return false;
278 }
279
280 SDL_WM_SetCaption(m_caption, 0);
281
282 if(m_specific->m_surf_window) SDL_FreeSurface(m_specific->m_surf_window);
283
284 m_specific->m_surf_window =
285 SDL_CreateRGBSurface(SDL_HWSURFACE,
286 m_specific->m_surf_screen->w,
287 m_specific->m_surf_screen->h,
288 m_specific->m_surf_screen->format->BitsPerPixel,
289 m_specific->m_rmask,
290 m_specific->m_gmask,
291 m_specific->m_bmask,
292 m_specific->m_amask);
293
294 if(m_specific->m_surf_window == 0)
295 {
296 std::fprintf(stderr,
297 "Unable to create image buffer %dx%d %d bpp: %s\n",
298 width,
299 height,
300 m_bpp,
301 SDL_GetError());
302 return false;
303 }
304
305 m_rbuf_window.attach((unsigned char*)m_specific->m_surf_window->pixels,
306 m_specific->m_surf_window->w,
307 m_specific->m_surf_window->h,
308 m_flip_y ? -m_specific->m_surf_window->pitch :
309 m_specific->m_surf_window->pitch);
310
311 if(!m_specific->m_initialized)
312 {
313 m_initial_width = width;
314 m_initial_height = height;
315 on_init();
316 m_specific->m_initialized = true;
317 }
318 on_resize(m_rbuf_window.width(), m_rbuf_window.height());
319 m_specific->m_update_flag = true;
320 return true;
321 }
322
323
324
325 //------------------------------------------------------------------------
326 void platform_support::update_window()
327 {
328 SDL_BlitSurface(m_specific->m_surf_window, 0, m_specific->m_surf_screen, 0);
329 SDL_UpdateRect(m_specific->m_surf_screen, 0, 0, 0, 0);
330 }
331
332
333 //------------------------------------------------------------------------
334 int platform_support::run()
335 {
336 SDL_Event event;
337 bool ev_flag = false;
338
339 for(;;)
340 {
341 if(m_specific->m_update_flag)
342 {
343 on_draw();
344 update_window();
345 m_specific->m_update_flag = false;
346 }
347
348 ev_flag = false;
349 if(m_wait_mode)
350 {
351 SDL_WaitEvent(&event);
352 ev_flag = true;
353 }
354 else
355 {
356 if(SDL_PollEvent(&event))
357 {
358 ev_flag = true;
359 }
360 else
361 {
362 on_idle();
363 }
364 }
365
366 if(ev_flag)
367 {
368 if(event.type == SDL_QUIT)
369 {
370 break;
371 }
372
373 int y;
374 unsigned flags = 0;
375
376 switch (event.type)
377 {
378 case SDL_VIDEORESIZE:
379 if(!init(event.resize.w, event.resize.h, m_window_flags)) return false;
380 on_resize(m_rbuf_window.width(), m_rbuf_window.height());
381 trans_affine_resizing(event.resize.w, event.resize.h);
382 m_specific->m_update_flag = true;
383 break;
384
385 case SDL_KEYDOWN:
386 {
387 flags = 0;
388 if(event.key.keysym.mod & KMOD_SHIFT) flags |= kbd_shift;
389 if(event.key.keysym.mod & KMOD_CTRL) flags |= kbd_ctrl;
390
391 bool left = false;
392 bool up = false;
393 bool right = false;
394 bool down = false;
395
396 switch(event.key.keysym.sym)
397 {
398 case key_left:
399 left = true;
400 break;
401
402 case key_up:
403 up = true;
404 break;
405
406 case key_right:
407 right = true;
408 break;
409
410 case key_down:
411 down = true;
412 break;
413 default:
414 break;
415 }
416
417 if(m_ctrls.on_arrow_keys(left, right, down, up))
418 {
419 on_ctrl_change();
420 force_redraw();
421 }
422 else
423 {
424 on_key(m_specific->m_cur_x,
425 m_specific->m_cur_y,
426 event.key.keysym.sym,
427 flags);
428 }
429 }
430 break;
431
432 case SDL_MOUSEMOTION:
433 y = m_flip_y ?
434 m_rbuf_window.height() - event.motion.y :
435 event.motion.y;
436
437 m_specific->m_cur_x = event.motion.x;
438 m_specific->m_cur_y = y;
439 flags = 0;
440 if(event.motion.state & SDL_BUTTON_LMASK) flags |= mouse_left;
441 if(event.motion.state & SDL_BUTTON_RMASK) flags |= mouse_right;
442
443 if(m_ctrls.on_mouse_move(m_specific->m_cur_x,
444 m_specific->m_cur_y,
445 (flags & mouse_left) != 0))
446 {
447 on_ctrl_change();
448 force_redraw();
449 }
450 else
451 {
452 on_mouse_move(m_specific->m_cur_x,
453 m_specific->m_cur_y,
454 flags);
455 }
456 SDL_Event eventtrash;
457 while (SDL_PeepEvents(&eventtrash, 1, SDL_GETEVENT, SDL_EVENTMASK(SDL_MOUSEMOTION))!=0){;}
458 break;
459
460 case SDL_MOUSEBUTTONDOWN:
461 y = m_flip_y
462 ? m_rbuf_window.height() - event.button.y
463 : event.button.y;
464
465 m_specific->m_cur_x = event.button.x;
466 m_specific->m_cur_y = y;
467 flags = 0;
468 switch(event.button.button)
469 {
470 case SDL_BUTTON_LEFT:
471 {
472 flags = mouse_left;
473
474 if(m_ctrls.on_mouse_button_down(m_specific->m_cur_x,
475 m_specific->m_cur_y))
476 {
477 m_ctrls.set_cur(m_specific->m_cur_x,
478 m_specific->m_cur_y);
479 on_ctrl_change();
480 force_redraw();
481 }
482 else
483 {
484 if(m_ctrls.in_rect(m_specific->m_cur_x,
485 m_specific->m_cur_y))
486 {
487 if(m_ctrls.set_cur(m_specific->m_cur_x,
488 m_specific->m_cur_y))
489 {
490 on_ctrl_change();
491 force_redraw();
492 }
493 }
494 else
495 {
496 on_mouse_button_down(m_specific->m_cur_x,
497 m_specific->m_cur_y,
498 flags);
499 }
500 }
501 }
502 break;
503 case SDL_BUTTON_RIGHT:
504 flags = mouse_right;
505 on_mouse_button_down(m_specific->m_cur_x,
506 m_specific->m_cur_y,
507 flags);
508 break;
509 } //switch(event.button.button)
510 break;
511
512 case SDL_MOUSEBUTTONUP:
513 y = m_flip_y
514 ? m_rbuf_window.height() - event.button.y
515 : event.button.y;
516
517 m_specific->m_cur_x = event.button.x;
518 m_specific->m_cur_y = y;
519 flags = 0;
520 if(m_ctrls.on_mouse_button_up(m_specific->m_cur_x,
521 m_specific->m_cur_y))
522 {
523 on_ctrl_change();
524 force_redraw();
525 }
526 on_mouse_button_up(m_specific->m_cur_x,
527 m_specific->m_cur_y,
528 flags);
529 break;
530 }
531 }
532 }
533 return 0;
534 }
535
536
537
538 //------------------------------------------------------------------------
539 const char* platform_support::img_ext() const { return ".bmp"; }
540
541 //------------------------------------------------------------------------
542 const char* platform_support::full_file_name(const char* file_name)
543 {
544 return file_name;
545 }
546
547 //------------------------------------------------------------------------
548 bool platform_support::load_img(unsigned idx, const char* file)
549 {
550 if(idx < max_images)
551 {
552 if(m_specific->m_surf_img[idx]) SDL_FreeSurface(m_specific->m_surf_img[idx]);
553
554 char fn[1024];
555 std::strcpy(fn, file);
556 int len = std::strlen(fn);
557 if(len < 4 || std::strcmp(fn + len - 4, ".bmp") != 0)
558 {
559 std::strcat(fn, ".bmp");
560 }
561
562 SDL_Surface* tmp_surf = SDL_LoadBMP(fn);
563 if (tmp_surf == 0)
564 {
565 std::fprintf(stderr, "Couldn't load %s: %s\n", fn, SDL_GetError());
566 return false;
567 }
568
569 SDL_PixelFormat format;
570 format.palette = 0;
571 format.BitsPerPixel = m_bpp;
572 format.BytesPerPixel = m_bpp >> 8;
573 format.Rmask = m_specific->m_rmask;
574 format.Gmask = m_specific->m_gmask;
575 format.Bmask = m_specific->m_bmask;
576 format.Amask = m_specific->m_amask;
577 format.Rshift = 0;
578 format.Gshift = 0;
579 format.Bshift = 0;
580 format.Ashift = 0;
581 format.Rloss = 0;
582 format.Gloss = 0;
583 format.Bloss = 0;
584 format.Aloss = 0;
585 format.colorkey = 0;
586 format.alpha = 0;
587
588 m_specific->m_surf_img[idx] =
589 SDL_ConvertSurface(tmp_surf,
590 &format,
591 SDL_SWSURFACE);
592
593 SDL_FreeSurface(tmp_surf);
594
595 if(m_specific->m_surf_img[idx] == 0) return false;
596
597 m_rbuf_img[idx].attach((unsigned char*)m_specific->m_surf_img[idx]->pixels,
598 m_specific->m_surf_img[idx]->w,
599 m_specific->m_surf_img[idx]->h,
600 m_flip_y ? -m_specific->m_surf_img[idx]->pitch :
601 m_specific->m_surf_img[idx]->pitch);
602 return true;
603
604 }
605 return false;
606 }
607
608
609
610
611 //------------------------------------------------------------------------
612 bool platform_support::save_img(unsigned idx, const char* file)
613 {
614 if(idx < max_images && m_specific->m_surf_img[idx])
615 {
616 char fn[1024];
617 std::strcpy(fn, file);
618 int len = std::strlen(fn);
619 if(len < 4 || std::strcmp(fn + len - 4, ".bmp") != 0)
620 {
621 std::strcat(fn, ".bmp");
622 }
623 return SDL_SaveBMP(m_specific->m_surf_img[idx], fn) == 0;
624 }
625 return false;
626 }
627
628
629
630 //------------------------------------------------------------------------
631 bool platform_support::create_img(unsigned idx, unsigned width, unsigned height)
632 {
633 if(idx < max_images)
634 {
635
636 if(m_specific->m_surf_img[idx]) SDL_FreeSurface(m_specific->m_surf_img[idx]);
637
638 m_specific->m_surf_img[idx] =
639 SDL_CreateRGBSurface(SDL_SWSURFACE,
640 width,
641 height,
642 m_specific->m_surf_screen->format->BitsPerPixel,
643 m_specific->m_rmask,
644 m_specific->m_gmask,
645 m_specific->m_bmask,
646 m_specific->m_amask);
647 if(m_specific->m_surf_img[idx] == 0)
648 {
649 std::fprintf(stderr, "Couldn't create image: %s\n", SDL_GetError());
650 return false;
651 }
652
653 m_rbuf_img[idx].attach((unsigned char*)m_specific->m_surf_img[idx]->pixels,
654 m_specific->m_surf_img[idx]->w,
655 m_specific->m_surf_img[idx]->h,
656 m_flip_y ? -m_specific->m_surf_img[idx]->pitch :
657 m_specific->m_surf_img[idx]->pitch);
658
659 return true;
660 }
661
662 return false;
663 }
664
665 //------------------------------------------------------------------------
666 void platform_support::start_timer()
667 {
668 m_specific->m_sw_start = SDL_GetTicks();
669 }
670
671 //------------------------------------------------------------------------
672 double platform_support::elapsed_time() const
673 {
674 int stop = SDL_GetTicks();
675 return double(stop - m_specific->m_sw_start);
676 }
677
678 //------------------------------------------------------------------------
679 void platform_support::message(const char* msg)
680 {
681 std::fprintf(stderr, "%s\n", msg);
682 }
683
684 //------------------------------------------------------------------------
685 void platform_support::force_redraw()
686 {
687 m_specific->m_update_flag = true;
688 }
689
690
691 //------------------------------------------------------------------------
692 void platform_support::on_init() {}
693 void platform_support::on_resize(int sx, int sy) {}
694 void platform_support::on_idle() {}
695 void platform_support::on_mouse_move(int x, int y, unsigned flags) {}
696 void platform_support::on_mouse_button_down(int x, int y, unsigned flags) {}
697 void platform_support::on_mouse_button_up(int x, int y, unsigned flags) {}
698 void platform_support::on_key(int x, int y, unsigned key, unsigned flags) {}
699 void platform_support::on_ctrl_change() {}
700 void platform_support::on_draw() {}
701 void platform_support::on_post_draw(void* raw_handler) {}
702
703
704 }
705
706
707 int agg_main(int argc, char* argv[]);
708
709 int main(int argc, char* argv[])
710 {
711 return agg_main(argc, argv);
712 }
713