Results 1 to 10 of 11

Thread: Convert from c++ to Pascal

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Post Convert from c++ to Pascal

    Hi guys,

    can you help me with this code?

    in C++
    Code:
    struct Stream {
        float *vertices;
    };
    
    void addValue(float *vertices){
    
        Stream stream;
        stream.vertices  = vertices;
    }
    
    void myProcedure
    {
        Array <vec3> vertices;
    
        for (uint i = 0; i < 10; i++)
           vertices.add(vec3(x, y, z);
    
        addValue((float *) vertices);
    }
    in Pascal
    Code:
    type
      PArraySingle = ^TArraySingle;
      TArraySingle = array of single;  
    
      Stream = record
        vertices: PArraySingle;
      end;
    
    procedure addValue(vertices: PArraySingle);
    var
       iStream: Stream;
    begin
        iStream.vertices := vertices;
    end;
    
    procedure myProcedure;
    var
       vertices: array of TVec3;
       i: integer;
    begin
       setlength(vertices, 10);
       for i:=0 to 10-1 do
           vertices[i] := vec3(x, y, z);
    
        addValue((float *) vertices); <--- This?!!!! addValue procedure wait array of single. With this line i call addValue with array of vec3!!!
    end;
    Thanks in advance
    Sesilla

  2. #2
    Code:
    addValue(@vertices[0]);
    should do it. I'm just not sure if the myProcedure disposes of the memory reservation after it ends. That might cause access violations later if so.

    And i find addValue a little misleading name, when what it does would better be described as setValue. Whatever exists in the stream before "adding" will be replaced with new one.

    edit: Fine, even the C++ code is just a dummy test code

  3. #3
    Thanks User137, you are right! This is test code...i'll try your solution!

    Thanks
    Sesilla

  4. #4
    Code:
    procedure addValue(vertices: PArraySingle);
      var   iStream: Stream;
    begin
        iStream.vertices := vertices;
    end;
    shouldn't iStream be created first? or passed as parameter

  5. #5

    Wink

    Hi,

    iStream is a record type. I don't create it!

    I have another c++ code!!!!!
    Then...

    C++
    Code:
    struct Stream {
        float *vertices;
        uint verticesCount;
    };
    
    Array <Stream> streams;
    
    void flip(const int index, const uint c0, const uint c1){
        
        for (uint i = 0; i < streams[index].verticesCount; i++){
            float *src = streams[index].vertices + i * 2;
    
            float temp = src[c0];
            src[c0] = src[c1];
            src[c1] = temp;
        }
    }
    Pascal
    Code:
      Stream = record
        vertices: array of single;
        verticesCount: longword;
      end;
    
      streams = array of Stream;
    
    procedure flip(const index: integer; const c0, c1: longword);
    var
      i: intger;
    begin  
        for i := 0 to streams[index].verticesCount-1 do
        begin
            float *src = streams[index].vertices + i * 2; <-- from this
    
            float temp = src[c0];
            src[c0] = src[c1];
            src[c1] = temp;
        end;
    end;
    Thanks
    Sesilla

  6. #6
    Code:
    var src: PFloatArray;
    var temp: Single;
    ...
    src := PFloatArray(@streams[index].vertices[i * 2]);
    temp := src^[c0];
    src^[c0] := src^[c1];
    src^[c1] := temp;

  7. #7
    Quote Originally Posted by User137 View Post
    Code:
    addValue(@vertices[0]);
    should do it. I'm just not sure if the myProcedure disposes of the memory reservation after it ends. That might cause access violations later if so.

    And i find addValue a little misleading name, when what it does would better be described as setValue. Whatever exists in the stream before "adding" will be replaced with new one.

    edit: Fine, even the C++ code is just a dummy test code
    Is possible otherwise? If i have an array of single and i want an array of vec3, is possible without a for loop?

    Sesilla

  8. #8
    Not sure if i understand the question, you could give a bit more details of what you want to do. Function only sends a pointer reference, not copying anything. You can typecast a pointer to anything you want; record or array is irrelevant.

    Code:
    TVec3Array = array[0..65535] of vec3;
    PVec3Array = ^TVec3Array;
    
    var arr: array[0..5] of single;
    begin
      // Now you can write the same sentence 3 ways:
      arr[3]:=0.2;
      PVec3Array(@arr[0])^[1].x:=0.2;
      PSingleArray(@arr[0])^[3]:=0.2;
    end;

  9. #9
    Thanks User137,

    this is the question:

    C++
    Code:
    float *vertices; //My array
    vec3 *vectors = (vec3 *) vertices;
    Pascal
    Code:
    var
       vertices: array of single;
       vectors: array of TVec3;
    begin
       ???????
    end;
    Sesilla

  10. #10
    float *vertices doesn't convert to array of single. That is 1 type of cast you can't do with dynamic array. The C++ code uses pointers, so more precise translation would be:
    Code:
    var
       vertices: PSingleArray;
       vectors: PVec3Array;
    begin
       vectors:=PVec3Array(vertices);
    end;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •