Jump to content

Alexander Brazda

Members
  • Content Count

    3
  • Joined

  • Last visited

Posts posted by Alexander Brazda


  1. For 2d/3d game better use DirectX/OpenGL libraries instead of canvas drawing

    For controls, painting is done using canvas, so speed it limited. Yo can draw component to a buffer using DirectX/OpenGL surfaces (for complex compositions) and transfer to canvas control, using some buffering to avoid redraw

    Forms and controls can be scaled from desing dpi to device dpi, check Handle.Scale from yout form

     

    For test pixel perfect, I made a simple app to place a Button, Label and TRectangle to get coordinates & dimension of the shape (no style) to see if they are scaled some way.

    TRectangle is positionated at (16,148) with a size of 64x64.

    I run on my samsung galaxy tab, click on button to update Label text with the rectangle position and size and compare with screen shot.

    Result, rectangle is positionated at 16,148 pixels in clientarea and is 64x64 pixels size, so no scaling has been done. Rectangle is show width 1 px arround the shape as desing. 

     

    • Like 1

  2. I agree, use TStringBuilder for string to buffer copy and prevent adding coma cheking before add a new line if buffer is empty.

    You can reduce memory reallocations if StringBuilder buffer is set to a initial capacity by calling EnsureCapacity or set a value to Capacity. Perheaps you can do some estimation based on max or average record size or just set a value like 4 MB as start point.

     

    For csv files with ansi or utf-8 encoding and with ram limitations perheaps you can use a buffered file stream with string encoding 

     

    Attached file FileBuffer.pas is a Buffered file with string encoding and in memory concatenation, support multiple code pages for input and output. It's optimized for the same input and output codepage.

    Encoding preamble is writed at start of file, comment if you don't need this.

    UTF-8 is default encoding

     

    Usage sample

        Buffer:=TFileBuffer.Create('test.txt',CP_UTF8);

        Buffer.WriteLn('FieldName'#9'Value');

        Buffer
          .Write('Name')
          .Write(#9)
          .Write('Alex');

        Buffer.Free;
     

     

    FileBuffer.pas


  3. On 12/21/2018 at 5:51 PM, Mike Torrettinni said:

    I use a lot of export to csv, so of course a lot of methods that are creating csv text lines add extra comma at the end, so it needs to be deleted.

    In a lot of cases the loop prevents it to know exactly which string is last in the line, so I can't always avoid adding this extra comma.

     

    So, I setup 2 utility methods:

     

    
    procedure DeleteLastChar(var aText: string; const aLastChar: string);
    begin
      If aText <> '' then
      if aText[Length(aText)] = aLastChar then
        SetLength(aText, Length(aText) - 1); // old: aText := Copy(aText, 1, Length(aText) - 1)
    end;
    
    procedure DeleteLastComma(var aText: string);
    begin
      DeleteLastChar(aText, ',');
    end;

    And I call DeleteLastComma(TextLine) in each method, to just make sure I don't leave last comma in the text.

    The reports could be long, so this could be called 10000x of times... with small or large lines of text.

     

    Since I use var it will avoid copying string parameters and also SetLength just reduces the length of the string and doesn't do the actual string copying like Copy would do, right?

     

    There is no diference between var AText:String and const AText:String and return the value as a function result

    AText is reference counted so assign a value to this variable inside the función is the same as assign the result of a función to a variable, 

    SetLength would allocate a new string and assign this to AText, so reference counting would decrease inside the funcion instead outside.

    String functions should work when ZEROBASEDSTRINGS compiler flag is enabled or not, to be safe for any platform. Use StringHelper funcions from System.SysUtils instead base 1 string functions

     

    Some optimizations are: declare DeleteLastComma as inline , change "const ALastChar:String" to "ALastChar:Char" 

     

    function DeleteLastChar(const AText:String;AChar:Char):String;
    begin
      if AText.IsEmpty then
        exit(AText);

      if AText.Chars[AText.Length-1]=AChar then
        exit(AText.Substring(0,AText.Length-1);
      Result:=AText;
    end;

     

    DeleteLastComma method can be a inline function.

     

    procedure DeleteLastComma(var AText:String); inline;

    begin

       AText:=DeleteLastChar(AText,',');

    end;


     

×