• 0

[c++] Writing a game


Question

I'm writing a game for an exploratory c++ project (just coming off java). I've used this method in the past with java, and its worked quite well for me so (naturally) I'm trying to use the same ideas now that I'm learning c++.

I write all my game classes as children of one generic class (I call the generic class a 'cell'). All other classes (player, brick, enemy1, enemy2, etc.) inherit this class. The cell class contains the objects coordinates and dimensions (for collisions) and methods to execute a "step" event (the objects logic, executed every frame) and of course a method for drawing the object.

In java, I would create an ArrayList<cell> and every frame it would run step() for each then draw() each. From what I can tell, the equivalent in c++ is the std::vector class, but I'm having trouble using this. First, vector is expecting all kinds of requirements of my cell class (copy constructors, etc.) and from what I can tell all its sub-classes, too (not to mention some interesting combinations of these as well :( ).

Second, I'm having trouble adding a sub-class of cell to the vector. In java, it was automatically type-casted and treated like just a generic cell for step and drawing purposes (I've made these virtual in c++ so I can override them). Are pointers considered a type? Would I have less casting trouble using vector<cell*> instead of vector<cell>? Do I have to make copy constructors if they're just pointers in the first place?

What I'd like to know is how everyone else manages their game objects. Am I on the right track? Should I ignore the vector class and write my own "arraylist" class (its not like I need those iterator functions anyway)? Should I try the pointer thing (I'm new to working with pointers in general, this isn't like java where everything is a pointer)?

Any help is appreciated... thanks!

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0
I'm writing a game for an exploratory c++ project (just coming off java). I've used this method in the past with java, and its worked quite well for me so (naturally) I'm trying to use the same ideas now that I'm learning c++.

I write all my game classes as children of one generic class (I call the generic class a 'cell'). All other classes (player, brick, enemy1, enemy2, etc.) inherit this class. The cell class contains the objects coordinates and dimensions (for collisions) and methods to execute a "step" event (the objects logic, executed every frame) and of course a method for drawing the object.

In java, I would create an ArrayList<cell> and every frame it would run step() for each then draw() each. From what I can tell, the equivalent in c++ is the std::vector class, but I'm having trouble using this. First, vector is expecting all kinds of requirements of my cell class (copy constructors, etc.) and from what I can tell all its sub-classes, too (not to mention some interesting combinations of these as well :( ).

Second, I'm having trouble adding a sub-class of cell to the vector. In java, it was automatically type-casted and treated like just a generic cell for step and drawing purposes (I've made these virtual in c++ so I can override them). Are pointers considered a type? Would I have less casting trouble using vector<cell*> instead of vector<cell>? Do I have to make copy constructors if they're just pointers in the first place?

What I'd like to know is how everyone else manages their game objects. Am I on the right track? Should I ignore the vector class and write my own "arraylist" class (its not like I need those iterator functions anyway)? Should I try the pointer thing (I'm new to working with pointers in general, this isn't like java where everything is a pointer)?

Any help is appreciated... thanks!

Hi, you on right track. Add to <cell> boolean which shows needs this class to be redrawn. And set it to true only if class changed its state (in ->step). But I think, you already did it :).

You need create std::vector<cell*> and add your sub-classes to it, maybe you need manual type cast: vector<cell*>.puch_back((cell*)sub-class*);

I think, you don't need copy constructors. Only if you want several copies of identical object. I didn't see your code, but I still think you don't need it.

Your own "arraylist" you need to write only when speed of std::vector is not enough. Then you have to try other realizations of std library (faster then MS C++ or what you are using), if their speed is not enough, then write your own :).

I'll give another advice, if you take your game serious take free game engine, and write your game based on it. You'll win in all ways:

1. You'll get cool working game :)

2. You'll see how big guys does it. This is priceless experience, believe me (since free game engines comes with sources).

3. You'll do it much faster.

4. You can focus on breathtaking gameplay, not on technical details.

Some links:

1. http://sourceforge.net/projects/popcapframework/ - gameengine of PopCap games, it simple, but powefull. Have enough to create small casual game.

2. http://hge.relishgames.com/ - another small nice engine.

3. and most powerful, but more complicated: https://developer.playfirst.com/

This engines all used for make commercial games all around world.

Good luck!

Link to comment
Share on other sites

  • 0
Hi, you on right track. Add to <cell> boolean which shows needs this class to be redrawn. And set it to true only if class changed its state (in ->step). But I think, you already did it :).

You need create std::vector<cell*> and add your sub-classes to it, maybe you need manual type cast: vector<cell*>.puch_back((cell*)sub-class*);

I think, you don't need copy constructors. Only if you want several copies of identical object. I didn't see your code, but I still think you don't need it.

Your own "arraylist" you need to write only when speed of std::vector is not enough. Then you have to try other realizations of std library (faster then MS C++ or what you are using), if their speed is not enough, then write your own :).

I'll give another advice, if you take your game serious take free game engine, and write your game based on it. You'll win in all ways:

1. You'll get cool working game :)

2. You'll see how big guys does it. This is priceless experience, believe me (since free game engines comes with sources).

3. You'll do it much faster.

4. You can focus on breathtaking gameplay, not on technical details.

Some links:

1. http://sourceforge.net/projects/popcapframework/ - gameengine of PopCap games, it simple, but powefull. Have enough to create small casual game.

2. http://hge.relishgames.com/ - another small nice engine.

3. and most powerful, but more complicated: https://developer.playfirst.com/

This engines all used for make commercial games all around world.

Good luck!

Thanks for the reply!! I needed those things cleared up... Yes I ended up getting the the vector<cell*> bit to work,i just made a couple errors in my syntax.. you know how it is ;) But now i've got vector doing what I want it to, so I can finally move on :D

And thanks for the links, although this time I'll be writing the engine from scratch (minus the graphics library :p), I'll keep them for the future when I'm not doing a project for school :)

Much appreciated!

Link to comment
Share on other sites

  • 0
Thanks for the reply!! I needed those things cleared up... Yes I ended up getting the the vector<cell*> bit to work,i just made a couple errors in my syntax.. you know how it is ;) But now i've got vector doing what I want it to, so I can finally move on :D

And thanks for the links, although this time I'll be writing the engine from scratch (minus the graphics library :p), I'll keep them for the future when I'm not doing a project for school :)

Much appreciated!

You can share parts of code where you stuck, so help will be more constructive. :D

Good Luck!

Link to comment
Share on other sites

  • 0

You should have always done cell *. The other way would have been a vector of sizeof(cell) * vector.length() in memory lol. That would have been awful. This is away easier. Plus, you'll get 4 * vector.length() in memory instead ;) much nicer.

Its very good that you are sticking to shallow inheritance though. If you want to go super hardcorez look into component based design. Good luck :)

Link to comment
Share on other sites

  • 0
You should have always done cell *. The other way would have been a vector of sizeof(cell) * vector.length() in memory lol. That would have been awful. This is away easier. Plus, you'll get 4 * vector.length() in memory instead ;) much nicer.

What are you on about?! Memory still needs to be allocated for the objects, so with the second solution you will have (sizeof(cell) * vector.length()) + (4 * vector.length()). The first way also has benefits in that the memory is contiguous, so there a fewer cache misses and therefore higher performance (when looping through them).

However, he needed to use the second way because that's just how C++'s polymorphism works.

Link to comment
Share on other sites

  • 0
What are you on about?! Memory still needs to be allocated for the objects, so with the second solution you will have (sizeof(cell) * vector.length()) + (4 * vector.length()). The first way also has benefits in that the memory is contiguous, so there a fewer cache misses and therefore higher performance (when looping through them).

However, he needed to use the second way because that's just how C++'s polymorphism works.

Hey maybe he wants to do reference counting and have one over-arching abstract class?

LOL though, my bad he's completely right, I have no idea what I was thinking. Sorry about that.

Link to comment
Share on other sites

  • 0

The main reason I'm sticking with vector<cell*> is that the pointer "type" is already compatible with vector, I don't have to write my own copy constructors.

Anyway I was about to write about an error I had, but I fixed it.

But I would still like to know why:

player p(100,200);
cells.push_back( &amp;p );

is different from:

player* p = new player(100,200);
cells.push_back( p );

Link to comment
Share on other sites

  • 0

The difference is where you store the variable in memory.

This one will probably crash

vector&lt;cell*&gt; vec;

{
	player p(100,200);
	cells.push_back( &amp;p );

   // p now goes out of scope and is destroyed
}

*vec[0]; // This will attempt to access the destroyed player, probably crashing the program

This one wont

vector&lt;cell*&gt; vec;

{
	player* p = new player(100,200);
	cells.push_back( p );
}

*vec[0]; // This will attempt to access the player that still exists

// Here you need to delete the memory allocated to each player

Link to comment
Share on other sites

  • 0
The difference is where you store the variable in memory.

This one will probably crash

vector&lt;cell*&gt; vec;

{
	player p(100,200);
	cells.push_back( &amp;p );

   // p now goes out of scope and is destroyed
}

*vec[0]; // This will attempt to access the destroyed player, probably crashing the program

This one wont

vector&lt;cell*&gt; vec;

{
	player* p = new player(100,200);
	cells.push_back( p );
}

*vec[0]; // This will attempt to access the player that still exists

// Here you need to delete the memory allocated to each player

Ohh!! I understand, thanks a lot!! :D

The game's coming along nicely, thanks to everyone!

Link to comment
Share on other sites

  • 0

Well, I'm having another vector issue...

In my cell.h, I have a constructor to pass in a reference to the vector containing all the cells (for collision purposes):

cell(int xx, int yy, vector&lt;cell*&gt;* c);

This won't compile. I'm assuming because I've referenced the cell type before it's been initialized. Yet this works fine:

bool collides(cell* c);

I'm guessing something with the template is the problem... How am I supposed to get around this?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.