Jump to content

  • Log In with Google      Sign In   
  • Create Account

petrusd987

Member Since 10 Nov 2012
Offline Last Active Nov 17 2012 10:15 AM

Topics I've Started

SDL_BlitSurface() Problem

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.

PARTNERS