Archived

This topic is now archived and is closed to further replies.

okouya

c coders

Recommended Posts

i''m trying to translate this code in delphi but i failed because i don''t know c++ if someone can help me to do it in delphi it would be so good it''s a class for a timer so if you can help me giving me some explanation i thanks you bafore. **************************************************************************** HiResTimer.h Wrapper for the high-resolution timer. Can''t be used if the hi-res timer doesn''t exist. Author : Dave Astle Date : 2/1/2001 Written for OpenGL Game Programming *****************************************************************************/ #ifndef __TIMER_H_INCLUDED__ #define __TIMER_H_INCLUDED__ #include <windows.h> class CHiResTimer { public: CHiResTimer() {} ~CHiResTimer() {} /***************************************************************************** Init() If the hi-res timer is present, the tick rate is stored and the function returns true. Otherwise, the function returns false, and the timer should not be used. *****************************************************************************/ bool Init() { if (!QueryPerformanceFrequency(&m_ticksPerSecond)) { // system doesn''t support hi-res timer return false; } else { QueryPerformanceCounter(&m_startTime); return true; } } // end Init() float GetElapsedSeconds(unsigned long elapsedFrames = 1) { static LARGE_INTEGER s_lastTime = m_startTime; LARGE_INTEGER currentTime; QueryPerformanceCounter(¤tTime); float seconds = ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart) / (float)m_ticksPerSecond.QuadPart; // reset the timer s_lastTime = currentTime; return seconds; } // end GetElapsedSeconds() /*************************************************************************** GetFPS() Returns the average frames per second over elapsedFrames, which defaults to one. If this is not called every frame, the client should track the number of frames itself, and reset the value after this is called. ***************************************************************************/ float GetFPS(unsigned long elapsedFrames = 1) { static LARGE_INTEGER s_lastTime = m_startTime; LARGE_INTEGER currentTime; QueryPerformanceCounter(¤tTime); float fps = (float)elapsedFrames * (float)m_ticksPerSecond.QuadPart / ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart); // reset the timer s_lastTime = currentTime; return fps; } // end GetFPS /*************************************************************************** LockFPS() Used to lock the frame rate to a set amount. This will block until enough time has passed to ensure that the fps won''t go over the requested amount. Note that this can only keep the fps from going above the specified level; it can still drop below it. It is assumed that if used, this function will be called every frame. The value returned is the instantaneous fps, which will be <= targetFPS. ***************************************************************************/ float LockFPS(unsigned char targetFPS) { if (targetFPS == 0) targetFPS = 1; static LARGE_INTEGER s_lastTime = m_startTime; LARGE_INTEGER currentTime; float fps; // delay to maintain a constant frame rate do { QueryPerformanceCounter(¤tTime); fps = (float)m_ticksPerSecond.QuadPart/((float)(currentTime.QuadPart - s_lastTime.QuadPart)); } while (fps > (float)targetFPS); // reset the timer s_lastTime = m_startTime; return fps; } // end LockFPS() private: LARGE_INTEGER m_startTime; LARGE_INTEGER m_ticksPerSecond; }; #endif // __TIMER_H_INCLUDED_ love and peace

Share this post


Link to post
Share on other sites
I won't do all the work for you, but here's some pointers:

The QueryPerformanceCounter and QueryPerformanceFrequency are functions in the Win32 API (In Delphi look under Help->Windows SDK). I would suggest you look them up first and see what they do and what parameters they take.

Next start converting the C++ code to Delphi, this isn't as hard as it sounds as Delphi and C++ have very similar structures, just different syntax. Heres a table to help you:

C++ Delphi
--- ------
{ begin
} end
! not
& @
unsigned long LongWord
LARGE_INTEGER Int64
float Single
hard easy


You'll also notice a difference in how functions are declared. Rather than function
function MyFunction(ParmaName: ParamType):ReturnType     

you have
ReturnType MyFunction(ParamType ParamName1)     

and rather than setting the value of Result and then exiting the function you use return , which returns a value and imediately exits the function.

EDIT: Silly smileys

EDIT: Die useless smileys!

EDIT: PS. In this fine example of coding (kissing up to Myopic Rhino), the function implementation is inside the declaration, rather than in a seperate section, so the entire source you have is one class declaration (All the static variables are fields in the class as well as the variables declared after private:

[edited by - Michalson on May 5, 2002 11:50:01 AM]

[edited by - Michalson on May 5, 2002 11:50:48 AM]

[edited by - Michalson on May 5, 2002 11:55:12 AM]

Share this post


Link to post
Share on other sites