Jump to content

  • Log In with Google      Sign In   
  • Create Account


SDL_BlitSurface() Problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 petrusd987   Members   -  Reputation: 108

Like
0Likes
Like

Posted 10 November 2012 - 02:23 PM

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 2012class 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.

Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 17130

Like
2Likes
Like

Posted 10 November 2012 - 02:52 PM

SDL_Surface* Sprite;
SDL_BlitSurface(spriteSheet, &this->spriteRect, 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. Posted Image

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. Posted Image

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.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#3 petrusd987   Members   -  Reputation: 108

Like
0Likes
Like

Posted 10 November 2012 - 05:09 PM

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.

#4 Servant of the Lord   Crossbones+   -  Reputation: 17130

Like
0Likes
Like

Posted 10 November 2012 - 06:33 PM

Np, glad you got it working.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS