// this is a modified version of Texture_ForName qtexture_t* WINAPI QERApp_TryTextureForName( const char* name ) { qtexture_t* q; char filename[1024]; for ( q = g_qeglobals.d_qtextures ; q ; q = q->next ) { if ( !strcmp( name, q->filename ) ) return q; } // try loading from file .. this is a copy of the worst part of Texture_ForName char cWork[1024]; sprintf( filename, "%s/%s.tga", ValueForKey( g_qeglobals.d_project_entity, "texturepath" ), name ); QE_ConvertDOSToUnixName( cWork, filename ); strcpy( filename, cWork ); unsigned char* pPixels = NULL; int nWidth; int nHeight; LoadImage( filename, &pPixels, &nWidth, &nHeight ); if ( pPixels == NULL ) { // try jpg // blatant assumption of .tga should be fine since we sprintf'd it above int nLen = strlen( filename ); filename[nLen - 3] = 'j'; filename[nLen - 2] = 'p'; filename[nLen - 1] = 'g'; LoadImage( filename, &pPixels, &nWidth, &nHeight );; if ( pPixels == NULL ) { // try png // blatant assumption of .tga should be fine since we sprintf'd it above int nLen = strlen( filename ); filename[nLen - 3] = 'p'; filename[nLen - 2] = 'n'; filename[nLen - 1] = 'g'; LoadImage( filename, &pPixels, &nWidth, &nHeight ); } } if ( pPixels ) { q = Texture_LoadTGATexture( pPixels, nWidth, nHeight, NULL, 0, 0, 0 ); //++timo storing the filename .. will be removed by shader code cleanup // this is dirty, and we sure miss some places were we should fill the filename info strcpy( q->filename, name ); SetNameShaderInfo( q, filename, name ); Sys_Printf( "done.\n", name ); free( pPixels ); return q; } return NULL; }
qtexture_t* WINAPI QERApp_Try_Texture_ForName(const char* name) { qtexture_t *q; char f1[1024],f2[1024]; unsigned char *pPixels = NULL; int nWidth,nHeight; // convert the texture name to the standard format we use in qtexture_t char *stdName = CleanTextureName(name); // use the hash table q = NULL; g_qeglobals.d_qtexmap->Lookup( stdName, (void *&)q ); if (q) return q; #ifdef QTEXMAP_DEBUG for (q=g_qeglobals.d_qtextures ; q ; q=q->next) { if (!strcmp(stdName, q->name)) { Sys_Printf("ERROR: %s is not in texture map, but was found in texture list\n"); return q; } } #endif #if 0 for (q=g_qeglobals.d_qtextures ; q ; q=q->next) { if (!strcmp(stdName, q->name)) return q; } #endif //++timo TODO: say something about loading the file? // try loading the texture //++timo FIXME: "texturepath" is no use now? sprintf(f1, "%s/%s", ValueForKey (g_qeglobals.d_project_entity, "basepath"), name); QE_ConvertDOSToUnixName( f2, f1 ); // NOTE: we may need a global strategy to support default extensions etc. strcpy(f1,f2); // check wether a filename extension was provided // NOTE: only works for 3 letters extensions ( .tga .jpg ... ) if (f1[strlen(f1)-4] == '.') { // try straight loading LoadImage( f1, &pPixels, &nWidth, &nHeight ); } if (!pPixels) { // try adding extensions, .tga first sprintf(f2,"%s.tga",f1); LoadImage( f2, &pPixels, &nWidth, &nHeight ); if (!pPixels) { // .jpg sprintf(f2,"%s.jpg",f1); LoadImage( f2, &pPixels, &nWidth, &nHeight ); } } if (!pPixels) // we failed return NULL; else { // TODO: display .pk3 file name if loaded from .pk3 (needs to write a VFS .. sort of) Sys_Printf("LOADED: %s\n", f2 ); } // instanciate a new qtexture_t // NOTE: when called by a plugin we must make sure we have set Radiant's GL context before binding the texture // we'll be binding the GL texture now // need to check we are using a right GL context // with GL plugins that have their own window, the GL context may be the plugin's, in which case loading textures will bug HDC currentHDC = qwglGetCurrentDC(); HGLRC currentHGLRC = qwglGetCurrentContext(); //++timo FIXME: this may duplicate with qtexture_t* WINAPI QERApp_Texture_ForName (const char *name) //++timo FIXME: we need a list of lawfull GL contexts or something? // I'd rather always use the same GL context for binding... if (currentHDC != g_qeglobals.d_hdcBase || currentHGLRC != g_qeglobals.d_hglrcBase) { #ifdef _DEBUG Sys_Printf("Switching context!\n"); #endif qwglMakeCurrent( g_qeglobals.d_hdcBase, g_qeglobals.d_hglrcBase ); } //++timo TODO: remove that and use our own implementation? q = Texture_LoadTGATexture( pPixels, nWidth, nHeight, NULL, 0, 0, 0); free(pPixels); // fill qtexture_t information //++timo FIXME: filename will be removed .. name for qtexture_t is actually the filename now // NOTE: see qtexture_s::name for naming conventions, must remove filename extension strcpy( q->filename, name ); strcpy( q->name, name ); // only strip extension if extension there is! if (q->name[strlen(q->name)-4] == '.') q->name[strlen(q->name)-4]='\0'; // hook into the main qtexture_t list q->next = g_qeglobals.d_qtextures; g_qeglobals.d_qtextures = q; // push it in the map g_qeglobals.d_qtexmap->SetAt( q->name, q ); return q; }