| 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 |
// It's not a part of the AGG library, it's just a helper class to create |
| 19 |
// interactive demo examples. Since the examples should not be too complex |
| 20 |
// this class is provided to support some very basic interactive graphical |
| 21 |
// functionality, such as putting the rendered image to the window, simple |
| 22 |
// keyboard and mouse input, window resizing, setting the window title, |
| 23 |
// and catching the "idle" events. |
| 24 |
// |
| 25 |
// The idea is to have a single header file that does not depend on any |
| 26 |
// platform (I hate these endless #ifdef/#elif/#elif.../#endif) and a number |
| 27 |
// of different implementations depending on the concrete platform. |
| 28 |
// The most popular platforms are: |
| 29 |
// |
| 30 |
// Windows-32 API |
| 31 |
// X-Window API |
| 32 |
// SDL library (see http://www.libsdl.org/) |
| 33 |
// MacOS C/C++ API |
| 34 |
// |
| 35 |
// This file does not include any system dependent .h files such as |
| 36 |
// windows.h or X11.h, so, your demo applications do not depend on the |
| 37 |
// platform. The only file that can #include system dependend headers |
| 38 |
// is the implementation file agg_platform_support.cpp. Different |
| 39 |
// implementations are placed in different directories, such as |
| 40 |
// ~/agg/src/platform/win32 |
| 41 |
// ~/agg/src/platform/sdl |
| 42 |
// ~/agg/src/platform/X11 |
| 43 |
// and so on. |
| 44 |
// |
| 45 |
// All the system dependent stuff sits in the platform_specific |
| 46 |
// class which is forward-declared here but not defined. |
| 47 |
// The platform_support class has just a pointer to it and it's |
| 48 |
// the responsibility of the implementation to create/delete it. |
| 49 |
// This class being defined in the implementation file can have |
| 50 |
// any platform dependent stuff such as HWND, X11 Window and so on. |
| 51 |
// |
| 52 |
//---------------------------------------------------------------------------- |
| 53 |
|
| 54 |
|
| 55 |
#ifndef AGG_PLATFORM_SUPPORT_INCLUDED |
| 56 |
#define AGG_PLATFORM_SUPPORT_INCLUDED |
| 57 |
|
| 58 |
|
| 59 |
#include "agg_basics.h" |
| 60 |
#include "agg_rendering_buffer.h" |
| 61 |
#include "agg_trans_viewport.h" |
| 62 |
#include "ctrl/agg_ctrl.h" |
| 63 |
|
| 64 |
namespace agg |
| 65 |
{ |
| 66 |
|
| 67 |
//----------------------------------------------------------window_flag_e |
| 68 |
// These are flags used in method init(). Not all of them are |
| 69 |
// applicable on different platforms, for example the win32_api |
| 70 |
// cannot use a hardware buffer (window_hw_buffer). |
| 71 |
// The implementation should simply ignore unsupported flags. |
| 72 |
enum window_flag_e |
| 73 |
{ |
| 74 |
window_resize = 1, |
| 75 |
window_hw_buffer = 2, |
| 76 |
window_keep_aspect_ratio = 4, |
| 77 |
window_process_all_keys = 8 |
| 78 |
}; |
| 79 |
|
| 80 |
//-----------------------------------------------------------pix_format_e |
| 81 |
// Possible formats of the rendering buffer. Initially I thought that it's |
| 82 |
// reasonable to create the buffer and the rendering functions in |
| 83 |
// accordance with the native pixel format of the system because it |
| 84 |
// would have no overhead for pixel format conersion. |
| 85 |
// But eventually I came to a conclusion that having a possibility to |
| 86 |
// convert pixel formats on demand is a good idea. First, it was X11 where |
| 87 |
// there lots of different formats and visuals and it would be great to |
| 88 |
// render everything in, say, RGB-24 and display it automatically without |
| 89 |
// any additional efforts. The second reason is to have a possibility to |
| 90 |
// debug renderers for different pixel formats and colorspaces having only |
| 91 |
// one computer and one system. |
| 92 |
// |
| 93 |
// This stuff is not included into the basic AGG functionality because the |
| 94 |
// number of supported pixel formats (and/or colorspaces) can be great and |
| 95 |
// if one needs to add new format it would be good only to add new |
| 96 |
// rendering files without having to modify any existing ones (a general |
| 97 |
// principle of incapsulation and isolation). |
| 98 |
// |
| 99 |
// Using a particular pixel format doesn't obligatory mean the necessity |
| 100 |
// of software conversion. For example, win32 API can natively display |
| 101 |
// gray8, 15-bit RGB, 24-bit BGR, and 32-bit BGRA formats. |
| 102 |
// This list can be (and will be!) extended in future. |
| 103 |
enum pix_format_e |
| 104 |
{ |
| 105 |
pix_format_undefined = 0, // By default. No conversions are applied |
| 106 |
pix_format_bw, // 1 bit per color B/W |
| 107 |
pix_format_gray8, // Simple 256 level grayscale |
| 108 |
pix_format_sgray8, // Simple 256 level grayscale (sRGB) |
| 109 |
pix_format_gray16, // Simple 65535 level grayscale |
| 110 |
pix_format_gray32, // Grayscale, one 32-bit float per pixel |
| 111 |
pix_format_rgb555, // 15 bit rgb. Depends on the byte ordering! |
| 112 |
pix_format_rgb565, // 16 bit rgb. Depends on the byte ordering! |
| 113 |
pix_format_rgbAAA, // 30 bit rgb. Depends on the byte ordering! |
| 114 |
pix_format_rgbBBA, // 32 bit rgb. Depends on the byte ordering! |
| 115 |
pix_format_bgrAAA, // 30 bit bgr. Depends on the byte ordering! |
| 116 |
pix_format_bgrABB, // 32 bit bgr. Depends on the byte ordering! |
| 117 |
pix_format_rgb24, // R-G-B, one byte per color component |
| 118 |
pix_format_srgb24, // R-G-B, one byte per color component (sRGB) |
| 119 |
pix_format_bgr24, // B-G-R, one byte per color component |
| 120 |
pix_format_sbgr24, // B-G-R, native win32 BMP format (sRGB) |
| 121 |
pix_format_rgba32, // R-G-B-A, one byte per color component |
| 122 |
pix_format_srgba32, // R-G-B-A, one byte per color component (sRGB) |
| 123 |
pix_format_argb32, // A-R-G-B, native MAC format |
| 124 |
pix_format_sargb32, // A-R-G-B, native MAC format (sRGB) |
| 125 |
pix_format_abgr32, // A-B-G-R, one byte per color component |
| 126 |
pix_format_sabgr32, // A-B-G-R, one byte per color component (sRGB) |
| 127 |
pix_format_bgra32, // B-G-R-A, native win32 BMP format |
| 128 |
pix_format_sbgra32, // B-G-R-A, native win32 BMP format (sRGB) |
| 129 |
pix_format_rgb48, // R-G-B, 16 bits per color component |
| 130 |
pix_format_bgr48, // B-G-R, native win32 BMP format. |
| 131 |
pix_format_rgb96, // R-G-B, one 32-bit float per color component |
| 132 |
pix_format_bgr96, // B-G-R, one 32-bit float per color component |
| 133 |
pix_format_rgba64, // R-G-B-A, 16 bits byte per color component |
| 134 |
pix_format_argb64, // A-R-G-B, native MAC format |
| 135 |
pix_format_abgr64, // A-B-G-R, one byte per color component |
| 136 |
pix_format_bgra64, // B-G-R-A, native win32 BMP format |
| 137 |
pix_format_rgba128, // R-G-B-A, one 32-bit float per color component |
| 138 |
pix_format_argb128, // A-R-G-B, one 32-bit float per color component |
| 139 |
pix_format_abgr128, // A-B-G-R, one 32-bit float per color component |
| 140 |
pix_format_bgra128, // B-G-R-A, one 32-bit float per color component |
| 141 |
|
| 142 |
end_of_pix_formats |
| 143 |
}; |
| 144 |
|
| 145 |
//-------------------------------------------------------------input_flag_e |
| 146 |
// Mouse and keyboard flags. They can be different on different platforms |
| 147 |
// and the ways they are obtained are also different. But in any case |
| 148 |
// the system dependent flags should be mapped into these ones. The meaning |
| 149 |
// of that is as follows. For example, if kbd_ctrl is set it means that the |
| 150 |
// ctrl key is pressed and being held at the moment. They are also used in |
| 151 |
// the overridden methods such as on_mouse_move(), on_mouse_button_down(), |
| 152 |
// on_mouse_button_dbl_click(), on_mouse_button_up(), on_key(). |
| 153 |
// In the method on_mouse_button_up() the mouse flags have different |
| 154 |
// meaning. They mean that the respective button is being released, but |
| 155 |
// the meaning of the keyboard flags remains the same. |
| 156 |
// There's absolut minimal set of flags is used because they'll be most |
| 157 |
// probably supported on different platforms. Even the mouse_right flag |
| 158 |
// is restricted because Mac's mice have only one button, but AFAIK |
| 159 |
// it can be simulated with holding a special key on the keydoard. |
| 160 |
enum input_flag_e |
| 161 |
{ |
| 162 |
mouse_left = 1, |
| 163 |
mouse_right = 2, |
| 164 |
kbd_shift = 4, |
| 165 |
kbd_ctrl = 8 |
| 166 |
}; |
| 167 |
|
| 168 |
//--------------------------------------------------------------key_code_e |
| 169 |
// Keyboard codes. There's also a restricted set of codes that are most |
| 170 |
// probably supported on different platforms. Any platform dependent codes |
| 171 |
// should be converted into these ones. There're only those codes are |
| 172 |
// defined that cannot be represented as printable ASCII-characters. |
| 173 |
// All printable ASCII-set can be used in a regular C/C++ manner: |
| 174 |
// ' ', 'A', '0' '+' and so on. |
| 175 |
// Since the class is used for creating very simple demo-applications |
| 176 |
// we don't need very rich possibilities here, just basic ones. |
| 177 |
// Actually the numeric key codes are taken from the SDL library, so, |
| 178 |
// the implementation of the SDL support does not require any mapping. |
| 179 |
enum key_code_e |
| 180 |
{ |
| 181 |
// ASCII set. Should be supported everywhere |
| 182 |
key_backspace = 8, |
| 183 |
key_tab = 9, |
| 184 |
key_clear = 12, |
| 185 |
key_return = 13, |
| 186 |
key_pause = 19, |
| 187 |
key_escape = 27, |
| 188 |
|
| 189 |
// Keypad |
| 190 |
key_delete = 127, |
| 191 |
key_kp0 = 256, |
| 192 |
key_kp1 = 257, |
| 193 |
key_kp2 = 258, |
| 194 |
key_kp3 = 259, |
| 195 |
key_kp4 = 260, |
| 196 |
key_kp5 = 261, |
| 197 |
key_kp6 = 262, |
| 198 |
key_kp7 = 263, |
| 199 |
key_kp8 = 264, |
| 200 |
key_kp9 = 265, |
| 201 |
key_kp_period = 266, |
| 202 |
key_kp_divide = 267, |
| 203 |
key_kp_multiply = 268, |
| 204 |
key_kp_minus = 269, |
| 205 |
key_kp_plus = 270, |
| 206 |
key_kp_enter = 271, |
| 207 |
key_kp_equals = 272, |
| 208 |
|
| 209 |
// Arrow-keys and stuff |
| 210 |
key_up = 273, |
| 211 |
key_down = 274, |
| 212 |
key_right = 275, |
| 213 |
key_left = 276, |
| 214 |
key_insert = 277, |
| 215 |
key_home = 278, |
| 216 |
key_end = 279, |
| 217 |
key_page_up = 280, |
| 218 |
key_page_down = 281, |
| 219 |
|
| 220 |
// Functional keys. You'd better avoid using |
| 221 |
// f11...f15 in your applications if you want |
| 222 |
// the applications to be portable |
| 223 |
key_f1 = 282, |
| 224 |
key_f2 = 283, |
| 225 |
key_f3 = 284, |
| 226 |
key_f4 = 285, |
| 227 |
key_f5 = 286, |
| 228 |
key_f6 = 287, |
| 229 |
key_f7 = 288, |
| 230 |
key_f8 = 289, |
| 231 |
key_f9 = 290, |
| 232 |
key_f10 = 291, |
| 233 |
key_f11 = 292, |
| 234 |
key_f12 = 293, |
| 235 |
key_f13 = 294, |
| 236 |
key_f14 = 295, |
| 237 |
key_f15 = 296, |
| 238 |
|
| 239 |
// The possibility of using these keys is |
| 240 |
// very restricted. Actually it's guaranteed |
| 241 |
// only in win32_api and win32_sdl implementations |
| 242 |
key_numlock = 300, |
| 243 |
key_capslock = 301, |
| 244 |
key_scrollock = 302, |
| 245 |
|
| 246 |
// Phew! |
| 247 |
end_of_key_codes |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
//------------------------------------------------------------------------ |
| 252 |
// A predeclaration of the platform dependent class. Since we do not |
| 253 |
// know anything here the only we can have is just a pointer to this |
| 254 |
// class as a data member. It should be created and destroyed explicitly |
| 255 |
// in the constructor/destructor of the platform_support class. |
| 256 |
// Although the pointer to platform_specific is public the application |
| 257 |
// cannot have access to its members or methods since it does not know |
| 258 |
// anything about them and it's a perfect incapsulation :-) |
| 259 |
class platform_specific; |
| 260 |
|
| 261 |
|
| 262 |
//----------------------------------------------------------ctrl_container |
| 263 |
// A helper class that contains pointers to a number of controls. |
| 264 |
// This class is used to ease the event handling with controls. |
| 265 |
// The implementation should simply call the appropriate methods |
| 266 |
// of this class when appropriate events occur. |
| 267 |
class ctrl_container |
| 268 |
{ |
| 269 |
enum max_ctrl_e { max_ctrl = 64 }; |
| 270 |
|
| 271 |
public: |
| 272 |
//-------------------------------------------------------------------- |
| 273 |
ctrl_container() : m_num_ctrl(0), m_cur_ctrl(-1) {} |
| 274 |
|
| 275 |
//-------------------------------------------------------------------- |
| 276 |
void add(ctrl& c) |
| 277 |
{ |
| 278 |
if(m_num_ctrl < max_ctrl) |
| 279 |
{ |
| 280 |
m_ctrl[m_num_ctrl++] = &c; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
//-------------------------------------------------------------------- |
| 285 |
bool in_rect(double x, double y) |
| 286 |
{ |
| 287 |
unsigned i; |
| 288 |
for(i = 0; i < m_num_ctrl; i++) |
| 289 |
{ |
| 290 |
if(m_ctrl[i]->in_rect(x, y)) return true; |
| 291 |
} |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
//-------------------------------------------------------------------- |
| 296 |
bool on_mouse_button_down(double x, double y) |
| 297 |
{ |
| 298 |
unsigned i; |
| 299 |
for(i = 0; i < m_num_ctrl; i++) |
| 300 |
{ |
| 301 |
if(m_ctrl[i]->on_mouse_button_down(x, y)) return true; |
| 302 |
} |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
//-------------------------------------------------------------------- |
| 307 |
bool on_mouse_button_up(double x, double y) |
| 308 |
{ |
| 309 |
unsigned i; |
| 310 |
bool flag = false; |
| 311 |
for(i = 0; i < m_num_ctrl; i++) |
| 312 |
{ |
| 313 |
if(m_ctrl[i]->on_mouse_button_up(x, y)) flag = true; |
| 314 |
} |
| 315 |
return flag; |
| 316 |
} |
| 317 |
|
| 318 |
//-------------------------------------------------------------------- |
| 319 |
bool on_mouse_move(double x, double y, bool button_flag) |
| 320 |
{ |
| 321 |
unsigned i; |
| 322 |
for(i = 0; i < m_num_ctrl; i++) |
| 323 |
{ |
| 324 |
if(m_ctrl[i]->on_mouse_move(x, y, button_flag)) return true; |
| 325 |
} |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
//-------------------------------------------------------------------- |
| 330 |
bool on_arrow_keys(bool left, bool right, bool down, bool up) |
| 331 |
{ |
| 332 |
if(m_cur_ctrl >= 0) |
| 333 |
{ |
| 334 |
return m_ctrl[m_cur_ctrl]->on_arrow_keys(left, right, down, up); |
| 335 |
} |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
//-------------------------------------------------------------------- |
| 340 |
bool set_cur(double x, double y) |
| 341 |
{ |
| 342 |
unsigned i; |
| 343 |
for(i = 0; i < m_num_ctrl; i++) |
| 344 |
{ |
| 345 |
if(m_ctrl[i]->in_rect(x, y)) |
| 346 |
{ |
| 347 |
if(m_cur_ctrl != int(i)) |
| 348 |
{ |
| 349 |
m_cur_ctrl = i; |
| 350 |
return true; |
| 351 |
} |
| 352 |
return false; |
| 353 |
} |
| 354 |
} |
| 355 |
if(m_cur_ctrl != -1) |
| 356 |
{ |
| 357 |
m_cur_ctrl = -1; |
| 358 |
return true; |
| 359 |
} |
| 360 |
return false; |
| 361 |
} |
| 362 |
|
| 363 |
private: |
| 364 |
ctrl* m_ctrl[max_ctrl]; |
| 365 |
unsigned m_num_ctrl; |
| 366 |
int m_cur_ctrl; |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
//---------------------------------------------------------platform_support |
| 372 |
// This class is a base one to the apllication classes. It can be used |
| 373 |
// as follows: |
| 374 |
// |
| 375 |
// class the_application : public agg::platform_support |
| 376 |
// { |
| 377 |
// public: |
| 378 |
// the_application(unsigned bpp, bool flip_y) : |
| 379 |
// platform_support(bpp, flip_y) |
| 380 |
// . . . |
| 381 |
// |
| 382 |
// //override stuff . . . |
| 383 |
// virtual void on_init() |
| 384 |
// { |
| 385 |
// . . . |
| 386 |
// } |
| 387 |
// |
| 388 |
// virtual void on_draw() |
| 389 |
// { |
| 390 |
// . . . |
| 391 |
// } |
| 392 |
// |
| 393 |
// virtual void on_resize(int sx, int sy) |
| 394 |
// { |
| 395 |
// . . . |
| 396 |
// } |
| 397 |
// // . . . and so on, see virtual functions |
| 398 |
// |
| 399 |
// |
| 400 |
// //any your own stuff . . . |
| 401 |
// }; |
| 402 |
// |
| 403 |
// |
| 404 |
// int agg_main(int argc, char* argv[]) |
| 405 |
// { |
| 406 |
// the_application app(pix_format_rgb24, true); |
| 407 |
// app.caption("AGG Example. Lion"); |
| 408 |
// |
| 409 |
// if(app.init(500, 400, agg::window_resize)) |
| 410 |
// { |
| 411 |
// return app.run(); |
| 412 |
// } |
| 413 |
// return 1; |
| 414 |
// } |
| 415 |
// |
| 416 |
// The reason to have agg_main() instead of just main() is that SDL |
| 417 |
// for Windows requires including SDL.h if you define main(). Since |
| 418 |
// the demo applications cannot rely on any platform/library specific |
| 419 |
// stuff it's impossible to include SDL.h into the application files. |
| 420 |
// The demo applications are simple and their use is restricted, so, |
| 421 |
// this approach is quite reasonable. |
| 422 |
// |
| 423 |
class platform_support |
| 424 |
{ |
| 425 |
public: |
| 426 |
enum max_images_e { max_images = 16 }; |
| 427 |
|
| 428 |
// format - see enum pix_format_e {}; |
| 429 |
// flip_y - true if you want to have the Y-axis flipped vertically. |
| 430 |
platform_support(pix_format_e format, bool flip_y); |
| 431 |
virtual ~platform_support(); |
| 432 |
|
| 433 |
// Setting the windows caption (title). Should be able |
| 434 |
// to be called at least before calling init(). |
| 435 |
// It's perfect if they can be called anytime. |
| 436 |
void caption(const char* cap); |
| 437 |
const char* caption() const { return m_caption; } |
| 438 |
|
| 439 |
//-------------------------------------------------------------------- |
| 440 |
// These 3 methods handle working with images. The image |
| 441 |
// formats are the simplest ones, such as .BMP in Windows or |
| 442 |
// .ppm in Linux. In the applications the names of the files |
| 443 |
// should not have any file extensions. Method load_img() can |
| 444 |
// be called before init(), so, the application could be able |
| 445 |
// to determine the initial size of the window depending on |
| 446 |
// the size of the loaded image. |
| 447 |
// The argument "idx" is the number of the image 0...max_images-1 |
| 448 |
bool load_img(unsigned idx, const char* file); |
| 449 |
bool save_img(unsigned idx, const char* file); |
| 450 |
bool create_img(unsigned idx, unsigned width=0, unsigned height=0); |
| 451 |
|
| 452 |
//-------------------------------------------------------------------- |
| 453 |
// init() and run(). See description before the class for details. |
| 454 |
// The necessity of calling init() after creation is that it's |
| 455 |
// impossible to call the overridden virtual function (on_init()) |
| 456 |
// from the constructor. On the other hand it's very useful to have |
| 457 |
// some on_init() event handler when the window is created but |
| 458 |
// not yet displayed. The rbuf_window() method (see below) is |
| 459 |
// accessible from on_init(). |
| 460 |
bool init(unsigned width, unsigned height, unsigned flags); |
| 461 |
int run(); |
| 462 |
|
| 463 |
//-------------------------------------------------------------------- |
| 464 |
// The very same parameters that were used in the constructor |
| 465 |
pix_format_e format() const { return m_format; } |
| 466 |
bool flip_y() const { return m_flip_y; } |
| 467 |
unsigned bpp() const { return m_bpp; } |
| 468 |
|
| 469 |
//-------------------------------------------------------------------- |
| 470 |
// The following provides a very simple mechanism of doing someting |
| 471 |
// in background. It's not multithreading. When wait_mode is true |
| 472 |
// the class waits for the events and it does not ever call on_idle(). |
| 473 |
// When it's false it calls on_idle() when the event queue is empty. |
| 474 |
// The mode can be changed anytime. This mechanism is satisfactory |
| 475 |
// to create very simple animations. |
| 476 |
bool wait_mode() const { return m_wait_mode; } |
| 477 |
void wait_mode(bool wait_mode) { m_wait_mode = wait_mode; } |
| 478 |
|
| 479 |
//-------------------------------------------------------------------- |
| 480 |
// These two functions control updating of the window. |
| 481 |
// force_redraw() is an analog of the Win32 InvalidateRect() function. |
| 482 |
// Being called it sets a flag (or sends a message) which results |
| 483 |
// in calling on_draw() and updating the content of the window |
| 484 |
// when the next event cycle comes. |
| 485 |
// update_window() results in just putting immediately the content |
| 486 |
// of the currently rendered buffer to the window without calling |
| 487 |
// on_draw(). |
| 488 |
void force_redraw(); |
| 489 |
void update_window(); |
| 490 |
|
| 491 |
//-------------------------------------------------------------------- |
| 492 |
// So, finally, how to draw anythig with AGG? Very simple. |
| 493 |
// rbuf_window() returns a reference to the main rendering |
| 494 |
// buffer which can be attached to any rendering class. |
| 495 |
// rbuf_img() returns a reference to the previously created |
| 496 |
// or loaded image buffer (see load_img()). The image buffers |
| 497 |
// are not displayed directly, they should be copied to or |
| 498 |
// combined somehow with the rbuf_window(). rbuf_window() is |
| 499 |
// the only buffer that can be actually displayed. |
| 500 |
rendering_buffer& rbuf_window() { return m_rbuf_window; } |
| 501 |
rendering_buffer& rbuf_img(unsigned idx) { return m_rbuf_img[idx]; } |
| 502 |
|
| 503 |
|
| 504 |
//-------------------------------------------------------------------- |
| 505 |
// Returns file extension used in the implementation for the particular |
| 506 |
// system. |
| 507 |
const char* img_ext() const; |
| 508 |
|
| 509 |
//-------------------------------------------------------------------- |
| 510 |
void copy_img_to_window(unsigned idx) |
| 511 |
{ |
| 512 |
if(idx < max_images && rbuf_img(idx).buf()) |
| 513 |
{ |
| 514 |
rbuf_window().copy_from(rbuf_img(idx)); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
//-------------------------------------------------------------------- |
| 519 |
void copy_window_to_img(unsigned idx) |
| 520 |
{ |
| 521 |
if(idx < max_images) |
| 522 |
{ |
| 523 |
create_img(idx, rbuf_window().width(), rbuf_window().height()); |
| 524 |
rbuf_img(idx).copy_from(rbuf_window()); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
//-------------------------------------------------------------------- |
| 529 |
void copy_img_to_img(unsigned idx_to, unsigned idx_from) |
| 530 |
{ |
| 531 |
if(idx_from < max_images && |
| 532 |
idx_to < max_images && |
| 533 |
rbuf_img(idx_from).buf()) |
| 534 |
{ |
| 535 |
create_img(idx_to, |
| 536 |
rbuf_img(idx_from).width(), |
| 537 |
rbuf_img(idx_from).height()); |
| 538 |
rbuf_img(idx_to).copy_from(rbuf_img(idx_from)); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
//-------------------------------------------------------------------- |
| 543 |
// Event handlers. They are not pure functions, so you don't have |
| 544 |
// to override them all. |
| 545 |
// In my demo applications these functions are defined inside |
| 546 |
// the the_application class (implicit inlining) which is in general |
| 547 |
// very bad practice, I mean vitual inline methods. At least it does |
| 548 |
// not make sense. |
| 549 |
// But in this case it's quite appropriate bacause we have the only |
| 550 |
// instance of the the_application class and it is in the same file |
| 551 |
// where this class is defined. |
| 552 |
virtual void on_init(); |
| 553 |
virtual void on_resize(int sx, int sy); |
| 554 |
virtual void on_idle(); |
| 555 |
virtual void on_mouse_move(int x, int y, unsigned flags); |
| 556 |
virtual void on_mouse_button_down(int x, int y, unsigned flags); |
| 557 |
virtual void on_mouse_button_up(int x, int y, unsigned flags); |
| 558 |
virtual void on_key(int x, int y, unsigned key, unsigned flags); |
| 559 |
virtual void on_ctrl_change(); |
| 560 |
virtual void on_draw(); |
| 561 |
virtual void on_post_draw(void* raw_handler); |
| 562 |
|
| 563 |
//-------------------------------------------------------------------- |
| 564 |
// Adding control elements. A control element once added will be |
| 565 |
// working and reacting to the mouse and keyboard events. Still, you |
| 566 |
// will have to render them in the on_draw() using function |
| 567 |
// render_ctrl() because platform_support doesn't know anything about |
| 568 |
// renderers you use. The controls will be also scaled automatically |
| 569 |
// if they provide a proper scaling mechanism (all the controls |
| 570 |
// included into the basic AGG package do). |
| 571 |
// If you don't need a particular control to be scaled automatically |
| 572 |
// call ctrl::no_transform() after adding. |
| 573 |
void add_ctrl(ctrl& c) { m_ctrls.add(c); c.transform(m_resize_mtx); } |
| 574 |
|
| 575 |
//-------------------------------------------------------------------- |
| 576 |
// Auxiliary functions. trans_affine_resizing() modifier sets up the resizing |
| 577 |
// matrix on the basis of the given width and height and the initial |
| 578 |
// width and height of the window. The implementation should simply |
| 579 |
// call this function every time when it catches the resizing event |
| 580 |
// passing in the new values of width and height of the window. |
| 581 |
// Nothing prevents you from "cheating" the scaling matrix if you |
| 582 |
// call this function from somewhere with wrong arguments. |
| 583 |
// trans_affine_resizing() accessor simply returns current resizing matrix |
| 584 |
// which can be used to apply additional scaling of any of your |
| 585 |
// stuff when the window is being resized. |
| 586 |
// width(), height(), initial_width(), and initial_height() must be |
| 587 |
// clear to understand with no comments :-) |
| 588 |
void trans_affine_resizing(int width, int height) |
| 589 |
{ |
| 590 |
if(m_window_flags & window_keep_aspect_ratio) |
| 591 |
{ |
| 592 |
//double sx = double(width) / double(m_initial_width); |
| 593 |
//double sy = double(height) / double(m_initial_height); |
| 594 |
//if(sy < sx) sx = sy; |
| 595 |
//m_resize_mtx = trans_affine_scaling(sx, sx); |
| 596 |
trans_viewport vp; |
| 597 |
vp.preserve_aspect_ratio(0.5, 0.5, aspect_ratio_meet); |
| 598 |
vp.device_viewport(0, 0, width, height); |
| 599 |
vp.world_viewport(0, 0, m_initial_width, m_initial_height); |
| 600 |
m_resize_mtx = vp.to_affine(); |
| 601 |
} |
| 602 |
else |
| 603 |
{ |
| 604 |
m_resize_mtx = trans_affine_scaling( |
| 605 |
double(width) / double(m_initial_width), |
| 606 |
double(height) / double(m_initial_height)); |
| 607 |
} |
| 608 |
} |
| 609 |
trans_affine& trans_affine_resizing() { return m_resize_mtx; } |
| 610 |
const trans_affine& trans_affine_resizing() const { return m_resize_mtx; } |
| 611 |
double width() const { return m_rbuf_window.width(); } |
| 612 |
double height() const { return m_rbuf_window.height(); } |
| 613 |
double initial_width() const { return m_initial_width; } |
| 614 |
double initial_height() const { return m_initial_height; } |
| 615 |
unsigned window_flags() const { return m_window_flags; } |
| 616 |
|
| 617 |
//-------------------------------------------------------------------- |
| 618 |
// Get raw display handler depending on the system. |
| 619 |
// For win32 its an HDC, for other systems it can be a pointer to some |
| 620 |
// structure. See the implementation files for detals. |
| 621 |
// It's provided "as is", so, first you should check if it's not null. |
| 622 |
// If it's null the raw_display_handler is not supported. Also, there's |
| 623 |
// no guarantee that this function is implemented, so, in some |
| 624 |
// implementations you may have simply an unresolved symbol when linking. |
| 625 |
void* raw_display_handler(); |
| 626 |
|
| 627 |
//-------------------------------------------------------------------- |
| 628 |
// display message box or print the message to the console |
| 629 |
// (depending on implementation) |
| 630 |
void message(const char* msg); |
| 631 |
|
| 632 |
//-------------------------------------------------------------------- |
| 633 |
// Stopwatch functions. Function elapsed_time() returns time elapsed |
| 634 |
// since the latest start_timer() invocation in millisecods. |
| 635 |
// The resolutoin depends on the implementation. |
| 636 |
// In Win32 it uses QueryPerformanceFrequency() / QueryPerformanceCounter(). |
| 637 |
void start_timer(); |
| 638 |
double elapsed_time() const; |
| 639 |
|
| 640 |
//-------------------------------------------------------------------- |
| 641 |
// Get the full file name. In most cases it simply returns |
| 642 |
// file_name. As it's appropriate in many systems if you open |
| 643 |
// a file by its name without specifying the path, it tries to |
| 644 |
// open it in the current directory. The demos usually expect |
| 645 |
// all the supplementary files to be placed in the current |
| 646 |
// directory, that is usually coincides with the directory where |
| 647 |
// the executable is. However, in some systems (BeOS) it's not so. |
| 648 |
// For those kinds of systems full_file_name() can help access files |
| 649 |
// preserving commonly used policy. |
| 650 |
// So, it's a good idea to use in the demos the following: |
| 651 |
// FILE* fd = fopen(full_file_name("some.file"), "r"); |
| 652 |
// instead of |
| 653 |
// FILE* fd = fopen("some.file", "r"); |
| 654 |
const char* full_file_name(const char* file_name); |
| 655 |
|
| 656 |
public: |
| 657 |
platform_specific* m_specific; |
| 658 |
ctrl_container m_ctrls; |
| 659 |
|
| 660 |
// Sorry, I'm too tired to describe the private |
| 661 |
// data membders. See the implementations for different |
| 662 |
// platforms for details. |
| 663 |
private: |
| 664 |
platform_support(const platform_support&); |
| 665 |
const platform_support& operator = (const platform_support&); |
| 666 |
|
| 667 |
pix_format_e m_format; |
| 668 |
unsigned m_bpp; |
| 669 |
rendering_buffer m_rbuf_window; |
| 670 |
rendering_buffer m_rbuf_img[max_images]; |
| 671 |
unsigned m_window_flags; |
| 672 |
bool m_wait_mode; |
| 673 |
bool m_flip_y; |
| 674 |
char m_caption[256]; |
| 675 |
int m_initial_width; |
| 676 |
int m_initial_height; |
| 677 |
trans_affine m_resize_mtx; |
| 678 |
}; |
| 679 |
|
| 680 |
|
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
#endif |
| 686 |
|