Jump to content
  • Advertisement
Sign in to follow this  
shikyohanami

SDL_BlitSurface generates memory access violation

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

So, I have a draw class with a draw function that just looks like:
void Wall::Draw(SDL_Surface *screen)
{
	SDL_BlitSurface(animationCtrl.ProcessFrame(0), NULL, screen, &coord); // This generates error (access violating reading loation 0x000000)
}
which generates the error above. I have two functions involved, both part of the clas animationCtrl which is a member of the Wall class, one for loading an image (which is called in Draw's constructor and one funtion which is called ProcessFrame, which is supposed to return the current frame from a vector of frames, the AnimationCtrl class looks like:
class AnimationCtrl
{
private:
	std::vector<SDL_Surface*> m_pictures;
	std::vector<SDL_Surface*>::iterator m_currentFrame;
	bool m_isAnimating;
	Uint32 m_lastTime; // Last time ProcessFrame was called
	int m_playTime; // Animation time for frame

public:
	AnimationCtrl();
	~AnimationCtrl();

	void LoadSprite(const char *fileName);
	bool PopImage(); // Remove last added; returns false if empty, true if popped
	void ClearImgVector();
	void StartAnimation(bool goToFirst); // Goes to frame 1 of animation if true, else at m_currentFrame
	void StopAnimation(bool goToFirst);  // Goes to frame 1 of animation if true, else at m_currentFrame
	void SetPlayTime(int playTime);

	/*
		if( m_pictures.size() / (newCount - m_lastTime > m_playTime / m_pictures.size() )
		equals to true, ProcessFrame will return next picture in animation (if StartAnimation is called)
		else it will return the same picture as last time. If StopAnimation is called
		last picture will always be returned
	*/
	SDL_Surface* ProcessFrame(Uint32 newCount);
};

void AnimationCtrl::LoadSprite(const char *fileName)
{
	SDL_Surface *loadImg = IMG_Load(fileName);
	if( !loadImg )
	{
		std::string temp = "Could not load image ";
		temp.append(fileName);
		temp.append(".");
		throw temp;
	}

	SDL_Surface *optimizeImg = SDL_DisplayFormat(loadImg);
	SDL_FreeSurface(loadImg);

	if( m_pictures.empty() )
	{
		m_pictures.push_back(optimizeImg);
		m_currentFrame = m_pictures.begin();
	}
	else
	{
		m_pictures.push_back(optimizeImg);
	}
	SDL_FreeSurface(optimizeImg);
}

SDL_Surface* AnimationCtrl::ProcessFrame(Uint32 newCount)
{
	if( m_pictures.empty() )
		throw std::string("No image to Process");

	if(m_isAnimating)
	{
		if( (newCount - m_lastTime) / m_pictures.size() > m_playTime / m_pictures.size() )
		{
			// Increase frame, if at last frame, go to first
			if( m_pictures.size() >= 1 ) // just return m_currentFrame's pic if only 1 exists
			{
				m_currentFrame++;
				if( m_currentFrame == m_pictures.end() )
					m_currentFrame = m_pictures.begin();
				else
					return *m_currentFrame;
			}
			else
				return *m_currentFrame;
		}
	}
	m_lastTime = newCount;
	return *m_currentFrame;
}
I can't really see where it's going wrong. Thanks for any help. (I only posted the parts which I assumed would be relevant.)

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!