PDA

View Full Version : Convert from c++ to Pascal



SesillaAndromeda
26-08-2013, 08:55 AM
Hi guys,

can you help me with this code?

in C++



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



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

User137
26-08-2013, 09:17 AM
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 :D

SesillaAndromeda
26-08-2013, 09:31 AM
Thanks User137, you are right! This is test code...i'll try your solution!

Thanks
Sesilla

laggyluk
26-08-2013, 09:50 AM
procedure addValue(vertices: PArraySingle);
var iStream: Stream;
begin
iStream.vertices := vertices;
end;
shouldn't iStream be created first? or passed as parameter

SesillaAndromeda
26-08-2013, 10:35 AM
Hi,

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

I have another c++ code!!!!! :-[
Then...

C++



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



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 :-*

Dan
26-08-2013, 01:49 PM
var src: PFloatArray;
var temp: Single;
...
src := PFloatArray(@streams[index].vertices[i * 2]);
temp := src^[c0];
src^[c0] := src^[c1];
src^[c1] := temp;

SesillaAndromeda
26-08-2013, 05:46 PM
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 :D

Is possible otherwise? If i have an array of single and i want an array of vec3, is possible without a for loop?

Sesilla

User137
26-08-2013, 06:25 PM
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.


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;

SesillaAndromeda
26-08-2013, 06:34 PM
Thanks User137,

this is the question:

C++


float *vertices; //My array
vec3 *vectors = (vec3 *) vertices;


Pascal


var
vertices: array of single;
vectors: array of TVec3;
begin
???????
end;


Sesilla

User137
27-08-2013, 04:40 AM
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:

var
vertices: PSingleArray;
vectors: PVec3Array;
begin
vectors:=PVec3Array(vertices);
end;

SesillaAndromeda
27-08-2013, 01:30 PM
Thanks User137, i'll try it.

Regards
Sesilla ��