hedgewars/uConsole.pas
changeset 8437 93b647d6a00f
parent 7151 ec15d9e1a7e3
child 9080 9b42757d7e71
equal deleted inserted replaced
8436:b89aacebb9db 8437:93b647d6a00f
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uConsole;
    21 unit uConsole;
    22 interface
    22 interface
    23 
    23 
    24 procedure initModule;
    24 
    25 procedure freeModule;
       
    26 procedure WriteToConsole(s: shortstring);
    25 procedure WriteToConsole(s: shortstring);
    27 procedure WriteLnToConsole(s: shortstring);
    26 procedure WriteLnToConsole(s: shortstring);
    28 function  GetLastConsoleLine: shortstring;
       
    29 function  ShortStringAsPChar(s: shortstring): PChar;
    27 function  ShortStringAsPChar(s: shortstring): PChar;
    30 
    28 
       
    29 var lastConsoleline : shortstring;
       
    30 
    31 implementation
    31 implementation
    32 uses Types, uVariables, uUtils {$IFDEF ANDROID}, log in 'log.pas'{$ENDIF};
    32 uses Types, uUtils {$IFDEF ANDROID}, log in 'log.pas'{$ENDIF};
    33 
    33 
    34 const cLinesCount = 8;
       
    35 var   cLineWidth: LongInt;
       
    36 
       
    37 type
       
    38     TTextLine = record
       
    39         s: shortstring
       
    40         end;
       
    41 
       
    42 var   ConsoleLines: array[byte] of TTextLine;
       
    43       CurrLine: LongInt;
       
    44 
       
    45 procedure SetLine(var tl: TTextLine; str: shortstring);
       
    46 begin
       
    47 with tl do
       
    48     s:= str;
       
    49 end;
       
    50 
    34 
    51 procedure WriteToConsole(s: shortstring);
    35 procedure WriteToConsole(s: shortstring);
    52 {$IFNDEF NOCONSOLE}
       
    53 var Len: LongInt;
       
    54     done: boolean;
       
    55 {$ENDIF}
       
    56 begin
    36 begin
    57 {$IFNDEF NOCONSOLE}
    37 {$IFNDEF NOCONSOLE}
    58 AddFileLog('[Con] ' + s);
    38     AddFileLog('[Con] ' + s);
    59 {$IFDEF ANDROID}
    39 {$IFDEF ANDROID}
       
    40     //TODO integrate this function in the uMobile record
    60     Log.__android_log_write(Log.Android_LOG_DEBUG, 'HW_Engine', ShortStringAsPChar('[Con]' + s));
    41     Log.__android_log_write(Log.Android_LOG_DEBUG, 'HW_Engine', ShortStringAsPChar('[Con]' + s));
    61 {$ELSE}
    42 {$ELSE}
    62 Write(stderr, s);
    43     Write(stderr, s);
    63 done:= false;
       
    64 
       
    65 while not done do
       
    66     begin
       
    67     Len:= cLineWidth - Length(ConsoleLines[CurrLine].s);
       
    68     SetLine(ConsoleLines[CurrLine], ConsoleLines[CurrLine].s + copy(s, 1, Len));
       
    69     Delete(s, 1, Len);
       
    70     if byte(ConsoleLines[CurrLine].s[0]) = cLineWidth then
       
    71         begin
       
    72         inc(CurrLine);
       
    73         if CurrLine = cLinesCount then
       
    74             CurrLine:= 0;
       
    75         PByte(@ConsoleLines[CurrLine].s)^:= 0
       
    76         end;
       
    77     done:= (Length(s) = 0);
       
    78     end;
       
    79 {$ENDIF}
    44 {$ENDIF}
    80 {$ENDIF}
    45 {$ENDIF}
    81 end;
    46 end;
    82 
    47 
    83 procedure WriteLnToConsole(s: shortstring);
    48 procedure WriteLnToConsole(s: shortstring);
    84 begin
    49 begin
    85 {$IFNDEF NOCONSOLE}
    50 {$IFNDEF NOCONSOLE}
    86 WriteToConsole(s);
    51     WriteToConsole(s);
       
    52     lastConsoleline:= s;
    87 {$IFNDEF ANDROID}
    53 {$IFNDEF ANDROID}
    88 WriteLn(stderr, '');
    54     WriteLn(stderr, '');
    89 inc(CurrLine);
       
    90 if CurrLine = cLinesCount then
       
    91     CurrLine:= 0;
       
    92 PByte(@ConsoleLines[CurrLine].s)^:= 0
       
    93 {$ENDIF}
    55 {$ENDIF}
    94 {$ENDIF}
    56 {$ENDIF}
    95 end;
    57 end;
    96 
    58 
    97 function ShortStringAsPChar(s: shortstring) : PChar;
    59 function ShortStringAsPChar(s: shortstring) : PChar;
   100         Dec(s[0]);
    62         Dec(s[0]);
   101     s[Ord(Length(s))+1] := #0;
    63     s[Ord(Length(s))+1] := #0;
   102     ShortStringAsPChar:= @s[1];
    64     ShortStringAsPChar:= @s[1];
   103 end;
    65 end;
   104 
    66 
   105 function GetLastConsoleLine: shortstring;
       
   106 var valueStr: shortstring;
       
   107     i: LongWord;
       
   108 begin
       
   109 i:= (CurrLine + cLinesCount - 2) mod cLinesCount;
       
   110 valueStr:= ConsoleLines[i].s;
       
   111 
       
   112 valueStr:= valueStr + #10;
       
   113 
       
   114 i:= (CurrLine + cLinesCount - 1) mod cLinesCount;
       
   115 valueStr:= valueStr + ConsoleLines[i].s;
       
   116 
       
   117 GetLastConsoleLine:= valueStr;
       
   118 end;
       
   119 
       
   120 procedure initModule;
       
   121 var i: LongInt;
       
   122 begin
       
   123     CurrLine:= 0;
       
   124 
       
   125     // initConsole
       
   126     cLineWidth:= cScreenWidth div 10;
       
   127     if cLineWidth > 255 then
       
   128         cLineWidth:= 255;
       
   129     for i:= 0 to Pred(cLinesCount) do
       
   130         PByte(@ConsoleLines[i])^:= 0;
       
   131 end;
       
   132 
       
   133 procedure freeModule;
       
   134 begin
       
   135 
       
   136 end;
       
   137 
    67 
   138 end.
    68 end.