Accessing DirectDraw object between classes?

Started by
11 comments, last by Nali 22 years, 3 months ago
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..
Advertisement
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...
...go on and live with no regrets, you only have one life...
oki!.. I''ll try that..
Maybee it''s time to look in my C++ book and refresh my knowledge?! =)

thanks!
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...
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...
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() {}; //constructorpublic:   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.
Why make it simple when you can make it sooo nice and complicated?
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.

Why make it simple when you can make it sooo nice and complicated?
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.
Why make it simple when you can make it sooo nice and complicated?
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..
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!
Why make it simple when you can make it sooo nice and complicated?

This topic is closed to new replies.

Advertisement