FreeWRL/FreeX3D  3.0.0
iglobal.c
1 #include <internal.h> //needed for opengl_utils.h included in iglobal.h
2 #include <iglobal.h>
3 
4 
5 void display_init(struct tdisplay* d);
6 void internalc_init(struct tinternalc* ic);
7 void resources_init(struct tresources* t);
8 void resources_clear(struct tresources* t);
9 void threads_init(struct tthreads* t);
10 
11 #if !defined(FRONTEND_DOES_SNAPSHOTS)
12 void Snapshot_init(struct tSnapshot *);
13 #endif
14 
15 void EAI_C_CommonFunctions_init(struct tEAI_C_CommonFunctions*);
16 void EAIEventsIn_init(struct tEAIEventsIn* t);
17 void EAIHelpers_init(struct tEAIHelpers* t);
18 
19 #if !defined(EXCLUDE_EAI)
20 void EAICore_init(struct tEAICore* t);
21 #endif
22 
23 void SensInterps_init(struct tSensInterps *t);
24 void ConsoleMessage_init(struct tConsoleMessage *t);
25 void Mainloop_init(struct tMainloop *t);
26 void Mainloop_clear(struct tMainloop *t);
27 void ProdCon_init(struct tProdCon *t);
28 void ProdCon_clear(struct tProdCon *t);
29 
30 #if defined (INCLUDE_NON_WEB3D_FORMATS)
31 void ColladaParser_init(struct tColladaParser *t);
32 #endif //INCLUDE_NON_WEB3D_FORMATS
33 
34 #if defined (INCLUDE_STL_FILES)
35 void STL_Handler_init (struct tSTLHandler *t);
36 #endif // INCLUDE_STL_FILES
37 
38 
39 
40 void Frustum_init(struct tFrustum *t);
41 void LoadTextures_init(struct tLoadTextures *t);
42 void OpenGL_Utils_init(struct tOpenGL_Utils *t);
43 void OpenGL_Utils_clear(struct tOpenGL_Utils *t);
44 #ifdef HAVE_OPENCL
45 void OpenCL_Utils_init(struct tOpenCL_Utils *t);
46 #endif
47 //void RasterFont_init(struct tRasterFont *t);
48 void RenderTextures_init(struct tRenderTextures *t);
49 void Textures_init(struct tTextures *t);
50 void Textures_clear(struct tTextures *t);
51 void PluginSocket_init(struct tPluginSocket *t);
52 void pluginUtils_init(struct tpluginUtils *t);
53 void collision_init(struct tcollision *t);
54 void collision_clear(struct tcollision *t);
55 void Component_CubeMapTexturing_init(struct tComponent_CubeMapTexturing *t);
56 void Component_CubeMapTexturing_clear(struct tComponent_CubeMapTexturing *t);
57 void Component_EnvironSensor_init(struct tComponent_EnvironSensor *t);
58 void Component_Geometry3D_init(struct tComponent_Geometry3D *t);
59 void Component_Geometry3D_clear(struct tComponent_Geometry3D *t);
60 void Component_Geospatial_init(struct tComponent_Geospatial *t);
61 void Component_HAnim_init(struct tComponent_HAnim *t);
62 void Component_HAnim_clear(struct tComponent_HAnim *t);
63 void Component_Layering_init(struct tComponent_Layering *t);
64 void Component_Layout_init(struct tComponent_Layout *t);
65 void Component_NURBS_init(struct tComponent_NURBS *t);
66 void Component_ParticleSystems_init(struct tComponent_ParticleSystems *t);
67 void Component_ParticleSystems_clear(struct tComponent_ParticleSystems *t);
68 void Component_ProgrammableShaders_init(struct tComponent_ProgrammableShaders *t);
69 void Component_ProgrammableShaders_clear(struct tComponent_ProgrammableShaders *t);
70 void Component_RigidBodyPhysics_init(struct tComponent_RigidBodyPhysics *t);
71 void Component_RigidBodyPhysics_clear(struct tComponent_RigidBodyPhysics *t);
72 void Component_Followers_init(struct tComponent_Followers *t);
73 void Component_Followers_clear(struct tComponent_Followers *t);
74 
75 void Component_KeyDevice_init(struct tComponent_KeyDevice *t);
76 
77 #ifdef OLDCODE
78 OLDCODEvoid Component_Networking_init(struct tComponent_Networking *t);
79 #endif
80 
81 void Component_Picking_init(struct tComponent_Picking *t);
82 void Component_Rendering_init(struct tComponent_Rendering *t);
83 void Component_Rendering_clear(struct tComponent_Rendering *t);
84 void Component_Shape_init(struct tComponent_Shape *t);
85 void Component_Sound_init(struct tComponent_Sound *t);
86 void Component_Text_init(struct tComponent_Text *t);
87 void Component_Text_clear(struct tComponent_Text *t);
88 void Component_Layering_clear(struct tComponent_Layering *t);
89 void Component_Layout_clear(struct tComponent_Layout *t);
90 void Component_VolumeRendering_init(struct tComponent_VolumeRendering *t);
91 void Component_VolumeRendering_clear(struct tComponent_VolumeRendering *t);
92 
93 void RenderFuncs_init(struct tRenderFuncs *t);
94 void RenderFuncs_clear(struct tRenderFuncs *t);
95 void StreamPoly_init(struct tStreamPoly *t);
96 void Tess_init(struct tTess *t);
97 void Viewer_init(struct tViewer *t);
98 
99 #if defined(STATUSBAR_HUD)
100 void statusbar_init(struct tstatusbar *t);
101 void statusbar_clear(struct tstatusbar *t);
102 #endif
103 
104 void CParse_init(struct tCParse *t);
105 void CParse_clear(struct tCParse *t);
106 void CParseParser_init(struct tCParseParser *t);
107 void CRoutes_init(struct tCRoutes *t);
108 void CRoutes_clear(struct tCRoutes *t);
109 void CScripts_init(struct tCScripts *t);
110 void JScript_init(struct tJScript *t);
111 
112 
113 void jsUtils_init(struct tjsUtils *t);
114 void jsVRMLBrowser_init(struct tjsVRMLBrowser *t);
115 void jsVRMLClasses_init(struct tjsVRMLClasses *t);
116 
117 void Bindable_init(struct tBindable *t);
118 void Bindable_clear(struct tBindable *t);
119 
120 void X3DParser_init(struct tX3DParser *t);
121 void X3DParser_clear(struct tX3DParser *t);
122 
123 void common_init(struct tcommon *t);
124 void common_clear(struct tcommon *t);
125 void CursorDraw_init(struct tCursorDraw *t);
126 
127 //static ttglobal iglobal; //<< for initial development witn single instance
128 static int done_main_UI_thread_once = 0;
129 pthread_key_t threadSpecificKey; //set like a global variable in the global scope in a .c file
130 
131 ttglobal iglobal_constructor() //(mainthreadID,parserthreadID,texturethreadID...)
132 {
133 
134  //JAS printf ("calling iglobal_constructor\n");
135 
136  //using Johns threadID method would:
137  //1. create global struct
138  // - malloc
139  // - initialize any that have initializers
140  //2. add 3 items to the thread2global[] list
141  //3. for each of those 3 items:
142  // - set thread2global[threadID] = global
143  // pthread_t uiThread;
144  ttglobal iglobal = MALLOCV(sizeof(struct iiglobal));
145  memset(iglobal,0,sizeof(struct iiglobal)); //set to zero/null by default
146 #ifdef DISABLER
147  if(!done_main_UI_thread_once){
148  pthread_key_create(&threadSpecificKey, NULL);
149  done_main_UI_thread_once = 1; //this assumes the iglobal is created in the shared UI main thread
150  }
151  fwl_setCurrentHandle(iglobal,__FILE__,__LINE__); //probably redundant but no harm
152 
153 #if defined(WRAP_MALLOC) || defined(DEBUG_MALLOC)
154  iglobal->__memTable_ShouldRegisterAllocation = TRUE;
155  freewrlInitMemTable();
156 #endif
157 #endif
158  //call initializer for each sub-struct
159  display_init(&iglobal->display);
160  internalc_init(&iglobal->internalc);
161  resources_init(&iglobal->resources);
162  threads_init(&iglobal->threads);
163 
164 
165  #if !defined(FRONTEND_DOES_SNAPSHOTS)
166  Snapshot_init(&iglobal->Snapshot);
167  #endif
168 
169  EAI_C_CommonFunctions_init(&iglobal->EAI_C_CommonFunctions);
170 #if !defined(EXCLUDE_EAI)
171  EAIEventsIn_init(&iglobal->EAIEventsIn);
172  EAIHelpers_init(&iglobal->EAIHelpers);
173  EAICore_init(&iglobal->EAICore);
174 #endif //EXCLUDE_EAI
175 
176  SensInterps_init(&iglobal->SensInterps);
177  ConsoleMessage_init(&iglobal->ConsoleMessage);
178  Mainloop_init(&iglobal->Mainloop);
179  ProdCon_init(&iglobal->ProdCon);
180 
181 #if defined (INCLUDE_NON_WEB3D_FORMATS)
182  ColladaParser_init(&iglobal->ColladaParser);
183 #endif //INCLUDE_NON_WEB3D_FORMATS
184 
185 #if defined (INCLUDE_STL_FILES)
186  STL_Handler_init (&iglobal->STLHandler);
187 #endif // INCLUDE_STL_FILES
188 
189  Frustum_init(&iglobal->Frustum);
190  LoadTextures_init(&iglobal->LoadTextures);
191  OpenGL_Utils_init(&iglobal->OpenGL_Utils);
192 #ifdef HAVE_OPENCL
193  OpenCL_Utils_init(&iglobal->OpenCL_Utils);
194 #endif
195 
196  //RasterFont_init(&iglobal->RasterFont);
197  RenderTextures_init(&iglobal->RenderTextures);
198  Textures_init(&iglobal->Textures);
199 #ifndef DISABLER
200 #ifndef NO_PLUGINSOCKET
201  PluginSocket_init(&iglobal->PluginSocket);
202 #endif
203  pluginUtils_init(&iglobal->pluginUtils);
204 #endif
205  collision_init(&iglobal->collision);
206  Component_CubeMapTexturing_init(&iglobal->Component_CubeMapTexturing);
207  Component_EnvironSensor_init(&iglobal->Component_EnvironSensor);
208  Component_Geometry3D_init(&iglobal->Component_Geometry3D);
209  Component_Geospatial_init(&iglobal->Component_Geospatial);
210  Component_HAnim_init(&iglobal->Component_HAnim);
211  Component_Layering_init(&iglobal->Component_Layering);
212  Component_Layout_init(&iglobal->Component_Layout);
213  Component_NURBS_init(&iglobal->Component_NURBS);
214  Component_ProgrammableShaders_init(&iglobal->Component_ProgrammableShaders);
215  Component_ParticleSystems_init(&iglobal->Component_ParticleSystems);
216  Component_RigidBodyPhysics_init(&iglobal->Component_RigidBodyPhysics);
217  Component_Followers_init(&iglobal->Component_Followers);
218  Component_KeyDevice_init(&iglobal->Component_KeyDevice);
219 #ifdef OLDCODE
220 OLDCODE Component_Networking_init(&iglobal->Component_Networking);
221 #endif // OLDCODE
222  Component_Picking_init(&iglobal->Component_Picking);
223  Component_Rendering_init(&iglobal->Component_Rendering);
224  Component_Shape_init(&iglobal->Component_Shape);
225  Component_Sound_init(&iglobal->Component_Sound);
226  Component_Text_init(&iglobal->Component_Text);
227  Component_VolumeRendering_init(&iglobal->Component_VolumeRendering);
228  RenderFuncs_init(&iglobal->RenderFuncs);
229  StreamPoly_init(&iglobal->StreamPoly);
230  Tess_init(&iglobal->Tess);
231  Viewer_init(&iglobal->Viewer);
232 #if defined(STATUSBAR_HUD)
233  statusbar_init(&iglobal->statusbar);
234 #endif
235  CParse_init(&iglobal->CParse);
236  CParseParser_init(&iglobal->CParseParser);
237  CRoutes_init(&iglobal->CRoutes);
238  CScripts_init(&iglobal->CScripts);
239  JScript_init(&iglobal->JScript);
240 
241 
242  jsUtils_init(&iglobal->jsUtils);
243  jsVRMLBrowser_init(&iglobal->jsVRMLBrowser);
244  jsVRMLClasses_init(&iglobal->jsVRMLClasses);
245 
246  Bindable_init(&iglobal->Bindable);
247  X3DParser_init(&iglobal->X3DParser);
248  common_init(&iglobal->common);
249 #ifndef DISABLER
250  CursorDraw_init(&iglobal->CursorDraw);
251 
252  //uiThread = pthread_self();
253  //set_thread2global(iglobal, uiThread ,"UI thread");
254 
255  if(!done_main_UI_thread_once){
256  pthread_key_create(&threadSpecificKey, NULL);
257  done_main_UI_thread_once = 1; //this assumes the iglobal is created in the shared UI main thread
258  }
259  fwl_setCurrentHandle(iglobal,__FILE__,__LINE__); //probably redundant but no harm
260 #endif
261  return iglobal;
262 }
263 
264 void __iglobal_fields_destructor(ttglobal tg);
265 void remove_iglobal_from_table(ttglobal tg);
266 void __iglobal_destructor(ttglobal tg);
267 
268 void iglobal_destructor(ttglobal tg)
269 {
270  __iglobal_fields_destructor(tg);
271  __iglobal_destructor(tg);
272 }
273 
274 void __iglobal_fields_destructor(ttglobal tg)
275 {
276  /* you should have stopped any worker threads for this instance */
277  //call individual destructors in reverse order to constructor
278  FREE_IF_NZ(tg->CursorDraw.prv);
279  common_clear(&tg->common); FREE_IF_NZ(tg->common.prv);
280  X3DParser_clear(&tg->X3DParser); FREE_IF_NZ(tg->X3DParser.prv);
281  Bindable_clear(&tg->Bindable); FREE_IF_NZ(tg->Bindable.prv);
282 
283  FREE_IF_NZ(tg->jsVRMLClasses.prv);
284  FREE_IF_NZ(tg->jsVRMLBrowser.prv);
285  FREE_IF_NZ(tg->jsUtils.prv);
286 
287  FREE_IF_NZ(tg->JScript.prv);
288  FREE_IF_NZ(tg->CScripts.prv);
289  CRoutes_clear(&tg->CRoutes); FREE_IF_NZ(tg->CRoutes.prv);
290  FREE_IF_NZ(tg->CParseParser.prv);
291  CParse_clear(&tg->CParse); FREE_IF_NZ(tg->CParse.prv);
292 #if defined(STATUSBAR_HUD)
293  statusbar_clear(&tg->statusbar);
294 #endif
295  FREE_IF_NZ(tg->statusbar.prv);
296  FREE_IF_NZ(tg->Viewer.prv);
297  FREE_IF_NZ(tg->Tess.prv);
298  Component_VolumeRendering_clear(&tg->Component_VolumeRendering); FREE_IF_NZ(tg->Component_VolumeRendering.prv);
299  FREE_IF_NZ(tg->StreamPoly.prv);
300  FREE_IF_NZ(tg->Component_Sound.prv);
301  RenderFuncs_clear(&tg->RenderFuncs); FREE_IF_NZ(tg->RenderFuncs.prv);
302  Component_Text_clear(&tg->Component_Text); FREE_IF_NZ(tg->Component_Text.prv);
303  FREE_IF_NZ(tg->Component_Shape.prv);
304  Component_Rendering_clear(&tg->Component_Rendering); FREE_IF_NZ(tg->Component_Rendering.prv);
305  FREE_IF_NZ(tg->Component_Picking.prv);
306 #ifdef OLDCODE
307 OLDCODE FREE_IF_NZ(tg->Component_Networking.prv);
308 #endif
309  FREE_IF_NZ(tg->Component_KeyDevice.prv);
310  Component_Followers_clear(&tg->Component_Followers); FREE_IF_NZ(tg->Component_Followers.prv);
311  Component_RigidBodyPhysics_clear(&tg->Component_RigidBodyPhysics); FREE_IF_NZ(tg->Component_RigidBodyPhysics.prv);
312  Component_ProgrammableShaders_clear(&tg->Component_ProgrammableShaders); FREE_IF_NZ(tg->Component_ProgrammableShaders.prv);
313  Component_ParticleSystems_clear(&tg->Component_ParticleSystems); FREE_IF_NZ(tg->Component_ParticleSystems.prv);
314  FREE_IF_NZ(tg->Component_NURBS.prv);
315  Component_Layering_clear(&tg->Component_Layering); FREE_IF_NZ(tg->Component_Layering.prv);
316  Component_Layout_clear(&tg->Component_Layout); FREE_IF_NZ(tg->Component_Layout.prv);
317  Component_HAnim_clear(&tg->Component_HAnim); FREE_IF_NZ(tg->Component_HAnim.prv);
318  FREE_IF_NZ(tg->Component_Geospatial.prv);
319  Component_Geometry3D_clear(&tg->Component_Geometry3D); FREE_IF_NZ(tg->Component_Geometry3D.prv);
320  FREE_IF_NZ(tg->Component_EnvironSensor.prv);
321  Component_CubeMapTexturing_clear(&tg->Component_CubeMapTexturing); FREE_IF_NZ(tg->Component_CubeMapTexturing.prv);
322  collision_clear(&tg->collision); FREE_IF_NZ(tg->collision.prv);
323  FREE_IF_NZ(tg->pluginUtils.prv);
324  FREE_IF_NZ(tg->PluginSocket.prv);
325  Textures_clear(&tg->Textures); FREE_IF_NZ(tg->Textures.prv);
326  FREE_IF_NZ(tg->RenderTextures.prv);
327  //FREE_IF_NZ(tg->RasterFont.prv);
328  OpenGL_Utils_clear(&tg->OpenGL_Utils); FREE_IF_NZ(tg->OpenGL_Utils.prv);
329  FREE_IF_NZ(tg->LoadTextures.prv);
330  FREE_IF_NZ(tg->Frustum.prv);
331 
332 #if defined (INCLUDE_NON_WEB3D_FORMATS)
333  FREE_IF_NZ(tg->ColladaParser.prv);
334 #endif //INCLUDE_NON_WEB3D_FORMATS
335 
336 #if defined (INCLUDE_STL_FILES)
337  FREE_IF_NZ(tg->STLHandler.prv);
338 #endif // INCLUDE_STL_FILES
339 
340  ProdCon_clear(&tg->ProdCon); FREE_IF_NZ(tg->ProdCon.prv);
341  Mainloop_clear(&tg->Mainloop); FREE_IF_NZ(tg->Mainloop.prv);
342  FREE_IF_NZ(tg->ConsoleMessage.prv);
343  FREE_IF_NZ(tg->SensInterps.prv);
344  FREE_IF_NZ(tg->EAICore.prv);
345  FREE_IF_NZ(tg->EAIHelpers.prv);
346  FREE_IF_NZ(tg->EAIEventsIn.prv);
347  FREE_IF_NZ(tg->EAI_C_CommonFunctions.prv);
348  FREE_IF_NZ(tg->Snapshot.prv);
349 
350  FREE_IF_NZ(tg->threads.prv);
351  resources_clear(&tg->resources); FREE_IF_NZ(tg->resources.prv);
352  FREE_IF_NZ(tg->internalc.prv);
353  FREE_IF_NZ(tg->display.prv);
354 }
355 
356 void __iglobal_destructor(ttglobal tg)
357 {
358  //destroy iglobal
359 #ifndef DISABLER
360  FREE_IF_NZ(tg);
361 #else
362  free(tg);
363 #endif
364  //remove_iglobal_from_table(tg);
365  fwl_clearCurrentHandle(__FILE__,__LINE__);
366 
367 }
368 
369 
370 
371 void *fwl_getCurrentHandle(char *fi, int li){
372  ttglobal currentHandle = (ttglobal)pthread_getspecific(threadSpecificKey);
373  //printf ("fwl_getCurrentHandle returning %p at %s:%d\n",currentHandle,fi,li);
374  return (void*)currentHandle;
375 }
376 int fwl_setCurrentHandle(void *handle, char *fi, int li)
377 {
378  //printf ("fwl_setCurrentHandle at to %p thread %p at %s:%d\n",handle,pthread_self(),fi,li);
379 
380  pthread_setspecific(threadSpecificKey,handle);
381  return 1; /* let caller know its not in the table yet */
382 }
383 void fwl_clearCurrentHandle(char *fi, int li)
384 {
385  void *currentHandle = NULL;
386  //printf ("fwl_clearCurrentHandle at %s:%d\n",fi,li);
387  pthread_setspecific(threadSpecificKey,currentHandle);
388 
389 }
390 ttglobal gglobal(char *fi, int *li){
391  ttglobal tg = NULL;
392 #ifdef DISABLER
393  if (done_main_UI_thread_once)
394 #endif
395  tg = (ttglobal)pthread_getspecific(threadSpecificKey);
396  if(!tg)
397  {
398  printf("Ouch - no state for this thread -- hit a key to exit\n");
399 #ifdef _MSC_VER
400  printf ("more info - thread %p\n\n",(void *)pthread_self().p);
401 #else
402  printf ("more info - thread %p\n\n",(void *)pthread_self());
403 #endif
404  getchar();
405  exit(-1);
406  }
407  return tg;
408 }
409 
410 ttglobal gglobal0()
411 {
412 #ifdef DISABLER
413  if (!done_main_UI_thread_once)
414  {
415  return NULL;
416  }
417 #endif
418  return (ttglobal)pthread_getspecific(threadSpecificKey);
419 }