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?
Sprite was never created. It's a pointer pointing to nothing. You can't draw one image onto a destination that doesn't exist.
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.
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.
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.