Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Refresh(), Paint(Draw& w), or something else nearby?
Refresh(), Paint(Draw& w), or something else nearby? [message #10381] Wed, 04 July 2007 20:58 Go to next message
kverko is currently offline  kverko
Messages: 8
Registered: December 2005
Location: Porva, Hungary
Promising Member
Hi!

I have coded a little cell automat program. When I have put the step to the next cycle into a left click event handler, it did work. Now I use the SetTimeCallback function from the AnimatedHello example and it does work.
What I can no manage is to put the cycle control into the execution flow. For example I did:
function Steps()
{
  for(int i=0;i<1000;i++)
  {
    Step();
  }
}
function Step()
{
  Refresh();
}

And it does not work. The Step function is the same as in the SetTimeCallback! (Or in LeftDown.) The counting of next generation is in the Paint(Draw& w) function.

I am afraid of the counting the next generation could be longer than the timing of callback on a slower machine. Anyway I can not see, why does not work the simple code above. Or how other way can I set up the cycle control?
Re: Refresh(), Paint(Draw& w), or something else nearby? [message #10390 is a reply to message #10381] Thu, 05 July 2007 12:07 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
The reason that this doesn't work is that Refresh doesn't call Paint immediately. Paint will only get called once and only after your code has finished.

One solution:
void Step() 
{
   bool quit = false;
   Refresh();
   ProcessEvents(&quit); // Force Refresh event to be executed
   // If quit is true here then exit the app
}

Thie would draw each step as it is generated. I would advise against having the cell update code in Paint though.

And to fix the the TimerCallback problem of the step taking longer than the callback, if you change the timing from a negative to a positive number then you have to re-issue the callback every time it executes. This means you could complete the calculation of the next generation before you set the callback:
void OnTimer()
{
   UpdateCells();
   Refresh();
   SetTimerCallback(40, THISBACK(OnTimer));
}


James

[Updated on: Thu, 05 July 2007 12:19]

Report message to a moderator

Re: Refresh(), Paint(Draw& w), or something else nearby? [message #10396 is a reply to message #10390] Thu, 05 July 2007 21:15 Go to previous messageGo to next message
kverko is currently offline  kverko
Messages: 8
Registered: December 2005
Location: Porva, Hungary
Promising Member
Thank for the response. I suspected some unknown property in the background.

The callback re-issue is good idea, it works well.
But the ProcessEvents did not help. I could not take timer out of order.

Here is the code in the actual test state.
  • Attachment: cyclic.zip
    (Size: 2.90KB, Downloaded 308 times)
Re: Refresh(), Paint(Draw& w), or something else nearby? [message #10401 is a reply to message #10396] Fri, 06 July 2007 12:02 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
The problem is that you are running the loop from the window's contructor, but at that point the window hasn't been shown yet so Paint will not be called.

If you add the code:
	virtual bool Key(dword key, int count) {
		if (key == K_SPACE) {
			for(int i=0;i<1000;i++)
			{
		    	step();	
			}	
			return true;	
		}
		return false;
	}

Then pressing the space bar runs the loop correctly, repainting after every calculate(). Also you could speed up your Paint routine using an ImageBuffer (In calculate put the pixels straight into an ImageBuffer, then create an Image and paint that instead).

Nice program though, it makes pretty pictures Razz

James
Re: Refresh(), Paint(Draw& w), or something else nearby? [message #10405 is a reply to message #10401] Fri, 06 July 2007 16:19 Go to previous message
kverko is currently offline  kverko
Messages: 8
Registered: December 2005
Location: Porva, Hungary
Promising Member
You are right. I had some diffuse bad feeling about the control flow, but could not catch the cause.
Thanks again.

The cellular automaton is from David Griffeath (University of Wisconsin).
Previous Topic: Using InstallKeyHook
Next Topic: Missing Functions for Layouter
Goto Forum:
  


Current Time: Sun Apr 28 22:16:32 CEST 2024

Total time taken to generate the page: 0.03467 seconds