# Creating a cursor from a surface

**URL:** <https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017>\
**Category:** SDL Development\
**Created:** [July 10, 2003, 2:31pm UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017 "2003-07-10T14:31:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_D\_Vargas](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/roger_d_vargas/32/70_2.png) [@Roger\_D\_Vargas](https://discourse.libsdl.org/u/Roger_D_Vargas)\
**Post date:** [July 10, 2003, 2:31pm UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017/1 "2003-07-10T14:31:01Z")

</div>

Can somebody give me a help with this? I want to load several cursors from  
png files. Is there some way to convert pixel data into a cursor?–  
Roger D. Vargas  
ICQ: 117641572  
Linux User: 180787

- Much to learn you still have \*  
Master Yoda, AOTC

–  
Este mensaje ha sido analizado por MailScanner  
en busca de virus y otros contenidos peligrosos,  
y se considera que est? limpio.

---

<div class="post-metadata">

**Author:** ![Thomas\_Eder](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Thomas\_Eder](https://discourse.libsdl.org/u/Thomas_Eder)\
**Post date:** [July 11, 2003, 1:04am UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017/2 "2003-07-11T01:04:01Z")

</div>

if you are using fullscreen mode, its suggested to use your own cursor, than you  
can of course use any bitmap you want (maybe look at the archives)\> ----- Original Message -----

> From: roger@eht.scu.tur.cu (Roger D. Vargas)  
> To:   
> Sent: Thursday, July 10, 2003 11:28 PM  
> Subject: [SDL] creating a cursor from a surface
> 
> > Can somebody give me a help with this? I want to load several cursors from  
> > png files. Is there some way to convert pixel data into a cursor?
> > 
> > –  
> > Roger D. Vargas  
> > ICQ: 117641572  
> > Linux User: 180787
> > 
> > - Much to learn you still have \*  
> > Master Yoda, AOTC
> > 
> > –  
> > Este mensaje ha sido analizado por MailScanner  
> > en busca de virus y otros contenidos peligrosos,  
> > y se considera que est? limpio.
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Florian\_Hufsky](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/f/e9a140/32.png) [@Florian\_Hufsky](https://discourse.libsdl.org/u/Florian_Hufsky)\
**Post date:** [July 11, 2003, 2:54am UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017/3 "2003-07-11T02:54:01Z")

</div>

you could disable the cursor and draw a surface at the mousecoordinates.\> ----- Original Message -----

> From: thomas.eder@nmi.at (Thomas Eder)  
> To:   
> Sent: Friday, July 11, 2003 9:53 AM  
> Subject: Re: [SDL] creating a cursor from a surface
> 
> > if you are using fullscreen mode, its suggested to use your own cursor,  
> > than you  
> > can of course use any bitmap you want (maybe look at the archives)
> > 
> > ----- Original Message -----  
> > From: “Roger D. Vargas”   
> > To:   
> > Sent: Thursday, July 10, 2003 11:28 PM  
> > Subject: [SDL] creating a cursor from a surface
> > 
> > > Can somebody give me a help with this? I want to load several cursors  
> > > from  
> > > png files. Is there some way to convert pixel data into a cursor?
> > > 
> > > –  
> > > Roger D. Vargas  
> > > ICQ: 117641572  
> > > Linux User: 180787
> > > 
> > > - Much to learn you still have \*  
> > > Master Yoda, AOTC
> > > 
> > > –  
> > > Este mensaje ha sido analizado por MailScanner  
> > > en busca de virus y otros contenidos peligrosos,  
> > > y se considera que est? limpio.
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [libsdl.org](http://libsdl.org)  
> > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Carlos\_Alvarez](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@Carlos\_Alvarez](https://discourse.libsdl.org/u/Carlos_Alvarez)\
**Post date:** [July 11, 2003, 3:10am UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017/4 "2003-07-11T03:10:01Z")

</div>

Isn’t the bmp cursor method to much expansive in display terms ?  
Doesn’t this requier to update Fullscreen on each mouvement ?

Carlos\> ----- Original Message -----

> From: fhufsky@phorus.at (Florian Hufsky)  
> To:   
> Sent: Friday, July 11, 2003 11:56 AM  
> Subject: Re: [SDL] creating a cursor from a surface
> 
> > you could disable the cursor and draw a surface at the mousecoordinates.
> > 
> > ----- Original Message -----  
> > From: “Thomas Eder” \<Thomas.Eder at nmi.at\>  
> > To:   
> > Sent: Friday, July 11, 2003 9:53 AM  
> > Subject: Re: [SDL] creating a cursor from a surface
> > 
> > > if you are using fullscreen mode, its suggested to use your own cursor,  
> > > than you  
> > > can of course use any bitmap you want (maybe look at the archives)
> > > 
> > > ----- Original Message -----  
> > > From: “Roger D. Vargas”   
> > > To:   
> > > Sent: Thursday, July 10, 2003 11:28 PM  
> > > Subject: [SDL] creating a cursor from a surface
> > > 
> > > > Can somebody give me a help with this? I want to load several cursors  
> > > > from  
> > > > png files. Is there some way to convert pixel data into a cursor?
> > > > 
> > > > –  
> > > > Roger D. Vargas  
> > > > ICQ: 117641572  
> > > > Linux User: 180787
> > > > 
> > > > - Much to learn you still have \*  
> > > > Master Yoda, AOTC
> > > > 
> > > > –  
> > > > Este mensaje ha sido analizado por MailScanner  
> > > > en busca de virus y otros contenidos peligrosos,  
> > > > y se considera que est? limpio.
> > > > 
> > > > * * *
> > > > 
> > > > SDL mailing list  
> > > > SDL at [libsdl.org](http://libsdl.org)  
> > > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [libsdl.org](http://libsdl.org)  
> > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Thomas\_Eder](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Thomas\_Eder](https://discourse.libsdl.org/u/Thomas_Eder)\
**Post date:** [July 11, 2003, 4:12am UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017/5 "2003-07-11T04:12:02Z")

</div>

maybe better do the reverse:  
in your mainloop at each screen update print your mouse-bmp at the current mouse  
position (hint: see docs for SDL\_MOUSEMOTION, SDL\_MouseMotionEvent),  
if you have \>30fps this should be sufficient.\> ----- Original Message -----

> From: [citrouille@wanadoo.fr](mailto:citrouille@wanadoo.fr) (Carlos Alvarez)  
> To:   
> Sent: Friday, July 11, 2003 12:09 PM  
> Subject: Re: [SDL] creating a cursor from a surface
> 
> > Isn’t the bmp cursor method to much expansive in display terms ?  
> > Doesn’t this requier to update Fullscreen on each mouvement ?
> > 
> > Carlos
> > 
> > ----- Original Message -----  
> > From: “Florian Hufsky”   
> > To:   
> > Sent: Friday, July 11, 2003 11:56 AM  
> > Subject: Re: [SDL] creating a cursor from a surface
> > 
> > > you could disable the cursor and draw a surface at the mousecoordinates.
> > > 
> > > ----- Original Message -----  
> > > From: “Thomas Eder” \<Thomas.Eder at nmi.at\>  
> > > To:   
> > > Sent: Friday, July 11, 2003 9:53 AM  
> > > Subject: Re: [SDL] creating a cursor from a surface
> > > 
> > > > if you are using fullscreen mode, its suggested to use your own cursor,  
> > > > than you  
> > > > can of course use any bitmap you want (maybe look at the archives)
> > > > 
> > > > ----- Original Message -----  
> > > > From: “Roger D. Vargas”   
> > > > To:   
> > > > Sent: Thursday, July 10, 2003 11:28 PM  
> > > > Subject: [SDL] creating a cursor from a surface
> > > > 
> > > > > Can somebody give me a help with this? I want to load several cursors  
> > > > > from  
> > > > > png files. Is there some way to convert pixel data into a cursor?
> > > > > 
> > > > > –  
> > > > > Roger D. Vargas  
> > > > > ICQ: 117641572  
> > > > > Linux User: 180787
> > > > > 
> > > > > - Much to learn you still have \*  
> > > > > Master Yoda, AOTC
> > > > > 
> > > > > –  
> > > > > Este mensaje ha sido analizado por MailScanner  
> > > > > en busca de virus y otros contenidos peligrosos,  
> > > > > y se considera que est? limpio.
> > > > > 
> > > > > * * *
> > > > > 
> > > > > SDL mailing list  
> > > > > SDL at [libsdl.org](http://libsdl.org)  
> > > > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > > > 
> > > > * * *
> > > > 
> > > > SDL mailing list  
> > > > SDL at [libsdl.org](http://libsdl.org)  
> > > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [libsdl.org](http://libsdl.org)  
> > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Andre\_de\_Leiradella](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/97f17d/32.png) [@Andre\_de\_Leiradella](https://discourse.libsdl.org/u/Andre_de_Leiradella)\
**Post date:** [July 11, 2003, 9:54am UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017/6 "2003-07-11T09:54:01Z")

</div>

I posted the message bellow some time ago in response to this very same  
question. It uses a function getpixel that does exactly that. Enjoy.

Andre de Leiradella  
[Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](http://www.geocities.com/andre_leiradella/)\> ----- Original Message -----

> From: @Andre_de_Leiradella (Andre de Leiradella)  
> To:   
> Sent: Thursday, April 10, 2003 4:48 PM  
> Subject: SDL\_Surface → SDL\_Cursor
> 
> > That’s what I use to build a cursor from a surface (no dithering):
> > 
> > SDL\_Cursor \*SurfaceToCursor(SDL\_Surface \*image, int hx, int hy) {  
> > int w, x, y;  
> > Uint8 \*data, \*mask, \*d, \*m, r, g, b;  
> > Uint32 color;  
> > SDL\_Cursor \*cursor;
> > 
> > ```
> > w = (image->w + 7) / 8;
> > data = (Uint8 *)alloca(w * image->h * 2);
> > if (data == NULL)
> > return NULL;
> > memset(data, 0, w * image->h * 2);
> > mask = data + w * image->h;
> > if (SDL_MUSTLOCK(image))
> > SDL_LockSurface(image);
> > for (y = 0; y < image->h; y++) {
> > d = data + y * w;
> > m = mask + y * w;
> > for (x = 0; x < image->w; x++) {
> > color = getpixel(image, x, y);
> > if ((image->flags & SDL_SRCCOLORKEY) == 0 || color
> > 
> > ```
> > 
> > != image-\>format-\>colorkey) {  
> > SDL\_GetRGB(color, image-\>format, &r, &g,  
> > &b);  
> > color = (r + g + b) / 3;  
> > m[x / 8] |= 128 \>\> (x & 7);  
> > if (color \< 128)  
> > d[x / 8] |= 128 \>\> (x & 7);  
> > }  
> > }  
> > }  
> > if (SDL\_MUSTLOCK(image))  
> > SDL\_UnlockSurface(image);  
> > cursor = SDL\_CreateCursor(data, mask, w, image-\>h, hx, hy);  
> > return cursor;  
> > }
> > 
> > Andre de Leiradella  
> > [Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](http://www.geocities.com/andre_leiradella/)

---

<div class="post-metadata">

**Author:** ![pirata](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@pirata](https://discourse.libsdl.org/u/pirata)\
**Post date:** [July 11, 2003, 10:05am UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017/7 "2003-07-11T10:05:00Z")

</div>

No, you only have to update previous & actual mouse area 😉  
It can be done really fast

Pirata

```
Isn't the bmp cursor method to much expansive in display terms ?
Doesn't this requier to update Fullscreen on each mouvement ?

CarlosOn Fri, 2003-07-11 at 07:09, Carlos wrote:

----- Original Message -----
From: "Florian Hufsky" <fhufsky at phorus.at>
To: <sdl at libsdl.org>
Sent: Friday, July 11, 2003 11:56 AM
Subject: Re: [SDL] creating a cursor from a surface

> you could disable the cursor and draw a surface at the mousecoordinates.
>
> ----- Original Message -----
> From: "Thomas Eder" <Thomas.Eder at nmi.at>
> To: <sdl at libsdl.org>
> Sent: Friday, July 11, 2003 9:53 AM
> Subject: Re: [SDL] creating a cursor from a surface
>
>
> >
> > if you are using fullscreen mode, its suggested to use your own cursor,
> than you
> > can of course use any bitmap you want (maybe look at the archives)
> >
> > ----- Original Message -----
> > From: "Roger D. Vargas" <roger at eht.scu.tur.cu>
> > To: <SDL at libsdl.org>
> > Sent: Thursday, July 10, 2003 11:28 PM
> > Subject: [SDL] creating a cursor from a surface
> >
> >
> > >
> > > Can somebody give me a help with this? I want to load several cursors
> from
> > > png files. Is there some way to convert pixel data into a cursor?
> > >
> > >
> > > --
> > > Roger D. Vargas
> > > ICQ: 117641572
> > > Linux User: 180787
> > > * Much to learn you still have *
> > > Master Yoda, AOTC
> > >
> > >
> > > --
> > > Este mensaje ha sido analizado por MailScanner
> > > en busca de virus y otros contenidos peligrosos,
> > > y se considera que est? limpio.
> > >
> > >
> > > _______________________________________________
> > > SDL mailing list
> > > SDL at libsdl.org
> > > http://www.libsdl.org/mailman/listinfo/sdl
> >
> >
> >
> > _______________________________________________
> > SDL mailing list
> > SDL at libsdl.org
> > http://www.libsdl.org/mailman/listinfo/sdl
> >
> >
>
>
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
>

_______________________________________________
SDL mailing list
SDL at libsdl.org

```

[http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Roger\_D\_Vargas](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/roger_d_vargas/32/70_2.png) [@Roger\_D\_Vargas](https://discourse.libsdl.org/u/Roger_D_Vargas)\
**Post date:** [July 11, 2003, 10:27am UTC](https://discourse.libsdl.org/t/creating-a-cursor-from-a-surface/9017/8 "2003-07-11T10:27:00Z")

</div>

> I posted the message bellow some time ago in response to this very same  
> question. It uses a function getpixel that does exactly that. Enjoy.
> 
> Andre de Leiradella  
> [Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](http://www.geocities.com/andre_leiradella/)  
> thanks a lot.On Fri, 11 Jul 2003, Andre de Leiradella wrote:
> 
> ----- Original Message -----  
> From: Andre de Leiradella   
> To:   
> Sent: Thursday, April 10, 2003 4:48 PM  
> Subject: SDL\_Surface → SDL\_Cursor
> 
> > That’s what I use to build a cursor from a surface (no dithering):
> > 
> > SDL\_Cursor \*SurfaceToCursor(SDL\_Surface \*image, int hx, int hy) {  
> > int w, x, y;  
> > Uint8 \*data, \*mask, \*d, \*m, r, g, b;  
> > Uint32 color;  
> > SDL\_Cursor \*cursor;
> > 
> > ```
> > w = (image->w + 7) / 8;
> > data = (Uint8 *)alloca(w * image->h * 2);
> > if (data == NULL)
> > return NULL;
> > memset(data, 0, w * image->h * 2);
> > mask = data + w * image->h;
> > if (SDL_MUSTLOCK(image))
> > SDL_LockSurface(image);
> > for (y = 0; y < image->h; y++) {
> > d = data + y * w;
> > m = mask + y * w;
> > for (x = 0; x < image->w; x++) {
> > color = getpixel(image, x, y);
> > if ((image->flags & SDL_SRCCOLORKEY) == 0 || color
> > 
> > ```
> > 
> > != image-\>format-\>colorkey) {  
> > SDL\_GetRGB(color, image-\>format, &r, &g,  
> > &b);  
> > color = (r + g + b) / 3;  
> > m[x / 8] |= 128 \>\> (x & 7);  
> > if (color \< 128)  
> > d[x / 8] |= 128 \>\> (x & 7);  
> > }  
> > }  
> > }  
> > if (SDL\_MUSTLOCK(image))  
> > SDL\_UnlockSurface(image);  
> > cursor = SDL\_CreateCursor(data, mask, w, image-\>h, hx, hy);  
> > return cursor;  
> > }
> > 
> > Andre de Leiradella  
> > [Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](http://www.geocities.com/andre_leiradella/)
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

–  
Roger D. Vargas  
ICQ: 117641572  
Linux User: 180787

- Much to learn you still have \*  
Master Yoda, AOTC

–  
Este mensaje ha sido analizado por MailScanner  
en busca de virus y otros contenidos peligrosos,  
y se considera que est? limpio.
