|
-
Aug 19th, 2000, 11:43 PM
#1
Thread Starter
Addicted Member
Hiyas,
Are there any alternatives other than a normal Timer?
I want something really fast, something faster than one millisecond (or whatever the interval is measured in).
Thanks,
-Git
-
Aug 20th, 2000, 12:33 AM
#2
Thread Starter
Addicted Member
Argh...
I've written a game here which was running using a timer. It uses a time system - you can set the speed to seconds, minutes, hours or days.
If you hit, for eg., days, it'll just add 1 to the D/M/Y display.
However, I've just realised when events occur, if they're supposed to happen between one hour and another hour, they will really happen at the other hour, after it's supposed it.
I thought about making the interval 0.01 milliseconds, and having it so it's always affecting all the time types, and then just have it so when you click the different settings (sec, min, hrs, days) it just makes the interval longer/shorter.
But, I can't set it to under 1 in the timer interval.
Does anyone have any ideas as to what I can do?
Btw I would've posted this in the Games section but I figured it's more of a general VB topic...
Thanks.
-Git
-
Aug 20th, 2000, 12:34 AM
#3
you can use these API's to do it
Code:
Private Declare Function QueryPerformanceCounter Lib "kernel32" Alias "QueryPerformanceCounter" (lpPerformanceCount As LARGE_INTEGER) As Long
Private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
you can use either GetTickCount or QueryPerformanceCounter.
GetTickCount gets to the millisecond.
QueryPerformanceCounter gets it to the microsecond.
to use QueryPerformanceCounter you need to do some simple math.
Code:
Dim liVariable As LARGE_INTEGER
Dim dblResult As Double
Call QueryPerformanceCounter(liVariable)
dblResult = liVariable.lowpart + (liVariable.highpart * (2 ^ 32))
the Return Value of QueryPerformanceCounter is a 64 bit number, so you need 2 32 bit numbers(Long) to make it 64, since the lowpart is 2 ^ 0 through 2 ^ 31 it can stay just where it is, but the highpart is the second 32 bits of the number which is 2 ^ 32 through 2 ^ 64 so you have to multiply the highpart by 2 ^ 32(which just happens to be 4294967296 but who wants to write that out all the time?) and add them(low part and high part) together, that gives you your 64bit number...
why MS didnt just change the Return value to a Double, i dont know....
I would write the code for the timer too, but I am so damn tired
if anybody thinks I got this wrong, please correct me.... I am so damn tired.
[Edited by denniswrenn on 08-20-2000 at 01:37 AM]
-
Aug 20th, 2000, 12:38 AM
#4
Thread Starter
Addicted Member
Thanks for the quick reply.
I'm not really sure how to use these commands though... how exactly would I go about implementing it? Anyone?
-
Aug 20th, 2000, 12:51 AM
#5
hehehe, that wasnt quick!
that was a response to your first question 
it just took me that long to type it up, because I am so tired,
I think you can store the first call to QueryPerfoetc in a variable, then continuously(with some kind of loop) compare that to the current time until the value ** milli/micro seconds apart.
like I said I am very tired and can not even see straight right now... so I dont know if that will lock up VB if you try it.. or etc.....
-
Aug 20th, 2000, 09:58 AM
#6
Try this.
Code:
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim bTime As Boolean
Sub TimerEx(Interval As Long)
Do Until bTime = False
Start = GetTickCount
Do While GetTickCount < Start + Interval
DoEvents
Loop
'<--Place Code here->
Print "Hello"
Loop
End Sub
Private Sub Command1_Click()
'Start the Timer
bTime = True
Call TimerEx(1000)
End Sub
Private Sub Command2_Click()
'Exit the Timer
bTime = False
End Sub
-
Aug 20th, 2000, 01:45 PM
#7
transcendental analytic
HEre's how to use Querryperformance counter:
Code:
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Dim Start as Currency, Finish as Currency, Freq as Currency
QueryPerformanceCounter start
'Your dealy
QueryPerformanceCounter finish
QueryPerformanceFrequency freq
MsgBox CDbl(finish - start) / CDbl(freq) * 1000000
It returns the result in milliseconds but with the decimal it's accurate up to 1 microsecond.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 20th, 2000, 01:55 PM
#8
I didnt know you could change datatypes in API Calls!
-
Aug 20th, 2000, 02:44 PM
#9
transcendental analytic
Of course you can, you can change pretty much everything, in the calls, but they have to match the arguments, Currency matches LARGE_INTEGER in the amount of memory.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 20th, 2000, 03:55 PM
#10
cool
-
Aug 20th, 2000, 04:03 PM
#11
Frenzied Member
It doesn't always have to match the amount of memory, as long as you can track what memory it's going to put where, for example I often use the ScreenToClient API to change the coords of a RECT so it's relitive to a particular window
Code:
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ScreenToClient Lib "user32" Alias "ScreenToClient" (ByVal hwnd As Long, lpPoint As Long) As Long 'Note lpPoint is a long
Private Sub RectToClient(hWnd As Long, ChangeRect As RECT)
Call ScreenToClient(hWnd,ChangeRect.Left)
Call ScreenToClient(hWnd,ChangeRect.Right)
End Sub
The SCreen to client function takes a pointer to a pointAPI structure, but we change this to a pointer to a long then we pass it to the API, it thinks it's a pointer to a POINTAPI, so alters that long and the next long in memory, so we can do this for the first 2 longs (Left,Top) and the last to (Right,Bottm)
No problem.
ByVal parameters however must stick to the same amount of memory.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|