hedgewars/uConsole.pas
branchhedgeroid
changeset 15515 7030706266df
parent 12626 d972b31f8881
equal deleted inserted replaced
7861:bc7b6aa5d67a 15515:7030706266df
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    12  * GNU General Public License for more details.
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17  *)
    17  *)
    18 
    18 
    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;
    27 
    29 function  ShortStringAsPChar(s: shortstring): PChar;
    28 var lastConsoleline : shortstring;
    30 
    29 
    31 implementation
    30 implementation
    32 uses Types, uVariables, uUtils {$IFDEF ANDROID}, log in 'log.pas'{$ENDIF};
    31 uses uUtils {$IFDEF ANDROID}, log in 'log.pas'{$ENDIF};
    33 
    32 
    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 
    33 
    51 procedure WriteToConsole(s: shortstring);
    34 procedure WriteToConsole(s: shortstring);
    52 {$IFNDEF NOCONSOLE}
       
    53 var Len: LongInt;
       
    54     done: boolean;
       
    55 {$ENDIF}
       
    56 begin
    35 begin
    57 {$IFNDEF NOCONSOLE}
    36 {$IFNDEF NOCONSOLE}
    58 AddFileLog('[Con] ' + s);
    37     AddFileLog('[Con] ' + s);
    59 {$IFDEF ANDROID}
    38 {$IFDEF ANDROID}
       
    39     //TODO integrate this function in the uMobile record
    60     Log.__android_log_write(Log.Android_LOG_DEBUG, 'HW_Engine', ShortStringAsPChar('[Con]' + s));
    40     Log.__android_log_write(Log.Android_LOG_DEBUG, 'HW_Engine', ShortStringAsPChar('[Con]' + s));
    61 {$ELSE}
    41 {$ELSE}
    62 Write(stderr, s);
    42     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}
    43 {$ENDIF}
    80 {$ENDIF}
    44 {$ENDIF}
    81 end;
    45 end;
    82 
    46 
    83 procedure WriteLnToConsole(s: shortstring);
    47 procedure WriteLnToConsole(s: shortstring);
    84 begin
    48 begin
    85 {$IFNDEF NOCONSOLE}
    49 {$IFNDEF NOCONSOLE}
    86 WriteToConsole(s);
    50     WriteToConsole(s);
    87 {$IFNDEF ANDROID}
    51 {$IFNDEF ANDROID}
    88 WriteLn(stderr, '');
    52     WriteLn(stderr, '');
    89 inc(CurrLine);
       
    90 if CurrLine = cLinesCount then
       
    91     CurrLine:= 0;
       
    92 PByte(@ConsoleLines[CurrLine].s)^:= 0
       
    93 {$ENDIF}
    53 {$ENDIF}
    94 {$ENDIF}
    54 {$ENDIF}
       
    55     lastConsoleline:= s;
    95 end;
    56 end;
    96 
    57 {$IFDEF ANDROID}
    97 function ShortStringAsPChar(s: shortstring) : PChar;
    58 function ShortStringAsPChar(s: shortstring) : PChar;
    98 begin
    59 begin
    99     if Length(s) = High(s) then
    60     if Length(s) = High(s) then
   100         Dec(s[0]);
    61         Dec(s[0]);
   101     s[Ord(Length(s))+1] := #0;
    62     s[Ord(Length(s))+1] := #0;
       
    63     // returning pointer to stack, rly?
   102     ShortStringAsPChar:= @s[1];
    64     ShortStringAsPChar:= @s[1];
   103 end;
    65 end;
   104 
    66 {$ENDIF}
   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.