Archived

This topic is now archived and is closed to further replies.

draw order using STL 2d vector of lists

This topic is 5176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am developing a 2d tile based RTS game. It is highly functional but my code to implement a proper drawing order is getting too complex as I add more bells and whistles. I am thinking I should use an STL container. Specifically a two dimensional vector containing pointers to STL lists of units to be drawn. Each tile in my map would have an entry (pointer to a list) in the 2d vector, ordered in typical draw order - l->r, t->b. Each list would have at least one item to draw and would rarely have more than 5 items or so. Items in the list would be ordered in draw order - back -> front. Currently I am using multiple 2d arrays - one for terrain, one for ground level units, one for units above ground level, several for various overlays (bridge, gems, craters, etc. Now that I have a basic particle generator working, I am having difficluty with implementing proper draw order and believe an STL approach could work well. I am stuck on the syntax for defining a 2D vector of lists of unit pointers. Here is my code with error description: typedef list < CUnit*, allocator < CUnit* > > LISTUNITS; typedef vector < vector > VECTOR2DUNITS; VECTOR2DUNITS v2dunits; for( x=0; x < 5; x++ ) v2dunits[x].reserve(10); // v2dunits is now like a 5x10 array CUnit* pUnit; // create Unit here pUnit = Create(TANK_ID, ...); v2dunits[2][3] = pUnit; only error message points last assignment: D:\My Projects\MFCFog\fog.cpp(2686) : error C2440: '=' : cannot convert from 'class CUnit *' to 'class std::list > *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast What is proper syntax for this? Do I need to define an allocator? [edited by - steve coward on October 10, 2003 5:14:36 PM] [edited by - steve coward on October 10, 2003 5:16:09 PM] [edited by - steve coward on October 10, 2003 5:16:51 PM] [edited by - steve coward on October 10, 2003 5:17:32 PM]

Share this post


Link to post
Share on other sites
Let me give it a shot. the space between the > is important, since > and >> are two different characters.
std::vector< std::vector< CUnit > > v2dunits;  

Ok, now, you are only reserving 1 set, not both, you need a double reserve.
v2dunit.reserve(5);

for(; i < 5 ; i++)
v2dunit.at(i).reserve(10);
//you now have a 5x10 array

now pUnit is a pointer, but it doesn't point to anything? If you want blank CUnits, then you are all set. If you want to define anything, I suggest doing this:
v2dunits.at(2).at(3) = CUnit(/*insert variables*/);  

you will have to add a constructor to do that, but it is prolly one of the easier ways.

On a side note, I use .at() rather than []. I suggest at least for debugging to use .at(). .at() throws an exception when you go out of bounds, [] doesn't. It is helpful for finding memory leaks.

~~~~~
"the best thing about betting on apathy is that even when you lose, you dont care." - nethead.
Download and play Slime King I.


[edited by - dede on October 10, 2003 4:41:07 PM]

Share this post


Link to post
Share on other sites
Thank you for your response.

I have edited my original post to fix the angled bracket syntax.

In reading your response I was able to find my major mistake.
I was assigning the pointer to the new unit to a vector entry instead of adding it to a list. The vector should be receiving a pointer to the list and not the unit itself.

Also I changed the reserve function to resize.

This seems to work for me:

typedef list > LISTUNITS;
typedef vector < vector > VECTOR2DUNITS;

VECTOR2DUNITS v2dunits;
LISTUNITS* pListUnits;
pListUnits = new LISTUNITS;

v2dunits.resize(5);
for( x=0; x < 5; x++ )
v2dunits.at(x).resize(10);

// v2dunits is now like a 5x10 array

CUnit* pMyUnit;
pMyUnit = MakeNewUnit(CRATE_ID, CPoint(10,10), 0, 0, 0);
pListUnits->push_back(pMyUnit);
v2dunits.at(2).at(3) = pListUnits;

Share this post


Link to post
Share on other sites