English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old Today, 03:01   #1
NovaCoder
Registered User
 
NovaCoder's Avatar
 
Join Date: Sep 2007
Location: Melbourne/Australia
Posts: 4,452
Getting millisecond elapsed time using Bebbo's Amiga GCC toolchain

Hi,

Wonder if anyone can help.

I need a fast way to tack how much time has passed in my main loop.

I normally use https://www.tutorialspoint.com/c_sta...tion_clock.htm which has a high frequency on the old GCC 3.4 (milliseconds) but in my Bebbo build it seems to be limited to 50fps.

I've hacked together the following code but I'm not really sure it's the best way to do it.

Code:
	gettimeofday(&lastFrameTime, NULL);

	while (true) {
		gettimeofday(&currentFrameTime, NULL);

	    double elapsedtime = (currentFrameTime.tv_sec - lastFrameTime.tv_sec) * 1000.0;      // sec to ms
	    elapsedtime += (currentFrameTime.tv_usec - lastFrameTime.tv_usec) / 1000.0;          // us to ms


		// Capture user input and update object positions.
		g_world->updateWorld(elapsedtime / 1000);
		g_world->updateFrame();


		memcpy(&lastFrameTime, &currentFrameTime, sizeof(struct timeval));
	}

Thanks
NovaCoder is offline  
Old Today, 21:51   #2
Ernst Blofeld
<optimized out>
 
Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 323
I don't have a lot of context, but if you're limited to 50 fps, maybe you're using the wrong CIA timer in your gettimeofday?

Anyway, in case it helps, this is how I do it, having taken over the system:

Code:
ULONG GetCurrentTime(void) {
    ULONG hi = ciab->ciatodhi;		// latch activated
    ULONG mid = ciab->ciatodmid;
    ULONG low = ciab->ciatodlow;	// latch deactivated

    return ((hi << 8 | mid) << 8 | low) << 8;
}
Code:
    Display_Show();

    game.isPaused = FALSE;

    ULONG previousTime = GetCurrentTime();
    ULONG lag = 0;

    Game_UpdateDisplay();

    for (;;) {
        ULONG currentTime = GetCurrentTime();
        ULONG elapsedTime = currentTime - previousTime;
        ULONG fps = (256 * CLOCK_CONSTANT) / (227 * elapsedTime);
        previousTime = currentTime;
        lag += elapsedTime;

        ULONG ticks = 0;
        while (lag >= TIMER_INCREMENT_PER_SIMULATION_STEP) {
            Game_ProcessUserInput();
			
            if (game.exitRequested)
                break;

                Game_UpdateState();
                lag -= TIMER_INCREMENT_PER_SIMULATION_STEP;
                ticks++;
            }
		
            if (game.exitRequested)
                break;

            Game_UpdateDisplay();

            TinyText_MoveTo(80 - 8, 0);
            TinyText_PrintFormatted("%4ld fps", fps);

            TinyText_MoveTo(80 - 8, 1);
            TinyText_PrintFormatted("%4ld ipf", ticks);
    }

    game.isPaused = TRUE;

    PrintStatistics();
with this bit when I take over the system:

Code:
    ciab->ciacrb &= ~CIACRBF_ALARM;
Ernst Blofeld is offline  
Old Today, 22:22   #3
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,447
Quote:
Originally Posted by NovaCoder View Post
I need a fast way to tack how much time has passed in my main loop.
Is that on Amiga or on *ix? On *ix, you have the clock function (as stated) with various clocks offering different resolutions. You also have gettimeofday() which has milliseconds precision which is in most cases precise enough. On Amiga, you have the ReadEClock() function of the timer.device (see its autodocs), which offers milliseconds resolution, though is not very precise over a very long time.



No, I do not recommend accessing the hardware. Reading the E-clock has certain quirks the Os takes care of.
Thomas Richter is online now  
 


Currently Active Users Viewing This Thread: 3 (2 members and 1 guests)
Thomas Richter, peceha
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting up a debugging environment for Bebbo's Amiga GCC toolchain Nightfox Coders. C/C++ 3 12 July 2024 19:48
GCC 6.2 toolchain for AmigaOS 3 Samurai_Crow Coders. C/C++ 1555 01 May 2024 19:56
Can't make bebbo's amiga-gcc work Bren McGuire Coders. General 18 12 June 2023 01:09
bebbo gcc+eclipse problem Raislin77it Coders. C/C++ 1 15 February 2022 08:37
gcc-bebbo: argc is always 0 sparhawk Coders. C/C++ 8 31 January 2021 18:42

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 22:24.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.11103 seconds with 15 queries