Trying to make an SVGA class that will handle all color depths.
Problem is I dont want to implement each depth in a new class.
SetMode() allocates the correct pointer types for the current screen mode. This allows me to use one class for all depths and it works fine until another class needs access to the SVGASystem members. I don't want to include a pointer to the SVGASystem in every class that needs access to it. Instead I want to derive off of SVGASystem so that I gain access to SVGASystem's members. But when I derive, I'm not sure how to tell the compiler that Polygon is derived from SVGASystem of type T, and not type void. If I explicitly declare that Polygon is derived from a certain type of SVGASystem, I again have to implement 3 different objects for each color depth which is worse than implementing 3 classes for each color depth.Code:template <class T> class SVGASystem { T *Buffer; T *Screen; T *FramePtr; T REDS[255]; T GRNS[255]; T BLUS[255]; public: SVGASystem<T> (void) {}; void SetMode(void); void PlotPixel(int x,int y,unsigned char color); void PlotPixel(int x,int y,RGB color); }; class Polygon:public SVGASystem<void> { public: Polygon(void) {}; void DrawPolygon(void); };
To illustrate:
For 32-bit color modesCode:typedef unsigned long COLOR16M; typedef unsigned int COLOR64K; typedef unsigned char COLOR256;
For 16-bit color modesCode:SVGASystem<COLOR16M> Video; Video.SetMode(0x112); Video.PlotPixel(50,50,255,255,255);
For 256 color modesCode:SVGASystem<COLOR64K> Video; Video.SetMode(0x111); Video.PlotPixel(50,50,255,255,255);
These all work perfect. But now I want to derive an object from the SVGASystem like polygons, 3D objects (composed of polygons), etc. etc. These obviously need access to the SVGASystem so they can draw lines or at least need access to the Buffer, Screen, and FramePtr pointers. How can I derive objects from SVGASystem so the compiler understands that at run-time the type will be decided. So polygon might be derived from a 256 color, 16-bit color, or 32-bit color SVGASystem.Code:SVGASystem<COLOR256> Video; Video.SetMode(0x13); Video.PlotPixel(50,50,255);
My only alternative right now is to do this:
But this requires designing 3 objects for each color depth. This won't do either.Code:class Polygon:public SVGASystem<COLOR256> { }
This says that type T is not known. So in my PlotPixel function:Code:template <class T> class Polygon:public SVGASystem<T> { }
It says that color's type is not known. Since REDS, GRNS, and BLUS are defined to be of type T, it will give these errors.Code:template <class T> SVGASystem<T>::PlotPixel(int x,int y,int red,int grn,int blu) { T color=REDS[red]+GRNS[grn]+BLUS[blu]; ... }
- Cannot convert [unsigned long | unsigned int | unsigned char] to type T (or in the first example to type void).
- Size of storage type color is not known.
Please someone that understands templates could you help me with this or guide me in another direction? I've looked at Bjarne's book closely and I'm sure you can do what I am trying to do, I just don't know how.
Otherwise you could not do this:
List<int> MyList;
or
List<MyClass> MyList;
So the heart of my question is how to you derive a non-template class off of a template base when the derived class's type is not known? I want the derived class to inherit the base template class's type.



LinkBack URL
About LinkBacks


