Jump to content
  • Advertisement
Sign in to follow this  

Odd SDL_Surface issue

This topic is 3559 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'm totally confused. I have a class called Animation which holds an SDL_Surface*. Animation has a method getFrame(int frameNumber) which returns a given frame. Instantiating Animation as AN and calling AN->getFrame(0) successfully returns the first frame of the animation as an SDL_Surface* and I can blit it to screen and all is funky dory. I then have a Sprite class which has a pointer to an animation. It has a method called getFrame() (no parameter) which calls the getFrame(n) method of its animation. However, when I try to blit this SDL_Surface to the screen nothing happens. No error, just nothing on screen. Below are the relevant parts of my files. Any ideas?

// ------- Animation class -------------

Animation::Animation(const char* filename, int w, int h, int frames){
    Uint32 rmask, gmask, bmask, amask;

    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;	
	cout << "" << endl;
	cout << "Loading " << filename << endl;

	image = IMG_Load(filename);

	cout << image->w << endl;

	this->w = w;
	this->h = h;
	this->numFrames = frames;

	tempImage = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, 32,
                                   rmask, gmask, bmask, amask);


SDL_Surface* Animation::getFrame(int frameNum){
	SDL_Rect r;
	r.x = (frameNum*this->w);
	r.y = 0;
	r.w = this->w;
	r.h = this->h;	

	SDL_Rect d;
	r.x = 0;
	r.y = 0;
	r.h = this->h;
	r.w = this->w;

	SDL_BlitSurface(image, NULL, tempImage, &d);
	return tempImage;

//---------- Sprite class ------------

Sprite::Sprite(Animation* anim){
	this->frame = anim;
	this->numFrames = this->frame->getNumFrames();
	this->currentFrame = 0;

// Hard wired this for testing
SDL_Surface* Sprite::getFrame(){
	SDL_Surface* s = frame->getFrame(0);
	return s;

//-------------- Main file -------------

int blitToScreen(SDL_Surface* image, int x, int y) {
	SDL_Rect src;
	src.x = 0;
	src.y = 0;
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface(image, &src, screen, &dest);

// And from the main loop:
Animation* a = new Animation("image.jpg",20,20,1);
Sprite* s = new Sprite(a);
blitToScreen(s->getFrame(),100,100); // Doesn't work

Share this post

Link to post
Share on other sites
I think you should put more error checking in your code. Most SDL functions may return a value indicating that an error occurred. A handful of examples are IMG_Load(), SDL_CreateRGBSurface() and SDL_BlitSurface().

Have you tried stepping through the code with a debugger to verify that the values in question are what you suspect. For example, I think SDL_BlitSurface() checks the pointers it is passed for being NULL, so if you tried to blit NULL to the screen it might silently fail.

If you are still having problems, post the updated code in a compilable fashion. This means include the full main() function and any dependencies it has.

On an unrelated note:

cout << "" << endl;

cout << "Loading " << filename << endl;

You can send std::endl directly to std::cout:

cout << endl;

cout << "Loading " << filename << endl;

You can simplify further:

cout << "\nLoading " << filename << endl;

And that is if you want to flush std::cout at that stage. Otherwise this would work:

cout << "\nLoading " << filename << '\n';

Share this post

Link to post
Share on other sites
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!