Jump to content
  • Advertisement
Sign in to follow this  
Melt

Problem with SDL_BlitSurface - Easy !

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

Hey there, I just started to work on some SDL tutorials. In the following code I tried to simply blit 4 shapes from a sprite and put them to the screen using SDL_BlitSurface. When I run the code I see a black screen and don't know what the problem is. Give me a hand here. Thanks

#include <SDL/SDL.h>
#include <SDL_image.h>

int main()
{
SDL_Init(SDL_INIT_EVERYTHING);

SDL_Surface* screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);

SDL_Rect rectRed;
rectRed.x = 0;
rectRed.y = 0;
rectRed.h = 100;
rectRed.w = 100;


SDL_Rect rectGreen;
rectGreen.x = 100;
rectGreen.y = 0;
rectGreen.h = 100;
rectGreen.w = 100;


SDL_Rect rectYellow;
rectYellow.x = 0;
rectYellow.y = 100;
rectYellow.h = 100;
rectYellow.w = 100;


SDL_Rect rectBlue;
rectBlue.x = 100;
rectBlue.y = 100;
rectBlue.h = 100;
rectBlue.w = 100;

SDL_Surface* sheet = IMG_Load("/home/melt/devmedia/sheet.jpg");
sheet = SDL_DisplayFormat(sheet);


if(sheet == NULL)
{
fprintf(stderr, "Error, sheet == NULL : %s", SDL_GetError());
return -1;
}

SDL_Surface* red = NULL;
SDL_Surface* blue = NULL;
SDL_Surface* yellow = NULL;
SDL_Surface* green = NULL;

SDL_BlitSurface(sheet, &rectRed, red, NULL);
SDL_BlitSurface(sheet, &rectYellow, yellow, NULL);
SDL_BlitSurface(sheet, &rectBlue, blue, NULL);
SDL_BlitSurface(sheet, &rectGreen, green, NULL);

SDL_BlitSurface(red, NULL, screen, &rectRed);
SDL_BlitSurface(blue, NULL, screen, &rectBlue);
SDL_BlitSurface(green, NULL, screen, &rectGreen);
SDL_BlitSurface(yellow, NULL, screen, &rectYellow);

if( SDL_Flip(screen) == -1)
{
fprintf(stderr, "Error SDL_Flip(screen) == -1 : %s", SDL_GetError());
return -1;
}

SDL_Delay(5000);

return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
red, blue, yellow and green are NULL pointers so when you try to blit to to/from these surfaces using SDL_BlitSurface it will not work because the surfaces doesn't exist. You will have to create the surfaces first using SDL_CreateRGBSurface or something like that.

Share this post


Link to post
Share on other sites

red, blue, yellow and green are NULL pointers so when you try to blit to to/from these surfaces using SDL_BlitSurface it will not work because the surfaces doesn't exist. You will have to create the surfaces first using SDL_CreateRGBSurface or something like that.


Wow, thank you. That was easy but perfectly worked of course.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!