Using an image as mouse cursor

I didnt got any answer about how to convert a surface to cursor, so I
tried another method I found in a demo app. I hide system cursor, then
use mouse motion events to change image coordinates. The demo works
smooth, but in my program for some reason the image floats slowly
following mouse path during some time instead of behaving like the
system cursor.
This is the code I use (copied exactly from demo)

if (event.motion.type==SDL_MOUSEMOTION) {
cx=event.button.x;
cy=event.button.y;
cur_pos.x=cx;
cur_pos.y=cy;
cur_pos.w=cursor->w;
cur_pos.h=cursor->h;

//after closing if…
SDL_BlitSurface(cursor,NULL, screen,&cur_pos);

What Im doing wrong?–
Roger D. Vargas
Linux user #180787
ICQ: 117641572

  • No hay nada tan importante que no pueda ser olvidado *
    Alzheimer

“Roger D. Vargas” wrote:

I didnt got any answer about how to convert a surface to cursor, so I
tried another method I found in a demo app. I hide system cursor, then
use mouse motion events to change image coordinates. The demo works
smooth, but in my program for some reason the image floats slowly
following mouse path during some time instead of behaving like the
system cursor.
This is the code I use (copied exactly from demo)

most likely you process one event at a time instead of all events in the
queue.

change your ‘if (SDL_PollEvent …)’ to a ‘while (SDL_PollEvent)’

clemens

I didnt got any answer about how to convert a surface to cursor, so I
tried another method I found in a demo app. I hide system cursor, then
use mouse motion events to change image coordinates. The demo works
smooth, but in my program for some reason the image floats slowly
following mouse path during some time instead of behaving like the
system cursor.
This is the code I use (copied exactly from demo)

if (event.motion.type==SDL_MOUSEMOTION) {
cx=event.button.x;
cy=event.button.y;
cur_pos.x=cx;
cur_pos.y=cy;
cur_pos.w=cursor->w;
cur_pos.h=cursor->h;

//after closing if…
SDL_BlitSurface(cursor,NULL, screen,&cur_pos);

What Im doing wrong?

Probably responding to each mouse motion event. You’ll get lots of them–if
you redraw for each one, it may bog down your system.

Try saving the x & y coordinates of each event and only redraw after there
are no more, or another event type occurs. If you’re using a typical

while (SDL_PollEvent(&event))

loop, you’ll catch enough mouse motion events for the cursor to move
smoothly, but not drag down the system doing unnecessary blits.

HTH,
JeffOn Saturday 22 January 2005 04:47 am, Roger D. Vargas wrote:

El s?b, 22-01-2005 a las 17:32, Clemens Kirchgatterer escribi?:

“Roger D. Vargas” wrote:

I didnt got any answer about how to convert a surface to cursor, so I
tried another method I found in a demo app. I hide system cursor, then
use mouse motion events to change image coordinates. The demo works
smooth, but in my program for some reason the image floats slowly
following mouse path during some time instead of behaving like the
system cursor.
This is the code I use (copied exactly from demo)

most likely you process one event at a time instead of all events in the
queue.

change your ‘if (SDL_PollEvent …)’ to a ‘while (SDL_PollEvent)’

Well, yes, that was the problem. But I still dont get the difference
between processing with if and processing with while.–
Roger D. Vargas
Linux user #180787
ICQ: 117641572

  • No hay nada tan importante que no pueda ser olvidado *
    Alzheimer

“Roger D. Vargas” wrote:

most likely you process one event at a time instead of all events in
the queue.

change your ‘if (SDL_PollEvent …)’ to a ‘while (SDL_PollEvent)’

Well, yes, that was the problem. But I still dont get the difference
between processing with if and processing with while.

do you have a SDL_Delay() or usleep() somewhere in your eventloop? if
yes - think about it a little bit. :slight_smile:

change your ‘if (SDL_PollEvent …)’ to a ‘while (SDL_PollEvent)’

Well, yes, that was the problem. But I still dont get the difference
between processing with if and processing with while.

While you move your mouse from a point to another, you get a lots of
events (one for each time the computer reads the mouse, I believe).
Using if, you process one event per frame, while using while, you
process all events at once, getting the cursor to the final destination
of the mouse in one frame.–
Lucas Clemente Vella
@Lucas_Clemente_Vella

El s?b, 22-01-2005 a las 17:58, Clemens Kirchgatterer escribi?:

“Roger D. Vargas” wrote:

most likely you process one event at a time instead of all events in
the queue.

change your ‘if (SDL_PollEvent …)’ to a ‘while (SDL_PollEvent)’

Well, yes, that was the problem. But I still dont get the difference
between processing with if and processing with while.

do you have a SDL_Delay() or usleep() somewhere in your eventloop? if
yes - think about it a little bit. :slight_smile:
No, i have a delay but out of event processing, inside game processing
loop.>


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Roger D. Vargas
Linux user #180787
ICQ: 117641572

  • No hay nada tan importante que no pueda ser olvidado *
    Alzheimer

Think of the event queue like a QUEUE; if there are several clients
(events) waiting today (this time around the loop), you may want to
serve all of them today (this time around the loop) and not tomorrow
(next time around the loop), because otherwise the queue will soon
reach all over town (the event queue will fill up). The while loop
takes care of the events this time around the loop, and the if takes
"one customer per day"!

/OlofOn Mon, 24 Jan 2005 11:25:58 +0000, Roger D. Vargas wrote:

El s?b, 22-01-2005 a las 17:58, Clemens Kirchgatterer escribi?:

“Roger D. Vargas” wrote:

most likely you process one event at a time instead of all events in
the queue.

change your ‘if (SDL_PollEvent …)’ to a ‘while (SDL_PollEvent)’

Well, yes, that was the problem. But I still dont get the difference
between processing with if and processing with while.

do you have a SDL_Delay() or usleep() somewhere in your eventloop? if
yes - think about it a little bit. :slight_smile:
No, i have a delay but out of event processing, inside game processing
loop.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Roger D. Vargas
Linux user #180787
ICQ: 117641572

  • No hay nada tan importante que no pueda ser olvidado *
    Alzheimer

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

El mar, 25-01-2005 a las 14:47, Olof Bjarnason escribi?:

Think of the event queue like a QUEUE; if there are several clients
(events) waiting today (this time around the loop), you may want to
serve all of them today (this time around the loop) and not tomorrow
(next time around the loop), because otherwise the queue will soon
reach all over town (the event queue will fill up). The while loop
takes care of the events this time around the loop, and the if takes
"one customer per day"!
Well, my problem is i was considering this queue as infinite, I mean, I
thought there is ALWAYS an event in the queue, because always the users
is moving mouse, clicking or pressing keys.>
/Olof

On Mon, 24 Jan 2005 11:25:58 +0000, Roger D. Vargas wrote:

El s?b, 22-01-2005 a las 17:58, Clemens Kirchgatterer escribi?:

“Roger D. Vargas” wrote:

most likely you process one event at a time instead of all events in
the queue.

change your ‘if (SDL_PollEvent …)’ to a ‘while (SDL_PollEvent)’

Well, yes, that was the problem. But I still dont get the difference
between processing with if and processing with while.

do you have a SDL_Delay() or usleep() somewhere in your eventloop? if
yes - think about it a little bit. :slight_smile:
No, i have a delay but out of event processing, inside game processing
loop.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Roger D. Vargas
Linux user #180787
ICQ: 117641572

  • No hay nada tan importante que no pueda ser olvidado *
    Alzheimer

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

Roger D. Vargas
Linux user #180787
ICQ: 117641572

  • No hay nada tan importante que no pueda ser olvidado *
    Alzheimer

Unfortunately, no mouse available to the average user has that kind of
resolution and/or frame rate. What looks like “always” to a human is
"every now and then" to a computer, if even that. :slight_smile:

(Your average CPU executes some 50 million instructions between each
event from a good, properly configured mouse. That’s 600000 printed
pages of asm code, or somewhere around 100000 printed pages of some
high level language.)

That said, to many applications, there is normally nothing to do if
there is no event to process, so you pretty much use the event
mechanism as the “time base” or driving force of the application. No
events, no cycles consumed. Simple, yet very effective.

Games, however, as opposed to “normal” GUI applications, almost
never work like this, so you’re better off basically forgetting all
about traditional GUI event handling when dealing with games. For
"action" games, you should preferably use the refresh rate (or the
best frame rate you can achieve) as your time base, rather than any
form of user input events. If smooth animation is less critical, or
minimal CPU usage is important, you can use some form of periodic
timer, which makes things more similar to, but still different from,
“normal” GUI programming. (Of course, any application that has
animations and stuff going on while waiting for user input is in a
gray zone. :slight_smile:

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Tuesday 25 January 2005 12.46, Roger D. Vargas wrote:

El mar, 25-01-2005 a las 14:47, Olof Bjarnason escribi?:

Think of the event queue like a QUEUE; if there are several
clients (events) waiting today (this time around the loop), you
may want to serve all of them today (this time around the loop)
and not tomorrow (next time around the loop), because otherwise
the queue will soon reach all over town (the event queue will
fill up). The while loop takes care of the events this time
around the loop, and the if takes “one customer per day”!

Well, my problem is i was considering this queue as infinite, I
mean, I thought there is ALWAYS an event in the queue, because
always the users is moving mouse, clicking or pressing keys.