Jump to content
  • Advertisement
Sign in to follow this  
Chrono1081

SDL Blitting Question

This topic is 3735 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 was just wondering if its possible to blit one surface to another that doesnt have the video mode assigned to it. For example i have this code:
#include "SDL\SDL.h"
#include <string>

SDL_Surface *screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT)
SDL_Surface *background = LoadBMP("background.bmp")
SDL_Surface *orange = LoadBMP("orange.bmp");

int main(int argc, char* args[])
{
     SDL_Init(SDL_INIT_EVERYTHING);

     SDL_BlitSurface(background, NULL, screen, NULL);
     SDL_BlitSurface(orange, NULL, screen, NULL);

     SDL_Flip(screen);
     SDL_Delay(5000);

     SDL_FreeSurface(orange);
     SDL_FreeSurface(background);
     SDL_Quit();

     return 0;
}

Is it possible to blit the orange straight to the background? because if I try something like this:
SDL_BlitSurface(orange, NULL, background, NULL);
It compiles fine but nothing shows up. Even if I try and call:
SDL_Flip(background);
Nothing happens. So is this possible? Or is it only possible to blit to the screen? (Or whatever surface holds the video information)

Share this post


Link to post
Share on other sites
Advertisement

SDL_Surface *screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT)
SDL_Surface *background = LoadBMP("background.bmp")
SDL_Surface *orange = LoadBMP("orange.bmp");



First, you don't call functions outisde of main().



int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_EVERYTHING);

SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(orange, NULL, screen, NULL);

SDL_Flip(screen);
SDL_Delay(5000);

SDL_FreeSurface(orange);
SDL_FreeSurface(background);
SDL_Quit();

return 0;
}



You need to actually blit stuff to the screen.

Share this post


Link to post
Share on other sites
Ah...Im an idiot. I didnt even catch that. Thank you both for the replies :) (I also will no longer call my functions outside my main. Thanks for the tip!)

Share this post


Link to post
Share on other sites
Quote:
Original post by Lazy Foo
You need to actually blit stuff to the screen.

His question was whether or not he can SDL_Blit 'orange' onto 'background'. Which he can, actually, as you know.

@Chrono1081: Don't call 'SDL_Flip();' on anything other than the surface created with SDL_SetVideoMode(). That function tells SDL to swap the 'back buffer' with the 'front buffer', when using Double Buffering. Normal surfaces in SDL don't have a second buffer, only your screen does.

Definately don't load your surfaces outside of a function, like LazyFoo mentioned. You're supposed to only load your surfaces after you call SDL_Init(), and if they are outside of a function, the program runs them before it runs 'int main()'.

You should also check every surface after you load it, to make sure it's loaded properly. It takes two lines of code, and it prevents alot of mistakes you might make.

#include "SDL\SDL.h"
#include <string>

int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_EVERYTHING);

SDL_Surface *screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT)
if(screen == NULL)
printf("An error occured: %s\n",SDL_GetError());

SDL_Surface *background = LoadBMP("background.bmp")
if(background == NULL)
printf("An error occured: %s\n",SDL_GetError());

SDL_Surface *orange = LoadBMP("orange.bmp");
if(orange == NULL)
printf("An error occured: %s\n",SDL_GetError());

//Blit 'orange' onto 'background' *before* you blit 'background' to the screen.
SDL_BlitSurface(orange, NULL, background, NULL);
SDL_BlitSurface(background, NULL, screen, NULL);

//Only 'Flip' the screen, to update what's shown on the window.
SDL_Flip(screen);
SDL_Delay(5000);

SDL_FreeSurface(orange);
SDL_FreeSurface(background);
SDL_Quit();

return 0;
}


Does that work for you?

Share this post


Link to post
Share on other sites
Thank you guys for all the help :) Everything worked great. I saw that I was hiding the orange by accident.

I have one more question if anyones still reading this thread: Does this optimize my background surface to be the same format as the screen?"


background = SDL_DisplayFormat(SDL_LoadBMP("background.bmp"));



I don't see why it wouldn't work and it compiles and runs just fine but I am no SDL expert so I thought Id ask as I have never seen this shortcut in any book or website.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chrono1081
I don't see why it wouldn't work and it compiles and runs just fine but I am no SDL expert so I thought Id ask as I have never seen this shortcut in any book or website.


It'll compile and give you and optimized image, but it leaks memory. The surface created from SDL_LoadBMP() is never freed.

I did go over this in tutorial 2.

Share this post


Link to post
Share on other sites
Ah ok. I didn't realize SDL_LoadBMP automatically created a surface. I was looking at your tutorials and figured there was a reason that you create a separate function. Thank you for the help :)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!