bool TextureAtlas::addImage(const ImageHandle& newImageHandle) { if(imageSubTextures.Find( newImageHandle ) != imageSubTextures.End()) return true; Image* newImage = newImageHandle.Resolve(); Rectangle newRect = rectanglePacker.Insert(newImage->getWidth()+1, newImage->getHeight()+1, gs_heuristic); if (newRect.height == 0 || newRect.width == 0){ if (atlasSize >= atlasMaxSize) return false; atlasSize = std::min(atlasSize*2, atlasMaxSize); resizeAtlas(atlasSize); return addImage(newImageHandle); } newRect.width--; newRect.height--; addImage(newImageHandle,newRect); return true; }
TexturePtr TextureManager::getTexture( const ImageHandle& imageHandle ) { Image* image = imageHandle.Resolve(); if (TexturePtr texture = getTexture(image)) return texture; // Create a new texture from image. Texture* texture = backend->createTexture(); texture->setImage(imageHandle); textures[image] = texture; return texture; }
void TextureManager::onUnloaded( const ResourceEvent& event ) { ImageHandle handleImage = HandleCast<Image>(event.handle); Image* image = handleImage.Resolve(); if( image->getResourceGroup() != ResourceGroup::Images ) return; if( textures.Find(image) == textures.End() ) return; LogDebug( "Removing texture '%s'", image->getPath().CString() ); removeTexture(image); }
void TextureManager::onLoaded( const ResourceEvent& event ) { ImageHandle handleImage = HandleCast<Image>(event.handle); Image* image = handleImage.Resolve(); if( image->getResourceGroup() != ResourceGroup::Images ) return; if( textures.Find(image) == textures.End() ) return; Texture* texture = textures[image].get(); texture->setImage(handleImage); backend->uploadTexture(texture); }
void TextureAtlas::addImage(ImageHandle newImageHandle, Rectangle newRect) { Image* newImage = newImageHandle.Resolve(); bool wasRotated = newImage->getWidth() == newRect.height && newImage->getHeight() == newRect.width; Image* atlasImage = atlasImageHandle.Resolve(); assert(newImage->getPixelFormat() == atlasImage->getPixelFormat()); if (wasRotated) RotateImage(newImage,atlasImage,Vector2i(newRect.x,newRect.y)); else atlasImage->setBuffer(newImage,Vector2i(newRect.x,newRect.y)); SubTexture subTexture; subTexture.atlas = this; float bottom = (float)(newRect.y+newRect.height)/atlasSize; float top = (float)newRect.y/atlasSize; float right = (float)(newRect.x+newRect.width)/atlasSize; float left = (float)newRect.x/atlasSize; if(!wasRotated) { subTexture.leftTopUV = Vector2(left,top); subTexture.rightTopUV = Vector2(right,top); subTexture.rightBottomUV = Vector2(right,bottom); subTexture.leftBottomUV = Vector2(left,bottom); } else { subTexture.rightTopUV = Vector2(left,top); subTexture.rightBottomUV = Vector2(right,top); subTexture.leftBottomUV = Vector2(right,bottom); subTexture.leftTopUV = Vector2(left,bottom); } imageSubTextures[newImageHandle] = subTexture; }
void TextureManager::onReloaded( const ResourceEvent& event ) { ImageHandle handleImage = HandleCast<Image>(event.handle); Image* newImage = handleImage.Resolve(); if( newImage->getResourceGroup() != ResourceGroup::Images ) return; Image* oldImage = (Image*) event.oldResource; if( textures.Find(oldImage) == textures.End() ) return; LogDebug( "Reloading texture '%s'", newImage->getPath().CString() ); Texture* texture = textures[oldImage].get(); texture->setImage(handleImage); textures.Erase(oldImage); textures[newImage] = texture; }
void wxImageComboBox::addImage( const ImageHandle& handle ) { Image* image = handle.Resolve(); Append( image->getPath() ); images.push_back(handle); }