Archived

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

Nali

Accessing DirectDraw object between classes?

Recommended Posts

I have 2 classes, C_Menu & C_Game. lpDDSbackbuffer (the secondary buffer) is a private member in C_Game and I need to access it in a member function of C_Menu C_Menu has a function called DrawGraphics() which needs the backbuffer in order to blit. How do I get access to the backbuffer in CMenu ? I have tried this "solution": I added a pointer to an DIRECTDRAWSURFACE in CMenu and made an overloaded function CMenu::CMenu(LPDIRECTDRAWSURFACE *pLpDDSBuffer); But I get this error when trying to blit (in CMenu using a pointer): "error C2227: left of ''->BltFast'' must point to class/struct/union" hope you understand my poor description..

Share this post


Link to post
Share on other sites
i''m having this same problem... the solution is a friend function, even though i can''t figure it out yet....

you declaration like this in C_Game:

friend void C_Menu::DrawGraphics();

something like that anyway, i didn''t actually get it working yet..

this gives C_Menu::DrawGraphics() access to all of C_Game''s private members.

...go on and live with no regrets, you only have one life...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
when adding the friend keyword I got this error:
error C2039: ''DrawGraphics'' : is not a member of ''C_Menu''
see declaration of ''C_Menu''

strange...

Share this post


Link to post
Share on other sites
when adding the friend keyword I got this error:
error C2039: ''DrawGraphics'' : is not a member of ''C_Menu''
see declaration of ''C_Menu''

strange...

Share this post


Link to post
Share on other sites
Hi guys.

One way of doing this is making your C?Game class a singleton. For example:

  
class C_Game
{
LPDIRECTDRAW7 m_lpdd;
LPDIRECTDRAWSURFACE7 m_lpPrimary;
LPDIRECTDRAWSURFACE7 m_lpSecondary;

C_Game() {}; //constructor


public:
LPDIRECTDRAW7 getDD() { return m_lpdd; };
LPDIRECTDRAWSURFACE7 getPrimary() {return m_lpPrimary; };
LPDIRECTDRAWSURFACE7 getSecondary() {return m_lpSecondary; };

static C_Game* getObj()
{
static C_Game m_pObj;
return &m_pObj;
}
}


Now, to access the primary: C_Game::getObj()->getPrimary()
Secondary: C_Game::getObj()->getSecondary()

You can add a member to your C_Menu class like so:
C_Game *m_cGame;
and initialize it like so:
m_cGame=C_Game::getObj();
and us it later like so:
m_cGame->getPrimary();

There are a number of way to create a singleton but learnt it cause it''s going to make things a lot easier and neater for you.

To help you out, read this:
http://www.gamedev.net/community/forums/topic.asp?topic_id=72269

Good luck!

Why make it simple when you can make it sooo nice and complicated.

Share this post


Link to post
Share on other sites
quote:
Original post by Nali
when adding the friend keyword I got this error:
error C2039: ''DrawGraphics'' : is not a member of ''C_Menu''
see declaration of ''C_Menu''

strange...


Did you forget to add the class name you know,

... class::function ...

Once again, I''d strongly recommend singleton. Don''t overuse it once you learn it though.

Share this post


Link to post
Share on other sites
Oh, and an idea to think of.

Do you have to have a Blt function in CMenu? I''d look into the possibility of having the Blt function int the C_Game class. Once you make C_Game a singleton you can access the Blt function in C_Game m_cGame->Blt(...args..) and the C_Game knows where to Blt.

Just an idea that you could think of, but depending on your way of building the program this might be a waste (extra function calls and dereferences)

Lycka till.

Share this post


Link to post
Share on other sites
well.. its not necessary to have a blt function in C_Menu but isn''t a little bit more professional =) ??

I dont really get this "singelton" stuff... but I''ll read it until I understand it..

Share this post


Link to post
Share on other sites
Hmm...the best solution is the fastest while keeping the quality, professional or not. Personally I don''t like to have Blt functions spread all over the application but it might be faster and then I''ll do it.

Trust me, you''ll be happy when you know how to use singletons and it is not hard just a little confusing to understand.

(huh! amerikanskt tangentbord, jag skriver fel! #$!@)

You know static variables? they keep their value even after they go out of scope. The same with singletons. The first time you access getObj() function above the C_Game singleton object is created. Next time this function is called the same object is returned! Sounds weird? Think of it you have one game object, never have to worry that the object is created twice and you can access it all over your application - no need for a global with all the extern declarations and names you might change.

As long as you keep the interface of the singleton untouched you can optimize it without reworking the rest of your application!

You should look into the construction and destruction of the singleton, there are different ways of doing it depending on what control you need.

Have fun!

Share this post


Link to post
Share on other sites
BTW I noticed something, if I remember the standard used by windows programers in the line :
LPDIRECTDRAWSURFACE *pLpDDSBuffer
you are creating a pointer to a pointer
so thats why you have the error
you can use either LPDIRECTDRAWSURFACE pLpDDSBuffer or DIRECTDRAWSURFACE *pLpDDSBuffer

Share this post


Link to post
Share on other sites
hm... I think I understand the basics of singletons..
but I didnt get the code to work

this is how it looks now:
class C_Game
{
LPDIRECTDRAW m_lpDD;
LPDIRECTDRAWSURFACE m_lpDDsPrimary;
LPDIRECTDRAWSURFACE m_lpDDsBuffer;

C_Game();

public:

LPDIRECTDRAWSURFACE GetBuffer(void) { return(m_lpDDsBuffer); }
LPDIRECTDRAWSURFACE GetPrimary(void) { return(m_lpDDsPrimary); }
LPDIRECTDRAW GetDD(void) {return(m_lpDD);}

static C_Game* GetObj()
{
static C_Game m_pObj;
return &m_pObj;
}
private:
...
};

in C_Menu I added C_Game *m_pGame under the private directive

but I get this error:
error C2352: ''C_Game::GetBuffer'' : illegal call of non-static member function

when I use this code in the C_Menu constructor
m_pGame = C_Game::GetObj();
m_plpDDsBuffer = C_Game::GetBuffer();

Share this post


Link to post
Share on other sites