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)?
Question
aeron
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