Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2005-12

Coding Questions

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

VisionForce

I have some coding questions:

1. Is there a synchronous readkey function, instead of that asynchronous one? I'm trying to make a simple game to help me learn Pascal, but I got stuck on this. I know that Turbo Pascal 7 has the synchronous readkey, but isn't there a way to do this in normal Pascal?

2. How would I delay the movement of something? When I use Delay(250), it delays the whole program. In VB, I would write this:

Dim DelayAmount, DelayCount As Integer

DelayCount = 250

If (Environment.TickCount - DelayAmount) >= DelayCount Then
   ' run code here
   DelayAmount = Environment.Tick

End If

So what do I do? Oh, and I need this to be strictly Pascal if possible, not Delphi; and I'm using the Crt code, so it's a shell (command prompt style), not Windows forms.

Thanks,

Alex C. Barberi

Reply by email

Felipe Monteiro de Carvalho
to VisionForce
VisionForce wrote
1. Is there a synchronous readkey function, instead of that asynchronous one? I'm trying to make a simple game to help me learn Pascal, but I got stuck on this. I know that Turbo Pascal 7 has the synchronous readkey, but isn't there a way to do this in normal Pascal?

What exactly is the difference between synchronous readkey and asynchronous?

Are you trying to read keys from the keyboard?

2. How would I delay the movement of something? When I use Delay(250), it delays the whole program. In VB, I would write this:

Normally I would use a timer. Set it´s interval property to the desired delay, set it's execute event to point to a method and set it's Enable to True when needed.

On the execute method would be something like:

MyTimer. Enabled := False;
// Code to be executed

but I think this requires Lazarus LCL to be used.

If you want something to execute without halting the main program and can't use a Timer component, then you really should think about using a separate Thread from this.

Read: http://wiki.lazarus.freepascal.org/index.php/Multithreaded_Application_Tutorial

Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to VisionForce: I have some coding questions: 1. Is there a synchronous readkey function, instead of that…
VisionForce wrote 7 lines
Dim DelayAmount, DelayCount As Integer DelayCount = 250 If (Environment.TickCount - DelayAmount) >= DelayCount Then ' run code here DelayAmount = Environment.Tick End If

Maybe you can check TEpikTimer. I used it to get nanosecond resolution for a Digital Osciloscope project witch needed precise timing between the voltage measures.

Works great on both Linux and Windows, but requires Lazarus LCL.

To wait some time before continuing I write code using it like this:

 DelayInSeconds := 8; // or any value
 OldTime := ET.Elapse;
 while ((1000000.0 * (ET.Elapsed - OldTime)
  < DelayInSeconds)) do
 begin
   ET.SystemSleep(0);
 end;
  // DelayInSeconds has elapsed and now you can continue executing the code

You can find more information (including download) here: http://wiki.lazarus.freepascal.org/index.php/EpikTimer#Usage

I recently improved the component, and the updated version is available for download.

One particular issue with it is that the component is in the standard LGPL, witch prohibits static linking between proprietary and the component, but you can contact the author if this is a problem for you.

Felipe Monteiro de Carvalho

Reply by email

Tomas Hajny
to VisionForce: I have some coding questions: 1. Is there a synchronous readkey function, instead of that…

From: "VisionForce"
To: "FreePascal Developer"
Date sent: Thu, 1 Dec 2005 21:26:49 -0600
Subject: [fpc-devel] Coding Questions
Send reply to: FPC developers' list

Show quoted text 7 lines
I have some coding questions: 1. Is there a synchronous readkey function, instead of that asynchronous one? I'm trying to make a simple game to help me learn Pascal, but I got stuck on this. I know that Turbo Pascal 7 has the synchronous readkey, but isn't there a way to do this in normal Pascal?

Have a look at unit Keyboard (function PollKeyEvent).

2. How would I delay the movement of something? When I use Delay(250), it delays the whole program. In VB, I would write this:

.
.

So what do I do? Oh, and I need this to be strictly Pascal if possible, not Delphi; and I'm using the Crt code, so it's a shell (command prompt style), not Windows forms.

One possible cross-platform solution for this (certainly not the only one, but probably fitting quite well here) would be to use function GetMsCount from unit Dos. Regardless of the exact value returned by this call, the difference of values provided by two calls to this function should always be equal to the amount of milliseconds between the two calls (or at least as close to this as the implementation for a particular platform allows).

Tomas

Reply by email

VisionForce
to Felipe Monteiro de Carvalho: What exactly is the difference between synchronous readkey and asynchronous? Are you…

the readkey() function freezes the whole app until you press a key; this is async.

Earlier message 40 lines
----- Original Message ----- From: "Felipe Monteiro de Carvalho" To: "FPC developers' list" Sent: Saturday, December 03, 2005 5:12 AM Subject: Re: [fpc-devel] Coding Questions On 12/2/05, VisionForce wrote: > 1. Is there a synchronous readkey function, instead of that asynchronous > one? I'm trying to make a simple game to help me learn Pascal, but I got > stuck on this. I know that Turbo Pascal 7 has the synchronous readkey, but > isn't there a way to do this in normal Pascal? What exactly is the difference between synchronous readkey and asynchronous? Are you trying to read keys from the keyboard? > 2. How would I delay the movement of something? When I use Delay(250), it > delays the whole program. In VB, I would write this: Normally I would use a timer. Set it´s interval property to the desired delay, set it's execute event to point to a method and set it's Enable to True when needed. On the execute method would be something like: MyTimer. Enabled := False; // Code to be executed but I think this requires Lazarus LCL to be used. If you want something to execute without halting the main program and can't use a Timer component, then you really should think about using a separate Thread from this. Read: http://wiki.lazarus.freepascal.org/index.php/Multithreaded_Application_Tutorial -- Felipe Monteiro de Carvalho

Reply by email

Vincent Snijders
to VisionForce
VisionForce wrote
the readkey() function freezes the whole app until you press a key; this is async.

You cannot change the behaviour of readkey, but you can use the keypressed function to see if there is a key to be read.

Vincent.

Reply by email

Jonas Maebe
to VisionForce: the readkey() function freezes the whole app until you press a key; this is async. -----…
VisionForce wrote
the readkey() function freezes the whole app until you press a key; this is async.

No, that's synchronous. Asynchronous means that you ask for something to be performed and that control is immediately returned to you, while "something" is performed in the asynchronously with the rest of the flow of your program (instead of that the flow of your program is synchronised with the completion of that operation).

That said, there's always the keypressed function if you want to check first whether a key has been pressed before calling readkey.

Jonas

Reply by email

VisionForce
to Jonas Maebe

ok, thanks

Earlier message 28 lines
----- Original Message ----- From: "Jonas Maebe" To: "FPC developers' list" Sent: Saturday, December 03, 2005 1:03 PM Subject: Re: [fpc-devel] Coding Questions > > On 03 Dec 2005, at 19:44, VisionForce wrote: > >> the readkey() function freezes the whole app until you press a key; >> this is async. > > No, that's synchronous. Asynchronous means that you ask for something > to be performed and that control is immediately returned to you, > while "something" is performed in the asynchronously with the rest of > the flow of your program (instead of that the flow of your program is > synchronised with the completion of that operation). > > That said, there's always the keypressed function if you want to > check first whether a key has been pressed before calling readkey. > > > Jonas > _______________________________________________ > fpc-devel maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-devel >

Reply by email