Sign in to follow this  

How to copy a certain portion of the screen to SDL_Surface

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

I am a beginner in SDL graphics. I am trying to copy a certain portion of the screen to SDL_Surface. And I tried using this code but it didn't work out

[b]SDL_Surface* original=NULL; [/b]
[b]SDL_Rect rct; [/b]
[b]rct.x=90; [/b]
[b]rct.y=70; [/b]
[b]rct.w=100; [/b]
[b]rct.h=100; [/b]


[b]SDL_BlitSurface(screen,&rct,original,NULL); [/b]

[b]//It should blit the area of the screen bounded by rct at 0,0 of the original [/b]

[b]apply_surface(500,100,original,screen); [/b]

[b]//now it should blit the original at 500,100 of the screen [/b]

[b]SDL_Flip(screen); [/b]

but nothing was blitted on the screen.


but the same code worked out well when it was written this way


[b]SDL_Surface* original=NULL; [/b]
[b]SDL_Rect rct; [/b]
[b]rct.x=90; [/b]
[b]rct.y=70; [/b]
[b]rct.w=100; [/b]
[b]rct.h=100; [/b]

[b]original=IMG_Load("pics/picture1.png"); //I loaded a picture in the original [/b]

[b]SDL_BlitSurface(screen,&rct,original,NULL); [/b]

[b]apply_surface(500,100,original,screen); [/b]

[b]SDL_Flip(screen); [/b]

And it worked out very well. It blitted a small part of my window i.e. screen to 500,100 of the screen

So I think there must be something wrong with my understanding about surface blitting function.
I am using SDL 1.2.14 and Code::Blocks for IDE.

And if there is a better way to do what I am trying to do, then please suggest

Thank you in advance.

Share this post


Link to post
Share on other sites
The problem is that in your first piece of code, original is not pointing to a valid SDL_Surface when you try to blit. As SDL_BlitSurface doesn't create surfaces for you, you need to use the SDL_CreateRGBSurface function to give a second surface to blit to. Keep in mind that you need to free any surface you create yourself using SDL_FreeSurface.

Share this post


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