Sign in to follow this  
Charabis

SDL_CreateCursor

Recommended Posts

I just couldn't resist playing with it, however followign the directions in "Focus on SDL" isn't getting me anywhere as I've lost the CD! I think I've followed his thinking, but hard to be sure I'm doing it right without looking at the code for what he documented. Anyway, all I was going for was a filled pyramid, 16 wide by 8 high, with the hot point under the tip, and came up with this:
    Uint8 cursorMask[8] = {0x8000, 0xC001, 0xE003, 0xF007, 0xF80F, 0xFC1F, 0xFE3F, 0xFF7E};
    Uint8 cursorData[8] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
    SDL_Cursor* cursor = SDL_CreateCursor(cursorData, cursorMask, 16, 8, 8, 0);
    SDL_SetCursor (cursor);


Makes a new cursor all right, but it's a jumbled mess instead of a pyramid [rolleyes]. Pretty sure I did something stupid, but without the CD, I can't see what since there's no example to check against.

Share this post


Link to post
Share on other sites
The SDL docs give this example about SDL_CreateCursor, hopefully it's helpful:


/* Stolen from the mailing list */
/* Creates a new mouse cursor from an XPM */


/* XPM */
static const char *arrow[] = {
/* width height num_colors chars_per_pixel */
" 32 32 3 1",
/* colors */
"X c #000000",
". c #ffffff",
" c None",
/* pixels */
"X ",
"XX ",
"X.X ",
"X..X ",
"X...X ",
"X....X ",
"X.....X ",
"X......X ",
"X.......X ",
"X........X ",
"X.....XXXXX ",
"X..X..X ",
"X.X X..X ",
"XX X..X ",
"X X..X ",
" X..X ",
" X..X ",
" X..X ",
" XX ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"0,0"
};

static SDL_Cursor *init_system_cursor(const char *image[])
{
int i, row, col;
Uint8 data[4*32];
Uint8 mask[4*32];
int hot_x, hot_y;

i = -1;
for ( row=0; row<32; ++row ) {
for ( col=0; col<32; ++col ) {
if ( col % 8 ) {
data[i] <<= 1;
mask[i] <<= 1;
} else {
++i;
data[i] = mask[i] = 0;
}
switch (image[4+row][col]) {
case 'X':
data[i] |= 0x01;
k[i] |= 0x01;
break;
case '.':
mask[i] |= 0x01;
break;
case ' ':
break;
}
}
}
sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
}

Share this post


Link to post
Share on other sites
Seen that many times actually, and might have to resort to it. Or maybe if I just step through that I can hand-assemble the array to see what the format looks like. Hmmmmm...

Share this post


Link to post
Share on other sites
Quote:
Original post by sprite_hound
The SDL docs give this example about SDL_CreateCursor, hopefully it's helpful:

*** Source Snippet Removed ***


There are some confusing bits in the OP's original code. First, Charabis, you have Uint8. I'm guessing that's an 8-bit integer. But your array uses 0x???? format which is in fact 16 bits( 4 nibbles of 4-bits per nibble). Might that be causing problems with overflow?

Try it more like this and see if it helps:

const int width = 16;
const int height = 8;
const int bitsInByte = 8;
Uint8 mask[width * height / bitsInByte] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
Uint8 mask[width * height / bitsInByte] = {0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };

Share this post


Link to post
Share on other sites
That got it :)


Uint8 mask[] = {0x00, 0x80, 0x01, 0xc0, 0x03, 0xE0, 0x07, 0xF0, 0x0F, 0xF8, 0x1F, 0xFC, 0x3F, 0xFE, 0x7F, 0xFF };
Uint8 data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
SDL_Cursor* cursor = SDL_CreateCursor(data, mask, 16, 8, 8, 0);
SDL_SetCursor (cursor);


Turns out a few things in the book were wrong on this, but it was published 4 years ago, which means the book itself is out of date most likely [smile]. Sometimes I wonder if maybe technology is moving TOO fast for those of us on a budget. [totally]

BTW, Uint8 is an unsigned, 8-bit integer, exactly what the SDL_CreateCursor function wants.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this