libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo es el conjunto de objetos y funciones, que permiten ejecutar las operaciones básicas de la plataforma Moldeo, y que compone su núcleo.
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Amigas 'defines' Grupos Páginas
moResourcePlugin.cpp
Ir a la documentación de este archivo.
1 /*******************************************************************************
2 
3  moResourcePlugin.cpp
4 
5  ****************************************************************************
6  * *
7  * This source 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  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 
30 *******************************************************************************/
31 
32 #include "moResourcePlugin.h"
33 #include <moPort.h>
34 
35 #include <iostream>
36 using namespace std;
37 
38 #include "moDataManager.h"
39 #include "moDebugManager.h"
40 
41 
42 #include "moArray.h"
43 moDefineDynamicArray( moResourcePluginsArray )
44 
45 moResourceFactory::~moResourceFactory() {
46 }
47 
48 
49 //===========================================
50 //
51 // moResourcePlugin
52 //
53 //===========================================
54 
55 void moResourcePlugin::Load(moText plugin_file)
56 {
57  name = plugin_file;
58  handle = moLoadPlugin(plugin_file);
59 
60  if(!handle) {
61  #if !defined(WIN32)
62  moDebugManager::Error( "moResourcePlugin::Load > Cannot open library: " + moText(dlerror()) );
63  #else
64  CHAR szBuf[80];
65  DWORD dw = GetLastError();
66  sprintf(szBuf, "%s failed: GetLastError returned %i\n",
67  (char*)plugin_file, (int)dw);
68  moDebugManager::Error( "moResourcePlugin::Load > Cannot open library: " + moText(szBuf) );
69 
70  #endif
71  return;
72  }
73 
74  #if defined(_WIN32)
75  FARPROC farp;
76  farp = GetProcAddress(handle, "DestroyResourceFactory");
77  CreateResourceFactory = CreateResourceFactoryFunction(GetProcAddress(handle, "CreateResourceFactory"));
78  DestroyResourceFactory = DestroyResourceFactoryFunction(GetProcAddress(handle, "DestroyResourceFactory"));
79  #else
80  CreateResourceFactory = CreateResourceFactoryFunction(dlsym(handle, "CreateResourceFactory"));
81  DestroyResourceFactory = DestroyResourceFactoryFunction(dlsym(handle, "DestroyResourceFactory"));
82  #endif
83 
84  if(this->CreateResourceFactory!=NULL)
85  m_factory = this->CreateResourceFactory();
86 
87 }
88 
89 void moResourcePlugin::Unload()
90 {
91  this->DestroyResourceFactory();
92 
93  CreateResourceFactory = NULL;
94  DestroyResourceFactory = NULL;
95 
96  moUnloadPlugin(handle);
97 }
98 
99 
100 moResource* moResourcePlugin::Create() {
101 
102  moResource **narray;
103 
104  if(m_factory!=NULL) {
105  moResource *pfx = m_factory->Create();
106 
107  if(pfx==NULL) return NULL;
108 
109  if(n==0) {//primera vez
110  array = new moResource* [1];
111  } else if(n>0) {//array dinamico
112  narray = new moResource* [n+1];//generamos el nuevo vacio
113  for(int i=0;i<n;i++) narray[i] = array[i];//copiamos el array
114  delete [] array;
115  array = narray;
116  }
117 
118  array[n] = pfx;
119  n++;
120  return pfx;
121  }
122  return NULL;
123 }
124 
125 bool moResourcePlugin::Destroy(moResource *EfectoMaestro) {
126 
127  moResource **narray;
128  int i,j;
129 
130  if(m_factory!=NULL) {
131 
132  for(j=0;j<n;j++)
133  if(array[j]==EfectoMaestro) {
134  m_factory->Destroy(EfectoMaestro);
135 
136  if(n==1) {//muere
137  delete [] array;
138  } else if(n>1) {//array dinamico
139  narray = new moResource* [n-1];//generamos el nuevo vacio
140  for(i=0;i<j;i++) narray[i] = array[i];//copiamos el array hasta el j(destruido)
141  for(i=j;i<(n-1);i++) narray[i] = array[i+1];//copiamos el array desde el j
142  delete [] array;
143  array = narray;
144  }
145  n--;
146  return true;
147  }
148 
149  }
150  return false;
151 }
152 
153 //===========================================
154 //
155 // moResourcePluginsArray
156 //
157 //===========================================
158 /*
159 void moResourcePluginsArray::Add(moResourcePlugin* plugin)
160 {
161  if(length < max_length)
162  {
163  if(plugin!=NULL) {
164  cout << "plugin agregado\n";
165  array[length] = plugin;
166  length++;
167  } else {
168  cout << "error: plugin no fue creado!\n";
169  }
170  }
171 }
172 
173 void moResourcePluginsArray::Init(MOuint nplugins)
174 {
175  max_length = 256;
176  length = nplugins;
177  array = new moResourcePlugin*[max_length];
178  for(MOuint i = 0; i < max_length; i++) array[i] = NULL;
179 }
180 
181 void moResourcePluginsArray::Finish()
182 {
183  if(array != NULL)
184  {
185  delete[] array;
186  array = NULL;
187  }
188 
189  length = 0;
190 }
191 */
192 
193 LIBMOLDEO_API moResource* moNewResource(moText resource_name, moResourcePluginsArray &plugins)
194 {
195  // Creando el nombre complete del plugin(incluyendo ruta por defecto)
196  // a partir del name del efecto.
197  moText complete_name;
198 
199  if(!stricmp(resource_name, "nil")) return NULL;
200 
201  #if defined(_WIN32)
202  complete_name = moText(moDataManager::GetModulesDir()+ "/resources/") + (moText)resource_name;
203  #ifdef _DEBUG
204  complete_name+= moText("_d");
205  #endif
206  complete_name += moText(".dll");
207  #else
208  complete_name = moText(moDataManager::GetModulesDir()+ "/resources/libmoldeo_") + (moText)resource_name;
209  #ifdef _DEBUG
210  complete_name+= moText("_d");
211  #endif
212  complete_name += moPluginExtension;
213  #endif
214 
215  // Indice del plugin que se utilizara para crear a este efecto.
216  int plg_index = -1;
217 
218  // First, revisa que el plugin ya no haya sido cargado antes.
219  for(MOuint i = 0; i < plugins.Count(); i++)
220  if(!stricmp(plugins[i]->GetName(), complete_name))
221  {
222  plg_index = i;
223  break;
224  }
225 
226  if(plg_index == -1)
227  {
228  // Es la primera vez que se intenta cargar este plugin. Se lo agrega al array.
229  // Falta control de errores(que pasa si la libreria no carga, etc?) :-)
230  plg_index = plugins.Count();
231  moResourcePlugin *pplugin = new moResourcePlugin(complete_name);
232  if (pplugin)
233  plugins.Add( pplugin );
234  else {
235  moDebugManager::Error( moText("Resource plugin error: ") + complete_name );
236  return NULL;
237  }
238  }
239 
240  // El plugin crea al efecto!
241  if(plugins[plg_index]->m_factory!=NULL)
242  return plugins[plg_index]->Create();
243  else return NULL;
244 }
245 
246 
247 LIBMOLDEO_API bool moDeleteResource(moResource *Resource, moResourcePluginsArray &plugins)
248 {
249  // Creando el nombre complete del plugin(incluyendo ruta por defecto)
250  // a partir del nombre del efecto.
251  moText complete_name;
252 
253  //if(!stricmp(Resource->GetResourceName(), "")) return false;
254 
255  #if defined(_WIN32)
256  complete_name = moText(moDataManager::GetModulesDir()+ "/resources/") + moText(Resource->GetName());
257  #ifdef _DEBUG
258  complete_name+= moText("_d");
259  #endif
260  complete_name += moText(".dll");
261  #else
262  complete_name = moText(moDataManager::GetModulesDir()+ "/resources/libmoldeo_") + moText(Resource->GetName());
263  #ifdef _DEBUG
264  complete_name+= moText("_d");
265  #endif
266  complete_name += moPluginExtension;
267  #endif
268 
269  // Indice del plugin que se utilizara para crear a este efecto.
270  int plg_index = -1;
271 
272  // First, revisa que el plugin ya no haya sido cargado antes.
273  for(MOuint i = 0; i < plugins.Count(); i++)
274  if(!stricmp(plugins[i]->GetName(), complete_name))
275  {
276  plg_index = i;
277  break;
278  }
279 
280  if(plg_index == -1)
281  {
283  return false;
284  }
285 
286  bool res = plugins[plg_index]->Destroy(Resource);
287 
289  if (res && plugins[plg_index]->n == 0) {
290  plugins[plg_index]->Unload();
291  plugins.Remove(plg_index);
292 
293  }
294 
295  return res;
296 }
297 
Recurso ( objeto para cargar y manipular objetos físicos de datos de imágenes, audio, video, 3d, 2d, fuentes, shaders y de cualquier otro tipo extendible por un plugin )
static void Error(moText p_text)
Anuncia un error.
LIBMOLDEO_API bool moDeleteResource(moResource *Resource, moResourcePluginsArray &plugins)
const moText & GetName() const
#define LIBMOLDEO_API
Definition: moTypes.h:180
clase de para manejar textos
Definition: moText.h:75
moTypes MOint moText moParamIndex moParamReference int iRow int int i int i
Definition: all_f.js:18
void moUnloadPlugin(MOpluginHandle &handle)
Definition: moBasePlugin.h:64
moText0 moText
Definition: moText.h:291
static const moText & GetModulesDir()
moDefineDynamicArray(moResourcePluginsArray) moResourceFactory
#define MOuint
Definition: moTypes.h:387
LIBMOLDEO_API moResource * moNewResource(moText resource_name, moResourcePluginsArray &plugins)
MOpluginHandle moLoadPlugin(moText fn)
Definition: moBasePlugin.h:55