Sign in to follow this  

help with my class

This topic is 4109 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

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?
 

//header-----------------------------------------------------------

#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();
	DXULoadImage(image, path, DXURGB(red, green, blue));

	for(int i = 0; i < cFrames; i++)
	{
		DXUCutSprite(image, v_mSprite[i], 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
1. 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[i] before calling DXUCutSprite or else make v_mSprite a vector< DXUSPRITE > and call DXUCutSprite as DXUCutSprite(image, &v_mSprite[i], i * sX, 0, (i * sX) + sX, sY, dX, dY);.

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
Quote:
Original post by Ezbez
1. 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.

Share this post


Link to post
Share on other sites

This topic is 4109 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this