Advertisement

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

Started by February 13, 2001 01:21 PM
3 comments, last by Ghost within 23 years, 11 months ago
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)
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 structurestruct Dog{int age;}int main(){Dog Fido; // an object of type fidoFido.age = 5; // normal way to access membersDog* Fluffy = new Dog; // a pointer to a Dog-block of memory on the free storeFluffy->age = 10; // way to access pointer membersdelete Fluffy;return 0;}  


-----------------
The Goblin
-----------------
"Oh, God..."
"Yes?" <- My Response
- The Goblin (madgob@aol.com)
Advertisement
All of the problem examples you gave are standard C/C++. They''re meant to be that way, maybe you should read over your C++ book again real quick .

Anyway, C is C++, some of C++ is C. Putting C code in a C++ file will almost never lead to any problems. Put C++ in a C file often times can though...



http://www.gdarchive.net/druidgames/
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
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.
If you missed a great moment in history don't worry, it'll repeat itself.

This topic is closed to new replies.

Advertisement