473,769 Members | 6,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting more precise timing and telling if a key is pressed - how?

I've had two issues plaguing me for 4 months now and I haven't gotten
anywhere. I'm into making 2D games and these things are essential to games.
These are my issues:

1. I need timing precise to about 4 milliseconds at most. Currently, I've
been using the clock() function, but it's only precise to 1/64 second (or
about 15.6 milliseconds). For those running their monitor at 60 Hz, this
isn't much of a problem, but for those running above 64 Hz (70 and 75 are
common), this would mean either processing two frames in one causing jerky
motions, or running at half the refresh rate. By using something with 4
milliseconds precision, up to 250 Hz works and the highest I've seen is 160
Hz. There's another one with 1/4 microsecond precision, but that's not
supported by every system and it's far more than I need. How do I get the 4
millisecond timing I'm after?
2. Lastly, how do I tell if a key is pressed or not. That is, if I press
the right arrow key, I expect the character to move right. I know how to
program the movement (and practically everything else in a 2D game like an
RPG), but I don't know how to tell if a key is pressed or not.

These are my requirements:
1. It must work with systems as old as Windows 95.
2. It must not use DirectX. If it is required for either of these, then
something that works with Windows 95. I strongly prefer going without
DirectX though.

I'm using Visual C++ 2005 Express if that helps.

--
Some programming:
void bluewater() {while(game_run s == 1)
{if fox.position == in_blue_water) {fox.health -= 1;}
else {fox.wants_to_g et_wet = true;} wait_frames(1); }}
Apr 6 '07 #1
5 1749
In article <10************ *************** *******@microso ft.com>,
=?Utf-8?B?dWxpbGxpbGx pYQ==?= <ul********@dis cussions.micros oft.comwrote:
>1. I need timing precise to about 4 milliseconds at most.
Use QueryPerformanc eFrequency/QueryPerformanc eCounter. See
http://msdn.microsoft.com/library/de...ncecounter.asp
for more info. Works all the way back to Win95.
>2. Lastly, how do I tell if a key is pressed or not. That is, if I press
the right arrow key, I expect the character to move right. I know how to
program the movement (and practically everything else in a 2D game like an
RPG), but I don't know how to tell if a key is pressed or not.
Tried GetAsyncKeyStat e()? See
http://msdn.microsoft.com/library/de...nckeystate.asp

>These are my requirements:
1. It must work with systems as old as Windows 95.
Do you have a *really* good reason for doing so, other than you
can't get some grandparent to upgrade? When MS doesn't support Win98,
Win95 support is asking for pain.
>2. It must not use DirectX. If it is required for either of these, then
something that works with Windows 95. I strongly prefer going without
DirectX though.
Read the links I posted above. They document what they need in
terms of OS, etc. I do feel that using DirectInput, even an early
version, is probably going to be easier and more reliable than using
GetAsyncKeyStat e().

Nathan Mates
--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Apr 6 '07 #2
That's the same timer thing I mentioned in the first. What bothers me about
using it is the "return value" section. Hardware supporting it? How common
are these high-res timers?

As to the GetAsyncKeyStat e thing, I've looked into it before, but didn't
know what values I needed to use for the "vKey" thing. I have the solution
for that and this is exactly what I was after for that.

All I need now are more details about the timers and how common they are (my
system supports them, but that doesn't mean all systems do). One last thing
I thought of, but not required - how do you switch to fullscreen mode and
back to windowed mode?

The reason for targeting Windows 95 is due to the high simplicity of my
early intended games/programs. 2D at 640x480 renders very fast. From tests
using another tool, it hardly used any of the CPU to display the graphics, so
little that even after an hour at running the poorly designed code (due to
limits with the tool), only 7 seconds or so of the CPU time was used and
that's with a rather outdated 3 GHz Pentium 4 processor. Windows 95 systems
would likely have a 200 MHz processor or so and the game would still not be
"hard" on it. With C, I can get a much better design so it could run even
faster.

--
Some programming:
void bluewater() {while(game_run s == 1)
{if fox.position == in_blue_water) {fox.health -= 1;}
else {fox.wants_to_g et_wet = true;} wait_frames(1); }}
"Nathan Mates" wrote:
In article <10************ *************** *******@microso ft.com>,
=?Utf-8?B?dWxpbGxpbGx pYQ==?= <ul********@dis cussions.micros oft.comwrote:
1. I need timing precise to about 4 milliseconds at most.

Use QueryPerformanc eFrequency/QueryPerformanc eCounter. See
http://msdn.microsoft.com/library/de...ncecounter.asp
for more info. Works all the way back to Win95.
2. Lastly, how do I tell if a key is pressed or not. That is, if I press
the right arrow key, I expect the character to move right. I know how to
program the movement (and practically everything else in a 2D game like an
RPG), but I don't know how to tell if a key is pressed or not.

Tried GetAsyncKeyStat e()? See
http://msdn.microsoft.com/library/de...nckeystate.asp

These are my requirements:
1. It must work with systems as old as Windows 95.

Do you have a *really* good reason for doing so, other than you
can't get some grandparent to upgrade? When MS doesn't support Win98,
Win95 support is asking for pain.
2. It must not use DirectX. If it is required for either of these, then
something that works with Windows 95. I strongly prefer going without
DirectX though.

Read the links I posted above. They document what they need in
terms of OS, etc. I do feel that using DirectInput, even an early
version, is probably going to be easier and more reliable than using
GetAsyncKeyStat e().

Nathan Mates
--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Apr 6 '07 #3
I don't know how to edit my previous message, but I thought of a potential
problem with the high-res timer. If I don't use sleep(), the CPU usage would
go to 100%. Sleep is only precise to 1/64 second. What other alternative
can I use for this?
Apr 6 '07 #4
In article <C9************ *************** *******@microso ft.com>,
=?Utf-8?B?dWxpbGxpbGx pYQ==?= <ul********@dis cussions.micros oft.comwrote:
>That's the same timer thing I mentioned in the first. What bothers
me about using it is the "return value" section. Hardware
supporting it? How common are these high-res timers?
For QueryPerformanc eCounter(), the high-res timers are usually from
the RDTSC assembly instruction. Intel was the first one to add them,
and they did so in the Pentium 1 (the original, which also had that
FDIV) bug. Non-Intel brands added this instruction eventually; see
http://en.wikipedia.org/wiki/RDTSC for more info. I'd guess that
anything made in the last 5 years probably supports it fine. But,
you're trying to target OSs nearly 12 years old, which means it can
probably run on HW 14+ years old.

If you read the docs on QPC(), MS does the right thing and falls
back to less-precise timers if RDTSC instruction isn't present. Which
means you can ship your title, and recommend that people to get with
the 21st century if it doesn't work.
>One last thing I thought of, but not required - how do you switch to
fullscreen mode and back to windowed mode?
Give up your foolish tilting at windmills and use DirectX,
already. That's what it's built to do. DX3 did this fine. You don't
have do use DX9.
>The reason for targeting Windows 95 is due to the high simplicity of
my early intended games/programs.
You're mistaking simplicity of your system requirements with
simplicity of OS. How many people *realistically* are out there with
Win95? And able to download/acquire/run your code on such a system?
Just because you can do something doesn't mean you *should* do
that. Make your game run w/ very low system requirements-- that's
good. Making yourself jump thru a lot more hoops to support a handful
of users -- that's pointless. Just because you "could" mow your lawn
with a elementary-school safety scissors doesn't mean you
should. Newer OSs and APIs like DirectX give you the power tools to
achieve your ends much faster.

Nathan Mates
--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Apr 6 '07 #5
In article <B6************ *************** *******@microso ft.com>,
=?Utf-8?B?dWxpbGxpbGx pYQ==?= <ul********@dis cussions.micros oft.comwrote:
>I don't know how to edit my previous message, but I thought of a
potential problem with the high-res timer. If I don't use sleep(),
the CPU usage would go to 100%. Sleep is only precise to 1/64
second. What other alternative can I use for this?
You really seem to be obsessed over things that are contradictory
and meaningless. First, you're obsessed about running on obsolete and
unsupported OSs like Win95, simply because you can. You want good
timers, but you're worried about pegging the CPU at 100%. Well, if
you're going to get any sort of smooth animation, etc, you *DO* want
to peg the CPU. Please, go to websites like http://www.gamedev.net/ or
http://www.flipcode.com/ where people have talked over how to program
games for Windows.

As to keeping your game running, basically, you should do something
like what I posted years ago, see
http://groups.google.com/group/comp....1c85bcd731aea5

void GameMainLoop(vo id)
{
// Local varbs snipped.

while(KeepRunni ng)
{
// Process all windows messages
while (PeekMessage(&m sg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return; // We're done.
TranslateMessag e(&msg);
DispatchMessage (&msg);
}

// Functions in the rest of your code to do one frame's worth of work.
// You may have slightly different names.
BeginFrame();
UpdateEverythin g();
DrawEverything( );
EndFrame();
}

}
--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Apr 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
17755
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
6
1822
by: S. David Rose | last post by:
Hello All! I am new to Python, and wanted to know if I might ask you a question regarding timing. I want a main loop which takes photos from 8 different web-cams, each can be addressed by http://ip-addr/image.jpg. That's the easy part, but I want to have 2 cameras poll 3-times a second, 4 cameras poll 2 times a second, and the remaining 2 cameras poll once a second. I have found lots of info suggesting the use of threads for this, all...
8
8850
by: Harlin Seritt | last post by:
I have the following script. Two widgets call the same function. How can I tell inside of the called function which button called it?: def say_hello(): print 'hello!' print widget root = Tk() button1 = Button(root, text='Button 1', command=say_hello) button1.pack()
3
13062
by: Lachlan Hunt | last post by:
Hi, I've been looking up lots of documentation and trying to work out a cross-platform method of capturing a keyboard event and working out which key was pressed. From what I can find, there doesn't appear to be any standardised keyboard event interface other than this DOM 3 Events W3C Working Group Note . However, it is only a note and doesn't appear to be implemented in any browser. Is there another standard that I've missed? The...
8
1355
by: Madhusudan Singh | last post by:
Hi I am using time.clock() to get the current time of the processor in seconds. For my application, I need really high resolution but currently seem to be limited to 0.01 second. Is there a way to specify the resolution (say 1-10 microseconds) ? My processor is a 1.4 MHz Intel processor. Surely, it should be able to report times a few (or at least 10) microseconds apart. Thanks.
3
2397
by: pbbriggs | last post by:
I will try to be as descriptive as possible, but this error has me stumped and I am not sure what relevant info to include.... I am running Access XP on a Windows XP machine. I initially began developing an app in Access 2000 but converted it several months ago to XP. So the app is now Access XP format also. My app is crashing on a regular basis, with the totally non-descript Microsoft error (Microsoft Access has encountered a...
2
3307
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy to use from the command line, but it is less convenient for timing large pieces of code or when working in the interactive interpreter. E.g. variations on this *don't* work: $ python Python 2.4.3 (#1, Jun 13 2006, 11:46:08)
9
3033
by: phopman | last post by:
Hello! I am currently working on a project. And have been assigned to get up to speed quickly on php. And even though I love the language, it's not easy to get up to speed in like 2 seconds :-) The code I will list below is supposed to have four parts: 1. Not logged in. List up all text from the database. 2. Logged in. List up all text from the database. But now with an "edit" button, with the possibility to jump to number 3. 3. Logged...
30
521
by: Ashit Vora | last post by:
Hi, I want to find the current time stamp in micro sec precision. The requirement is, my function is executed in an infinite loop. I measure a value and check if the value computed is what i expected 9forget all this stuff). I want to print the time stamp every time that event occurs. So my requirement is printing the current timestamps.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.