Jump to content
  • Advertisement
Sign in to follow this  
chiraggystyle

How do I access a member from another class?

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I am attempting to make a Pong game using Direct3D as practice for basic D3D9 and getting better C++ skills. I have the following classes: Cd3d - This class is basically for all the d3d initialization stuff it also contains the pointer to the device... gameEntity - An interface for any object, like paddles, ball, and score board. It has x and y position variables, and virtual Update() and Render() member functions. Cball - the class for the ball, it inherits from gameEntity. My problem starts at Cball, i want to create a mesh for ball, but i need to be able to access my device from Cd3d in order to use BeginScene(), EndScene(), and D3DXCreateSphere(). Is there a better way to design my code? Should I create a namespace for all the d3d stuff? I dont know. Whats the best way to go about this kinda stuff? Thanks for any input!!

Share this post


Link to post
Share on other sites
Advertisement
Hey there.

You can create a namespace if you want to qualify your Direct3d stuff. Namespaces will allow you to use the same function names, if you so wish, as the qualification via namespace provides this seperation.

As to your graphics classes there are a few ways to do this either make the gameEntity class a friend of the Cd3d class, or pass a pointer to the Cd3d Class to the paddles, ball etc. classes when you instansiate a paddle, ball etc. object

OR

It might be an idea to make the Cd3d class a singleton, if there is only ever going to be one instance of it, and provide static functions to access the functions and data within it.

Just some ideas, am sure that there'll be plenty more...good luck.

Share this post


Link to post
Share on other sites
Quote:
Original post by garyfletcher
Hey there.

You can create a namespace if you want to qualify your Direct3d stuff. Namespaces will allow you to use the same function names, if you so wish, as the qualification via namespace provides this seperation.

As to your graphics classes there are a few ways to do this either make the gameEntity class a friend of the Cd3d class, or pass a pointer to the Cd3d Class to the paddles, ball etc. classes when you instansiate a paddle, ball etc. object

OR

It might be an idea to make the Cd3d class a singleton, if there is only ever going to be one instance of it, and provide static functions to access the functions and data within it.

Just some ideas, am sure that there'll be plenty more...good luck.


Yes, make your Cd3d class a singleton, since there will only ever be one instance of the Cd3d object.

or an easier (but dumber) way is not to have a Cd3d class, instead, declare your d3d interfaces globally in a file eg globals.h . In other files eg GameEntity.cpp, you can use extern to declare those interfaces, and use them:


--global.h--
LPDIRECT3DDEVICE9 gp_d3ddevice;

--GameEntity.cpp--
extern LPDIRECT3DDEVICE9 gp_d3ddevice;

//start using it...


hope that helps...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!