• Advertisement
Sign in to follow this  

SDL_BlitSurface() Problem

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

Hey everyone, I just started game development in C++ with SDL and I'm having some trouble with the SDL_BlitSurface() function. Basically, if i try to blit a surface using NULL for the two SDL_Rects, the image will be blitted as it should be. If I try to blit only a certain portion of the image, it won't work and returns -1. Any ideas?


Main.cpp

[source lang="cpp"]
int main ( int argc, char** argv ){
SDL_Surface* screen = NULL;
SDL_Init(SDL_INIT_VIDEO);

screen = SDL_SetVideoMode(DEFAULT_WINDOW_WIDTH,DEFAULT_WINDOW_HEIGHT,DEFAULT_WINDOW_COLOR_DEPTH, SDL_SWSURFACE);

SDL_WM_SetCaption(WINDOW_TITLE,NULL);

SDL_Surface* DefaultFontSheet = SDL_LoadBMP("font.bmp");

SpriteSheetReader ssr;

ssr.setSpriteSheet(DefaultFontSheet);
ssr.setSpriteSize(12,12);
SDL_Surface* letter = ssr.getSprite(1,1);
SDL_BlitSurface(letter, NULL, screen, NULL);
SDL_Flip(screen);
MainLoop();
}[/source]

SpriteSheetReader.h
[source lang="cpp"]
//Sprite Sheet Reader version 1.00
//Peter Dreyer
//10 November 2012

class SpriteSheetReader{
public:
//spriteSheet Getters and Setters
void setSpriteSheet(SDL_Surface* spriteSheet);
SDL_Surface* getSpriteSheet(void);
//Sprite Size Controlling
void setSpriteSize(int width, int height);
SDL_Rect getSpriteSize();
//Sprite Retrieval
SDL_Surface* getSprite(int x, int y);
private:
SDL_Surface* spriteSheet;
SDL_Rect spriteRect;
};

void SpriteSheetReader::setSpriteSheet(SDL_Surface* ss){
this->spriteSheet = ss;
}

SDL_Surface* SpriteSheetReader::getSpriteSheet(void){
return this->spriteSheet;
}

void SpriteSheetReader::setSpriteSize(int width, int height){
this->spriteRect.w = width;
this->spriteRect.h = height;
}

SDL_Rect SpriteSheetReader::getSpriteSize(void){
return spriteRect;
}

SDL_Surface* SpriteSheetReader::getSprite(int x, int y){
this->spriteRect.x = x * this->spriteRect.w;
this->spriteRect.y = y * this->spriteRect.h;

SDL_Surface* Sprite;
SDL_BlitSurface(spriteSheet, &this->spriteRect, Sprite, NULL);

return Sprite;
}
[/source]

I've done some testing and found that I can blit the whole sprite sheet and the problem is only introduced when I start trying to use the SDL_Rects.

Share this post


Link to post
Share on other sites
Advertisement
[color=#ff0000]SDL_Surface* Sprite;
SDL_BlitSurface(spriteSheet, &this->spriteRect, [color=#ff0000]Sprite, NULL);

Sprite was never created. It's a pointer pointing to nothing. You can't draw one image onto a destination that doesn't exist. smile.png

SDL_BlitSurface() returns -1 on error, but you didn't check the return result. SDL tells you, "something went wrong" by returning -1, and by calling SDL_GetError(), you could find out *what* went wrong. Error checking your code is very important. wink.png

So, your problem is now: "How do I create an empty surface so I can draw to it?"
The answer is: SDL_CreateRGBSurface().

I usually wrap it in a function, to make it easier to use. I posted such a function in this thread, along with some links to great SDL tutorials, and wrapping code for loading and drawing SDL surfaces - I strongly encourage you to read through that thread for the resources I posted, even though the problem the OP is having is different than yours.

Share this post


Link to post
Share on other sites
Thanks for your help Servant of the Lord! For the record, I did check the return result of the blitting function several times and I didn't just assume that result as my post made it look. I've just tested it again and it worked.

Share this post


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

  • Advertisement