blitting with colorkeys in SDL

blitting with colorkeys in SDL

Postby Magevania » 13 Sep 2013, 21:46

Hello! I'm newbie with SDL and I have got a problem about blitting with colorkeys in SDL. I did compile the blitting-surface-sdl.c from Programming Linux Games (code listing 4-3, page 84 - chap.4), loading this bitmap image

penguin.jpg
Penguin
penguin.jpg (10.79 KiB) Viewed 6476 times


But SDL_BlitSurface doesn't work properly, here is what I get

sdl-blitting.jpg
Window SDL Blitting


The source code is here http://nostarch.com/download/plg-listin ... 001.tar.gz

{l Code}: {l Select All Code}

/* Example of blitting with colorkeys in SDL. */
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
SDL_Surface *screen;
SDL_Surface *background;
SDL_Surface *image;
SDL_Rect src, dest;
Uint32 colorkey;
/* Initialize SDL’s video system and check for errors. */
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
/* Make sure SDL_Quit gets called when the program exits! */
atexit(SDL_Quit);
/* Attempt to set a 640x480 hicolor (16-bit) video mode. */
screen = SDL_SetVideoMode(640, 480, 16, 0);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
/* Load the bitmap files. */
background = SDL_LoadBMP("/home/users/turk2000/Workspace/PROJECT/prototype/dist/frame_0002.bmp");
if (background == NULL) {
printf("Unable to load bitmap.\n");
return 1;
}
image = SDL_LoadBMP("/home/users/turk2000/Workspace/PROJECT/prototype/dist/penguin.bmp");
if (image == NULL) {
printf("Unable to load bitmap.\n");
return 1;
}
/* Draw the background. */
src.x = 0;
src.y = 0;
src.w = background->w;
src.h = background->h;
dest.x = 0;
dest.y = 0;
dest.w = background->w;
dest.h = background->h;
SDL_BlitSurface(background, &src, screen, &dest);
/* Draw the penguin without a colorkey. */
src.x = 0;
src.y = 0;
src.w = image->w;
src.h = image->h;
dest.x = 30;
dest.y = 90;
dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image, &src, screen, &dest);
/* The penguin is stored on a blue background. We
can use the SDL_MapRGB function to obtain the
correct pixel value for pure blue. */
colorkey = SDL_MapRGB(image->format, 0, 0, 255);
/* We’ll now enable this surface’s colorkey and draw
it again. To turn off the colorkey again, we would
replace the SDL_SRCCOLORKEY flag with zero. */
SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
src.x = 0;
src.y = 0;
src.w = image->w;
src.h = image->h;
dest.x = screen->w - image->w - 30;
dest.y = 90;
dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image, &src, screen, &dest);
/* Ask SDL to update the entire screen. */
SDL_UpdateRect(screen, 0, 0, 0, 0);
/* Pause for a few seconds as the viewer gasps in awe. */
SDL_Delay(10000);
/* Free the memory that was allocated to the bitmaps. */
SDL_FreeSurface(background);
SDL_FreeSurface(image);
return 0;
}
Magevania
 
Posts: 2
Joined: 13 Sep 2013, 21:23

Re: blitting with colorkeys in SDL

Postby Magevania » 14 Sep 2013, 01:12

Oh, I'm sorry! It was my fault. I did save penguin.bmp with alpha channel. Pls delete the message. Thank you.
Magevania
 
Posts: 2
Joined: 13 Sep 2013, 21:23

Who is online

Users browsing this forum: No registered users and 1 guest