Archived

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

vbisme

DirectDraw in a class

Recommended Posts

vbisme    100
I''m trying to wrap up all of DirectDraw objects into a single class called DDHandler. However, declaring those objects as private, I found it extremly difficult for other classes to access them. Like my sprite class would need the DirectDraw object to create the surfaces. Does anyone have a good solution? If you would like to look at my code to see what I''m doing wrong, just let me know. Thanks.

Share this post


Link to post
Share on other sites
vTheHero    136
I would just make several public functions to return the various DirectDraw Objects you need

LPDIRECTDRAW7 DDHandle::GetDDObject(void)
{
return lpDD;
}

or alternatively you could program all surface creation and manipulation into DDHandle.

Dan

Share this post


Link to post
Share on other sites
Lowas    122

DirectDraw object and Primary Surface does well as singletons IMO.

In the past I''ve had the DD objects in a base class and anything that needs the DD objects inherits from the DD base class, works very well for me.


// a snippet from one of my games
class CDD_Base
{
protected:
static LPDIRECTDRAW7 pDD;
static LPDIRECTDRAWSURFACE7 pDDSPrime;
static LPDIRECTDRAWSURFACE7 pDDSBack;
static LPDIRECTDRAWSURFACE7 pDDSFrame;
static LPDIRECTDRAWCLIPPER pDDClipper;
};

// class Sprite or something that needs access to the pointers
class BmpImage : protected CDD_Base
{
public:
// use DD surface when loading a bitmap etc..
};

class Sprite : protected CDD_Base
{
public:
//use DD pointers for blitting etc...
};




Next Time I may use the singleton instead ...
As for globals? ...I don''t use em for important stuff

Share this post


Link to post
Share on other sites
Warden    122
Hey, just a point of interest. I''m actually working on something that not only envelops "DirectDraw" (it''s actually DX8.0 now) but all windows components as well. Though its currently in working order, there''s little things that don''t work exactly right, and some components need a little bit of work. It also doesn''t allow for an overloaded WinProc (yet). If you''re interested in seeing it, drop me an email and I''ll send it your way.

Anyway, here''s how I solved the problem. Since we''re working on porting ot other systems besides windows, we''ve created a variable in each class that is a void pointer to a struct that is created with several different macros, but is allows us to get any platform specific information that we want quickly and easily with a call to a function called "getPrivateData()". Though you might not want to play with void pointers and long complicated macros, you could hold any DX and windows stuff in a struct inside the class which you could easilly get and reference. Also, depending on where you need the data, you can hold a pointer to a DX interface in an hwnd (I hold pointers to a parent class there). Here''s the fun code:

SetWindowLong(hwnd, GWL_USERDATA, (LONG)pDirectDraw);
// Some point later
pDirectDraw = (LPDIRECTDRAW)GetWindowLong(hwnd, GWL_USERDATA);

This exsists in a LOT of windows structs by the way and be extreamly useful. Hope that helps in some way.

-Warden

Share this post


Link to post
Share on other sites
vbisme    100
One more thing. I tried the BaseClass method. If I write a WrapperClass and inherit it from the base class I get link errors. But if I declare the DirectDraw variables within the WrapperClass nothing is wrong. There errors are:

error LNK2001: unresolved external symbol "protected: static struct IDirectDraw7 * DD_OBJ_VARS::DDObj" (?DDObj@DD_OBJ_VARS@@1PAUIDirectDraw7@@A)

etc......

Share this post


Link to post
Share on other sites
Oluseyi    2110
quote:
Original post by Warden
Since we''re working on porting ot other systems besides windows...

"..we really shouldn''t use a platform-specific API where possible."

quote:
Original post by vbisme
What are singleton classes?

A singleton class is a class of which only one instance may exist at any one time. To implement this, it''s constructor is usually made private and calling procedures obtain the instance by calling a static class method often named Create():

class CSimpleSingleton
{
public:
static CSimpleSingleton *Create();
//
private:
CSimpleSington() : m_iRef(1) {;}
static int m_iRef; // reference count
static CSimpleSingleton *pInstance; // the instance
};
// we need to define static data at file scope
int CSimpleSingleton::m_iRef = 0;
//
// Create() will return a pointer to the only existing instance;
// if the instance hasn''t been constructed, it is created then
CSimpleSingleton *CSimpleSingleton::Create()
{
if(m_iRef++)
;
else pInstance = new CSimpleSingleton();
return pInstance;
}

This is a horribly redundant example, but it illustrates the salient points.



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
TheTramp    122
You have the static member declared in the header file, but it doesn''t exist anywhere. You need to add a line like this in the cpp file for the class:

LPDIRECTDRAW7 DDWrapper::lpDD;

etc...

Share this post


Link to post
Share on other sites
jwalker    122
if the static is in the header ,
you have to declare it in the cpp also .

  
//a.h

class a
{
static int b;
}

//a.cpp

int a::b = 0;



if not you will get linker errors..




{ Stating the obvious never helped any situation !! }

Share this post


Link to post
Share on other sites
Shannon Barber    1681
Inside a class, static makes a property global to that class.
Since it''s static (ie class-global) it needs to be allocated outside a class instance. The declaration (the part in the cpp file) creates this storage space.

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites
Lowas    122

You can also say that a static member belongs to the class and a non-static member belongs to the object.

You only want ONE LPDIRECTDRAW pointer so if you use inheritance it needs to be static, other wise every class you derive from the base class will have it''s own LPDIRECTDRAW pointer which is not what you want, therefor at least make the LPDIRECTDRAW pointer static.

I also made my members in the base class protected so you could only get to them by deriving from the baseclass, otherwise one could use DD_Base::pDD anywhere in the program.

Share this post


Link to post
Share on other sites
Warden    122
quote:
Original post by Oluseyi
[quote]Original post by Warden
Since we''re working on porting ot other systems besides windows...

"..we really shouldn''t use a platform-specific API where possible."


True, but since I''m actually doing it only to learn all the different APIs.....

And it also helps us think about what we might want to do in projects were portability or large key variable exchange is nessicary, or even dynamic data exchange (wouldn''t that be neet!?) Anyway.... yeah, just thought I''d explain myself

-Warden

Share this post


Link to post
Share on other sites