Jump to content
  • Advertisement
Sign in to follow this  
Ezani

DirectInput : SPACEBAR fires lasers too quickly

This topic is 3718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all I am in the midst of developing a 2D Starship Shooter using DirectInput. I am capturing the SPACEBAR (DIK_SPACE) to fire lasers from my ship. Problem is the SPACEBAR is too sensitive and pressing it just a shortwhile fires too many lasers (5 or more) when I only need it to fire once for a normal human finger hitting the space bar at a normal human rate. So if the players hits the space bar faster, two or more lasers get fired silmutaneously. But my problem is : just a short, quick hit on the spacebar and I have 5 and more lasers fired! I've tried delaying using TickCount but it becomes too insensitive and most of the time I'm firing only one lasers no matter how fast I hit the spacebar. Occassionally I get two lasers silmutaheously. I've tried tweaking the delay times but it becomes too sensitive (too many lasers fired) or not sensitive (only on laser fired no matter how fast I hit the spacebar many, many times). How does one program the laser fire portion using DirectInput so that the user can comfortably fire many lasers by hitting the spacebar at the normal human speed of hand ? Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
You could try using the performance counter.

QueryPerformanceFrequency and QueryPerformanceCounter are the two routines you need.

On GHz CPUs, the performance counter can read intervals of time on the order of nano-seconds.

Of course this may be a little too sensitive (and thus leave you with the original problem), but with a little tweaking here and there, you could easily measure delays of microseconds or fractions of milliseconds without much difficulty.

Share this post


Link to post
Share on other sites
Are you just responding to a KeyDown event and firing when the key is down. What you could do is keep a key map(a Boolean vector for all keyboard values), now fire a laser and set the space key in the Boolean key map to true.
Next time you come in to the keyboard event handler and that key in the key map is true it means that the key hasn't been released yet, so don't fire a new laser.
On the key up event you set the Boolean value in the key map back to false, so you know a key up event has been seen and you know the user has released the key.

In this way you can be always sure that the user is tapping the space bar instead of keeping it pressed.
You could alter the response on the next time you hit the keyboard event handler and have a timer in there as well.

TerrorFLOP there is a problem with accessing the performance counter on mutli-core cpu's. They are highly inaccurate because they can give negative delta times on these kinds of cpus. This is because the function is not guaranteed to always access the same core.

Share this post


Link to post
Share on other sites
Thanks all.

It seems the recommended way to catch keyboard input is with DirectInput just as you use DirectDraw7 (yep, the old one!)for spriting and blitting. Without any timer delays, just the slightest feather tap on the spacebar, and you're firing 5-10 lasers. But the UP, DOWN, RIGHT and LEFT keys are just fine for moving my spaceship. They really move fast utilizing the hardware.

For the delay, I'm using : timeElapsed = (GetTickCount - oldTickCount).

I might try several other variations as you guys said, such as : For...Next loops and Timer.

In reply to one of the posters, I don't want to just fire one missile at a time. I'm giving an array of about 20 silmutaneous missiles max on the screen although I don't think the player can get up to this level.

My multi-missile routine also depends on getting the spacebar timing just right because I need to increase the missile counter if the previous missile hasn't reached the top of the screen.

Code:
-----
.
.
.
Case DIK_SPACEBAR

laser_y(LaserCounter%) = shp_y
laser_x(LaserCounter%) = shp_x
'my delay loop
If (GetTickCount - oldTickCount) > 10 Then LaserCounter% = LaserCounter% + 1
If LaserCounter% > 20 Then LaserCounter% = 1


End Select

For icount% = 1 to MAX_LASER_COUNT%
If laser_y(LaserCounter%) > 0 Then laser_y(LaserCounter%) = laser_y(LaserCounter%) - 1 'scroll up
Next icount%


For icount% = 1 to LaserCounter%
BltFast laser_x(icount%), laser_y(icount%), Laser....
Next icount%
.
.
.
'Flipping routines and GetCollission below
------------------------------------------

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezani
It seems the recommended way to catch keyboard input is with DirectInput just as you use DirectDraw7 (yep, the old one!)for spriting and blitting.
That's 100% wrong. DirectInput should NEVER be used for keyboard input - no exceptions whatsoever. If you want to do sprites, use D3D in orthographic mode - DirectDraw has no hardware acceleration, is harder to get up and running with (IMO), and hasn't been updated in years.

Share this post


Link to post
Share on other sites
Yes, you're right Steve -- but I'm limiting myself to 2D graphics and my favourite VB6. Juz trying to get back those old college memories when I was challenging my friends on Commando and Starship Troopers. I love it when you reach to the end of the level and have that big mothership trying to take you down.

But honestly Steve...I read your blog on DirectInput and I'm interested when you say DirectInput doesn't match the user's response rate set in Windows...what do you mean by this ? Is it related to my having problems with getting the multi-lasers to fire properly everytime the user hits the SPACEBAR ? How would you do it Steve...use KeyDown/KeyPress event ?

I would love to do 3D games but not now. That would mean a whole new ballgame, much more complex. I've already downloaded all the tools ... 3D Engine...Blender...terrain generator ... but 3D games just don't seem as fun as the 2D shoot-em-ups. And you're normally limited to role-playing and everyday sceneries. The thing I can't stand is the shooting accuracy in 3D games...I mean how would you know your bullet hits the target some distance away...in Doom, it always seemed you hit the target no matter how far they are...that turned me off...its not realistic!

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezani
Yes, you're right Steve -- but I'm limiting myself to 2D graphics and my favourite VB6.
He's not telling you to do 3D graphics. He's telling you to use Direct3D to do 2D graphics.

Share this post


Link to post
Share on other sites
Yep...I know DirectX7 is deprecated...but its way better than D3D (Direct8 and above) for 2D games. Much of the best 2d functionalities have disappeared in D3D (no I dunno why...go ask Microsoft) such as BltFast. DirectDraw is also way easier to use. Performance : great and fast!

I will go for DirectX10 and D3D when and if I decide to do 3D games but not at the moment...its way too complex!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!