Recent

Author Topic: fptimer bug?  (Read 566 times)

lazarusprogrammer

  • New Member
  • *
  • Posts: 21
fptimer bug?
« on: May 29, 2026, 05:49:09 pm »
Hi, i am trying to use fptimer as a "game tick". But the problem i got is that i can't print char. If i do write('@'); then it prints the string but if i do write(chr(4)); then it prints nothing. It's literally nothing not even chr(0) just like write command were ignored;


Code: Pascal  [Select][+][-]
  1. program project1;
  2. uses crt, fptimer;
  3. type
  4. TEvent = object
  5. procedure TIMER_EVENT(Sender: TObject);
  6. end;
  7.  
  8. var
  9. GAME_TICK : TFPTimer;
  10. EVENT     : TEvent;
  11.  
  12. procedure CREATE_TIMER;
  13. begin
  14. GAME_TICK                := TFPTimer.Create(nil);
  15. GAME_TICK.Enabled        := false;
  16. GAME_TICK.UseTimerThread := true;
  17. GAME_TICK.Interval       := 1000;
  18. GAME_TICK.OnTimer        := @EVENT.TIMER_EVENT;
  19. GAME_TICK.Enabled        := true;
  20. end;
  21. procedure DESTORY_TIMER;
  22. begin
  23. GAME_TICK.Enabled := False;
  24. GAME_TICK.Free;
  25. end;
  26.  
  27. procedure TEvent.TIMER_EVENT(Sender: TObject);
  28. begin
  29. //Writeln('1');
  30. WriteLn(Chr(3));
  31. end;
  32.  
  33. begin
  34. CREATE_TIMER;
  35. ReadLn;
  36. DESTORY_TIMER;
  37. end.
  38.  
  39.  
« Last Edit: May 29, 2026, 10:49:53 pm by lazarusprogrammer »

Nimbus

  • Jr. Member
  • **
  • Posts: 89
Re: fptimer bug?
« Reply #1 on: May 29, 2026, 06:03:19 pm »
Hello. What would you expect it to print for Chr(3) ?
Are you aware of the control characters, which are non-printable by nature?

https://www.ascii-code.com/characters/control-characters

lazarusprogrammer

  • New Member
  • *
  • Posts: 21
Re: fptimer bug?
« Reply #2 on: May 29, 2026, 08:32:35 pm »
Hello. What would you expect it to print for Chr(3) ?
Are you aware of the control characters, which are non-printable by nature?

https://www.ascii-code.com/characters/control-characters

Maybe i am using wrong word i mean print intro console or write intro console.

Write(Chr(3)); should print a heart symbol and number doesn't matter it wont print any char.

Write(Chr(3)); are working without timer and writing the heart symbol intro console but if i use timer it prints nothing like null.

Lansdowne

  • New Member
  • *
  • Posts: 41
Re: fptimer bug?
« Reply #3 on: May 29, 2026, 10:05:17 pm »
WriteLn('@') and WriteLn(Chr(64)) should produce the identical result.

SO why don't you use Chr(64), or Chr(48+3) which gives the digit '3', make sure that works, then find out the correct way to produce the non-ASCII symbols you are wanting to display.

Thausand

  • Hero Member
  • *****
  • Posts: 560
Re: fptimer bug?
« Reply #4 on: May 29, 2026, 10:08:00 pm »
Maybe i am using wrong word i mean print intro console or write intro console.
User nimbus is talk same.

Quote
Write(Chr(3)); should print a heart symbol and number doesn't matter it wont print any char.
This is need more explain you write. documentate write chr(3) is mean return character that have ascii code 3. ascii code 3 is not visible character. https://www.freepascal.org/docs-html/rtl/system/chr.html

Then become question what font have character ascii code 3 and print heart ?

Quote
Write(Chr(3)); are working without timer and writing the heart symbol intro console but if i use timer it prints nothing like null.
code is chr(3) is not print any thing for my Linux terminal (it make print but it invisible for terminal).

PS: for code and make work is need unit threads when use linux... I not know if this is need for windows or other OS.
« Last Edit: May 29, 2026, 10:11:55 pm by Thausand »
A docile goblin always follow HERMES.md

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: fptimer bug?
« Reply #5 on: May 29, 2026, 10:53:45 pm »
Writing Chr(3) to the console and then seeing a ♥︎ used to be the case on the old DOS age (at least with codepag 437).
The ♥︎ is Unicode Codepoint $2665, and if you console is UTF-8 you can use this to write the heart symbol.

On my Windows 11 (Dutch locale) printing Chr(3) to the console just prints nothing at all.
And this is the case for almost all control characters.
Only Chr(26) will print a ␦, Chr(9) will print 4 spaces.

So, either use Unicode for this, or use actual old DOS hardware to get the heart character printed out using Chr(3).

Bart

lazarusprogrammer

  • New Member
  • *
  • Posts: 21
Re: fptimer bug?
« Reply #6 on: May 29, 2026, 11:02:12 pm »
WriteLn('@') and WriteLn(Chr(64)) should produce the identical result.

SO why don't you use Chr(64), or Chr(48+3) which gives the digit '3', make sure that works, then find out the correct way to produce the non-ASCII symbols you are wanting to display.
Yes chr(48+3) giving digit '3' but it shouldn't it should give a heart symbol.

Look at screenshot i have write(chr(3)) in the begin end. section and writeln(chr(3)) in TIMER_EVENT procedure and on the side you can see console result. Heart symbol are printed from begin end. section but from TIMER_EVENT procedure it's not working.
« Last Edit: May 29, 2026, 11:14:44 pm by lazarusprogrammer »

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: fptimer bug?
« Reply #7 on: May 30, 2026, 01:12:50 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   i: Integer;
  3. begin
  4.   for i := 1 to 31 do writeln('#',i,'="',Chr(i),'"');
  5. end.

Outputs:
Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
#1=""
#2=""
#3=""
#4=""
#5=""
#6=""
#7=""
#8="
#9="    "
#10="
"
#11="
     "
#12="
     "
"13="
#14=""
#15=""
#16=""
#17=""
#18=""
#19=""
#20=""
#21=""
#22=""
#23=""
#24=""
#25=""
#26="␦"
#27="
8=""
#29=""
#30=""
#31=""

(And it produces a beep)

All this on Win11, fpc3.2.4RC1 and fpc main.

Bart

Thausand

  • Hero Member
  • *****
  • Posts: 560
Re: fptimer bug?
« Reply #8 on: May 30, 2026, 01:59:12 am »
Writing Chr(3) to the console and then seeing a ♥︎ used to be the case on the old DOS age (at least with codepag 437).
The ♥︎ is Unicode Codepoint $2665, and if you console is UTF-8 you can use this to write the heart symbol.
O, this old codepage... is this exist for today windows OS ?

Unicode is work and print heart for Linux when use unit cwstring.
A docile goblin always follow HERMES.md

cdbc

  • Hero Member
  • *****
  • Posts: 2818
    • http://www.cdbc.dk
Re: fptimer bug?
« Reply #9 on: May 30, 2026, 02:01:50 am »
Hi
This example code writes your heart 10 times via 'fpTimer':
Code: Pascal  [Select][+][-]
  1. (* This test makes use of the codepoints obtained from this website:
  2.      https://www.cogsci.ed.ac.uk/~richard/utf-8.cgi?input=0x2665&mode=hex
  3.  
  4.    In particular the "Hex code point" row in the table.
  5.    ref: cdbc 2026.05.30
  6. *)
  7. program test;
  8. {$mode objfpc}{$H+}
  9. uses
  10.   {$ifdef linux}
  11.   cthreads,
  12.   {$endif}
  13.   sysutils, fptimer;
  14.  
  15. const
  16.   ucheart = $2665;
  17.  
  18. type
  19.   TEventHandlers = object
  20.     procedure TimerEvent(Sender: TObject);
  21.   end;
  22.  
  23. (*BEGIN** UniCode conversion routines ***)
  24. function tuiUnicodeToUTF8Inline(CodePoint: cardinal; Buf: pchar): integer;
  25. begin /// below lifted from lazutf8 ///
  26.   case CodePoint of
  27.     0..$7f: begin
  28.               Result:= 1;
  29.               Buf[0]:= char(byte(CodePoint));
  30.             end;
  31.     $80..$7ff: begin
  32.                  Result:= 2;
  33.                  Buf[0]:= char(byte($c0 or (CodePoint shr 6)));
  34.                  Buf[1]:= char(byte($80 or (CodePoint and $3f)));
  35.                end;
  36.     $800..$ffff: begin
  37.                    Result:= 3;
  38.                    Buf[0]:= char(byte($e0 or (CodePoint shr 12)));
  39.                    Buf[1]:= char(byte((CodePoint shr 6) and $3f) or $80);
  40.                    Buf[2]:= char(byte(CodePoint and $3f) or $80);
  41.                  end;
  42.     $10000..$10ffff: begin
  43.                        Result:= 4;
  44.                        Buf[0]:= char(byte($f0 or (CodePoint shr 18)));
  45.                        Buf[1]:= char(byte((CodePoint shr 12) and $3f) or $80);
  46.                        Buf[2]:= char(byte((CodePoint shr 6) and $3f) or $80);
  47.                        Buf[3]:= char(byte(CodePoint and $3f) or $80);
  48.                      end;
  49.     else Result:= 0;
  50.   end; { case }
  51. end;
  52.  
  53. function tuiUnicodeToUTF8Codepoint(aCodePoint: cardinal): string;
  54. var
  55.   Buf: array[0..6] of char;
  56.   Len: integer;
  57. begin
  58.   if (aCodePoint = 0) then Result:= #0 //StrPas does not like #0
  59.   else begin
  60.     Len:= tuiUnicodeToUTF8Inline(aCodePoint, @Buf[0]);
  61.     Buf[Len]:= #0;
  62.     Result:= StrPas(@Buf[0]);
  63.   end;
  64. end;
  65.  
  66. function tuiUTF8CPToUnicode(P: PChar; out CodepointLen: integer): Cardinal;
  67. { if p=nil then CodepointLen=0 otherwise CodepointLen>0
  68.   If there is an encoding error the Result is 0 and CodepointLen=1.
  69.   Use UTF8FixBroken to fix UTF-8 encoding.
  70.   It does not check if the codepoint is defined in the Unicode tables.
  71. }
  72. begin
  73.   if p <> nil then begin
  74.     if ord(p^) < %11000000 then begin
  75.       // regular single byte character (#0 is a normal char, this is pascal ;)
  76.       Result:= ord(p^);
  77.       CodepointLen:= 1;
  78.     end else if ((ord(p^) and %11100000) = %11000000) then begin
  79.       // starts with %110 => could be double byte character
  80.       if (ord(p[1]) and %11000000) = %10000000 then begin
  81.         CodepointLen:= 2;
  82.         Result:= ((ord(p^) and %00011111) shl 6) or (ord(p[1]) and %00111111);
  83.         if Result < (1 shl 7) then begin
  84.           // wrong encoded, could be an XSS attack
  85.           Result:= 0;
  86.         end;
  87.       end else begin
  88.         Result:= ord(p^);
  89.         CodepointLen:= 1;
  90.       end;
  91.     end else if ((ord(p^) and %11110000) = %11100000) then begin
  92.       // starts with %1110 => could be triple byte character
  93.       if ((ord(p[1]) and %11000000) = %10000000)
  94.       and ((ord(p[2]) and %11000000) = %10000000) then begin
  95.         CodepointLen:= 3;
  96.         Result:= ((ord(p^) and %00011111) shl 12)
  97.                  or ((ord(p[1]) and %00111111) shl 6)
  98.                  or (ord(p[2]) and %00111111);
  99.         if Result < (1 shl 11) then begin
  100.           // wrong encoded, could be an XSS attack
  101.           Result:= 0;
  102.         end;
  103.       end else begin
  104.         Result:= ord(p^);
  105.         CodepointLen:= 1;
  106.       end;
  107.     end else if ((ord(p^) and %11111000) = %11110000) then begin
  108.       // starts with %11110 => could be 4 byte character
  109.       if ((ord(p[1]) and %11000000) = %10000000)
  110.       and ((ord(p[2]) and %11000000) = %10000000)
  111.       and ((ord(p[3]) and %11000000) = %10000000) then begin
  112.         CodepointLen:= 4;
  113.         Result:= ((ord(p^) and %00001111) shl 18)
  114.                  or ((ord(p[1]) and %00111111) shl 12)
  115.                  or ((ord(p[2]) and %00111111) shl 6)
  116.                  or (ord(p[3]) and %00111111);
  117.         if Result < (1 shl 16) then begin
  118.           // wrong encoded, could be an XSS attack
  119.           Result:= 0;
  120.         end else if Result > $10FFFF then begin
  121.           // out of range
  122.           Result:= 0;
  123.         end;
  124.       end else begin
  125.         Result:= ord(p^);
  126.         CodepointLen:= 1;
  127.       end;
  128.     end else begin
  129.       // invalid character
  130.       Result:= ord(p^);
  131.       CodepointLen:= 1;
  132.     end;
  133.   end else begin { apparently p = nil }
  134.     Result:= 0;
  135.     CodepointLen:= 0;
  136.   end;
  137. end;
  138.  
  139. function tuiUTF8CodepointToUnicode(aCpUtf8: string): cardinal;
  140. var lencp: integer;
  141. begin
  142.   Result:= tuiUTF8CPToUnicode(pchar(aCpUtf8),lencp);
  143. end; /// above lifted from lazutf8 ///
  144. (*END** UniCode conversion routines ***)  
  145.  
  146. var
  147.   Count  : integer = 0;
  148.  
  149. procedure TEventHandlers.TimerEvent(Sender: TObject);
  150. begin
  151.   writeln('timer event fired, Heart -> UniCode($2665) => UTF8(',tuiUnicodeToUTF8Codepoint(ucheart),')');
  152.   inc(count);
  153. end;
  154.  
  155.  
  156. var
  157.   Events : TEventHandlers;
  158.   Timer  : TFPTimer;
  159.  
  160.  
  161. begin
  162.   Timer                := TFPTimer.Create(nil);
  163.   Timer.Enabled        := false;
  164.   Timer.UseTimerThread := true;
  165.   Timer.Interval       := 1000;
  166.   Timer.OnTimer        := @Events.TimerEvent;
  167.   Timer.Enabled        := true;
  168.  
  169.   writeln('waiting... ');
  170.   while count < 10 do
  171.   begin
  172.     sleep(1);
  173.   end;
  174.  
  175.   Timer.Free;
  176. end.
  177.  
  178.  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

lazarusprogrammer

  • New Member
  • *
  • Posts: 21
Re: fptimer bug?
« Reply #10 on: May 30, 2026, 02:31:19 pm »

cdbc

  • Hero Member
  • *****
  • Posts: 2818
    • http://www.cdbc.dk
Re: fptimer bug?
« Reply #11 on: May 30, 2026, 03:38:17 pm »
Hi
No wonder -- You're on winders and god only knows which codepage?!?  :P
I programmed it under Linux & UTF8...  8-)
Perhaps someone from winders-land can help you figure out, the codepage-hell on your system...
Regards Benny

edit: AFAICS, it's single-byte codepage -- Happy hunting...
« Last Edit: May 30, 2026, 03:40:58 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

cdbc

  • Hero Member
  • *****
  • Posts: 2818
    • http://www.cdbc.dk
Re: fptimer bug?
« Reply #12 on: May 30, 2026, 03:45:46 pm »
Hi
Here's how it looks like...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

sstvmaster

  • Sr. Member
  • ****
  • Posts: 307
Re: fptimer bug?
« Reply #13 on: May 30, 2026, 05:40:59 pm »
I am on Windows. You must use the package LazUtils in the project inspector. And then you must include LazUTF8 in uses clause.

Code: Pascal  [Select][+][-]
  1. ...
  2.  
  3. uses
  4.   {$ifdef linux}
  5.   cthreads,
  6.   {$endif}
  7.   sysutils, fptimer, LazUTF8;
  8.  
  9. ...
  10.  
greetings Maik

Windows 10,
- Lazarus 4.4 (stable) + fpc 3.2.2 (stable)
- Lazarus 4.99 (trunk) + fpc 3.3.1 (main/trunk)

cdbc

  • Hero Member
  • *****
  • Posts: 2818
    • http://www.cdbc.dk
Re: fptimer bug?
« Reply #14 on: May 30, 2026, 06:13:25 pm »
Hi Maik
Thanks mate =^
Ofc. 'lazutf8' sets the codepage to UTF8 on init...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018