Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / Game Design & Programming / Pointer to differtent structs Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Vincent Broeren

December 24, 2004, 08:17 AM

I want to pass a pointer as an argument to a function (in C++) which can point to different structs.
I hope this code example is clear enough to show my problem.

  1.  
  2. LPDIRECT3DVERTEXBUFFER9 pVB;
  3.  
  4. struct COLORVERTEX
  5. {
  6.         D3DVECTOR position;
  7.         D3DCOLOR color;
  8. };
  9.  
  10. struct TEXVERTEX
  11. {
  12.         D3DVECTOR position;
  13.         float tu, tv;
  14. };
  15.  
  16. #define COLORFVF ( D3DFVF_XYZ | D3DFVF_DIFFUSE )
  17. #define TEXFVF ( D3DFVF_XYZ | D3DFVF_TEX1 )
  18.  
  19. void FillVertexBuffer(type_info& structName, FVF defFVF, int numOfVertices)
  20. {
  21.  
  22.         void* lpVertex;
  23.  
  24.         //Cast the void pointer to the given struct (as the argument)
  25.         lVertex = (structName) lVertex;
  26.  
  27.         pVB->Lock( 0, sizeof(defFVF), (void**)&lpVertex, 0 );
  28.  
  29.         for (int i = 0; i < numOfVertices)
  30.         {
  31.  
  32.                 if (deFVF & D3DFVF_XYZ)
  33.                 {
  34.                         lVertex->position.x = 10.0f;
  35.                         lVertex->position.y = 10.0f;
  36.                         lVertex->position.z = 10.0f;
  37.                 }
  38.                 if (devFVF & D3DFVF_TEX1)
  39.                 {
  40.                         lVertex->tu = 0.0f;
  41.                         lVertex->tv = 0.0f;
  42.                 }
  43.                 if (defFVF & D3DCOLOR)
  44.                 {
  45.                         lVertex->color = 0xff121212;
  46.                 }
  47.         }
  48.  
  49.         pVB->Unlock();
  50. }
  51.  


I've tried a lot but nothing seems to work. I tried things like typeid's and dynamic_cast of static_cast. I've look on the internet for info about reflection (like in Java), but I don't think that that would be the solution. Does anyone know how to get this to work? Or am I totally on the wrong track?

Many thanks in advance,

Vincent Broeren

 
Altair

December 24, 2004, 10:48 AM

You can do this easily by using templates & mutator methods, like:

  1.  
  2. struct COLORVERTEX
  3. {
  4.   D3DVECTOR position;
  5.   D3DCOLOR color;
  6.  
  7.   // mutators
  8.   inline void setPosition(float x_, float y_, float z_)  {position.X=x_; position.Y=y_; position.Z=z_;}
  9.   inline void setColor(uint32 color_)                    {color=color_;}
  10.   inline void setUV(float u_, float v_)                  {}
  11. };
  12.  
  13. struct TEXVERTEX
  14. {
  15.   D3DVECTOR position;
  16.   float tu, tv;
  17.  
  18.   // mutators
  19.   inline void setPosition(float x_, float y_, float z_)  {position.X=x_; position.Y=y_; position.Z=z_;}
  20.   inline void setColor(uint32 color_)                    {}
  21.   inline void setUV(float u_, float v_)                  {tu=u_; tv=v_;}
  22. };
  23.  
  24. template<typename T_>
  25. void FillVertexBuffer(T_ *verts_, int nVertices_)
  26. {
  27.   for (int i=0; i<nVertices; ++i)
  28.   {
  29.     verts_[i].setPosition(10.0f, 10.0f, 10.0f);
  30.     verts_[i].setUV(0, 0);
  31.     verts_[i].setColor(0xff121212);
  32.   }
  33. }
  34.  

Once optimized, the empty mutator implementations will get automatically eliminated from the code by the compiler.

You can also define a vertex base class which implements all empty mutators and derive vertex classes from that one, so when you add new properties, you only need to add empty mutators to the base class. It's a bit easier to maintain that way.

Cheers, Altair

 
Vincent Broeren

December 24, 2004, 12:58 PM

I've checked the possibility of using classes in stead of structs, but isn't it so that you get much more overhead with a class compared to a struct?
There will be around 500,000 of this kind of vertices. Is it still efficient enough if you use classes? (regarding to speed and memory)

Greets,

Vincent

 
Altair

December 24, 2004, 01:25 PM

There is no difference in performance or memory usage in using structs or classes. The piece of code I gave you will perform better when optimized & properly inlined than your original code (even if it would work), since there is no run-time branching.

Cheers, Altair

 
Vincent Broeren

December 24, 2004, 04:36 PM

Thanks, I will use the way you suggested then.

Happy holidays,

Vincent

 
This thread contains 5 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.