| 1 | /* |
| 2 | Copyright (C) 2001-2006, William Joseph. |
| 3 | All Rights Reserved. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 7 | GtkRadiant is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GtkRadiant is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GtkRadiant; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "image.h" |
| 23 | |
| 24 | #include "modulesystem.h" |
| 25 | #include "iimage.h" |
| 26 | #include "ifilesystem.h" |
| 27 | #include "iarchive.h" |
| 28 | |
| 29 | #include "generic/reference.h" |
| 30 | #include "os/path.h" |
| 31 | #include "stream/stringstream.h" |
| 32 | |
| 33 | typedef Modules<_QERPlugImageTable> ImageModules; |
| 34 | ImageModules& Textures_getImageModules(); |
| 35 | ImageModules& Textures_getFallbackImageModules(); |
| 36 | |
| 37 | /// \brief Returns a new image for the first file matching \p name in one of the available texture formats, or 0 if no file is found. |
| 38 | Image* QERApp_LoadImage( void* environment, const char* name ){ |
| 39 | Image* image = 0; |
| 40 | class LoadImageVisitor : public ImageModules::Visitor |
| 41 | { |
| 42 | const char* m_name; |
| 43 | Image*& m_image; |
| 44 | public: |
| 45 | LoadImageVisitor( const char* name, Image*& image ) |
| 46 | : m_name( name ), m_image( image ){ |
| 47 | } |
| 48 | void visit( const char* name, const _QERPlugImageTable& table ) const { |
| 49 | if ( m_image == 0 ) { |
| 50 | StringOutputStream fullname( 256 ); |
| 51 | fullname << m_name << '.' << name; |
| 52 | ArchiveFile* file = GlobalFileSystem().openFile( fullname.c_str() ); |
| 53 | |
| 54 | // also look for .dds image in dds/ prefix like Doom3 or DarkPlaces |
| 55 | if ( file == 0 && !string_compare( name, "dds" ) ) |
| 56 | { |
| 57 | fullname.clear(); |
| 58 | fullname << name << '/' << m_name << '.' << name; |
| 59 | file = GlobalFileSystem().openFile( fullname.c_str() ); |
| 60 | } |
| 61 | |
| 62 | if ( file != 0 ) { |
| 63 | // tell user which image file is found for the given texture path |
| 64 | globalOutputStream() << "Found image file: " << makeQuoted( fullname.c_str() ) << "\n"; |
| 65 | |
| 66 | m_image = table.loadImage( *file ); |
| 67 | file->release(); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | Textures_getImageModules().foreachModule( LoadImageVisitor( name, image ) ); |
| 74 | |
| 75 | // Games can provide their own fallback, so only do this when previous |
| 76 | // loading attempt did not work. |
| 77 | if ( image == 0 && !!string_compare_nocase( name, "textures/radiant" ) ) |
| 78 | { |
| 79 | Textures_getFallbackImageModules().foreachModule( LoadImageVisitor( name, image ) ); |
| 80 | } |
| 81 | |
| 82 | return image; |
| 83 | } |