• 13
• 18
• 19
• 27
• 10

# help with my class

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

## Recommended Posts

hi i create a sprite container for my engine but when i make an instance i.e test = new SpriteContainer(200, 300, 2.0, 0.34, 4); test->cutSprite("Data\\Characters\\punch.bmp", 255, 0, 255, 224, 143); test->draw(); the game crashes when i try to run it. are the sprites being added to the v_mSprite vector using the resize function?


#ifndef SPRITECONTAINER_H
#define SPRITECONTAINER_H

#include <dxu.h>
#include <vector>

using namespace std;

class SpriteContainer
{
public:
SpriteContainer(float x, float y, float scaleAmount, double ratio, int frames);
~SpriteContainer();

void cutSprite(TCHAR *path, UCHAR red, UCHAR green, UCHAR blue, int sX, int sY);
void draw();

protected:
vector<DXUSPRITE *> v_mSprite;

private:
int cFrames;
float dX, dY;
float sX, sY;
DXUIMAGE *image;
int curSprite;
double cRatio, temp;
};

//class----------------------------------------------------------

#include "SpriteContainer.h"

SpriteContainer::SpriteContainer(float x, float y, float scaleAmount, double ratio, int frames)
{
cFrames = frames;
v_mSprite.resize(cFrames);
dX = x;
dY = y;
sX = sY = scaleAmount;
curSprite = 0;
cRatio = ratio;
temp = 0.0;
}

SpriteContainer::~SpriteContainer()
{

}

void SpriteContainer::cutSprite(TCHAR *path, UCHAR red, UCHAR green, UCHAR blue, int sX, int sY)
{
image = new DXUIMAGE();

for(int i = 0; i < cFrames; i++)
{
DXUCutSprite(image, v_mSprite, i * sX, 0, (i * sX) + sX, sY, dX, dY);
}
}

void SpriteContainer::draw()
{
temp += cRatio;

if(temp > 1.2)
{
curSprite = (curSprite + 1) % cFrames;
temp = 0.0;
}

DXUMoveSprite(v_mSprite[curSprite], dX, dY);
DXUDrawSprite(v_mSprite[curSprite]);
}

#endif



##### Share on other sites
Ummm.. not quite sure, but i think its cuz you have to define the member functions in a cpp file ,not a header file [smile]

I could be wrong, but test it out :D

##### Share on other sites
Which line is it crashing on? Use the debugger to narrow down to where it is crashing, and then take a look at the variables to see what is going on.

##### Share on other sites
hi there are both in a header and a cpp file, i am using vs 2005. The problem is when i use the debugger it doesn't really tell me anything i get this when i debug using vs 2005 c++?

it just shows me some memory allocation list doesn't show pointers at the code?

##### Share on other sites
1. resize() does not add more to the vector, it just makes it longer. Kinda confusing, but it happens. You're looking for append().

2. Are the files in the correct place?

To let you know, assuming that the long comment indicates a new file, your declarations and definitions of the member functions are fine. That is, except for one thing. The #endif should be in the header not in the cpp file. It will actually make the #ifndef guards pointless. That would be a compile-time error, not run-time, though.

##### Share on other sites
Quote:
 Original post by Ezbez1. resize() does not add more to the vector, it just makes it longer. Kinda confusing, but it happens. You're looking for append().

You're thinking of reserve. resize does add more items to the vector. The only problem is that it adds default initialised values to the vector, which in this case is the null pointer. vector also doesn't have an append member function, although insert can be used to append by passing vector.end() as the insert location.

I don't know DXU, but I suspect the problem is that DXUCutSprite is expecting a pointer to a valid DXUSPRITE as its second parameter, but you're passing a null pointer. You'll either need to allocate a new DXUSPRITE in v_mSprite before calling DXUCutSprite or else make v_mSprite a vector< DXUSPRITE > and call DXUCutSprite as DXUCutSprite(image, &v_mSprite, i * sX, 0, (i * sX) + sX, sY, dX, dY);.

Σnigma

##### Share on other sites
Quote:
Original post by Enigma
Quote:
 Original post by Ezbez1. resize() does not add more to the vector, it just makes it longer. Kinda confusing, but it happens. You're looking for append().

You're thinking of reserve. resize does add more items to the vector. The only problem is that it adds default initialised values to the vector, which in this case is the null pointer. vector also doesn't have an append member function, although insert can be used to append by passing vector.end() as the insert location.
Σnigma

Ahhh! Too much time using lists and other things! Thanks for the correction.