LightWrap++
AC++wrapperfortheLightWave3DSDK
shader_handler.h
Go to the documentation of this file.
1 #ifndef LWPP_SHADER_HANDLER
2 #define LWPP_SHADER_HANDLER
3 #include "lwpp/plugin_handler.h"
4 #include <lwshader.h>
5 #include <lwsurf.h>
6 
7 namespace lwpp
8 {
10 
15 {
16  protected:
17  LWSurfaceID Context;
18  public:
19  ShaderHandler(void *g, void *context, LWError *err) : InstanceHandler(g, context, err, LWSHADER_HCLASS)
20  {
21  Context = static_cast<LWSurfaceID>(context);
22  }
23  virtual ~ShaderHandler() {;}
24  virtual void Evaluate(LWShaderAccess *sa) = 0;
25  virtual unsigned int Flags() {return 0;}
26 };
27 
29 
32  template <class T, int maxVersion, int minVersion>
33  class ShaderAdaptor : public InstanceAdaptor <T>, public ItemAdaptor <T>, public RenderAdaptor <T>
34  {
35  public:
36  ShaderAdaptor(const char *name, ServerTagInfo *tags)
37  {
38  LWServer::AddPlugin(LWSHADER_HCLASS, name, Activate, tags);
39  }
41  static int Activate (int version, GlobalFunc *global, LWInstance inst, void *serverData)
42  {
43  UNUSED(serverData);
44  try
45  {
46  if ( version > maxVersion ) return AFUNC_BADVERSION;
47  if ( version < minVersion ) return AFUNC_BADVERSION;
48  lwpp::SetSuperGlobal(global);
49  LWShaderHandler *plugin = static_cast<LWShaderHandler *>(inst);
50 
51  InstanceAdaptor<T>::Activate(plugin->inst);
52  ItemAdaptor<T>::Activate(plugin->item);
53  RenderAdaptor<T>::Activate(plugin->rend);
54  plugin->evaluate = ShaderAdaptor::Evaluate;
55  plugin->flags = ShaderAdaptor::Flags;
56  return AFUNC_OK;
57  }
58  catch (std::exception &e)
59  {
60  lwpp::LWMessage::Error("An exception occured in ShaderAdaptor::Activate():", e.what());
61  return AFUNC_BADAPP;
62  }
63  }
64  private:
65  static void Evaluate (LWInstance instance, LWShaderAccess *sa)
66  {
67  try
68  {
69  T *plugin = (T *) instance;
70  plugin->Evaluate(sa);
71  }
72  catch (std::exception &e)
73  {
74  lwpp::LWMessage::Error("An exception occured in ShaderHandler::Evaluate():", e.what());
75  }
76  }
77 
78  static unsigned int Flags (LWInstance instance)
79  {
80  try
81  {
82  T *plugin = (T *) instance;
83  return plugin->Flags();
84  }
85  catch (std::exception &e)
86  {
87  lwpp::LWMessage::Error("An exception occured in ShaderHandler::Evaluate():", e.what());
88  return 0;
89  }
90  }
91  };
92 
98 }
99 
100 #endif // LWPP_SHADER_HANDLER