Archived

This topic is now archived and is closed to further replies.

Merwin van Dijk

allocate templated classes

Recommended Posts

I''ve made a templated class ''CBasicDraw'' it can draw pixels on the screen in 8, 16, 24, 32 bpp. I want to make a windowed direct X program. This means that I have to find out the pixel format during run-time. This is no problem I know how to do this. But it also mean that I must make a ''CBasicDraw'' object and during run-time let determine with which sort of variables it is templated. I''ve thought about the following opportunitie:
  
CBasicdraw *g_lpScreen;

void InitGame(){
  // code where I can determine the bpp

  g_lpScreen =  new CBasicDraw<char>;
}

  
Above is just a simple example but I want to know if this is a correct way to solve the problem.

Share this post


Link to post
Share on other sites
quote:
Original post by Merwin van Dijk
I''ve made a templated class ''CBasicDraw'' it can draw pixels on the screen in 8, 16, 24, 32 bpp.

So what''re the template parameters?
quote:

Above is just a simple example but I want to know if this is a correct way to solve the problem.

Not enough information. If the template parameters can only be determined at run-time then no. A template *must* be instantiated at compile-time, which means you have to have all the relevant information at compile-time.

Also, you have a syntax-error in your code:

CBasicdraw *g_lpScreen;

You must specify the template parameters for the CBasicdraw class here. I think what you''re saying is that you can''t because you don''t know them, in which case it shouldn''t be a templated class.

Share this post


Link to post
Share on other sites
The above code was more a guess how I could solve this problem.

The more important code of CBasicDraw:

  
// basicdraw.h

//

//



#ifndef __basicdraw_h__
#define __basicdraw_h__

template <typename var>
class CBasicDraw{
protected:
DDSURFACEDESC2 m_ddsd;
long m_lPitch;
var *m_lpsDest;
public:
CBasicDraw(){
INIT_DX_STRUCT(m_ddsd);
m_lPitch=0;
m_lpsDest=NULL;
}

void DrawPixel(int x, int y, var color){

m_lpsDest[m_lPitch*y+x]=color;
}

var GetValuePixel(int x, int y){
return m_lpsDest[m_lPitch*y+x];
}

BOOL Lock(LPDIRECTDRAWSURFACE4 lpdd){
BOOL ok;

ok=SUCCEEDED(lpdd->Lock(NULL, &m_ddsd, DDLOCK_WAIT, NULL));
m_lPitch = m_ddsd.lPitch/sizeof(var);
m_lpsDest = (var*)m_ddsd.lpSurface;

return ok;
}

BOOL Unlock(LPDIRECTDRAWSURFACE4 lpdd){
return SUCCEEDED(lpdd->Unlock(NULL));
}

void DrawLine(int x1Dest, int y1Dest, int x2Dest, int y2Dest, var color){
// a lot of code

}
void DrawRectangle(RECT rDest, var color, BOOL fill=0){
// a lot of code

}
};

#endif


It''s true that I don''t know the template parameters for the CBasicDraw class, I can only determine them during run-time.
What if I template only the neccesary member-functions and not whole class? Ofcourse I have to change the implementation of serveral functions but do I solve the problem with way?

Share this post


Link to post
Share on other sites
What if I declare the g_lpScreen pointer like this:


  
void *g_lpScreen;

// and later in my code use something like this:


g_lpScreen = new CBasicDraw<char>


Is this a valid to solve the problem? I just keep asking because I still didn't receive anu replies to my last message..




[edited by - Merwin van Dijk on August 15, 2002 5:49:51 PM]

Share this post


Link to post
Share on other sites
Var could be an ''unsigned char'' (BYTE), ''unsigned short'' (WORD) or ''unsigned long'' (DWORD). This 3 types respectively can hold the value of an 8, 16, 32 bpp color. I''ve never thought about how this class would use 24 bpp colors. Maybe I use specialized templated member-functions to solve this problem.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
templates are for compile-time polymorphism;
inheritance is for run-time polymorphism.

You can''t get around this fact unless you hack around it with a void pointer (at which point you might as well chop your whole leg off now to avoid shotting yourself in the foot later).

Share this post


Link to post
Share on other sites