Page 3 of 5

Re:serious this time

Posted: Fri Mar 05, 2004 5:36 am
by Joey
Sams Teach Yourself Game Programming in 24 Hours. I dont really like the Sams books, but this one is good.

Re:serious this time

Posted: Fri Mar 05, 2004 9:43 am
by Joel (not logged in)
my personal opinion is that the book you're using is probably not the best book to use to learn C++. it might be a good book to use to learn how to write Windows games.

in looking into that book a little bit, it looks like it's starting you out using the Windows API, which ain't exactly the easiest place in the world to start learning C++.

don't take this as me saying you must not use that book. if you're actually understanding what it's saying and what the code is doing, then by all means use it. however, be wary of drawing the conclusion that C++ is super-hard based on using the windows API (which, incidentally, is entirely C-based), and don't look to the windows api as an example of how programming should be done.

Re:serious this time

Posted: Fri Mar 05, 2004 1:48 pm
by Joey
i dont understand ALL of it, but i think i am beginning to understand some. the author stated that what we have done so far, I dont need to know, because he just wanted to show us how a windows program is set up and stuff. IF i get through this game good, i will probably eventually read another c++ book to increase my knowledge.

Re:serious this time

Posted: Fri Mar 05, 2004 1:49 pm
by Eero Ränik
Good luck on your quest to programming! ;) Now, you certainly need to read more books... I think that after reading this book you should know how to do simple text editors and stuff, but not much more. Not that you perfectly know game programming either... For that (supposing you want "step-by-step" books), try books like "Managed DirectX? 9 Kick Start: Graphics and Game Programming" or "Tricks of the Windows? Game Programming Gurus", then there's "Core Techniques and Algorithms in Game Programming", which is a nice book too. Just check your local library...

Re:serious this time

Posted: Sat Mar 06, 2004 4:13 am
by Schol-R-LEA
As it happens, I was able to borrow the SAMS book (2003 edition) from the local public library, so I think what I will do is go through the lessons myself. That way, if you have a specific question about something mentioned in the book, I should be able to help you with it.

Re:serious this time

Posted: Sat Mar 06, 2004 9:36 am
by Joey
wait you rented the Game Programming book I have? The copyright on mine is 2003. I have a major question if you do have the book. I was just reading chapter 3, and i got to the coding part. If you do have the book Schol, open up to page 51 and look at a few pages after that as well. I need to type in all that code, but what kind of files do i save it as? it doesnt say what to name each file, or what type of file to save it as. Its saying a Game Engine Class, then on the next set of code, there is all the code to be but for the WinMain() function, but i wouldnt know where to put all that code either.

I am just confused as to what kind of file to save this first set of code as, then where to put all the other sets of code. would it be in the same file, or another file?

Re:serious this time

Posted: Sat Mar 06, 2004 11:17 am
by chris
For C++ source files use ".cpp"

Re:serious this time

Posted: Sat Mar 06, 2004 1:16 pm
by Candy
For general c++ classes, use the classname as the filename. For the winmain, I'd make a WinMain.cpp for that.

Note though, even though windows programmers seem to prefer cpp, I (and others with me) like cc more.

Re:serious this time

Posted: Sat Mar 06, 2004 2:05 pm
by Joey
ahh ok. i thought that you would have to do a different filename for a class or something. so just use .cpp then like the others?

Re:serious this time

Posted: Sat Mar 06, 2004 2:21 pm
by Candy
Joey wrote: ahh ok. i thought that you would have to do a different filename for a class or something. so just use .cpp then like the others?
Yup. Not sure whether you know about headers & code files though:

The following is NOT required, there are a *lot* of ways to do this. This one is the clearest (imo):

You make a class. The class { .... }; goes in the header. The header has an inclusion guard, and has blank class xx; lines for all classes you need in the class definition of that class. There are no headers included from a header.

Each class has at least one code file (same name, extension .cc or .cpp) which all together contain all the class functions (don't declare them in the header, messes up the linking). They each include all the headers they need, so it's useful to put functions of the same type together.

You can use a pointer to a type without including the header. You can NOT use the object pointed to, nor do pointer math with the pointer, but you can use the pointer as a char * or as a passthrough value.

Examples:

Code: Select all

Car.h:

#ifndef CAR_H
#define CAR_H

class Passenger;

class Car {
 private:
  Passenger *passengers[4];
 public:
  Car();
  ~Car();
  void Enter(Passenger *person);
  void Leave(Passenger *person);
};

#endif

file Car-basic.cc:

#include "Car.h"

Car::Car() {
  // do something useful, nothing now
  for (int i=0; i<4; i++) passengers[i] = NULL;
}

Car::~Car() {
  // do something useful here
}

file Car-modify.cc:

#include "Car.h"
#include "Passenger.h"

Car::Enter(Passenger *passenger) {
  for (int i=0; i<4; i++) {
    if (passengers[i] != NULL) break;
  }
  if (i == 4) throw(passenger);  // personally like to throw with real things
  passengers[i] = passenger;
  if (passenger->isInCar()) passenger->leaveCar();
  passenger->enterCar(this);
}

Car::Leave(Passenger *passenger) {
  for (int i=0; i<4; i++) {
    if (passengers[i] == passenger) {
      passengers[i] = NULL;
      passenger->leaveCar();
    }
  }
}

HTH, Candy

Re:serious this time

Posted: Sat Mar 06, 2004 3:15 pm
by Tim
Candy wrote:There are no headers included from a header.
You do need to include the headers anywhere a class is used not as a pointer or a reference. So the example below is fine, but if you declared [tt]Passenger passengers[4];[/tt] (note: no pointer), you'd have to include "Passenger.h".
You can use a pointer to a type without including the header. You can NOT use the object pointed to, nor do pointer math with the pointer, but you can use the pointer as a char * or as a passthrough value.
Note that the same restrictions apply to references. You can declare a reference to a class (or other type) without including the class's header file, but you can't actually 'look inside' the reference if the class is not defined (i.e. if you haven't included the header).

Re:serious this time

Posted: Sat Mar 06, 2004 3:50 pm
by Candy
Tim Robinson wrote:
Candy wrote:There are no headers included from a header.
You do need to include the headers anywhere a class is used not as a pointer or a reference. So the example below is fine, but if you declared [tt]Passenger passengers[4];[/tt] (note: no pointer), you'd have to include "Passenger.h".
Ok, that's true. To make the story complete, all cases when you /NEED/ to include the header of another class in this header:

It's a part of this class.

That's all reasons. There are multiple ways (2 I can think of, inheritance & instantiation) but it all comes down to being a part of the class.

Note that I would still advise you to include all the headers from the source files, but this at least does keep out all the cyclic links (and if you do get a cycle with this you've tried to create an object that included itself. That's not possible. If you don't see why not, try sizeof()ing it in your head).

Re:serious this time

Posted: Sat Mar 06, 2004 8:48 pm
by Schol-R-LEA
Sorry for taking so long to get back to you on this, but...

OK, looking at the code you mention, you would actually want to put the class declaration for [tt]GameEngine[/tt] in a header file rather than a source file. This will allow you to share the class declaration with other files while letting you actually define the functions separately. So name that file gameengine.h and save it.

Perhaps an explanation is in order. Header files are a C and C++ convention for sharing common declarations between source files. By tradtion, header files use the extension .h instead of .c or .cpp (there are actually several common extensions used for C++, such .cc, .C, or .cxx; they all mean a C++ source file, that is to say, a file with the actual function code in it). By putting the type and class definitions and the function prototypes in the header, it lets the same definitions appear in several different source files. To use a header file with in a source file, you would have a line near the top of the source file like this:

[tt]#include "foo.h"[/tt]

The #include statement is a preprocessor directive that says, 'take the contents of foo.h and insert them into the source file right here before compiling'. It is a straight text inclusion, with no other immediate side effects.

There is a special convention for standard headers, which you've probably seen, in which the double quotes are replaced by angle brackets:

[tt]#include <stdcheader.h>[/tt]

or even

[tt]#include <cplusplusheader>[/tt]

This tells the preprocessor to look in certain standard directories for the header rather than in the local directory. The ones without the extension are certain C++ only headers, which needed to be distinguished from the headers used by both C and C++.

It looks like the rest of the code is meant be compiled more or less as a single file; crappy programming style, but it is I suppose simpler that way. When I tried to break it up into separate sections, I had to make a handful of minor changes, and still ended up with a linker error which I haven't been able to resolve. I'll get back to you later on this.

Re:serious this time

Posted: Sun Mar 07, 2004 5:50 am
by Eero Ränik
By the way Joey, didn't you get a CD with that book? It has all the code files in there, so you could easily check if you have questions like that. Just check at the Examples folder, depending on the IDE (it has project files) either BCPP (Borland C++) or VCPP (Visual C++) and in your case Chap03\GameSkeleton.

Re:serious this time

Posted: Sun Mar 07, 2004 11:20 am
by Joey
oh crap! i forgot all about that eero! sorry i dont have time to read over your guys posts right now cause im going out, but ill read it later and ill check the source later. thanks guys again.