Array of object C++ -


in image below (character.cpp), may know how create 1 initialize method can called stored many sprites? need change texture1,sprite,posx,posy, etc array? initialize method called in main.cpp. sorry if explaination not enough. idea of doing there better ones instead of having many arrays?

void character::initialize1(string image1, float posx1, float posy1, float centerx1, float centery1) {     d3dxcreatetexturefromfile(pull->getd3ddev(), image.c_str(), &texture1);     d3dxcreatesprite(pull->getd3ddev(), &sprite1);     rect spriterect1;     spriterect1.top = 0;     spriterect1.bottom = 127;     spriterect1.left = 0;     spriterect1.right = 128;     spritepos1 = d3dxvector2(posx1, posy1);     spritecenter1 = d3dxvector2(centerx1, centery1); }  void character::initialize2(string image2, float posx2, float posy2, float centerx2, float centery2) {     d3dxcreatetexturefromfile(pull->getd3ddev(), image.c_str(), &texture2);     d3dxcreatesprite(pull->getd3ddev(), &sprite2);     rect spriterect2;     spriterect2.top = 0;     spriterect2.bottom = 14;     spriterect2.left = 0;     spriterect2.right = 14;     spritepos2 = d3dxvector2(posx2, posy2);     spritecenter2 = d3dxvector2(centerx2, centery2); } 

create necessary initialization method in sprite class. in main() create sprites , call appropriate initialization methods. if using lots of sprites, handy put created sprites inside vector in order have less code cleanup , other things.

a quick example sprite class called spriteconfig, contains position parameters.

#include <iostream> #include <vector>  using namespace std;  class spriteconfig { public:     spriteconfig() {         top = 0;         bottom = 0;         left = 0;         right = 0;     }      void setpos(float atop, float abottom, float aleft, float aright) {         mtop = atop;         mbottom = abottom;         mleft = aleft;         mright = aright;     } private:     // position parameters     float mtop;     float mbottom;     float mleft;     float mright; };   int main() {     vector<spriteconfig*> spriteconfigs;     spriteconfig *sprcfg;      sprcfg = new spriteconfig();     sprcfg->setpos(1,1,1,1);     spriteconfigs.push_back(sprcfg);      sprcfg = new spriteconfig();     sprcfg->setpos(2,2,2,2);     spriteconfigs.push_back(sprcfg);      // have number of sprites     // add code here it.     // ...      for(vector<spriteconfig*>::iterator = spriteconfigs.begin();         != spriteconfigs.end();         ++it) {         // cleanup         delete (*it);         (*it) = null;     }      return 0; } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -