Slow get key :-/

Hi to all, i have a problem about performance of code:
im the main cicle of my application i use this code

while(true)
{

get_key();
}

where get_key() is so defined:

void get_key()
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN: …

etc etc… i need get_key to allow interaction of the user with the application or to break the execution of code, but the problem is that this piece of code is really, REALLY slow, i have make some tests and it slows down the execution approximately the half!!! :((

how can refine this code in a faster version? Thanks a lot!!!

Muzero

Hello people,

Hi to all, i have a problem about performance of code:
im the main cicle of my application i use this code

while(true)
{

get_key();
}

where get_key() is so defined:

void get_key()
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN: …

 IMHO, this get_key() isn't so bad, but that eternal cycle is. Try to
handle your task without that "while(true)". Though that depends
on final task of your application. It can make my advice useless
- sorry then.-- 

Best regards,
Flashback

 IMHO, this get_key() isn't so bad, but that eternal cycle is. Try to
handle your task without that "while(true)". Though that depends
on final task of your application. It can make my advice useless
- sorry then.

this kind of main cicle is an example :slight_smile: but unfortunately i have really to use it … the problem is the get key :frowning:

-Muzero

Could it be that other functions are slowing down your application. Here
is what my keyboard handler looks like

[snip]
SDL_Event g_Event;
// g_Event is set globally

void KeyBrd (void) {
while (SDL_PollEvent(&g_Event)) {
switch (g_Event.type) {
case SDL_KEYDOWN:
switch (g_Event.key.keysym.sym) {

// you can pretty much figure out the rest from here

[/snip]

I haven’t had any noticeable lag in my controls, check for other
functions such as blitting that may slow down your app.

Anyhow good luck.On Tue, 2004-09-07 at 12:26, Muzero wrote:

this kind of main cicle is an example :slight_smile: but unfortunately
i have really to use it … the problem is the get key :frowning:

LesHauSsebons wrote:

I had some lag with mouse movements ( pollevent ) too.
<…>

I don’t know if I am wrong, and if it’s clear, but, for the mouse event
that was the problem.

you can tell to SDL to ignore the mouse event :
SDL_EventState( SDL_MOUSEMOTION, SDL_IGNORE );

Btw, it has been a long time since I’ve put my nose in SDL…

Murlock

I had some lag with mouse movements ( pollevent ) too.

I noticed my loop was reading ALL the events the computer gets, so it
took a lot of time to handle this all, until I stop moving the mouse (
for example ). ( If I made 3 circles, then stop, it was still moving the
cursor on the screen of the first circle, then the others stored in the
poolevent )

The hack I thought about was to flush the pool and get only the last
event ( mouse position ? ) so that was looking fine.

Maybe, when you hit a key, you don’t get only ONE event, but thousands,
just check your poolevent, what was the single significant event, and
consider there was only one … ( keypress, or several keypress )

I don’t know if I am wrong, and if it’s clear, but, for the mouse event
that was the problem.

Btw, it has been a long time since I’ve put my nose in SDL…

Les.

Muzero a ?crit :> Hi to all, i have a problem about performance of code:

im the main cicle of my application i use this code

while(true)
{

get_key();
}

where get_key() is so defined:

void get_key()
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN: …

etc etc… i need get_key to allow interaction of the user with the application or to break the execution of code, but the problem is that this piece of code is really, REALLY slow, i have make some tests and it slows down the execution approximately the half!!! :((

how can refine this code in a faster version? Thanks a lot!!!

Muzero


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

Hi,

Here is my experience if you need the mouse events.
This also applies to whatever other events.
I’ve read about this somewhere on the SDL site.

All the events must be processed first in a loop.
Then comes the drawing;

Here is my run() function.

void Game::run(void)
{
//…
ticks = SDL_GetTicks();
//SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
while (true)
{
tmp_res = SDL_PollEvent(&the_event);
///…
if (tmp_res == 1)
{
process_event();
continue;
}

     t = SDL_GetTicks();
     update(t-ticks); // update "visual" components for example
     ticks = t;
     if (update_screen != 0)
     {
         show();
         update_screen = 0;
     }
     else
     {
         SDL_Delay(1);
     }
 }

}

I hope this helps.

Velyo

LesHauSsebons wrote:> I had some lag with mouse movements ( pollevent ) too.

I noticed my loop was reading ALL the events the computer gets, so it
took a lot of time to handle this all, until I stop moving the mouse (
for example ). ( If I made 3 circles, then stop, it was still moving the
cursor on the screen of the first circle, then the others stored in the
poolevent )

The hack I thought about was to flush the pool and get only the last
event ( mouse position ? ) so that was looking fine.

Maybe, when you hit a key, you don’t get only ONE event, but thousands,
just check your poolevent, what was the single significant event, and
consider there was only one … ( keypress, or several keypress )

I don’t know if I am wrong, and if it’s clear, but, for the mouse event
that was the problem.

Btw, it has been a long time since I’ve put my nose in SDL…

Les.

Muzero a ?crit :

Hi to all, i have a problem about performance of code:
im the main cicle of my application i use this code

while(true)
{

get_key();
}

where get_key() is so defined:

void get_key()
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN: …

etc etc… i need get_key to allow interaction of the user with the
application or to break the execution of code, but the problem is that
this piece of code is really, REALLY slow, i have make some tests
and it slows down the execution approximately the half!!! :((

how can refine this code in a faster version? Thanks a lot!!!

Muzero


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

This may be a dumb question, but seeing as how we really haven’t solved
the problem…

What kind of hardware are you running on? Do other SDL apps have
problems running on your system? Can you run modern games?

How old is your machine?On Sep 7, 2004, at 11:12 AM, Muzero wrote:

Hi to all, i have a problem about performance of code:
im the main cicle of my application i use this code

while(true)
{

get_key();
}

where get_key() is so defined:

void get_key()
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN: …

etc etc… i need get_key to allow interaction of the user with the
application or to break the execution of code, but the problem is that
this piece of code is really, REALLY slow, i have make some tests
and it slows down the execution approximately the half!!! :((

how can refine this code in a faster version? Thanks a lot!!!

Muzero


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

actually I was using the mouse in my program …

Ignoring the pollevent and taking only the last one each loop granted me
smooth and fast motion.

Murlock a ?crit :> LesHauSsebons wrote:

I had some lag with mouse movements ( pollevent ) too.

<…>

I don’t know if I am wrong, and if it’s clear, but, for the mouse
event that was the problem.

you can tell to SDL to ignore the mouse event :
SDL_EventState( SDL_MOUSEMOTION, SDL_IGNORE );

Btw, it has been a long time since I’ve put my nose in SDL…

Murlock

Another answer is to process every event and only redraw the screen after all events are processed.
if the processing is taking too long, then you can stop handling events for a redraw every 1/min_fps seconds.
but most people don’t have to worry about processing taking too long and emptying the queue happens much faster
than redrawing after every event. another thing to do is to realize a max_fps (since updating the screen faster
than the refresh rate of the screen is futile) and at least keep polling for events until the time for a frame
arrives. This can increase responsiveness as well, since then you are not wasting time redrawing when nothing
has happened or the user wouldn’t see anything happen anyway because of the screen refresh rate. most games
don’t bother with this. Some people use threaded event handling to update the state for the next frame, making
the redraw not subtract from the response time.

if you only take the last event, then it’s possible you may skip an important one…

LesHauSsebons wrote:> actually I was using the mouse in my program …

Ignoring the pollevent and taking only the last one each loop granted me
smooth and fast motion.

Murlock a ?crit :

LesHauSsebons wrote:

I had some lag with mouse movements ( pollevent ) too.

<…>

I don’t know if I am wrong, and if it’s clear, but, for the mouse
event that was the problem.

you can tell to SDL to ignore the mouse event :
SDL_EventState( SDL_MOUSEMOTION, SDL_IGNORE );

Btw, it has been a long time since I’ve put my nose in SDL…

Murlock


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

LesHauSsebons wrote:

actually I was using the mouse in my program …

Ignoring the pollevent and taking only the last one each loop granted me
smooth and fast motion.
Your software doesn’t use keyboard ?

Do you track the mouse ? or only when user click on mouse buttons ?

SDL_MOUSEMOTION => mouse is moving
SDL_MOUSEBUTTONUP/DOWN => mouse buttons clicked / released

Cheers,
Murlock