0

In the image below (Character.cpp), may I know how to create only one Initialize method that can be called to stored many sprites? Do I need to change the Texture1,Sprite,PosX,PosY, etc to array? The initialize method will be called in my MAIN.cpp. Sorry if the explaination is not good enough. That is just my idea of doing it but will there be a better ones instead of having so 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);
}
2
  • @Biffen Sorry. Edited it Commented Dec 5, 2014 at 21:23
  • You could have array of structs containing your variables Commented Dec 5, 2014 at 23:35

1 Answer 1

1

Create the necessary initialization method in your sprite class. Then in main() create your sprites and call the appropriate initialization methods. If using lots of Sprites, it will be probably handy to put the created sprites inside a vector in order to have less code for cleanup and probably other things.

A quick example for a Sprite class called SpriteConfig, which only 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);

    // We now have a number of Sprites
    // Add code here to do something with it.
    // ...

    for(vector<SpriteConfig*>::iterator it = spriteConfigs.begin();
        it != spriteConfigs.end();
        ++it) {
        // cleanup
        delete (*it);
        (*it) = null;
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hey @Gio..But how am I gonna differentiate and call a certain sprite?
In case your sprites are inside a vector, as in the above example. Then there are various ways to obtain your sprite from the vector. E.g. spriteConfigs.at(idx). If you want more diffentation you can consider creating a define for you index. E.g. #define CIRCLE 0, and then you put your circle sprite at index 0 of the vector. Now you can obtain your sprite by using spriteConfigs.at(CIRCLE).
Note that this is perhaps not the most beautiful solution. An alternative would be to use a Map instead of a vector. This is an associative container and therefore give you better possibilities to differentiate the various sprites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.