SDL mouse events

I use ‘while(SDL_PollEvent…’ to manage my mouse events and the following
questions came to mind:

Can there be multiple event per type on the queue?
If so, how many mouse events would there be on the queue if my program runs
once every second and my mouse gets polled by windows at 500Hz(and moves
randomly)? 500?

Thanks.

I use ‘while(SDL_PollEvent…’ to manage my mouse events and the following
questions came to mind:

Yes, there can be many events of every type on the queue.

Can there be multiple event per type on the queue?

If so, how many mouse events would there be on the queue if my program
runs
once every second and my mouse gets polled by windows at 500Hz(and moves
randomly)? 500?

No, not quite 500, the queue is only 128 items long. Once the queue is full
SDL just discards any new events. That means you never have to process more
than 128 events at a time, but if you only check for events every second and
you can have 500 events pile up during a second, you will lose 372 events.
The best idea is to use SDL_WaitEvents() to process events as they come in.

  Bob Pendleton

Thanks.On 3/7/08, Lucas <lucas_pet at live.co.uk> wrote:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

±-------------------------------------+

i’ve had this issue before, i’m sorry if it is blaringly obvious, but
how can i
pass my char to the funtion IMG_ReadXPMFromArray( char **xpm )

right now my function does this

SDL_Surface *getimage(int mask , char *name )
{
/snip/
strcat(name, “_xpm”);
surface=IMG_ReadXPMFromArray(name);
/snip/
};
which gives
warning: passing argument 1 of ?IMG_ReadXPMFromArray? from incompatible
pointer type

last time i was using xpms from arrays there were a whole 4 of them,
now i need
to be able to use a getimage function as there are 88 images

thanks

( sorry if as usual i am being a noob )

i’ve had this issue before, i’m sorry if it is blaringly obvious, but
how can i
pass my char to the funtion IMG_ReadXPMFromArray( char **xpm )

right now my function does this

SDL_Surface *getimage(int mask , char *name )
{
/snip/
strcat(name, “_xpm”);
surface=IMG_ReadXPMFromArray(name);
/snip/
};
which gives
warning: passing argument 1 of ?IMG_ReadXPMFromArray? from incompatible
pointer type

The IMG_ReadXPMFromArrary() function expects the entire file to already be
in memory in a char array in memory. The function you want is IMG_Load()
which takes one argument which the the path and name of the file that
contains the .xpm. It will figure out that it is an XPM and load it. At
least it will if XPM support is compiled into the image library. You could
also use IMG_LoadXPM_RW() if you really want to make sure it treats the file
as a .xpm file. Please google for these function names to find the
documentation.

BTW, the way you are using strcat() is very dangerous. If you do not have 4
free bytes after the \0 byte at the end of each string you will wind up
corrupting the memory at the end of each string. It is better to just store
the name with the .xpm attached.

 Bob Pendleton

last time i was using xpms from arrays there were a whole 4 of them,On 3/7/08, neil at cloudsprinter.com wrote:

now i need
to be able to use a getimage function as there are 88 images

thanks

( sorry if as usual i am being a noob )


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

±-------------------------------------+

I think it should be mentioned explicitly here that it’s fine to use “while(SDL_PollEvent)” for things that need non-blocking behavior (e.g. single-threaded) and you’re probably polling the queue more frequently than once per second. For example, with a logic loop running at 50Hz and the mouse pushing events at 500 Hz, you’ll be getting an average of 10 events per loop.

Jonny DDate: Fri, 7 Mar 2008 15:19:58 -0600
From: bob@pendleton.com
To: sdl at lists.libsdl.org
Subject: Re: [SDL] SDL mouse events.

On 3/7/08, Lucas <lucas_pet at live.co.uk> wrote:
I use ‘while(SDL_PollEvent…’ to manage my mouse events and the following
questions came to mind:
Yes, there can be many events of every type on the queue.

Can there be multiple event per type on the queue?
If so, how many mouse events would there be on the queue if my program runs
once every second and my mouse gets polled by windows at 500Hz(and moves
randomly)? 500?

No, not quite 500, the queue is only 128 items long. Once the queue is full SDL just discards any new events. That means you never have to process more than 128 events at a time, but if you only check for events every second and you can have 500 events pile up during a second, you will lose 372 events. The best idea is to use SDL_WaitEvents() to process events as they come in.

  Bob Pendleton

Thanks.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

±-------------------------------------+


Climb to the top of the charts!?Play the word scramble challenge with star power.
http://club.live.com/star_shuffle.aspx?icid=starshuffle_wlmailtextlink_jan

i’ve had this issue before, i’m sorry if it is blaringly obvious, but
how can i
pass my char to the funtion IMG_ReadXPMFromArray( char **xpm )

right now my function does this

SDL_Surface *getimage(int mask , char *name )
{
/snip/
strcat(name, “_xpm”);
surface=IMG_ReadXPMFromArray(name);
/snip/
};
which gives
warning: passing argument 1 of ?IMG_ReadXPMFromArray? from incompatible
pointer type

The IMG_ReadXPMFromArrary() function expects the entire file to already be
in memory in a char array in memory. The function you want is IMG_Load()
which takes one argument which the the path and name of the file that
contains the .xpm. It will figure out that it is an XPM and load it. At
least it will if XPM support is compiled into the image library. You could
also use IMG_LoadXPM_RW() if you really want to make sure it treats the file
as a .xpm file. Please google for these function names to find the
documentation.

er… still not sure what i am doing wrong…

i have a .h file being loaded/used with the arrays like this

static char backg3_xpm[] = {
/
columns rows colors chars-per-pixel */
“200 200 256 2”,
" c #151417",
". c #071835",

— etc

i appended the _xpm because some of the image names are the same as thier
surfaces

if i use IMG_ReadXPMFromArray( backg3_xpm ); it will load fine, but i need to
make a function that i can use custom names, which i am failing on

agauin sorry if i a noobing out in some way, and sorry if i dint make my first
post clear about what my issues are

SDL_Surface *getimage(int mask , char *name )
{
/snip/
strcat(name, “_xpm”);
surface=IMG_ReadXPMFromArray(name);
/snip/
};
^ wrong!

which gives warning: passing argument 1 of ?IMG_ReadXPMFromArray?
from incompatible pointer type

i have a .h file being loaded/used with the arrays like this

static char backg3_xpm[] = {
/
columns rows colors chars-per-pixel */
“200 200 256 2”,
" c #151417",
". c #071835",

This looks like an XPM, which actually is a snippet of C code! Now, what you
want to do is pass this (backg3_xpm) to IMG_ReadXPMFromArray(). Note that
this is an array of pointers while you ‘name’ variable is a pointer. I’m
pretty sure you also couldn’t call getimage(backg3_xpm[n]) either and with a
similar error.

i appended the _xpm because some of the image names are the same as thier
surfaces

This doesn’t make sense to me either. FYI, IMG_ReadXPMFromArray() requires
that you pass it an array, just like the one of the XPM. If you want to read
a file and ‘name’ is the filename, then you want to use IMG_Load() instead.
In no case can I imagine that you have to add anything anywhere, and
particularly not at runtime.

if i use IMG_ReadXPMFromArray( backg3_xpm ); it will load fine, but i need
to make a function that i can use custom names, which i am failing on

What is ‘custom names’? The question what is correct simply depends on whether
you embed the XMP (remember, it’s C code!) or if you want to load it from
file.

BTW: there is too much guessing going on here, and that is the result of not
having the relevant info. What you should have done is create a minimal
program that only loads your bitmap, outputs success/failure and exits. There
should not be a single line that could still be removed. Then, you firstly
have the problem pinned down to the smallest possible amount of code (which
is helpful for you to understand it) and secondly you have exactly the amount
of code that you should post here so others can try it and give you precise
help without guessing.

One last thing: Your initial mail was written by answering a totally unrelated
mail and then erasing the subject line. This is called “stealing threads” and
a bad thing (do a websearch). Now, this mail of yours seems to be not a reply
at all, which is also bad for the same reasons.

cheers!

UliOn Saturday 08 March 2008 09:23, neil at cloudsprinter.com wrote:

LoadXPMFromArray gets passed the address of the array containing the
XPM data. What you seem to be trying to do is pass it the symbol name
of the array containing the XPM data. You can’t do that directly in C,
you’d need to do something non-portable like SDL_LoadObject your
executable.
If you must access the graphics this way, instead of directly, what
you should do is set up an associative array containing a mapping
between names and arrays.
Something like:

// Keep this sorted by name
static struct name_array_map {
const char* name;
char** array;
} name_array_map[] = {
{“food”, food_xpm},
{“name”, name_xpm},
{“pie”, pie_xpm}, // can anyone tell what’s on my mind?
{“rootbeer”, rootbeer_xpm},
};

static int namcmp(const void* key, const void* base) {
return strcmp((const char*)key, ((const struct name_array_map*)base)->name);
}

SDL_Surface* getimage(int mask, const char* name) {
struct name_array_map* entry;
SDL_Surface* surface;
// snip
entry = (struct name_array_map*)bsearch(name, name_array_map,
sizeof(name_array_map)/sizeof(*name_array_map),
sizeof(*name_array_map), namcmp);
if(!entry) {
SDL_SetError(“No such image”);
return NULL;
}
surface = IMG_ReadXPMFromArray(entry->array);
// snip
}

-:sigma.SB

/* sorry i have snipped everything // sorry i am braking some rules of list
posting… will try not to in future */

basically this is way to much grief… i tested the new bits to IMG_xpm.c i
added, that code snippet now means you should be able to throw any xpm at
SDL_image now and it will cope loading them with named colors.

i am trying to do a low colour low power low res version of my game, and i
thought xpm was a good format to hold some low color gfx, but i think i will
just attempt to use png, and bin to hex them and rwop them in, it is critical
that the gfx of the game are not messed with, so i want them compiled in the
binary, the gfx are ‘made’ while the game plays from colors textures and
shapes.

anyway i am rambling again, thanks for all your advice on my crazy xpm
schemeing
:wink:

back to staring blanky at my code now :wink:

No, not quite 500, the queue is only 128 items long. Once the queue is full
SDL just discards any new events. That means you never have to >process
more than 128 events at a time, but if you only check for events every
second and you can have 500 events pile up during a second, >you will lose
372 events. The best idea is to use SDL_WaitEvents() to process events as
they come in.

 Bob Pendleton

thanks everybody.

Would this mean that when the queue is full of mouse events, all key events
are gone as well?
Is it possible to not let SDL push any mouse events on the queue? (I know
people who poll their mouse at 1000Hz :/)
I think getting the mouse state manually (GetMouseState and
GetRelativeMouseState) is best for me.
Does GetRelativeMouseState gets the relative state from the last time I
called that function or relative to last mouse poll (system)?

Lucas

No, not quite 500, the queue is only 128 items long. Once the queue is
full
SDL just discards any new events. That means you never have to >process
more than 128 events at a time, but if you only check for events every
second and you can have 500 events pile up during a second, >you will
lose
372 events. The best idea is to use SDL_WaitEvents() to process events as
they come in.

 Bob Pendleton

thanks everybody.

Would this mean that when the queue is full of mouse events, all key
events
are gone as well?

Yep.

Is it possible to not let SDL push any mouse events on the queue? (I know

people who poll their mouse at 1000Hz :/)

That statement doesn’t make a lot of sense to me. The rate at which the
mouse is polled is not necessarily the rate at which mouse events are
generated. Often physical mouse movements are accumulated and a motion event
is only generated when the position changes by some minimum amount or a
button changes state. Not to mention that a lot (most?) mouse information is
processed as interrupts and not polled at all.

Bob Pendleton

I think getting the mouse state manually (GetMouseState and

GetRelativeMouseState) is best for me.
Does GetRelativeMouseState gets the relative state from the last time I
called that function or relative to last mouse poll (system)?

The last time you called it.

LucasOn 3/16/08, Lucas <lucas_pet at live.co.uk> wrote:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

±-------------------------------------+

Thanks.

Yes, polled was the wrong word there, my bad. I meant sample rate.

I probably have this wrong, but this is how I see it:
You would want to be able to measure mouse motion of only one pixel.
A sample rate of 1000, constant mouse movement and a high screen resolution
this would amount to 1000 events per second on the queue.
Lets say that some part of your 3d game runs slow on that computer (lets say
5 fps), that would lead to event loss.
I’d rather set the sample rate of SDL to a lower frequency or
not let mouse events flood the queue by only accepting key events and use
GetRelativeMouseState for mouse movement.>Is it possible to not let SDL push any mouse events on the queue? (I know

people who poll their mouse at 1000Hz :/)

That statement doesn’t make a lot of sense to me. The rate at which the
mouse is polled is not necessarily the rate at which mouse events are

generated. Often physical mouse movements are accumulated and a motion
event is only generated when the position changes by some minimum >amount
or a button changes state. Not to mention that a lot (most?) mouse
information is processed as interrupts and not polled at all.

Bob Pendleton

I probably have this wrong, but this is how I see it:
You would want to be able to measure mouse motion of only one pixel.
A sample rate of 1000, constant mouse movement and a high screen resolution
this would amount to 1000 events per second on the queue.
Lets say that some part of your 3d game runs slow on that computer (lets say
5 fps), that would lead to event loss.

That’s correct using those numbers, however I’ve never actually seen mouse
events that always only have 1 pixel delta. Usually it’s in the 3-10 pixel
range, and depends on speed of mouse movement.

If you think about the way a mouse actually works, it’s translating some
physical movement into screen coordinate motion, and I think you would have
a very hard time moving the mouse at exactly the right rate of speed to
generate exactly one pixel of motion at the hardware update rate. :slight_smile:

I’d rather set the sample rate of SDL to a lower frequency or
not let mouse events flood the queue by only accepting key events and use
GetRelativeMouseState for mouse movement.

Yes, you’re always welcome to ignore the events using SDL_EventState()
and they won’t fill the queue, but you’ll still be able to poll the current
position.

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

I probably have this wrong, but this is how I see it:
You would want to be able to measure mouse motion of only one pixel.
A sample rate of 1000, constant mouse movement and a high screen
resolution
this would amount to 1000 events per second on the queue.
Lets say that some part of your 3d game runs slow on that computer (lets
say
5 fps), that would lead to event loss.

That’s correct using those numbers, however I’ve never actually seen mouse
events that always only have 1 pixel delta. Usually it’s in the 3-10
pixel
range, and depends on speed of mouse movement.

If you think about the way a mouse actually works, it’s translating some
physical movement into screen coordinate motion, and I think you would
have
a very hard time moving the mouse at exactly the right rate of speed to
generate exactly one pixel of motion at the hardware update rate. :slight_smile:

I didn’t say the motion would always be 1 px/mouseSample, I just said you
would want to be able to measure a movement of one pixel (when they occur).
If you constantly move the mouse, all 1000 samples would be used regardless
of the speed.
Btw. I’ve seen a razor mouse which has a 1200Hz sample rate which would fill
the queue at ~10fps, which isn’t unheard of.

Maybe it would be a nice addition to make the pollrate adjustible?

I’d rather set the sample rate of SDL to a lower frequency or
not let mouse events flood the queue by only accepting key events and use
GetRelativeMouseState for mouse movement.

Yes, you’re always welcome to ignore the events using SDL_EventState()
and they won’t fill the queue, but you’ll still be able to poll the
current
position.

Thanks!
Somehow I missed that one.>

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

Btw. I’ve seen a razor mouse which has a 1200Hz sample rate which would fill
the queue at ~10fps, which isn’t unheard of.

Have you actually seen this fill the SDL queue on any platform?
Even with ~15 FPS, I’ve never seen more than 20 or 30 mouse events in the
queue with any mouse on any platform.

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

I set my program to 1fps (SDL_DELAY) and printed the event count, but
somehow a mousemovement only adds a single event?
Typing did as expected.
Let me guess, SDL_DELAY disables mouse events. No that isn’t it, as
replacing the delay with a long dumb loop doesn’t change a thing.
What did I do wrong?

int eventCount=0;
while (SDL_PollEvent(&event))
{
eventCount++;
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
//handling code
break;
case SDL_MOUSEMOTION:
//handling code
break;
case SDL_QUIT:
//handling code
break;
case SDL_KEYDOWN:
//handling code
break;
default:
break;
}
}
print(eventCount);>> the queue at ~10fps, which isn’t unheard of.

Have you actually seen this fill the SDL queue on any platform?
Even with ~15 FPS, I’ve never seen more than 20 or 30 mouse events in the
queue with any mouse on any platform.

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

On Behalf Of Lucas

I set my program to 1fps (SDL_DELAY) and printed the event count, but
somehow a mousemovement only adds a single event?
Typing did as expected.
Let me guess, SDL_DELAY disables mouse events. No that isn’t it, as
replacing the delay with a long dumb loop doesn’t change a thing.
What did I do wrong?

Maybe your OS combines mouse (motion) events if more than one occur
before the app handles them? Try adding an SDL_PumpEvents() call inside
your dumb loop to allow SDL to move input events to the (SDL) queue
(from the OS queue) and see what happens then.

-J

----- Original Message -----
From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org]
Subject: Re: [SDL] SDL mouse events.

Sam Lantinga wrote:

Btw. I’ve seen a razor mouse which has a 1200Hz sample rate which would fill
the queue at ~10fps, which isn’t unheard of.

Have you actually seen this fill the SDL queue on any platform?
Even with ~15 FPS, I’ve never seen more than 20 or 30 mouse events in the
queue with any mouse on any platform.

Hmmm. Does this mean that in 1.3 the mouse input is still gathered using
a thread? This is one of my biggest gripes with 1.2. The problem is that
if your input gathering rate differs from your game’s framerate (which
it almost always will), you get beats in the mouse input.

Time SDL input thread Game loop
|
|/ Read mouse 1px Process mouse 1px

          Read mouse 1px
                                      Process mouse 1px

          Read mouse 1px

                                      Process mouse 1px
          Read mouse 1px


          Read mouse 1px              Process mouse 2px


          Read mouse 1px
                                      Process mouse 1px

          Read mouse 1px

                                      Process mouse 1px

…etc.

As you can see the mouse movement as far as the game is concerned is
uneven, despite the input being perfectly smooth. This problem only gets
worse when your render rate is variable.

Given that I don’t have the reaction times of a hummingbird, personally
I don’t really care that much; I only really notice it if I’m looking
for it. Unfortunately we’ve been getting widespread antipathy towards
SDL for the Windows version of ioquake3.org for reasons of the mouse
input alone. I have to admit that it’s quite apparent when compared
against an older build which uses dinput directly.

Note that the SDL_Get*MouseState API functions don’t help since these
still query the SDL event queue. It would be nice if these calls simply
passed through to the platform specific mouse query functions directly.
Obviously this approach is mutually exclusive with handling mouse events
on the SDL queue, so it would need to be disabled. I think this is what
is required to placate the twitch-shooter brigade :frowning:

However, I was under the impression (without actually looking at the
evidence) that 1.3 had fixed this issue with ManyMouse stuff? Is this
not the case?

Maybe your OS combines mouse (motion) events if more than one occur
before the app handles them? Try adding an SDL_PumpEvents() call inside
your dumb loop to allow SDL to move input events to the (SDL) queue
(from the OS queue) and see what happens then.

-J

Thanks, that did the job.

At 5Fps, I get 96-102 mouse events when moving the mouse, meaning a razor
mouse would fill the queue.
I don’t understand why key events did queue up properly without
SDL_PumpEvents() as I read it they shouldn’t.
Windows XP btw.

Note that the SDL_Get*MouseState API functions don’t help since these
still query the SDL event queue. It would be nice if these calls simply
passed through to the platform specific mouse query functions directly.
Obviously this approach is mutually exclusive with handling mouse events
on the SDL queue, so it would need to be disabled. I think this is what
is required to placate the twitch-shooter brigade :frowning:

However, I was under the impression (without actually looking at the
evidence) that 1.3 had fixed this issue with ManyMouse stuff? Is this
not the case?

&&
Sam Lantinga wrote:

Yes, you’re always welcome to ignore the events using SDL_EventState()
and they won’t fill the queue, but you’ll still be able to poll the
current
position.

Aren’t these two statements contradicting?