vmap/plugins/model/plugin.cpp

194 lines
4.3 KiB
C++
Raw Normal View History

2020-11-17 11:16:16 +00:00
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include "picomodel.h"
typedef unsigned char byte;
#include <stdlib.h>
#include <algorithm>
#include <list>
#include "iscenegraph.h"
#include "irender.h"
#include "iselection.h"
#include "iimage.h"
#include "imodel.h"
#include "igl.h"
#include "ifilesystem.h"
#include "iundo.h"
#include "ifiletypes.h"
#include "modulesystem/singletonmodule.h"
#include "stream/textstream.h"
#include "string/string.h"
#include "stream/stringstream.h"
#include "typesystem.h"
#include "model.h"
void PicoPrintFunc(int level, const char *str)
{
2021-08-04 20:23:18 +00:00
if (str == 0) {
return;
}
switch (level) {
case PICO_NORMAL:
globalOutputStream() << str << "\n";
break;
case PICO_VERBOSE:
//globalOutputStream() << "PICO_VERBOSE: " << str << "\n";
break;
case PICO_WARNING:
globalErrorStream() << "PICO_WARNING: " << str << "\n";
break;
case PICO_ERROR:
globalErrorStream() << "PICO_ERROR: " << str << "\n";
break;
case PICO_FATAL:
globalErrorStream() << "PICO_FATAL: " << str << "\n";
break;
}
2020-11-17 11:16:16 +00:00
}
void PicoLoadFileFunc(const char *name, byte **buffer, int *bufSize)
{
2021-08-04 20:23:18 +00:00
*bufSize = vfsLoadFile(name, (void **) buffer);
2020-11-17 11:16:16 +00:00
}
void PicoFreeFileFunc(void *file)
{
2021-08-04 20:23:18 +00:00
vfsFreeFile(file);
2020-11-17 11:16:16 +00:00
}
void pico_initialise()
{
2021-08-04 20:23:18 +00:00
PicoInit();
PicoSetMallocFunc(malloc);
PicoSetFreeFunc(free);
PicoSetPrintFunc(PicoPrintFunc);
PicoSetLoadFileFunc(PicoLoadFileFunc);
PicoSetFreeFileFunc(PicoFreeFileFunc);
2020-11-17 11:16:16 +00:00
}
class PicoModelLoader : public ModelLoader {
2021-08-04 20:23:18 +00:00
const picoModule_t *m_module;
2020-11-17 11:16:16 +00:00
public:
2021-08-04 20:23:18 +00:00
PicoModelLoader(const picoModule_t *module) : m_module(module)
{
}
scene::Node &loadModel(ArchiveFile &file)
{
return loadPicoModel(m_module, file);
}
2020-11-17 11:16:16 +00:00
};
class ModelPicoDependencies :
2021-08-04 20:23:18 +00:00
public GlobalFileSystemModuleRef,
public GlobalOpenGLModuleRef,
public GlobalUndoModuleRef,
public GlobalSceneGraphModuleRef,
public GlobalShaderCacheModuleRef,
public GlobalSelectionModuleRef,
public GlobalFiletypesModuleRef {
2020-11-17 11:16:16 +00:00
};
class ModelPicoAPI : public TypeSystemRef {
2021-08-04 20:23:18 +00:00
PicoModelLoader m_modelLoader;
2020-11-17 11:16:16 +00:00
public:
2021-08-04 20:23:18 +00:00
typedef ModelLoader Type;
ModelPicoAPI(const char *extension, const picoModule_t *module) :
m_modelLoader(module)
{
StringOutputStream filter(128);
filter << "*." << extension;
GlobalFiletypesModule::getTable().addType(Type::Name(), extension,
filetype_t(module->displayName, filter.c_str()));
}
ModelLoader *getTable()
{
return &m_modelLoader;
}
2020-11-17 11:16:16 +00:00
};
class PicoModelAPIConstructor {
2021-08-04 20:23:18 +00:00
CopiedString m_extension;
const picoModule_t *m_module;
2020-11-17 11:16:16 +00:00
public:
2021-08-04 20:23:18 +00:00
PicoModelAPIConstructor(const char *extension, const picoModule_t *module) :
m_extension(extension), m_module(module)
{
}
const char *getName()
{
return m_extension.c_str();
}
ModelPicoAPI *constructAPI(ModelPicoDependencies &dependencies)
{
return new ModelPicoAPI(m_extension.c_str(), m_module);
}
void destroyAPI(ModelPicoAPI *api)
{
delete api;
}
2020-11-17 11:16:16 +00:00
};
typedef SingletonModule<ModelPicoAPI, ModelPicoDependencies, PicoModelAPIConstructor> PicoModelModule;
typedef std::list<PicoModelModule> PicoModelModules;
PicoModelModules g_PicoModelModules;
2021-06-02 08:49:27 +00:00
extern "C" void
#ifdef _WIN32
__declspec(dllexport)
#else
__attribute__((visibility("default")))
#endif
Radiant_RegisterModules(ModuleServer &server)
2020-11-17 11:16:16 +00:00
{
2021-08-04 20:23:18 +00:00
initialiseModule(server);
pico_initialise();
const picoModule_t **modules = PicoModuleList(0);
while (*modules != 0) {
const picoModule_t *module = *modules++;
if (module->canload && module->load) {
for (char *const *ext = module->defaultExts; *ext != 0; ++ext) {
g_PicoModelModules.push_back(PicoModelModule(PicoModelAPIConstructor(*ext, module)));
g_PicoModelModules.back().selfRegister();
}
}
}
2020-11-17 11:16:16 +00:00
}