Odd SDL_Surface issue

Started by
0 comments, last by rip-off 15 years, 3 months ago
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;

    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
    #else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;	
    #endif
	
	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
SDL_Flip(screen);

"All the girls that turn me on (turn me down)"
Advertisement
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';

This topic is closed to new replies.

Advertisement