LightWrap++
AC++wrapperfortheLightWave3DSDK
animloader_handler.h
Go to the documentation of this file.
1 #ifndef ANIMLOADER_HANDLER_H
2 #define ANIMLOADER_HANDLER_H
3 
4 #include "lwpp/plugin_handler.h"
5 #include <lwpp/lw_server.h>
6 #include <lwpp/monitor.h>
7 #include <lwpp/message.h>
8 #include <lwtypes.h>
9 #include <lwanimlod.h>
10 
11 namespace lwpp
12 {
13 
16  {
17  protected:
18  LWAnimLoaderHandler *local;
19  //SimpleMonitor monitor;
20  int version;
21  std::string fileName;
22  public:
23  AnimLoaderHandler(void* g, void* context, LWError* err)
24  : InstanceHandler(g,context,err, LWANIMLOADER_HCLASS)
25  {
26  fileName = (const char*) context;
27  }
28  virtual ~AnimLoaderHandler() {;}
29  bool virtual isValid() = 0;
30  virtual int GetFrameCount () = 0;
31  virtual double GetFrameRate () = 0;
32  virtual double GetAspect (int *w, int *h, double *pixAspect) = 0;
33  virtual void Evaluate (double, LWAnimFrameAccess *) = 0;
34  };
35 
37  template <class T>
39  {
40  public:
41  AnimLoaderAdaptor(const char *name, ServerTagInfo *tags)
42  {
43  LWServer::AddPlugin(LWANIMLOADER_HCLASS, name, Activate, tags);
44  }
46  static int Activate( int version, GlobalFunc *global, void *l, void *serverData )
47  {
48  if (version > LWANIMLOADER_VERSION) return AFUNC_BADVERSION;
49  try
50  {
51  lwpp::SetSuperGlobal(global);
52  LWAnimLoaderHandler *plugin = static_cast<LWAnimLoaderHandler *>(l);
53  InstanceAdaptor<T>::Activate(plugin->inst);
54  plugin->inst->create = AnimLoaderAdaptor::Create;
55  plugin->frameCount = AnimLoaderAdaptor::FrameCount;
56  plugin->frameRate = AnimLoaderAdaptor::FrameRate;
57  plugin->aspect = AnimLoaderAdaptor::Aspect;
58  plugin->evaluate = AnimLoaderAdaptor::Evaluate;
59 
60  return AFUNC_OK;
61  }
62  catch (std::exception &e)
63  {
64  lwpp::LWMessage::Error("An exception occured when attempting to load an Animation:", e.what());
65  return AFUNC_BADAPP_SILENT;
66  }
67  }
68 
69  private:
70  static LWInstance Create (void *priv, void *context, LWError *err)
71  {
72  try
73  {
74  LWError localErr = "\0";
75  T *plugin = new T(priv, context, &localErr);
76  if (*localErr)
77  {
78  delete plugin;
79  plugin = 0;
80  *err = localErr;
81  }
82  if (!plugin->isValid())
83  {
84  delete plugin;
85  plugin = 0;
86  }
87  return static_cast<LWInstance> (plugin);
88  }
89  catch (std::exception &e)
90  {
91  static LWError error = e.what();
92  err = &error;
93  return 0;
94  }
95  }
96 
97  static int FrameCount (LWInstance instance)
98  {
99  try
100  {
101  T *plugin = (T *) instance;
102  return plugin->GetFrameCount();
103  }
104  catch (std::exception &e)
105  {
106  lwpp::LWMessage::Error("An exception occured in AnimLoader::GetFrameCount():", e.what());
107  return 0;
108  }
109  }
110 
111  static double FrameRate (LWInstance instance)
112  {
113  try
114  {
115  T *plugin = (T *) instance;
116  return plugin->GetFrameRate();
117  }
118  catch (std::exception &e)
119  {
120  lwpp::LWMessage::Error("An exception occured in AnimLoader::GetFrameRate():", e.what());
121  return 0.0;
122  }
123  }
124 
125  static double Aspect (LWInstance instance, int *w, int *h, double *pixAspect)
126  {
127  try
128  {
129  T *plugin = (T *) instance;
130  return plugin->GetAspect(w, h, pixAspect);
131  }
132  catch (std::exception &e)
133  {
134  lwpp::LWMessage::Error("An exception occured in AnimLoader::GetAspect():", e.what());
135  return 0.0;
136  }
137  }
138 
139  static void Evaluate (LWInstance instance, double time, LWAnimFrameAccess *fa)
140  {
141  try
142  {
143  T *plugin = (T *) instance;
144  plugin->Evaluate(time, fa);
145  }
146  catch (std::exception &e)
147  {
148  lwpp::LWMessage::Error("An exception occured in AnimLoader::Evaluate():", e.what());
149  }
150  }
151  };
152 }
153 
154 #endif // ANIMLOADER_HANDLER_H