TexturePair AssetsManager::showTexture(const std::string textureName) { // if (!mOgreCEGUITexture) { // S_LOG_WARNING("You must first create a valid OgreCEGUITexture instance."); // return; // } if (Ogre::TextureManager::getSingleton().resourceExists(textureName)) { Ogre::TexturePtr texturePtr = static_cast<Ogre::TexturePtr>(Ogre::TextureManager::getSingleton().getByName(textureName)); if (!texturePtr.isNull()) { if (!texturePtr->isLoaded()) { try { texturePtr->load(); } catch (...) { S_LOG_WARNING("Error when loading " << textureName << ". This texture will not be shown."); return TexturePair(); } } std::string textureName(texturePtr->getName()); std::string imageSetName(textureName + "_AssetsManager"); return createTextureImage(texturePtr, imageSetName); // mOgreCEGUITexture->setOgreTexture(texturePtr); } } return TexturePair(); }
TexturePair AssetsManager::createTextureImage(Ogre::TexturePtr texturePtr, const std::string& imageSetName) { // if (mOgreCEGUITexture) { // GUIManager::getSingleton().getGuiRenderer()->destroyTexture(mOgreCEGUITexture); // mOgreCEGUITexture = 0; // } CEGUI::Imageset* textureImageset; if (CEGUI::ImagesetManager::getSingleton().isDefined(imageSetName)) { textureImageset = &CEGUI::ImagesetManager::getSingleton().get(imageSetName); } else { //create a CEGUI texture from our Ogre texture S_LOG_VERBOSE("Creating new CEGUI texture from Ogre texture."); CEGUI::Texture* ogreCEGUITexture = &GUIManager::getSingleton().getGuiRenderer()->createTexture(texturePtr); //we need a imageset in order to create GUI elements from the ceguiTexture S_LOG_VERBOSE("Creating new CEGUI imageset with name " << imageSetName); textureImageset = &CEGUI::ImagesetManager::getSingleton().create(imageSetName, *ogreCEGUITexture); //we only want one element: the whole texture textureImageset->defineImage("full_image", CEGUI::Rect(0, 0, texturePtr->getWidth(), texturePtr->getHeight()), CEGUI::Point(0, 0)); } //assign our image element to the StaticImage widget const CEGUI::Image* textureImage = &textureImageset->getImage("full_image"); return TexturePair(texturePtr, textureImage, textureImageset); }
void ResourceManager::AddTexture( const std::string& path, const sf::Texture& texture, bool managed ) { TextureMap::iterator texture_iter( m_textures.find( path ) ); if( texture_iter != m_textures.end() && texture_iter->second.second ) { delete texture_iter->second.first; } m_textures[path] = TexturePair( &texture, managed ); }
const sf::Texture* ResourceManager::GetTexture( const std::string& path ) { TextureMap::const_iterator texture_iter( m_textures.find( path ) ); if( texture_iter != m_textures.end() ) { return texture_iter->second.first; } // Try to load. ResourceLoader* loader( GetMatchingLoader( path ) ); if( !loader ) { return NULL; } const sf::Texture* texture( loader->LoadTexture( GetFilename( path, *loader ) ) ); if( !texture ) { return NULL; } // Cache. m_textures[path] = TexturePair( texture, true ); return texture; }
TexturePair AssetsManager::createTextureImage(Ogre::TexturePtr texturePtr, const std::string& imageName) { // if (mOgreCEGUITexture) { // GUIManager::getSingleton().getGuiRenderer()->destroyTexture(mOgreCEGUITexture); // mOgreCEGUITexture = 0; // } auto renderer = CEGUI::System::getSingleton().getRenderer(); CEGUI::Texture* ogreCEGUITexture; if (renderer->isTextureDefined(texturePtr->getName())) { ogreCEGUITexture = &renderer->getTexture(texturePtr->getName()); static_cast<CEGUI::OgreTexture*>(ogreCEGUITexture)->setOgreTexture(texturePtr); } else { //create a CEGUI texture from our Ogre texture S_LOG_VERBOSE("Creating new CEGUI texture from Ogre texture."); ogreCEGUITexture = &GUIManager::getSingleton().createTexture(texturePtr); } //assign our image element to the StaticImage widget CEGUI::Image* textureImage; if (CEGUI::ImageManager::getSingleton().isDefined(imageName)) { textureImage = &CEGUI::ImageManager::getSingleton().get(imageName); } else { textureImage = &CEGUI::ImageManager::getSingleton().create("BasicImage", imageName); } CEGUI::BasicImage* basicImage = static_cast<CEGUI::BasicImage*>(textureImage); basicImage->setTexture(ogreCEGUITexture); auto area = CEGUI::Rectf(0, 0, ogreCEGUITexture->getSize().d_width, ogreCEGUITexture->getSize().d_height); basicImage->setArea(area); basicImage->setNativeResolution(area.getSize()); basicImage->setAutoScaled(CEGUI::ASM_Both); return TexturePair(texturePtr, textureImage); }
bool CL_SpritePacker::pack(int max_width, int max_height, int min_width, int min_height, int max_textures) { // Fetch all frames from SpriteDescription. std::list<CL_SpriteDescription::FramePair>::const_iterator it_frames; const std::list<CL_SpriteDescription::FramePair> &frames = impl->description.get_frames(); std::list<TexturePair>::iterator it_packed_frames; // Make a new list with just as many elements as spritedescription. // A texture index of -1 means the texture hasn't been packed yet. for(int i = frames.size(); i > 0; i--) impl->packed_frames.push_back(TexturePair(-1, CL_Rect())); // Initial values for texture-size int width = min_width; int height = min_height; impl->texture_sizes.push_back(CL_Size(width, height)); CL_SpritePacker_Generic::Node root; int current_texture = 0; bool all_fit; do { root.rect = CL_Rect(0, 0, width, height); all_fit = true; // Pack all frames! for(it_frames = frames.begin(), it_packed_frames = impl->packed_frames.begin(); it_frames != frames.end(); ++it_frames, ++it_packed_frames) { if((*it_frames).second.get_width() > max_width || (*it_frames).second.get_height() > max_height) { #ifdef _DEBUG CL_Log::log("debug", "Error: Trying to pack sprite larger than the maximum"); CL_Log::log("debug", "Width: %1, Max Width %2", (*it_frames).second.get_width(), max_width); CL_Log::log("debug", "Height: %1, Max Height %2", (*it_frames).second.get_height(), max_height); #endif return false; } if((*it_packed_frames).first == -1) // Frame not yet been packed. { CL_SpritePacker_Generic::Node *node = root.insert(*it_frames); if(node) // Frame was packed ok. { (*it_packed_frames).first = current_texture; (*it_packed_frames).second = node->rect; } else // Frame couldn't fit into current texture. all_fit = false; } } if(all_fit == false) // All textures didnt fit. { // A maximum texture-size ? if((width == height && (width * 2) <= max_width) || (height * 2) <= max_height) { if(width == height && (width * 2) <= max_width) width *= 2; else height *= 2; // Clear current texture. for(it_packed_frames = impl->packed_frames.begin(); it_packed_frames != impl->packed_frames.end(); ++it_packed_frames) { if((*it_packed_frames).first == current_texture) (*it_packed_frames).first = -1; } impl->texture_sizes[current_texture] = CL_Size(width, height); } else { current_texture++; // Check if we still have textures available. if(current_texture >= max_textures && max_textures != -1) return false; // Start a minimum size for next texture. width = min_width; height = min_height; impl->texture_sizes.push_back(CL_Size(width, height)); } root.clear(); } } while(all_fit == false); return true; }
Texture* TextureManager::addTexture( const std::string& filename ) { Texture* ret = nullptr; FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP* dib = nullptr; // TODO This name is nonsense, change it // Find format from file signature fif = FreeImage_GetFileType( filename.c_str(), 0 ); if ( FIF_UNKNOWN == fif ) { // Find format from file extension fif = FreeImage_GetFIFFromFilename( filename.c_str() ); } if ( FIF_UNKNOWN == fif ) { // Still unknown std::string error = "Cannot determine " + filename + " image format."; LOG( logERROR ) << error; CORE_ASSERT( 0, error.c_str() ); return nullptr; } if ( FreeImage_FIFSupportsReading( fif ) ) { dib = FreeImage_Load( fif, filename.c_str() ); } std::string error = "Something went wrong while trying to load " + filename + "."; //CORE_ASSERT(dib, error.c_str()); if ( nullptr == dib ) { LOG( logERROR ) << error; return nullptr; } ret = new Texture( filename, GL_TEXTURE_2D ); unsigned char* data = FreeImage_GetBits( dib ); int bpp = FreeImage_GetBPP( dib ); int format = ( bpp == 24 ? GL_BGR : 0 ); // TODO Handle other formats int internal = ( bpp == 24 ? GL_RGB : 0 ); // TODO Handle other formats int w = FreeImage_GetWidth( dib ); int h = FreeImage_GetHeight( dib ); // FIXME(Charly): Use VLOG instead of the check if ( m_verbose ) { LOG( logINFO ) << "Image stats (" << filename << ") :\n" << "\tBPP : 0x" << std::hex << bpp << std::dec << std::endl << "\tFormat : 0x" << std::hex << format << std::dec << std::endl << "\tSize : " << w << ", " << h; } CORE_ASSERT( data, "Data is null" ); ret->initGL( internal, w, h, format, GL_UNSIGNED_BYTE, data ); ret->genMipmap( GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR ); m_textures.insert( TexturePair( filename, ret ) ); FreeImage_Unload( dib ); return ret; }