DirectDraw in a class

Started by
15 comments, last by vbisme 22 years, 5 months ago
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.
Advertisement
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
Is it a good idea though, to declare it in the wrapper class rather than global?
class DDWrap
{
friend CSomeClassThatUsesThisClass''sRivateMembers; // ummyeh
private:
int somedumbvarthatwillbeusedbytheclassabove;
}


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 gamesclass 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 pointersclass 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

C++, C++, C++
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 laterpDirectDraw = (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
-Warden
What are singleton classes?
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......
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 scopeint CSimpleSingleton::m_iRef = 0;  //// Create() will return a pointer to the only existing instance;// if the instance hasn''t been constructed, it is created thenCSimpleSingleton *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!
What''s up with the link error?

This topic is closed to new replies.

Advertisement