Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dynamic arrays

Started by _Sigma Nov 25, 2004 at 4:04 PM 4 replies 900+ views
Original Post
_Sigma
_Sigma
I have code like such


//contains the frames of animation -- Walking
       Frame Animation_Walk_Left[3];
       Frame Animation_Walk_Right[3];
       Frame Animation_Walk_Up[3];
       Frame Animation_Walk_Down[3];

       //contains the frames of animation -- fighting
       Frame Animation_Fight_Left[3];
       Frame Animation_Fight_Right[3];
       Frame Animation_Fight_Up[3];
       Frame Animation_Fight_Down[3];

       //contains the frames of animation -- Running
       Frame Animation_Run_Left[3];
       Frame Animation_Run_Right[3];
       Frame Animation_Run_Up[3];
       Frame Animation_Run_Down[3];

       //contains the frames of animation -- Die
       Frame Animation_Die_1[3];
       Frame Animation_Die_2[3];
       Frame Animation_Die_3[3];


But I want the the Animation sequences' size to be able to change, not stay locked at 3 members. In VB we ReDimmed the arrays. Can you do this in C++? I've never really had to do this before. Well..I did once, and I used vectors I think, and that was slow/painful. FYI, i'm using BCB 6. Also, would the template contained in sysdyn.h work fast enough for a game? Cheers Sigma
Jingo
Jingo
std::vector will not be a bottleneck if used correctly. It is also pretty trivial to use.
Arcibald Wearlot
Arcibald Wearlot
The best way is using std::vector. The speed overhead is really small so don't care.

std::vector<Frame> Animation_Walk_Left(3);//blah blahAnimation_Walk_Left[0].DoStuff();//Insert a new elementFrame MyNewFrame;Animation_Walk_Left.push_back(MyNewFrame);


If you don't change the dimensions of the array often, you could also use a pointer, but if you do you will have to deallocate and reallocate memory, and it's going to be slow if you do this every frame.

Frame* Animation_Walk_Left = new Frame[3];//use this like an arrayAnimation_Walk_Left[0].DoStuff();//Destroy it and recreate to change the sizedelete[] Animation_Walk_Left;Animation_Walk_Left = new Frame[5];
_Sigma
_Sigma
Ok thx. I'll look into thos
Moose6912
Moose6912
Ever considered a linked list?
_Sigma
_Sigma
I thought they were kinda slow....Besides, i'm only resizing once. So linked list would be overkill I think

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.