]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/model/plugin.cpp
netradiant: strip 16-bit png to 8-bit, fix #153
[xonotic/netradiant.git] / plugins / model / plugin.cpp
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 <stdio.h>
23 #include "picomodel.h"
24 typedef unsigned char byte;
25 #include <stdlib.h>
26 #include <algorithm>
27 #include <list>
28
29 #include "iscenegraph.h"
30 #include "irender.h"
31 #include "iselection.h"
32 #include "iimage.h"
33 #include "imodel.h"
34 #include "igl.h"
35 #include "ifilesystem.h"
36 #include "iundo.h"
37 #include "ifiletypes.h"
38
39 #include "modulesystem/singletonmodule.h"
40 #include "stream/textstream.h"
41 #include "string/string.h"
42 #include "stream/stringstream.h"
43 #include "typesystem.h"
44
45 #include "model.h"
46
47 void PicoPrintFunc( int level, const char *str ){
48         if ( str == 0 ) {
49                 return;
50         }
51         switch ( level )
52         {
53         case PICO_NORMAL:
54                 globalOutputStream() << str << "\n";
55                 break;
56
57         case PICO_VERBOSE:
58                 //globalOutputStream() << "PICO_VERBOSE: " << str << "\n";
59                 break;
60
61         case PICO_WARNING:
62                 globalErrorStream() << "PICO_WARNING: " << str << "\n";
63                 break;
64
65         case PICO_ERROR:
66                 globalErrorStream() << "PICO_ERROR: " << str << "\n";
67                 break;
68
69         case PICO_FATAL:
70                 globalErrorStream() << "PICO_FATAL: " << str << "\n";
71                 break;
72         }
73 }
74
75 void PicoLoadFileFunc( const char *name, byte **buffer, int *bufSize ){
76         *bufSize = vfsLoadFile( name, (void**) buffer );
77 }
78
79 void PicoFreeFileFunc( void* file ){
80         vfsFreeFile( file );
81 }
82
83 void pico_initialise(){
84         PicoInit();
85         PicoSetMallocFunc( malloc );
86         PicoSetFreeFunc( free );
87         PicoSetPrintFunc( PicoPrintFunc );
88         PicoSetLoadFileFunc( PicoLoadFileFunc );
89         PicoSetFreeFileFunc( PicoFreeFileFunc );
90 }
91
92
93 class PicoModelLoader : public ModelLoader
94 {
95 const picoModule_t* m_module;
96 public:
97 PicoModelLoader( const picoModule_t* module ) : m_module( module ){
98 }
99 scene::Node& loadModel( ArchiveFile& file ){
100         return loadPicoModel( m_module, file );
101 }
102 };
103
104 class ModelPicoDependencies :
105         public GlobalFileSystemModuleRef,
106         public GlobalOpenGLModuleRef,
107         public GlobalUndoModuleRef,
108         public GlobalSceneGraphModuleRef,
109         public GlobalShaderCacheModuleRef,
110         public GlobalSelectionModuleRef,
111         public GlobalFiletypesModuleRef
112 {
113 };
114
115 class ModelPicoAPI : public TypeSystemRef
116 {
117 PicoModelLoader m_modelLoader;
118 public:
119 typedef ModelLoader Type;
120
121 ModelPicoAPI( const char* extension, const picoModule_t* module ) :
122         m_modelLoader( module ){
123         StringOutputStream filter( 128 );
124         filter << "*." << extension;
125         GlobalFiletypesModule::getTable().addType( Type::Name(), extension, filetype_t( module->displayName, filter.c_str() ) );
126 }
127 ModelLoader* getTable(){
128         return &m_modelLoader;
129 }
130 };
131
132 class PicoModelAPIConstructor
133 {
134 CopiedString m_extension;
135 const picoModule_t* m_module;
136 public:
137 PicoModelAPIConstructor( const char* extension, const picoModule_t* module ) :
138         m_extension( extension ), m_module( module ){
139 }
140 const char* getName(){
141         return m_extension.c_str();
142 }
143 ModelPicoAPI* constructAPI( ModelPicoDependencies& dependencies ){
144         return new ModelPicoAPI( m_extension.c_str(), m_module );
145 }
146 void destroyAPI( ModelPicoAPI* api ){
147         delete api;
148 }
149 };
150
151
152 typedef SingletonModule<ModelPicoAPI, ModelPicoDependencies, PicoModelAPIConstructor> PicoModelModule;
153 typedef std::list<PicoModelModule> PicoModelModules;
154 PicoModelModules g_PicoModelModules;
155
156
157 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){
158         initialiseModule( server );
159
160         pico_initialise();
161
162         const picoModule_t** modules = PicoModuleList( 0 );
163         while ( *modules != 0 )
164         {
165                 const picoModule_t* module = *modules++;
166                 if ( module->canload && module->load ) {
167                         for ( char*const* ext = module->defaultExts; *ext != 0; ++ext )
168                         {
169                                 g_PicoModelModules.push_back( PicoModelModule( PicoModelAPIConstructor( *ext, module ) ) );
170                                 g_PicoModelModules.back().selfRegister();
171                         }
172                 }
173         }
174 }