[gtkmm] Re: How to do call a function once every 'frame'? And some documentation feedback.



Leslie Polzer wrote:

On Sat, 09 Nov 2002 14:29:58 +0100
Dirk Gerrits  wrote:


>This little piece of documentation tells you ABSOLUTELY NOTHING that the
>function signature doesn't tell you.

Yes it does; it even gives you an hint: "you want to use SignalIdle::connect()."
I strongly suggest you re-read the tutorial section on signals.

I think having read that tutorial section is a DISadvantage here. I know what connecting to a signal means, so why would I click the link to SignalIdle::connect()? If I hadn't read about signals, I might have clicked on it and then I would have found the documentation I wanted to find.

So I stand corrected. It still doesn't tell you anything, but it does link to the text that does. Perhaps the wording could be improved though. How about "for more information see: ...::connect()" instead of "you want to use ...::connect()"?

Yet the classes you mentioned are the wrong approach to this problem. The idle handler is very unreliable and the MainLoop is something quite different.

How should I know that?

The right class for your purpose is Glib::signal_timeout()

See http://gtkmm.sourceforge.net/gtkmm2/docs/reference/html/classGlib_1_1SignalTimeout.html
where you can find a quite detailed description on how to use it.
In your case you may probably (I can't know; see below) want something like that:

//---

void myFrameFunc()
{
     DoFrame();
}

int main(int argc, char** argv)
{
	// init stuff for Gtk::Main...
	// [...]

	//call frame function every 1000 milliseconds = 1 second
 	Glib::signal_timeout().connect(SigC::slot(&myFrameFunc), 1000);

	//now run the main loop...
	// [...]

	return 0;
}

//---


>The tutorial is very good IMHO, but it does not tell you everything.
>That's what the reference documentation is supposed to do, right?

Yes, but you have to connect both of them.


>So can anyone who knows gtkmm2 well enough that he doesn't need the
>reference tell me how I would accomplish my once every 'frame' function?

What's a 'frame' for you? An animated GIF may have 5 frames; Quake3 may have 40 frames on your graphics adapter. Please be more specific.

Yes I'm sorry. I didn't really express myself clearly. What I need to do, is run some kind of computation at a fixed rate, say once every 50 ms (20 times per second). With my Win32 example, my DoFrame function would have to calculate how many computational 'clock ticks' have passed and then run the computation that many times: (pseudo)

const unsigned int ms_per_tick = 50;

void DoFrame()
{
    static unsigned int last_time = get_current_time_in_ms();

    unsigned int current_time = get_current_time_in_ms();
    unsigned int delta_time = current_time - last_time;
    last_time = current_time;

    while (delta_time > ms_per_tick)
    {
        DoComputation();

        delta_time -= ms_per_tick;
    }
}

But if I understand correctly, with Glib::signal_timeout() the whole DoFrame function becomes unnecessary:

Glib::signal_timeout().connect(SigC::slot(&DoComputation), ms_per_tick);

That's just plain cool! :) Thanks.

Dirk






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]