Thread: Sorting Out The C From The C++

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Sorting Out The C From The C++

    Ok, so I bought the book "Beginning Game Programming" by J. S. Harbour which includes Direct X. And I think I follow it really well (well the 2D the 3d seems harder and not very indepth so I will look at 2D games for the moment). But the book said it was written mostly in C and I am a C++ guy but it didn't seem to heavily reliant on C so I bought it. I am just trying to come to terms with where it is written in C:

    Mr Harbour uses typedef structs in the book for things like sprites, but is it common practice to use classes with C++ or doesn't it matter?

    For getting the file name of the image file he used sprintf() with a variable that incremented to alter the file name (sorry that is pretty vague butI don't have the book with me). What is the C++ equiv. to this?

    Also is things like d3dev->BeginScene() a correct sysntax in C++ and all other calls like it.

    Thanks,

    Bumfluff

    P.S Again sorry if all this is really vague.

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I have the same book. Classes and structs are the same thing. Except, if no identifier is given (public,private, protected), structs default to public and classes to private. Also, if you see any arrays for storing filenames, you can change them to use the 'string' type. You can also use things like vectors or better techniques, but the main concept of the book, is learning all the things that go into DX. Such as DX Surfaces and creating windows, so just change whatever you think is better, but leave the DX stuff the same.

    You made 2 threads lol!
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Yeah, I noticed...I hate Laptops they are sooooooo slow and the Keyboard is not pronounced enough.

    What do you happen to think of the book? I thought it was a lot better than the other one I have (Game Programming in 24Hrs)?

    How would I do what Mr Harbour did in his book with the char of filenames with a string?

    Ie. how do I get it to increment image1.bmp, image2, image3 etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How would I do what Mr Harbour did in his book with the char of filenames with a string?

    Ie. how do I get it to increment image1.bmp, image2, image3 etc.
    You'd most likely have to use stringstreams to convert a number to a string. Or you could make a char* and make a string out of it.
    Code:
    char str[X];
    // ...
    string s = str;
    See the FAQ under "how do I convert a number to a string (C++)" or something for stringstream information.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Ie. how do I get it to increment image1.bmp, image2, image3 etc.
    That's easy! You can use a 'string' data type quite easily. For example:

    Code:
    string img[5]={"blah.bmp","blah2.bmp","blah3.bmp"};
    
    for(int i=0; i < img[5].size();i++)
         LoadIMG(img[i]);
    Last edited by Sentral; 10-12-2006 at 05:26 PM.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > Mr Harbour uses typedef structs in the book for things like sprites, but is it common practice to use classes with C++ or doesn't it matter?

    Same thing, no problem. Although you can drop the typedef bit. It's only needed in C. I don't suggest you do any changes from struct to classes for now unless you wrap the whole of the struct members inside a public label.

    > For getting the file name of the image file he used sprintf() with a variable that incremented to alter the file name (sorry that is pretty vague butI don't have the book with me). What is the C++ equiv. to this?

    Technically the C++ equivalent to sprintf is... sprintf But it is true that there are better ways in C++. stringstreams.

    > Also is things like d3dev->BeginScene() a correct sysntax in C++ and all other calls like it.

    Absolutely. It's dereferencing the pointer d3dev and calling the member function of the resulting object. Although I find it strange you saw that example on C code. C structs don't allow function members.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The equivalent to sprintf in C++ is std::stringstream. It is faster, typesafe and extendable.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    >>Although I find it strange you saw that example on C code. C structs don't allow function members.

    He said that he would use some C++ code for the direct X but I don't think the direct X stuff is structs, as d3ddev was an original directX class not a struct.

    Anyway, I am back at my home PC now so I can begin to try DirectX for real, are there any good resources for functions and declarations so that I don't have to do as much trawling through my book?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM