| 508 | } |
| 509 | |
| 510 | Int32 TexturePacker::packTextures() { |
| 511 | TexturePackerTex* t = NULL; |
| 512 | |
| 513 | addBorderToTextures( (Int32)mPixelBorder ); |
| 514 | |
| 515 | newFree( 0, 0, mWidth, mHeight ); |
| 516 | |
| 517 | mCount = (Int32)mTextures.size(); |
| 518 | |
| 519 | // We must place each texture |
| 520 | std::vector<TexturePackerTex*>::iterator it; |
| 521 | for ( it = mTextures.begin(); it != mTextures.end(); ++it ) { |
| 522 | // For the texture with the longest edge we place it according to this criteria. |
| 523 | // (1) If it is a perfect match, we always accept it as it causes the least amount of |
| 524 | // fragmentation. (2) A match of one edge with the minimum area left over after the split. |
| 525 | // (3) No edges match, so look for the node which leaves the least amount of area left |
| 526 | // over after the split. |
| 527 | |
| 528 | if ( PackBig == mStrategy ) { |
| 529 | t = getLongestEdge(); |
| 530 | } else if ( PackTiny == mStrategy ) { |
| 531 | t = getShortestEdge(); |
| 532 | } |
| 533 | |
| 534 | TexturePackerNode* previousBestFit = NULL; |
| 535 | Int32 edgeCount = 0; |
| 536 | TexturePackerNode* bestFit = getBestFit( t, &previousBestFit, &edgeCount ); |
| 537 | |
| 538 | if ( NULL == bestFit ) { |
| 539 | if ( PackBig == mStrategy ) { |
| 540 | Log::debug( "TexturePacker: Changing Strategy to Tiny. %s faults.", |
| 541 | t->name().c_str() ); |
| 542 | reset(); |
| 543 | addBorderToTextures( -( (Int32)mPixelBorder ) ); |
| 544 | mStrategy = PackTiny; |
| 545 | return packTextures(); |
| 546 | } else if ( PackTiny == mStrategy ) { |
| 547 | mStrategy = PackFail; |
| 548 | Log::warning( "TexturePacker: Strategy fail, must expand image or create a new " |
| 549 | "one. %s faults.", |
| 550 | t->name().c_str() ); |
| 551 | } |
| 552 | } else { |
| 553 | insertTexture( t, bestFit, edgeCount, previousBestFit ); |
| 554 | mCount--; |
| 555 | } |
| 556 | |
| 557 | if ( PackFail == mStrategy ) { |
| 558 | if ( mWidth < mMaxSize.getWidth() || mHeight < mMaxSize.getHeight() ) { |
| 559 | reset(); |
| 560 | addBorderToTextures( -( (Int32)mPixelBorder ) ); |
| 561 | |
| 562 | if ( mWidth <= mHeight ) { |
| 563 | mWidth *= 2; |
| 564 | |
| 565 | if ( mWidth > mMaxSize.getWidth() ) |
| 566 | mWidth = mMaxSize.getWidth(); |
| 567 | } else { |