| 1581 | } |
| 1582 | |
| 1583 | gl::TextureRef Texture2dCache::cache( const Surface8u &originalData ) |
| 1584 | { |
| 1585 | const Surface8u &surfaceData = ( mIntermediateSurface ) ? *mIntermediateSurface : originalData; |
| 1586 | // If mIntermediateSurface isn't null then we need to use that instead. |
| 1587 | // This is to accommodate rowBytes values which aren't the same as width * bytesPerPixel |
| 1588 | if( mIntermediateSurface ) |
| 1589 | mIntermediateSurface->copyFrom( originalData, originalData.getBounds() ); |
| 1590 | |
| 1591 | // find an available slot and update that if possible |
| 1592 | for( vector<pair<int,TextureRef>>::iterator texIt = mTextures.begin(); texIt != mTextures.end(); ++texIt ) { |
| 1593 | if( texIt->first == -1 ) { // this texture is available, let's use it! |
| 1594 | texIt->second->update( surfaceData ); |
| 1595 | texIt->first = mNextId++; |
| 1596 | // normally this would be very wrong, but when the result TextureRef is destroyed, it calls markTextureAsFree rather than deleting the master texture |
| 1597 | return TextureRef( texIt->second.get(), std::bind( &Texture2dCache::markTextureAsFree, shared_from_this(), texIt->first ) ); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | // we didn't find an available slot, so let's make a new texture |
| 1602 | TextureRef masterTex( new Texture( surfaceData, mFormat ) ); |
| 1603 | mTextures.push_back( make_pair( mNextId++, masterTex ) ); |
| 1604 | // normally this would be very wrong, but when the result TextureRef is destroyed, it calls markTextureAsFree rather than deleting the master texture |
| 1605 | return TextureRef( mTextures.back().second.get(), std::bind( &Texture2dCache::markTextureAsFree, shared_from_this(), mTextures.back().first ) ); |
| 1606 | } |
| 1607 | |
| 1608 | void Texture2dCache::markTextureAsFree( int id ) |
| 1609 | { |