void CTilesetManager::Init(const char * szGfxPack) { //Remove all existing tilesets std::vector<CTileset*>::iterator iter = tilesetlist.begin(), lim = tilesetlist.end(); while (iter != lim) { delete (*iter); iter = tilesetlist.erase(iter); lim = tilesetlist.end(); } //Add in tilesets from the new gfxpack (if the gfxpack isn't "Classic") if (strcmp(getFileFromPath(szGfxPack).c_str(), "Classic")) { std::string s = convertPath("gfx/packs/tilesets", szGfxPack) + getDirectorySeperator(); SimpleDirectoryList dirlist(s); short iLength = dirlist.GetCount(); for (short i = 0; i < iLength; i++) { CTileset * tileset = new CTileset(dirlist.current_name()); tilesetlist.push_back(tileset); //If the tileset is "classic" then keep it's index for common operations if (!strcmp(tileset->GetName(), "Classic")) { tClassicTileset = tileset; iClassicTilesetIndex = i; } dirlist.next(); } } //Add in tilesets from the classic tileset to fill the gaps short iLength = filelist.size(); char szTilesetName[128]; for (short i = 0; i < iLength; i++) { strncpy(szTilesetName, getFileFromPath(filelist[i]).c_str(), 128); szTilesetName[127] = 0; //See if the new tileset already exists and if it does, skip it short iTilesetSize = tilesetlist.size(); bool fFound = false; for (short iTileset = 0; iTileset < iTilesetSize; iTileset++) { if (!strcmp(tilesetlist[iTileset]->GetName(), szTilesetName)) { fFound = true; break; } } //Add the tileset if another one by the same name isn't already in the tileset if (!fFound) { CTileset * tileset = new CTileset(filelist[i].c_str()); tilesetlist.push_back(tileset); if (!strcmp(tileset->GetName(), "Classic")) { tClassicTileset = tileset; iClassicTilesetIndex = i; } } } }
CTileset::CTileset(const char * szpath) { strncpy(szName, getFileFromPath(szpath).c_str(), 256); szName[255] = 0; char szFile[256]; strcpy(szFile, szpath); strcat(szFile, "/large.png"); gfx_loadimage(&sSprites[0], convertPartialPath(szFile), false); sSurfaces[0] = sSprites[0].getSurface(); //optimization for repeat surface use iWidth = sSprites[0].getWidth() / TILESIZE; iHeight = sSprites[0].getHeight() / TILESIZE; strcpy(szFile, szpath); strcat(szFile, "/medium.png"); gfx_loadimage(&sSprites[1], convertPartialPath(szFile), false); sSurfaces[1] = sSprites[1].getSurface(); strcpy(szFile, szpath); strcat(szFile, "/small.png"); gfx_loadimage(&sSprites[2], convertPartialPath(szFile), false); sSurfaces[2] = sSprites[2].getSurface(); strcpy(szFile, szpath); strcat(szFile, "/tileset.tls"); strcpy(szTilesetPath, convertPartialPath(szFile).c_str()); ReadTileTypeFile(szTilesetPath); }
int SloxTextureFactory::readImageFile( const char* filename, unsigned int* texture_r ) { int ret = 0 ; unsigned int texture ; SDL_Surface* surface ; int nOfColors ; GLenum texture_format ; std::string realpath ; *texture_r = 0; if( getFileFromPath( filename, realpath ) ) { m_message = string( "File ") + filename + " does not exist on the path!" ; return -1 ; } if( (surface = IMG_Load( realpath.c_str() ) ) ) { if ( surface->w & (surface->w - 1) ) { m_message = "Warning: width not power of 2" ; ret = 1 ; } if ( surface->h & (surface->h - 1) ) { m_message = "Warning: height not power of 2" ; ret = 1 ; } nOfColors = surface->format->BytesPerPixel ; if (nOfColors == 4) // contains an alpha channel { if (surface->format->Rmask == 0x000000ff) texture_format = GL_RGBA; else texture_format = GL_BGRA; } else if (nOfColors == 3) // no alpha channel { if (surface->format->Rmask == 0x000000ff) texture_format = GL_RGB; else texture_format = GL_BGR; } else { m_message = "warning: image not truecolor\n"; ret = 1; } // Have OpenGL generate a texture object handle for us glGenTextures( 1, &texture ); // Bind the texture object glBindTexture( GL_TEXTURE_2D, texture ); // Set the texture's stretching properties glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // Edit the texture object's image data using the information SDL_Surface gives us glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels ); } else { char error[ 256 ]; snprintf( error, 256, "Error: could not load image: %s\n", SDL_GetError() ); m_message = error; return -1; } // Free the SDL_Surface only if it was successfully created if ( surface ) { SDL_FreeSurface( surface ); } *texture_r = texture; return ret ; }