Jump to content
  • Advertisement

Archived

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

Ghost within

a quick Q on C, C++, and Directx

This topic is 6395 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

I learned c++ originally, then bought this great book, "tricks of the windows game programming gurus", in order to try my hand at game programming. The book though, uses c. Now, of course I know that c + c++ are compatible, but how much so? can i use snippets of both languages in the same *.cpp file? The problem is that i dont know how much of the stuff that the book has taught me is straight c, and how much is c/c++ (like using structs instead of classes, whats that about?) will this be a problem? for example, heres a call from the books directx section: lpdd->SetVideoMode(640, 480, 256); now it seems to me that the -> should be a ::. or is the -> a COM thing? or this piece of code near the beginning of WinMain() WNDCLASSEX wndclassex; there''s no assignment operator! is this ok? is this a c thing? IF ANYONE HAS HAD A SIMILAR PROBLEM ****I WOULD APPRECIATE ANY HELP/QUICK LESSONS YOU COULD GIVE ME**** (on changing c to c++ or using both or whatever)

Share this post


Link to post
Share on other sites
Advertisement
Woah, there...

quote:

for example, heres a call from the books directx section:
lpdd->SetVideoMode(640, 480, 256);
now it seems to me that the -> should be a ::. or is the -> a COM thing?
or this piece of code near the beginning of WinMain()
WNDCLASSEX wndclassex;
there''s no assignment operator! is this ok? is this a c thing?



The -> operator is standard C. Read over your books a bit more carefully

It''s basically the equivalent of the "." (i.e. struct.member) operator, except that it works on pointers to structs or classes.

And if you studied your notation, lp dd means that it''s a long pointer to an object, so the -> operator has to be used, or some really funky syntax.

And WNDCLASSEX is a structure, so he''s defining a variable of type WNDCLASSEX, not assigning it something.

Here''s an example of both:

  
// the Dog structure

struct Dog
{
int age;
}

int main()
{
Dog Fido; // an object of type fido

Fido.age = 5; // normal way to access members


Dog* Fluffy = new Dog; // a pointer to a Dog-block of memory on the free store

Fluffy->age = 10; // way to access pointer members


delete Fluffy;

return 0;
}


-----------------
The Goblin
-----------------
"Oh, God..."
"Yes?" <- My Response

Share this post


Link to post
Share on other sites
Just to make a point, while LaMothe uses C-style syntax (read procedural) throughout the book, he is definitely using C++ and NOT C. You do not have to use classes in C++. And for those who would argue that I''m nitpicking, consider this:

You want to release your DDraw object when your app shuts down. With a C++ compiler you would write:

lpdd->Release ();

Change your file extension to .c and that code WILL NOT compile. In C, you have to make the call like this:

lpdd->Release (lpdd)

This has to do with the this pointer that is always passed along in C++ object member functions (including COM objects). So, to reiterate, that is most definitely C++ code in the book.

To clear up what Goblin said, LPDIRECTDRAW7 is defined as a pointer to an IDirectDraw object in ddraw.h, like so:

#define LPDIRECTDRAW7 IDirectDraw7 *

This means that you could also declare your ddraw objects like this:

IDirectDraw7 *lpdd;

-> is not only standard C, it is the only way to access data through a C-style pointer in C or C++. So, because you can only have access to IDirectDraw7 objects via pointers, you will always use -> to call member functions.




GamesToGO: The Console Gamer''s Paradise

Share this post


Link to post
Share on other sites
ALdacron is not entirely correct the call should be:

lpdd->vtbl->Release(lpdd);

in C.

It''s me, it''s that T-H-E-D.

Share this post


Link to post
Share on other sites

  • 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!