Forum > General

Message loop event interaction ???

(1/3) > >>

LSE:
How can window message loop events communicate with each other ?

I have a window with several "action buttons" and events controlled by a message loop.

One event is an animation, which I would like to run in a repeat loop until a "Stop" button is pressed.

The animation event is running in a procedure a la:

REPEAT
...
...
UNTIL (???);

How can the UNTIL condition access window message loop to detect whether the "Stop" buttonhas been pressed (and then terminate) ?

Any help or hint would be much appreciated.
I am a little novice in the Windows concept of message loop and anarchistic user interaction (as opposed to old fashioned strict sequential control).

Regards, LSE


   

Handoko:
I'm not going to answer your question about message loop event interaction.

It seems you want to build an animation program or maybe game. Do you use Lazarus or Free Pascal only? If you don't use Lazarus, I suggest you to try it.

Nothing wrong with Free Pascal. But it will be easier for beginners to use Lazarus to create animations and games.

Here are some Lazarus examples showing how to do simple animations and games for you to download, test and study:

Sprite Demo by wp:
https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/examples/sprites/?root=lazarus

Simple Form Transition by Handoko:
http://forum.lazarus.freepascal.org/index.php/topic,37943.msg256549.html#msg256549

Moving Objects Moving2.zip by Handoko:
https://forum.lazarus.freepascal.org/index.php/topic,38136.msg263143.html#msg263143

Simple Pong Game by Handoko
http://forum.lazarus.freepascal.org/index.php/topic,42439.msg297949.html#msg297949

In Simple Pong Game you can learn:
- Use a TTimer as the game loop
- Use GetKeyState to detect user keyboard inputs
- How to pause/resume game if user switch to/from other program
- How to handle form resize

jamie:
Simple put don't do that...

Use a Ttimer and do that code within..

allow the event method block to exit.

lucamar:
Your question has little to do with events. Simplifying a lot, an event is something that happens generally from outside your application and to which it can respond through an event handler. For example, in your application an event  would be the pressing of a button and the response woud be in the OnClick event handler, something like TForm1.MyButtonClick(). The only way for events, message loops, etc. to have anything to do with your animation would be having an animation class that responds to events by itself.

Now to your question. It's difficult to answer without seeing your code since there are mny ways of doing animation but let's supposse you just show a series of pictures one after the other. Your animation class should have methods to start and stop it, something like:


--- Code: ---{ Ultra-simplified! }
type
  TAnimation = class
  protected
    StopNow: Boolean;
  public
    procedure Start;
    procedure Stop;
  end;

{. . .}

procedure TAnimation.Start;
{ Just an example!!!
  It will not return until terminated!}
begin
  StopNow := False;
  repeat
    {Do the animation thingy}
  until StopNow;
end;

procedure Stop;
begin
  StopNow := True;
end;
--- End code ---

As you can see, responding to "Play" and "Stop" buttons in your form is then very simple:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TMyForm.PlayButtonClick(Sender: TObject);begin  if not Assigned(Animation) then begin    Animation := TAnimation.Create;    { other initialzations of Animation,      like LoadFromStream, setting size, etc.}  end;  Animation.Start;end; procedure TMyForm.StopButtonClick(Sender: TObject);begin  if Assigned(Animation) then    Animation.Stop;end;
Yes, it's just an extremely simplified example but that's really all there is to it. The rest really depends on your other code, which we don't know.

HTH! :)

Handoko:
@LSE

I found it. Here is a demo I wrote to show a simple animation with pause/resume feature:

https://forum.lazarus.freepascal.org/index.php/topic,39790.msg274096.html#msg274096

Navigation

[0] Message Index

[#] Next page

Go to full version