Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL Blitting Question

Started by Chrono1081 Jun 30, 2008 at 10:02 AM 6 replies 3.4k views
Original Post
Chrono1081
Chrono1081
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)
Portmanteau
Portmanteau
You have to then blit background to the screen, otherwise it will just sit in some memory not visible to you.
Lazy Foo
Lazy Foo
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.
Learn to make games with my SDL 2 Tutorials
Chrono1081
Chrono1081
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!)
Servant of the Lord
Servant of the Lord
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?
Chrono1081
Chrono1081
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.
Lazy Foo
Lazy Foo
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.
Learn to make games with my SDL 2 Tutorials
Chrono1081
Chrono1081
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 :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.