| 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 |
//---------------------------------------------------------------------------- |
| 13 |
// |
| 14 |
// class platform_support |
| 15 |
// |
| 16 |
//---------------------------------------------------------------------------- |
| 17 |
|
| 18 |
#include <new> |
| 19 |
#include <cstdio> |
| 20 |
|
| 21 |
#include <Alert.h> |
| 22 |
#include <Application.h> |
| 23 |
#include <Bitmap.h> |
| 24 |
#include <Message.h> |
| 25 |
#include <MessageRunner.h> |
| 26 |
#include <Messenger.h> |
| 27 |
#include <Path.h> |
| 28 |
#include <Roster.h> |
| 29 |
#include <TranslationUtils.h> |
| 30 |
#include <View.h> |
| 31 |
#include <Window.h> |
| 32 |
|
| 33 |
#include <cstring> |
| 34 |
#include "platform/agg_platform_support.h" |
| 35 |
#include "util/agg_color_conv_rgb8.h" |
| 36 |
|
| 37 |
using std::nothrow; |
| 38 |
|
| 39 |
|
| 40 |
static void |
| 41 |
attach_buffer_to_BBitmap(agg::rendering_buffer& buffer, BBitmap* bitmap, bool flipY) |
| 42 |
{ |
| 43 |
uint8* bits = (uint8*)bitmap->Bits(); |
| 44 |
uint32 width = bitmap->Bounds().IntegerWidth() + 1; |
| 45 |
uint32 height = bitmap->Bounds().IntegerHeight() + 1; |
| 46 |
int32 bpr = bitmap->BytesPerRow(); |
| 47 |
if (flipY) { |
| 48 |
// XXX: why don't I have to do this?!? |
| 49 |
// bits += bpr * (height - 1); |
| 50 |
bpr = -bpr; |
| 51 |
} |
| 52 |
buffer.attach(bits, width, height, bpr); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
static color_space |
| 57 |
pix_format_to_color_space(agg::pix_format_e format) |
| 58 |
{ |
| 59 |
color_space bitmapFormat = B_NO_COLOR_SPACE; |
| 60 |
switch (format) { |
| 61 |
case agg::pix_format_rgb555: |
| 62 |
|
| 63 |
bitmapFormat = B_RGB15; |
| 64 |
break; |
| 65 |
|
| 66 |
case agg::pix_format_rgb565: |
| 67 |
|
| 68 |
bitmapFormat = B_RGB16; |
| 69 |
break; |
| 70 |
|
| 71 |
case agg::pix_format_rgb24: |
| 72 |
case agg::pix_format_bgr24: |
| 73 |
|
| 74 |
bitmapFormat = B_RGB24; |
| 75 |
break; |
| 76 |
|
| 77 |
case agg::pix_format_rgba32: |
| 78 |
case agg::pix_format_argb32: |
| 79 |
case agg::pix_format_abgr32: |
| 80 |
case agg::pix_format_bgra32: |
| 81 |
|
| 82 |
bitmapFormat = B_RGBA32; |
| 83 |
break; |
| 84 |
} |
| 85 |
return bitmapFormat; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
// #pragma mark - |
| 90 |
|
| 91 |
|
| 92 |
class AGGView : public BView { |
| 93 |
public: |
| 94 |
AGGView(BRect frame, agg::platform_support* agg, |
| 95 |
agg::pix_format_e format, bool flipY); |
| 96 |
virtual ~AGGView(); |
| 97 |
|
| 98 |
virtual void AttachedToWindow(); |
| 99 |
virtual void DetachedFromWindow(); |
| 100 |
|
| 101 |
virtual void MessageReceived(BMessage* message); |
| 102 |
virtual void Draw(BRect updateRect); |
| 103 |
virtual void FrameResized(float width, float height); |
| 104 |
|
| 105 |
virtual void KeyDown(const char* bytes, int32 numBytes); |
| 106 |
|
| 107 |
virtual void MouseDown(BPoint where); |
| 108 |
virtual void MouseMoved(BPoint where, uint32 transit, |
| 109 |
const BMessage* dragMesage); |
| 110 |
virtual void MouseUp(BPoint where); |
| 111 |
|
| 112 |
BBitmap* Bitmap() const; |
| 113 |
|
| 114 |
uint8 LastKeyDown() const; |
| 115 |
uint32 MouseButtons(); |
| 116 |
|
| 117 |
void Update(); |
| 118 |
void ForceRedraw(); |
| 119 |
|
| 120 |
unsigned GetKeyFlags(); |
| 121 |
|
| 122 |
private: |
| 123 |
BBitmap* fBitmap; |
| 124 |
agg::pix_format_e fFormat; |
| 125 |
bool fFlipY; |
| 126 |
|
| 127 |
agg::platform_support* fAGG; |
| 128 |
|
| 129 |
uint32 fMouseButtons; |
| 130 |
int32 fMouseX; |
| 131 |
int32 fMouseY; |
| 132 |
|
| 133 |
uint8 fLastKeyDown; |
| 134 |
|
| 135 |
bool fRedraw; |
| 136 |
|
| 137 |
BMessageRunner* fPulse; |
| 138 |
bigtime_t fLastPulse; |
| 139 |
bool fEnableTicks; |
| 140 |
}; |
| 141 |
|
| 142 |
AGGView::AGGView(BRect frame, |
| 143 |
agg::platform_support* agg, |
| 144 |
agg::pix_format_e format, |
| 145 |
bool flipY) |
| 146 |
: BView(frame, "AGG View", B_FOLLOW_ALL, |
| 147 |
B_FRAME_EVENTS | B_WILL_DRAW), |
| 148 |
fFormat(format), |
| 149 |
fFlipY(flipY), |
| 150 |
|
| 151 |
fAGG(agg), |
| 152 |
|
| 153 |
fMouseButtons(0), |
| 154 |
fMouseX(-1), |
| 155 |
fMouseY(-1), |
| 156 |
|
| 157 |
fLastKeyDown(0), |
| 158 |
|
| 159 |
fRedraw(true), |
| 160 |
|
| 161 |
fPulse(NULL), |
| 162 |
fLastPulse(0), |
| 163 |
fEnableTicks(true) |
| 164 |
{ |
| 165 |
SetViewColor(B_TRANSPARENT_32_BIT); |
| 166 |
|
| 167 |
frame.OffsetTo(0.0, 0.0); |
| 168 |
fBitmap = new BBitmap(frame, 0, pix_format_to_color_space(fFormat)); |
| 169 |
if (fBitmap->IsValid()) { |
| 170 |
attach_buffer_to_BBitmap(fAGG->rbuf_window(), fBitmap, fFlipY); |
| 171 |
} else { |
| 172 |
delete fBitmap; |
| 173 |
fBitmap = NULL; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
AGGView::~AGGView() |
| 179 |
{ |
| 180 |
delete fBitmap; |
| 181 |
delete fPulse; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void |
| 186 |
AGGView::AttachedToWindow() |
| 187 |
{ |
| 188 |
BMessage message('tick'); |
| 189 |
BMessenger target(this, Looper()); |
| 190 |
delete fPulse; |
| 191 |
// BScreen screen; |
| 192 |
// TODO: calc screen retrace |
| 193 |
fPulse = new BMessageRunner(target, &message, 40000); |
| 194 |
|
| 195 |
// make sure we call this once |
| 196 |
fAGG->on_resize(Bounds().IntegerWidth() + 1, |
| 197 |
Bounds().IntegerHeight() + 1); |
| 198 |
MakeFocus(); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
void |
| 203 |
AGGView::DetachedFromWindow() |
| 204 |
{ |
| 205 |
delete fPulse; |
| 206 |
fPulse = NULL; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
void |
| 211 |
AGGView::MessageReceived(BMessage* message) |
| 212 |
{ |
| 213 |
bigtime_t now = system_time(); |
| 214 |
switch (message->what) { |
| 215 |
case 'tick': |
| 216 |
// drop messages that have piled up |
| 217 |
if (/*now - fLastPulse > 30000*/fEnableTicks) { |
| 218 |
fLastPulse = now; |
| 219 |
if (!fAGG->wait_mode()) |
| 220 |
fAGG->on_idle(); |
| 221 |
Window()->PostMessage('entk', this); |
| 222 |
fEnableTicks = false; |
| 223 |
} else { |
| 224 |
// printf("dropping tick message (%lld)\n", now - fLastPulse); |
| 225 |
} |
| 226 |
break; |
| 227 |
case 'entk': |
| 228 |
fEnableTicks = true; |
| 229 |
if (now - fLastPulse > 30000) { |
| 230 |
fLastPulse = now; |
| 231 |
if (!fAGG->wait_mode()) |
| 232 |
fAGG->on_idle(); |
| 233 |
} |
| 234 |
break; |
| 235 |
default: |
| 236 |
BView::MessageReceived(message); |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
void |
| 243 |
AGGView::Draw(BRect updateRect) |
| 244 |
{ |
| 245 |
if (fBitmap) { |
| 246 |
if (fRedraw) { |
| 247 |
fAGG->on_draw(); |
| 248 |
fRedraw = false; |
| 249 |
} |
| 250 |
if (fFormat == agg::pix_format_bgra32) { |
| 251 |
DrawBitmap(fBitmap, updateRect, updateRect); |
| 252 |
} else { |
| 253 |
BBitmap* bitmap = new BBitmap(fBitmap->Bounds(), 0, B_RGBA32); |
| 254 |
|
| 255 |
agg::rendering_buffer rbufSrc; |
| 256 |
attach_buffer_to_BBitmap(rbufSrc, fBitmap, false); |
| 257 |
|
| 258 |
agg::rendering_buffer rbufDst; |
| 259 |
attach_buffer_to_BBitmap(rbufDst, bitmap, false); |
| 260 |
|
| 261 |
switch(fFormat) { |
| 262 |
case agg::pix_format_rgb555: |
| 263 |
agg::color_conv(&rbufDst, &rbufSrc, |
| 264 |
agg::color_conv_rgb555_to_bgra32()); |
| 265 |
break; |
| 266 |
case agg::pix_format_rgb565: |
| 267 |
agg::color_conv(&rbufDst, &rbufSrc, |
| 268 |
agg::color_conv_rgb565_to_bgra32()); |
| 269 |
break; |
| 270 |
case agg::pix_format_rgb24: |
| 271 |
agg::color_conv(&rbufDst, &rbufSrc, |
| 272 |
agg::color_conv_rgb24_to_bgra32()); |
| 273 |
break; |
| 274 |
case agg::pix_format_bgr24: |
| 275 |
agg::color_conv(&rbufDst, &rbufSrc, |
| 276 |
agg::color_conv_bgr24_to_bgra32()); |
| 277 |
break; |
| 278 |
case agg::pix_format_rgba32: |
| 279 |
agg::color_conv(&rbufDst, &rbufSrc, |
| 280 |
agg::color_conv_rgba32_to_bgra32()); |
| 281 |
break; |
| 282 |
case agg::pix_format_argb32: |
| 283 |
agg::color_conv(&rbufDst, &rbufSrc, |
| 284 |
agg::color_conv_argb32_to_bgra32()); |
| 285 |
break; |
| 286 |
case agg::pix_format_abgr32: |
| 287 |
agg::color_conv(&rbufDst, &rbufSrc, |
| 288 |
agg::color_conv_abgr32_to_bgra32()); |
| 289 |
break; |
| 290 |
case agg::pix_format_bgra32: |
| 291 |
agg::color_conv(&rbufDst, &rbufSrc, |
| 292 |
agg::color_conv_bgra32_to_bgra32()); |
| 293 |
break; |
| 294 |
} |
| 295 |
DrawBitmap(bitmap, updateRect, updateRect); |
| 296 |
delete bitmap; |
| 297 |
} |
| 298 |
} else { |
| 299 |
FillRect(updateRect); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
void |
| 305 |
AGGView::FrameResized(float width, float height) |
| 306 |
{ |
| 307 |
BRect r(0.0, 0.0, width, height); |
| 308 |
BBitmap* bitmap = new BBitmap(r, 0, pix_format_to_color_space(fFormat)); |
| 309 |
if (bitmap->IsValid()) { |
| 310 |
delete fBitmap; |
| 311 |
fBitmap = bitmap; |
| 312 |
attach_buffer_to_BBitmap(fAGG->rbuf_window(), fBitmap, fFlipY); |
| 313 |
|
| 314 |
fAGG->trans_affine_resizing((int)width + 1, |
| 315 |
(int)height + 1); |
| 316 |
|
| 317 |
// pass the event on to AGG |
| 318 |
fAGG->on_resize((int)width + 1, (int)height + 1); |
| 319 |
|
| 320 |
fRedraw = true; |
| 321 |
Invalidate(); |
| 322 |
} else |
| 323 |
delete bitmap; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
void |
| 328 |
AGGView::KeyDown(const char* bytes, int32 numBytes) |
| 329 |
{ |
| 330 |
if (bytes && numBytes > 0) { |
| 331 |
fLastKeyDown = bytes[0]; |
| 332 |
|
| 333 |
bool left = false; |
| 334 |
bool up = false; |
| 335 |
bool right = false; |
| 336 |
bool down = false; |
| 337 |
|
| 338 |
switch (fLastKeyDown) { |
| 339 |
|
| 340 |
case B_LEFT_ARROW: |
| 341 |
left = true; |
| 342 |
break; |
| 343 |
|
| 344 |
case B_UP_ARROW: |
| 345 |
up = true; |
| 346 |
break; |
| 347 |
|
| 348 |
case B_RIGHT_ARROW: |
| 349 |
right = true; |
| 350 |
break; |
| 351 |
|
| 352 |
case B_DOWN_ARROW: |
| 353 |
down = true; |
| 354 |
break; |
| 355 |
} |
| 356 |
|
| 357 |
/* case key_f2: |
| 358 |
fAGG->copy_window_to_img(agg::platform_support::max_images - 1); |
| 359 |
fAGG->save_img(agg::platform_support::max_images - 1, "screenshot"); |
| 360 |
break; |
| 361 |
}*/ |
| 362 |
|
| 363 |
|
| 364 |
if (fAGG->m_ctrls.on_arrow_keys(left, right, down, up)) { |
| 365 |
fAGG->on_ctrl_change(); |
| 366 |
fAGG->force_redraw(); |
| 367 |
} else { |
| 368 |
fAGG->on_key(fMouseX, fMouseY, fLastKeyDown, GetKeyFlags()); |
| 369 |
} |
| 370 |
// fAGG->on_key(fMouseX, fMouseY, fLastKeyDown, GetKeyFlags()); |
| 371 |
|
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
void |
| 377 |
AGGView::MouseDown(BPoint where) |
| 378 |
{ |
| 379 |
BMessage* currentMessage = Window()->CurrentMessage(); |
| 380 |
if (currentMessage) { |
| 381 |
if (currentMessage->FindInt32("buttons", (int32*)&fMouseButtons) < B_OK) |
| 382 |
fMouseButtons = B_PRIMARY_MOUSE_BUTTON; |
| 383 |
} else |
| 384 |
fMouseButtons = B_PRIMARY_MOUSE_BUTTON; |
| 385 |
|
| 386 |
fMouseX = (int)where.x; |
| 387 |
fMouseY = fFlipY ? (int)(Bounds().Height() - where.y) : (int)where.y; |
| 388 |
|
| 389 |
// pass the event on to AGG |
| 390 |
if (fMouseButtons == B_PRIMARY_MOUSE_BUTTON) { |
| 391 |
// left mouse button -> see if to handle in controls |
| 392 |
fAGG->m_ctrls.set_cur(fMouseX, fMouseY); |
| 393 |
if (fAGG->m_ctrls.on_mouse_button_down(fMouseX, fMouseY)) { |
| 394 |
fAGG->on_ctrl_change(); |
| 395 |
fAGG->force_redraw(); |
| 396 |
} else { |
| 397 |
if (fAGG->m_ctrls.in_rect(fMouseX, fMouseY)) { |
| 398 |
if (fAGG->m_ctrls.set_cur(fMouseX, fMouseY)) { |
| 399 |
fAGG->on_ctrl_change(); |
| 400 |
fAGG->force_redraw(); |
| 401 |
} |
| 402 |
} else { |
| 403 |
fAGG->on_mouse_button_down(fMouseX, fMouseY, GetKeyFlags()); |
| 404 |
} |
| 405 |
} |
| 406 |
} else if (fMouseButtons & B_SECONDARY_MOUSE_BUTTON) { |
| 407 |
// right mouse button -> simple |
| 408 |
fAGG->on_mouse_button_down(fMouseX, fMouseY, GetKeyFlags()); |
| 409 |
} |
| 410 |
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
void |
| 415 |
AGGView::MouseMoved(BPoint where, uint32 transit, const BMessage* dragMesage) |
| 416 |
{ |
| 417 |
// workarround missed mouse up events |
| 418 |
// (if we react too slowly, app_server might have dropped events) |
| 419 |
BMessage* currentMessage = Window()->CurrentMessage(); |
| 420 |
int32 buttons = 0; |
| 421 |
if (currentMessage->FindInt32("buttons", &buttons) < B_OK) { |
| 422 |
buttons = 0; |
| 423 |
} |
| 424 |
if (!buttons) |
| 425 |
MouseUp(where); |
| 426 |
|
| 427 |
fMouseX = (int)where.x; |
| 428 |
fMouseY = fFlipY ? (int)(Bounds().Height() - where.y) : (int)where.y; |
| 429 |
|
| 430 |
// pass the event on to AGG |
| 431 |
if (fAGG->m_ctrls.on_mouse_move(fMouseX, fMouseY, |
| 432 |
(GetKeyFlags() & agg::mouse_left) != 0)) { |
| 433 |
fAGG->on_ctrl_change(); |
| 434 |
fAGG->force_redraw(); |
| 435 |
} else { |
| 436 |
if (!fAGG->m_ctrls.in_rect(fMouseX, fMouseY)) { |
| 437 |
fAGG->on_mouse_move(fMouseX, fMouseY, GetKeyFlags()); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
void |
| 444 |
AGGView::MouseUp(BPoint where) |
| 445 |
{ |
| 446 |
fMouseX = (int)where.x; |
| 447 |
fMouseY = fFlipY ? (int)(Bounds().Height() - where.y) : (int)where.y; |
| 448 |
|
| 449 |
// pass the event on to AGG |
| 450 |
if (fMouseButtons == B_PRIMARY_MOUSE_BUTTON) { |
| 451 |
fMouseButtons = 0; |
| 452 |
|
| 453 |
if (fAGG->m_ctrls.on_mouse_button_up(fMouseX, fMouseY)) { |
| 454 |
fAGG->on_ctrl_change(); |
| 455 |
fAGG->force_redraw(); |
| 456 |
} |
| 457 |
fAGG->on_mouse_button_up(fMouseX, fMouseY, GetKeyFlags()); |
| 458 |
} else if (fMouseButtons == B_SECONDARY_MOUSE_BUTTON) { |
| 459 |
fMouseButtons = 0; |
| 460 |
|
| 461 |
fAGG->on_mouse_button_up(fMouseX, fMouseY, GetKeyFlags()); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
BBitmap* |
| 467 |
AGGView::Bitmap() const |
| 468 |
{ |
| 469 |
return fBitmap; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
uint8 |
| 474 |
AGGView::LastKeyDown() const |
| 475 |
{ |
| 476 |
return fLastKeyDown; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
uint32 |
| 481 |
AGGView::MouseButtons() |
| 482 |
{ |
| 483 |
uint32 buttons = 0; |
| 484 |
if (LockLooper()) { |
| 485 |
buttons = fMouseButtons; |
| 486 |
UnlockLooper(); |
| 487 |
} |
| 488 |
return buttons; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
void |
| 493 |
AGGView::Update() |
| 494 |
{ |
| 495 |
// trigger display update |
| 496 |
if (LockLooper()) { |
| 497 |
Invalidate(); |
| 498 |
UnlockLooper(); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
void |
| 504 |
AGGView::ForceRedraw() |
| 505 |
{ |
| 506 |
// force a redraw (fRedraw = true;) |
| 507 |
// and trigger display update |
| 508 |
if (LockLooper()) { |
| 509 |
fRedraw = true; |
| 510 |
Invalidate(); |
| 511 |
UnlockLooper(); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
unsigned |
| 517 |
AGGView::GetKeyFlags() |
| 518 |
{ |
| 519 |
uint32 buttons = fMouseButtons; |
| 520 |
uint32 mods = modifiers(); |
| 521 |
unsigned flags = 0; |
| 522 |
if (buttons & B_PRIMARY_MOUSE_BUTTON) flags |= agg::mouse_left; |
| 523 |
if (buttons & B_SECONDARY_MOUSE_BUTTON) flags |= agg::mouse_right; |
| 524 |
if (mods & B_SHIFT_KEY) flags |= agg::kbd_shift; |
| 525 |
if (mods & B_COMMAND_KEY) flags |= agg::kbd_ctrl; |
| 526 |
return flags; |
| 527 |
} |
| 528 |
|
| 529 |
// #pragma mark - |
| 530 |
|
| 531 |
|
| 532 |
class AGGWindow : public BWindow { |
| 533 |
public: |
| 534 |
AGGWindow() |
| 535 |
: BWindow(BRect(-50.0, -50.0, -10.0, -10.0), |
| 536 |
"AGG Application", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS) |
| 537 |
{ |
| 538 |
} |
| 539 |
|
| 540 |
virtual bool QuitRequested() |
| 541 |
{ |
| 542 |
be_app->PostMessage(B_QUIT_REQUESTED); |
| 543 |
return true; |
| 544 |
} |
| 545 |
|
| 546 |
bool Init(BRect frame, agg::platform_support* agg, agg::pix_format_e format, |
| 547 |
bool flipY, uint32 flags) |
| 548 |
{ |
| 549 |
MoveTo(frame.LeftTop()); |
| 550 |
ResizeTo(frame.Width(), frame.Height()); |
| 551 |
|
| 552 |
SetFlags(flags); |
| 553 |
|
| 554 |
frame.OffsetTo(0.0, 0.0); |
| 555 |
fView = new AGGView(frame, agg, format, flipY); |
| 556 |
AddChild(fView); |
| 557 |
|
| 558 |
return fView->Bitmap() != NULL; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
AGGView* View() const |
| 563 |
{ |
| 564 |
return fView; |
| 565 |
} |
| 566 |
private: |
| 567 |
AGGView* fView; |
| 568 |
}; |
| 569 |
|
| 570 |
// #pragma mark - |
| 571 |
|
| 572 |
|
| 573 |
class AGGApplication : public BApplication { |
| 574 |
public: |
| 575 |
AGGApplication() |
| 576 |
: BApplication("application/x-vnd.AGG-AGG") |
| 577 |
{ |
| 578 |
fWindow = new AGGWindow(); |
| 579 |
} |
| 580 |
|
| 581 |
virtual void ReadyToRun() |
| 582 |
{ |
| 583 |
if (fWindow) { |
| 584 |
fWindow->Show(); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
virtual bool Init(agg::platform_support* agg, int width, int height, |
| 589 |
agg::pix_format_e format, bool flipY, uint32 flags) |
| 590 |
{ |
| 591 |
BRect r(50.0, 50.0, |
| 592 |
50.0 + width - 1.0, |
| 593 |
50.0 + height - 1.0); |
| 594 |
uint32 windowFlags = B_ASYNCHRONOUS_CONTROLS; |
| 595 |
if (!(flags & agg::window_resize)) |
| 596 |
windowFlags |= B_NOT_RESIZABLE; |
| 597 |
|
| 598 |
return fWindow->Init(r, agg, format, flipY, windowFlags);; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
AGGWindow* Window() const |
| 603 |
{ |
| 604 |
return fWindow; |
| 605 |
} |
| 606 |
|
| 607 |
private: |
| 608 |
AGGWindow* fWindow; |
| 609 |
}; |
| 610 |
|
| 611 |
|
| 612 |
// #pragma mark - |
| 613 |
|
| 614 |
|
| 615 |
namespace agg |
| 616 |
{ |
| 617 |
|
| 618 |
class platform_specific { |
| 619 |
public: |
| 620 |
platform_specific(agg::platform_support* agg, |
| 621 |
agg::pix_format_e format, bool flip_y) |
| 622 |
: fAGG(agg), |
| 623 |
fApp(NULL), |
| 624 |
fFormat(format), |
| 625 |
fFlipY(flip_y), |
| 626 |
fTimerStart(system_time()) |
| 627 |
{ |
| 628 |
memset(fImages, 0, sizeof(fImages)); |
| 629 |
fApp = new AGGApplication(); |
| 630 |
fAppPath[0] = 0; |
| 631 |
// figure out where we're running from |
| 632 |
app_info info; |
| 633 |
status_t ret = fApp->GetAppInfo(&info); |
| 634 |
if (ret >= B_OK) { |
| 635 |
BPath path(&info.ref); |
| 636 |
ret = path.InitCheck(); |
| 637 |
if (ret >= B_OK) { |
| 638 |
ret = path.GetParent(&path); |
| 639 |
if (ret >= B_OK) { |
| 640 |
std::sprintf(fAppPath, "%s", path.Path()); |
| 641 |
} else { |
| 642 |
std::fprintf(stderr, "getting app parent folder failed: %s\n", std::strerror(ret)); |
| 643 |
} |
| 644 |
} else { |
| 645 |
std::fprintf(stderr, "making app path failed: %s\n", std::strerror(ret)); |
| 646 |
} |
| 647 |
} else { |
| 648 |
std::fprintf(stderr, "GetAppInfo() failed: %s\n", std::strerror(ret)); |
| 649 |
} |
| 650 |
} |
| 651 |
~platform_specific() |
| 652 |
{ |
| 653 |
for (int32 i = 0; i < agg::platform_support::max_images; i++) |
| 654 |
delete fImages[i]; |
| 655 |
delete fApp; |
| 656 |
} |
| 657 |
|
| 658 |
bool Init(int width, int height, unsigned flags) |
| 659 |
{ |
| 660 |
return fApp->Init(fAGG, width, height, fFormat, fFlipY, flags); |
| 661 |
} |
| 662 |
|
| 663 |
int Run() |
| 664 |
{ |
| 665 |
status_t ret = B_NO_INIT; |
| 666 |
if (fApp) { |
| 667 |
fApp->Run(); |
| 668 |
ret = B_OK; |
| 669 |
} |
| 670 |
return ret; |
| 671 |
} |
| 672 |
|
| 673 |
void SetTitle(const char* title) |
| 674 |
{ |
| 675 |
if (fApp && fApp->Window() && fApp->Window()->Lock()) { |
| 676 |
fApp->Window()->SetTitle(title); |
| 677 |
fApp->Window()->Unlock(); |
| 678 |
} |
| 679 |
} |
| 680 |
void StartTimer() |
| 681 |
{ |
| 682 |
fTimerStart = system_time(); |
| 683 |
} |
| 684 |
double ElapsedTime() const |
| 685 |
{ |
| 686 |
return (system_time() - fTimerStart) / 1000.0; |
| 687 |
} |
| 688 |
|
| 689 |
void ForceRedraw() |
| 690 |
{ |
| 691 |
fApp->Window()->View()->ForceRedraw(); |
| 692 |
} |
| 693 |
void UpdateWindow() |
| 694 |
{ |
| 695 |
fApp->Window()->View()->Update(); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
agg::platform_support* fAGG; |
| 700 |
AGGApplication* fApp; |
| 701 |
agg::pix_format_e fFormat; |
| 702 |
bool fFlipY; |
| 703 |
bigtime_t fTimerStart; |
| 704 |
BBitmap* fImages[agg::platform_support::max_images]; |
| 705 |
|
| 706 |
char fAppPath[B_PATH_NAME_LENGTH]; |
| 707 |
char fFilePath[B_PATH_NAME_LENGTH]; |
| 708 |
}; |
| 709 |
|
| 710 |
|
| 711 |
//------------------------------------------------------------------------ |
| 712 |
platform_support::platform_support(pix_format_e format, bool flip_y) : |
| 713 |
m_specific(new platform_specific(this, format, flip_y)), |
| 714 |
m_format(format), |
| 715 |
m_bpp(32/*m_specific->m_bpp*/), |
| 716 |
m_window_flags(0), |
| 717 |
m_wait_mode(true), |
| 718 |
m_flip_y(flip_y), |
| 719 |
m_initial_width(10), |
| 720 |
m_initial_height(10) |
| 721 |
{ |
| 722 |
std::strcpy(m_caption, "Anti-Grain Geometry Application"); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
//------------------------------------------------------------------------ |
| 727 |
platform_support::~platform_support() |
| 728 |
{ |
| 729 |
delete m_specific; |
| 730 |
} |
| 731 |
|
| 732 |
//------------------------------------------------------------------------ |
| 733 |
void platform_support::caption(const char* cap) |
| 734 |
{ |
| 735 |
std::strcpy(m_caption, cap); |
| 736 |
m_specific->SetTitle(cap); |
| 737 |
} |
| 738 |
|
| 739 |
//------------------------------------------------------------------------ |
| 740 |
void platform_support::start_timer() |
| 741 |
{ |
| 742 |
m_specific->StartTimer(); |
| 743 |
} |
| 744 |
|
| 745 |
//------------------------------------------------------------------------ |
| 746 |
double platform_support::elapsed_time() const |
| 747 |
{ |
| 748 |
return m_specific->ElapsedTime(); |
| 749 |
} |
| 750 |
|
| 751 |
//------------------------------------------------------------------------ |
| 752 |
void* platform_support::raw_display_handler() |
| 753 |
{ |
| 754 |
// TODO: if we ever support BDirectWindow here, that would |
| 755 |
// be the frame buffer pointer with offset to the window top left |
| 756 |
return NULL; |
| 757 |
} |
| 758 |
|
| 759 |
//------------------------------------------------------------------------ |
| 760 |
void platform_support::message(const char* msg) |
| 761 |
{ |
| 762 |
BAlert* alert = new BAlert("AGG Message", msg, "Ok"); |
| 763 |
alert->Go(/*NULL*/); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
//------------------------------------------------------------------------ |
| 768 |
bool platform_support::init(unsigned width, unsigned height, unsigned flags) |
| 769 |
{ |
| 770 |
m_initial_width = width; |
| 771 |
m_initial_height = height; |
| 772 |
m_window_flags = flags; |
| 773 |
|
| 774 |
if (m_specific->Init(width, height, flags)) { |
| 775 |
on_init(); |
| 776 |
return true; |
| 777 |
} |
| 778 |
|
| 779 |
return false; |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
//------------------------------------------------------------------------ |
| 784 |
int platform_support::run() |
| 785 |
{ |
| 786 |
return m_specific->Run(); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
//------------------------------------------------------------------------ |
| 791 |
const char* platform_support::img_ext() const { return ".ppm"; } |
| 792 |
|
| 793 |
|
| 794 |
const char* platform_support::full_file_name(const char* file_name) |
| 795 |
{ |
| 796 |
std::sprintf(m_specific->fFilePath, "%s/%s", m_specific->fAppPath, file_name); |
| 797 |
return m_specific->fFilePath; |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
//------------------------------------------------------------------------ |
| 802 |
bool platform_support::load_img(unsigned idx, const char* file) |
| 803 |
{ |
| 804 |
if (idx < max_images) |
| 805 |
{ |
| 806 |
char path[B_PATH_NAME_LENGTH]; |
| 807 |
std::sprintf(path, "%s/%s%s", m_specific->fAppPath, file, img_ext()); |
| 808 |
BBitmap* transBitmap = BTranslationUtils::GetBitmap(path); |
| 809 |
if (transBitmap && transBitmap->IsValid()) { |
| 810 |
if(transBitmap->ColorSpace() != B_RGB32 && transBitmap->ColorSpace() != B_RGBA32) { |
| 811 |
// ups we got a smart ass Translator making our live harder |
| 812 |
delete transBitmap; |
| 813 |
return false; |
| 814 |
} |
| 815 |
|
| 816 |
color_space format = B_RGB24; |
| 817 |
|
| 818 |
switch (m_format) { |
| 819 |
case pix_format_gray8: |
| 820 |
format = B_GRAY8; |
| 821 |
break; |
| 822 |
case pix_format_rgb555: |
| 823 |
format = B_RGB15; |
| 824 |
break; |
| 825 |
case pix_format_rgb565: |
| 826 |
format = B_RGB16; |
| 827 |
break; |
| 828 |
case pix_format_rgb24: |
| 829 |
format = B_RGB24_BIG; |
| 830 |
break; |
| 831 |
case pix_format_bgr24: |
| 832 |
format = B_RGB24; |
| 833 |
break; |
| 834 |
case pix_format_abgr32: |
| 835 |
case pix_format_argb32: |
| 836 |
case pix_format_bgra32: |
| 837 |
format = B_RGB32; |
| 838 |
break; |
| 839 |
case pix_format_rgba32: |
| 840 |
format = B_RGB32_BIG; |
| 841 |
break; |
| 842 |
} |
| 843 |
BBitmap* bitmap = new (nothrow) BBitmap(transBitmap->Bounds(), 0, format); |
| 844 |
if (!bitmap || !bitmap->IsValid()) { |
| 845 |
std::fprintf(stderr, "failed to allocate temporary bitmap!\n"); |
| 846 |
delete transBitmap; |
| 847 |
delete bitmap; |
| 848 |
return false; |
| 849 |
} |
| 850 |
|
| 851 |
delete m_specific->fImages[idx]; |
| 852 |
|
| 853 |
rendering_buffer rbuf_tmp; |
| 854 |
attach_buffer_to_BBitmap(rbuf_tmp, transBitmap, m_flip_y); |
| 855 |
|
| 856 |
m_specific->fImages[idx] = bitmap; |
| 857 |
|
| 858 |
attach_buffer_to_BBitmap(m_rbuf_img[idx], bitmap, m_flip_y); |
| 859 |
|
| 860 |
rendering_buffer* dst = &m_rbuf_img[idx]; |
| 861 |
|
| 862 |
switch(m_format) |
| 863 |
{ |
| 864 |
case pix_format_gray8: |
| 865 |
return false; |
| 866 |
// color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_gray8()); break; |
| 867 |
break; |
| 868 |
|
| 869 |
case pix_format_rgb555: |
| 870 |
color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgb555()); break; |
| 871 |
break; |
| 872 |
|
| 873 |
case pix_format_rgb565: |
| 874 |
color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgb565()); break; |
| 875 |
break; |
| 876 |
|
| 877 |
case pix_format_rgb24: |
| 878 |
color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgb24()); break; |
| 879 |
break; |
| 880 |
|
| 881 |
case pix_format_bgr24: |
| 882 |
color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_bgr24()); break; |
| 883 |
break; |
| 884 |
|
| 885 |
case pix_format_abgr32: |
| 886 |
color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_abgr32()); break; |
| 887 |
break; |
| 888 |
|
| 889 |
case pix_format_argb32: |
| 890 |
color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_argb32()); break; |
| 891 |
break; |
| 892 |
|
| 893 |
case pix_format_bgra32: |
| 894 |
color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_bgra32()); break; |
| 895 |
break; |
| 896 |
|
| 897 |
case pix_format_rgba32: |
| 898 |
color_conv(dst, &rbuf_tmp, color_conv_bgra32_to_rgba32()); break; |
| 899 |
break; |
| 900 |
} |
| 901 |
delete transBitmap; |
| 902 |
|
| 903 |
return true; |
| 904 |
|
| 905 |
} else { |
| 906 |
std::fprintf(stderr, "failed to load bitmap: '%s'\n", full_file_name(file)); |
| 907 |
} |
| 908 |
} |
| 909 |
return false; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
//------------------------------------------------------------------------ |
| 915 |
bool platform_support::save_img(unsigned idx, const char* file) |
| 916 |
{ |
| 917 |
// TODO: implement using BTranslatorRoster and friends |
| 918 |
return false; |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
//------------------------------------------------------------------------ |
| 924 |
bool platform_support::create_img(unsigned idx, unsigned width, unsigned height) |
| 925 |
{ |
| 926 |
if(idx < max_images) |
| 927 |
{ |
| 928 |
if(width == 0) width = m_specific->fApp->Window()->View()->Bitmap()->Bounds().IntegerWidth() + 1; |
| 929 |
if(height == 0) height = m_specific->fApp->Window()->View()->Bitmap()->Bounds().IntegerHeight() + 1; |
| 930 |
BBitmap* bitmap = new BBitmap(BRect(0.0, 0.0, width - 1, height - 1), 0, B_RGBA32);; |
| 931 |
if (bitmap && bitmap->IsValid()) { |
| 932 |
delete m_specific->fImages[idx]; |
| 933 |
m_specific->fImages[idx] = bitmap; |
| 934 |
attach_buffer_to_BBitmap(m_rbuf_img[idx], bitmap, m_flip_y); |
| 935 |
return true; |
| 936 |
} else { |
| 937 |
delete bitmap; |
| 938 |
} |
| 939 |
} |
| 940 |
return false; |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
//------------------------------------------------------------------------ |
| 945 |
void platform_support::force_redraw() |
| 946 |
{ |
| 947 |
m_specific->ForceRedraw(); |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
//------------------------------------------------------------------------ |
| 953 |
void platform_support::update_window() |
| 954 |
{ |
| 955 |
m_specific->UpdateWindow(); |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
//------------------------------------------------------------------------ |
| 960 |
void platform_support::on_init() {} |
| 961 |
void platform_support::on_resize(int sx, int sy) {} |
| 962 |
void platform_support::on_idle() {} |
| 963 |
void platform_support::on_mouse_move(int x, int y, unsigned flags) {} |
| 964 |
void platform_support::on_mouse_button_down(int x, int y, unsigned flags) {} |
| 965 |
void platform_support::on_mouse_button_up(int x, int y, unsigned flags) {} |
| 966 |
void platform_support::on_key(int x, int y, unsigned key, unsigned flags) {} |
| 967 |
void platform_support::on_ctrl_change() {} |
| 968 |
void platform_support::on_draw() {} |
| 969 |
void platform_support::on_post_draw(void* raw_handler) {} |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
//---------------------------------------------------------------------------- |
| 978 |
int agg_main(int argc, char* argv[]); |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
int |
| 983 |
main(int argc, char* argv[]) |
| 984 |
{ |
| 985 |
return agg_main(argc, argv); |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|