nbs at sonic.net
Wed Apr 9 13:30:01 PDT 2003
On Wed, Apr 09, 2003 at 02:41:45PM -0700, COSKU BAS wrote:
> does anyone know how to create a mouse cursor with an image
> file(bmp,png,jpg.....) ????
I've only done it with XBMs (which I #include in my source),
but it shouldn't be too hard.
Just use SDL (for BMP) or SDL_Image (for PNG, JPG, ...) to load the file
into a SDL_Surface.
Then, use a routine to examine all of the pixels in the surface
and use them to generate a bitmap (and, probably also a bitmask) to send
You could do something like:
SDL_Surface * curosr_surf;
Uint8 bitmap, bitmask;
int width, height, x, y;
Uint8 r, g, b, a;
int bit, mask;
/* Load the cursor into a surface: */
cursor_surf = IMG_Load("cursor.png");
... test for errors! ...
width = cursor_surf->w;
height = cursor_surf->h;
if (((width + 7) / 8) * height > 128)
... abort! too big!
for (i = 0; i < 128; i++)
bitmap[i] = 0;
bitmask[i] = 0;
/* Create bitmap & bitmask based on the surface: */
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
/* Get the pixel at (x,y): */
pix = getpixel(cursor_surf, x, y);
/* Determine RGB values of the pixel: */
SDL_GetRGBA(cursor_surf, x, y, &r, &g, &b, &a);
/* What's the mask color: */
/* (Crappy threshold... you may want to do dithering or something...) */
if (((r + g + b) / 3) > 128)
map = 1;
map = 0;
/* (Not sure if high alpha;mask=1...it'll be obvious if this is wrong) */
/* (BTW, not all file formats support alpha/transparency, so
you might need to determine mask based on some special 'colorkey'
color, like "obnoxious purple" or something... How all of this
map/mask decision stuff works depends on the image(s) you're loading,
really. Here, I'm making it generic, and assuming a format like
if (a > 128)
mask = 1;
mask = 0;
/* (This math could be wrong, but you get the idea :^) ) */
bitmap[(x / 8) * y] |= (map >> (x % 8));
bitmask[(x / 8) * y] |= (mask >> (x % 8));
Anyway, that's more-or-less how to do it.
It'd be nice if SDL had an "SDL_CreatCursorFromSurface()" function that
did the above for you.
bill at newbreedsoftware.com Hire me!
More information about the SDL