Recent

Author Topic: Mapping 2d array onto 1d buffer.  (Read 3046 times)

Rave

  • Full Member
  • ***
  • Posts: 170
Mapping 2d array onto 1d buffer.
« on: March 05, 2013, 02:25:56 pm »
I have problem with that.

Code:=
Code: [Select]
function BGFDisplayToBGFImage(BGFDisplay:TBGFDisplay):TBGFImage;
var tmpImage:TBGFImage;
var xLen,yLen,x,y:Integer;
begin
  xLen:=Length(BGFDisplay);
  if xLen>0 then yLen:=Length(BGFDisplay[0]) else begin
    Application.MessageBox('Malformed BGFDisplay!','Error',MB_OK + MB_ICONERROR);
    tmpImage.columns:=-1;
    Result:=tmpImage;
    exit;
  end;
  tmpImage.columns:=xLen;
  tmpImage.rows:=ylen;
  SetLength(tmpImage.data,xlen*ylen);
  for x:=0 to xlen-1 do begin
     for y:=0 to ylen-1 do begin
        tmpImage.data[y+x]:=BGFDisplay[x,y];
     end;
  end;
  Result:=tmpImage;
end;

TBGFDisplay is open (2d) array.

For 3x3 of it with following values:
Code: [Select]
0,1,2,
3,4,5,
6,7,0
buffer should look like that:
Code: [Select]
0,1,2,3,4,5,6,7,0Instead with my code it looks like that
Code: [Select]
0,1,2,5,0,0,0,0,0.

Can you help me?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Mapping 2d array onto 1d buffer.
« Reply #1 on: March 05, 2013, 02:34:46 pm »
Error is in indexing:
This:
Code: [Select]
  for x:=0 to xlen-1 do begin
     for y:=0 to ylen-1 do begin
        tmpImage.data[y+x]:=BGFDisplay[x,y];
     end;
  end;
should be:
Code: [Select]
  for x:=0 to xlen-1 do begin
     for y:=0 to ylen-1 do begin
        tmpImage.data[x*ylen+y]:=BGFDisplay[x,y];
     end;
  end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Mapping 2d array onto 1d buffer.
« Reply #2 on: March 05, 2013, 03:35:37 pm »
data[x*ylen+y]
I'm not familiar with term BGF. Anyway the usual "standard" for 2D->1D indexing is
y*xlen+x

Rave

  • Full Member
  • ***
  • Posts: 170
Re: Mapping 2d array onto 1d buffer.
« Reply #3 on: March 05, 2013, 04:26:44 pm »
data[x*ylen+y]
I'm not familiar with term BGF. Anyway the usual "standard" for 2D->1D indexing is
y*xlen+x
Thanks. And BGF is my custom graphics format tailored specifically for game I'm making.

 

TinyPortal © 2005-2018