Please login or register.

Login with username, password and session length
Advanced search  

News:

Forum rules - please read before posting, it can save you a lot of time.

Pages: [1] 2  All

Author Topic: How to increase keyboard handling speed?  (Read 11157 times)

0 Members and 1 Guest are viewing this topic.

pyros

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 15
    • View Profile
How to increase keyboard handling speed?
« on: August 03, 2007, 02:28:06 PM »

Hi all.
 Im novice in WME and perhaps this question has been considered earlier.
But im really tired.

I just need to move 1 sprite on the screen by using keyboard buttons UP, DOWN, LEFT, RIGHT.

It works, but VERY slowly - moreover my FPS  - 630, sleep scripts timing - 20 msec (standard) and i think that all Ok with my sprites.
i've checked it in VS 2005. Default "Direct Input" keyboard handling speed with my sprites very impressive - my protagonist sprite fly over screen less than 1 sec (from left to right 1280/1024, without using bitblt only with X screen coordinates incrementing by +1).
Sample was written in C#. I suspect that WME synchronized with some kind of internal timer, therefore keyboard handling in essence doesn't depends on graphic and scripting processing.

But i'm very interested in WME - Please help me find out how to increase keyboard handling speed? I mean how to increase number of keyboard event handler per second?

It should be very useful for me.
Thanks a lot.




Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: How to increase keyboard handling speed?
« Reply #1 on: August 03, 2007, 03:25:37 PM »

And how does your keyboard handling code look?
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

pyros

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 15
    • View Profile
Re: How to increase keyboard handling speed?
« Reply #2 on: August 04, 2007, 06:09:40 PM »

Here it is...
First block for UP, DOWN,LEFT, RIGHT inquiries. Next for diagonals.

on "Keypress"
{
     if(Keyboard.IsKeyDown(VK_UP)) { obj.Y = obj.Y - 1; }
     if(Keyboard.IsKeyDown(VK_DOWN)) { obj.Y = obj.Y + 1; }
              if(Keyboard.IsKeyDown(VK_LEFT)) { obj.X = obj.X - 1; }
     if(Keyboard.IsKeyDown(VK_RIGHT)) { obj.X = obj.X + 1; }
 
  if(Keyboard.IsKeyDown(VK_UP) && Keyboard.IsKeyDown(VK_LEFT))
     {
      obj.X = obj.X - 1;
      obj.Y = obj.Y - 1;
     }
  if(Keyboard.IsKeyDown(VK_UP) && Keyboard.IsKeyDown(VK_RIGHT))
     {
      obj.X = obj.X + 1;
      obj.Y = obj.Y - 1;
     }
       if(Keyboard.IsKeyDown(VK_DOWN) && Keyboard.IsKeyDown(VK_LEFT))
  {
      obj.X = obj.X - 1;
      obj.Y = obj.Y + 1;
     }
     if(Keyboard.IsKeyDown(VK_DOWN) && Keyboard.IsKeyDown(VK_RIGHT))
     {
   obj.X = obj.X + 1;
   obj.Y = obj.Y + 1;
     }
}
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: How to increase keyboard handling speed?
« Reply #3 on: August 04, 2007, 06:21:24 PM »

If you need continuous keyboard checking, you shouldn't rely on the "Keypress" event. Instead you should be checking the key state in a loop:

Code: WME Script
  1. while(true)
  2. {
  3.   if(Keyboard.IsKeyDown(VK_UP)) ....
  4.   // etc.
  5.  
  6.   Sleep(20);
  7. }
  8.  
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

pyros

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 15
    • View Profile
Re: How to increase keyboard handling speed?
« Reply #4 on: August 04, 2007, 06:48:20 PM »

Thanks. I will try just now. But what difference (in execution time I mean) between main cycle and "on key press" checking ?
I think that keypress is some kind of callback? Or not? Please, explain me shortly events hierarchy in execution from while (true) to end-user scripts.
I dont understand clearly how fired event translates to the specific object? Where can i find possible events list?
What kinds of events included in "INTERACTIVE" (in object description) identifier?
Does exist limited number of predefined events or I can create my own events too?

Thank you for your help.
Logged

pyros

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 15
    • View Profile
Re: How to increase keyboard handling speed?
« Reply #5 on: August 04, 2007, 07:53:14 PM »

As result of experiment - most fast code for single sprite moving is:

// infinite loop
while(true){

   if(Keyboard.IsKeyDown(VK_UP))
  {
  SpriteObj.Y = SpriteObj.Y - 2;
   Sleep(1);
  }
  if(Keyboard.IsKeyDown(VK_DOWN))
  {
  SpriteObj.Y = SpriteObj.Y + 2;
   Sleep(1);
  }
  if(Keyboard.IsKeyDown(VK_LEFT))
  {
  SpriteObj.X = SpriteObj.X - 2;
   Sleep(1);
  }
  if(Keyboard.IsKeyDown(VK_RIGHT))
  {
  SpriteObj.X = SpriteObj.X + 2;
   Sleep(1);
  }
 
  if(Keyboard.IsKeyDown(VK_UP) && Keyboard.IsKeyDown(VK_LEFT))
  {
   SpriteObj.X = SpriteObj.X - 2;
   SpriteObj.Y = SpriteObj.Y - 2;
   Sleep(1);

  }
  if(Keyboard.IsKeyDown(VK_UP) && Keyboard.IsKeyDown(VK_RIGHT))
  {
   SpriteObj.X = SpriteObj.X + 2;
   SpriteObj.Y = SpriteObj.Y - 2;
    Sleep(1);
  }
    if(Keyboard.IsKeyDown(VK_DOWN) && Keyboard.IsKeyDown(VK_LEFT))
  {
   SpriteObj.X = SpriteObj.X - 2;
   SpriteObj.Y = SpriteObj.Y + 2;
  Sleep(1);
  }
  if(Keyboard.IsKeyDown(VK_DOWN) && Keyboard.IsKeyDown(VK_RIGHT))
  {
   SpriteObj.X = SpriteObj.X + 2;
   SpriteObj.Y = SpriteObj.Y + 2;
  Sleep(1);
  }
  Sleep(1);
 
 
}
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: How to increase keyboard handling speed?
« Reply #6 on: August 06, 2007, 07:45:34 AM »

Quote
But what difference (in execution time I mean) between main cycle and "on key press" checking ?
on "keypress" is meant for typing. That is, it's affected by typing repeat rate etc. While the while(true) loop allows you to check keyboard state asynchronously.

Quote
I dont understand clearly how fired event translates to the specific object? Where can i find possible events list?
The low level events triggered by the engine are listed in the documentation, in the "Script language reference" chapter.


Quote
What kinds of events included in "INTERACTIVE" (in object description) identifier?
Pleas rephrase, I don't know what you mean.


Quote
Does exist limited number of predefined events or I can create my own events too?
There are "low level events", triggered by the engine automatically (mouse, keyboard and a few others), plus you can define any custom high level events, and apply them yourself when needed. For example, WME demos use events such as "LookAt" or "TalkTo" to implement the game logic.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

pyros

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 15
    • View Profile
Re: How to increase keyboard handling speed?
« Reply #7 on: August 06, 2007, 11:20:14 AM »

Thanks for your help :)

I spent all night yesterday trying to understand what happened with my previous script (). First time when i run it (after your advice) -  sprite reaction was perfect but when I restarted my computer and loaded WME and my project again - moving speed has decreased in three times!!! (FPS was stable) I didn't make any changes to my project.

And with script which was very fast when run first  - now i have STABLE LOW  sprite moving speed. 3x-4x difference approximately.
I create:
a) new project
b) all structures has been deleted except:
             one sprite
main cycle (within daemon)

 What else can affect the number of keyboard events per second? I dont understand what happened  :o
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: How to increase keyboard handling speed?
« Reply #8 on: August 06, 2007, 12:00:25 PM »

Perhaps the "Sleep(1)" isn't such a good idea. That way the speed of your script will be dependant on framerate (the script will get executed in every frame). You should probably use higher values to get constant speed independent on framerate.
Or perhaps you should use a completely different approach, like checking if the key is down, and if it is, move your object using some constant speed based on time delta. That's how it's usually done.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

pyros

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 15
    • View Profile
Re: How to increase keyboard handling speed?
« Reply #9 on: August 06, 2007, 05:30:46 PM »

Please look into my table...

Sorry I don't know how to insert picture into post. Is it possible?
« Last Edit: August 06, 2007, 05:34:01 PM by pyros »
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: How to increase keyboard handling speed?
« Reply #10 on: August 06, 2007, 08:23:25 PM »

You'll need to upload the picture somewhere (like to flickr.com) and link it here.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

pyros

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 15
    • View Profile
Re: How to increase keyboard handling speed?
« Reply #11 on: August 07, 2007, 03:29:38 PM »

I will use simple text better...
I've made some tests and i can't explain dependency.

Basic test conditions:

My workstation - CPU AthlonXP 2400+/RAM 768 Mb/ NVidia 6600 GT AGP Video

Screen resolution 1280x1024x32 + 4x antialiasing
30-frames 256x256 TGA-sprite moves straight down Y-axis with +5 px increment

A) Vertical synchronization is OFF - 400 FPS
-----------------------------------------------------------------------------
Sprite moving time for gamer's eyes  (in sec on the screen) top-to-bottom Y-axis with:
1. Sleep (0) in main cycle -  1 sec (OK) smooth animation (normal)
2. Sleep (5) in main cycle -  3 sec (3x slow) smooth animation (normal)
3. Sleep (10) in main cycle -  3 sec (3x slow) smooth animation (normal)
4. Sleep (20) in main cycle -  5 sec (5x slow) smooth animation (average)
5. Sleep (30) in main cycle -  7 sec (7x slow) smooth animation (average)
6. Sleep (50) in main cycle -  9 sec (9x slow) flicker animation (not acceptable)

B) Vertical synchronization is ON - 60 FPS
-----------------------------------------------------------------------------
Sprite moving time for gamer's eyes  (in sec on the screen) top-to-bottom Y-axis with:
1. Sleep (0) in main cycle -  4 sec (4x slow) smooth animation (normal)
2. Sleep (5) in main cycle -  4 sec (4x slow smooth animation (normal)
3. Sleep (10) in main cycle -  4 sec (4x slow) smooth animation (normal)
4. Sleep (20) in main cycle -  6 sec (6x slow) smooth animation (average)
5. Sleep (30) in main cycle -  7 sec (7x slow) smooth animation (average)
6. Sleep (50) in main cycle -  9 sec (9x slow) flicker animation (not acceptable)

Number of executed Keyboard.IsKeyDown calls are EQUAL IN ALL THESE CASES - 174 for both states of vertical synchronization (on/off) and l Sleep(0,5,10,20,30,50) values (for my hardware profile of course) .

Why performance with the test cases B) 1, 2, 3 4x-decreased relative to the A) 1 test case?
Logically if in each of keyboard's event/call we will increase sprite Y-axis position  by +5  therefore our sprite speed should be constant .
Why for gamer's eyes sprite time moving speed depends on FPS if number of executed generative events per real-time second (for coordinates changing) are equal in all cases?






Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: How to increase keyboard handling speed?
« Reply #12 on: August 07, 2007, 06:57:19 PM »

Like I said, if you use Sleep(0), it's like saying "wait till next frame", because it always takes more than 0 milliseconds to process one frame. So in the end you're calling IsKeyDown in every frame. And higher framerate = more IsKeyDown calls per second.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

pyros

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 15
    • View Profile
Re: How to increase keyboard handling speed?
« Reply #13 on: August 08, 2007, 01:11:45 AM »

No. For any values of Sleep(x)  - number of Keyboard.IsKeyDown() calls are CONSTANT. But real speed of sprite moving is DIFFERENT. For my code calls value is 174.  Because in my script sprite path on Y-axis has limit of 870 px. If sprite moves with Y=Y+5 increments then  870px/5px = 174 times (I've checked it very carefully - when I changed in script Y-axis increment to +10 number of Keyboard.IsKeyDown() calls has decreased to 87). I don't understand why it works this way ???.

This is very strange problem for me - if number of keyboard calls are constant in both cases and FPS under forced control (vsync on/off,  FPS 60/400) then logically smoothness of the animation must change only, but NOT the time period of sprite moving for gamer's  eyes (if I call keyboard and increase Y=Y+5 174 times per REAL second - my sprite must achieve bottom of the screen in one second in ALL CASES). And for these FPS values (60/400) I shall not see any visible difference in speed and smoothness down to FPS value equal 30 and less (approximately eyesight limit for animation).
Moreover internal animation cycle in any sprite entity works CORRECTLY with dependency on REAL TIME msec delays between its frames.

I think correct way is:
 I changed sprite Y position - if engine has time to render frame with current sprite position smoothly- it will render (acceptable FPS > 30), if  has not  - engine should calculate sprite in the next position but frame will not be rendered correctly for your eyes due to video hardware limitations (not acceptable FPS < 30) , therefore animation flickering will appear.

In WME real time sprite moving speed is decreasing proportionally to FPS fall (sleep() delays too but it is reasonable)
But I mean situation only when sprite controlled by gamer with cycled keyboard state checking.
Within any sprite entity internal frames animation renders correctly without explicit fps dependency.

It means obviously that WME internal time ticker synchronized to fixed reference FPS value as base of engine synchronization - may be equal 60 or another fixed value but NOT to REAL TIME? Therefore all processes within engine depend on this base reference FPS value?
Can be that any external script executed by engine (especially event handlers) cannot be synchronized to real time?
In this case how to make laser shot sprite? Or energy ball sprite with fast moving over screen? (without large coordinates increment because it will lose smothness)?
« Last Edit: August 08, 2007, 01:55:14 AM by pyros »
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: How to increase keyboard handling speed?
« Reply #14 on: August 08, 2007, 03:26:33 PM »

Quote
But real speed of sprite moving is DIFFERENT
Yes, but only as long as you use too low sleep values (i.e. lower than how much time it takes to render one frame). Naturally, the script can't sleep for shorter time than the frame render time. Even if you call Sleep(0), it will sleep for at least 1000/FPS millisecons.

Quote
It means obviously that WME internal time ticker synchronized to fixed reference FPS value as base of engine synchronization
Nope, not true.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave
Pages: [1] 2  All
 

Page created in 0.062 seconds with 20 queries.