- Several code cleanups
- Fixed TArt.RefreshBuffer using the wrong variable (j <> i) - Suppressed some hints
This commit is contained in:
648
UOLib/UArt.pas
648
UOLib/UArt.pas
@@ -1,323 +1,325 @@
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2009 Andreas Schneider
|
||||
*)
|
||||
unit UArt;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, Imaging, ImagingTypes, ImagingCanvases, ImagingClasses,
|
||||
UMulBlock, UGenericIndex, UHue;
|
||||
|
||||
type
|
||||
TArtType = (atLand, atStatic, atLandFlat);
|
||||
TArt = class(TMulBlock)
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType); overload;
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AHue: THue; APartialHue: Boolean); overload;
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AArtColor: Word; AHue: THue; APartialHue: Boolean); overload;
|
||||
destructor Destroy; override;
|
||||
function Clone: TArt; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure Write(AData: TStream); override;
|
||||
procedure RefreshBuffer;
|
||||
protected
|
||||
FArtType: TArtType;
|
||||
FHeader: LongInt;
|
||||
FGraphic: TSingleImage;
|
||||
FBuffer: TStream;
|
||||
public
|
||||
property ArtType: TArtType read FArtType write FArtType;
|
||||
property Header: LongInt read FHeader write FHeader;
|
||||
property Graphic: TSingleImage read FGraphic;
|
||||
property Buffer: TStream read FBuffer;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
type
|
||||
PWordArray = ^TWordArray;
|
||||
TWordArray = array[0..16383] of Word;
|
||||
|
||||
constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType);
|
||||
begin
|
||||
Create(AData, AIndex, AArtType, 0, nil, False);
|
||||
end;
|
||||
|
||||
constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AHue: THue; APartialHue: Boolean);
|
||||
begin
|
||||
Create(AData, AIndex, AArtType, 0, AHue, APartialHue);
|
||||
end;
|
||||
|
||||
constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AArtColor: Word; AHue: THue; APartialHue: Boolean);
|
||||
var
|
||||
i, x, y, start: Integer;
|
||||
iCurrentHeight, iCurrentWidth: Integer;
|
||||
width, height: SmallInt;
|
||||
lookup: array of integer;
|
||||
color, run, offset: Word;
|
||||
block: TMemoryStream;
|
||||
P: PWordArray;
|
||||
r, g, b: Byte;
|
||||
|
||||
begin
|
||||
FBuffer := TMemoryStream.Create;
|
||||
FArtType := AArtType;
|
||||
AArtColor := AArtColor or $8000; //set alpha bit on background
|
||||
if Assigned(AData) and (AIndex.Lookup > -1) then
|
||||
begin
|
||||
AData.Position := AIndex.Lookup;
|
||||
block := TMemoryStream.Create;
|
||||
block.CopyFrom(AData, AIndex.Size);
|
||||
block.Position := 0;
|
||||
|
||||
if AArtType = atLand then
|
||||
begin
|
||||
FGraphic:= TSingleImage.CreateFromParams(44, 44, ifA1R5G5B5);
|
||||
FillWord(FGraphic.Bits^, 44 * 44, AArtColor);
|
||||
for y := 0 to 21 do
|
||||
begin
|
||||
P := FGraphic.Bits + y * 44 * 2;
|
||||
block.Read(P^[22 - (y + 1)], (y + 1) * 4);
|
||||
end;
|
||||
for y := 0 to 21 do
|
||||
begin
|
||||
P := FGraphic.Bits + (22 + y) * 44 * 2;
|
||||
block.Read(P^[y], (22 - y) * 4);
|
||||
end;
|
||||
for i := 0 to 44 * 44 - 1 do
|
||||
PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit
|
||||
end else if AArtType = atLandFlat then
|
||||
begin
|
||||
FGraphic:= TSingleImage.CreateFromParams(44, 44, ifA1R5G5B5);
|
||||
for i := 1 to 22 do
|
||||
begin
|
||||
for x := 0 to i * 2 - 1 do
|
||||
begin
|
||||
y := i * 2 - x - 1;
|
||||
block.Read(color, SizeOf(Word));
|
||||
PWordArray(FGraphic.Bits + y * 44 * 2)^[x] := color;
|
||||
if y > 0 then
|
||||
PWordArray(FGraphic.Bits + (y - 1) * 44 * 2)^[x] := color;
|
||||
end;
|
||||
end;
|
||||
for i := 22 to 43 do
|
||||
begin
|
||||
for y := 0 to (44 - i) * 2 - 1 do
|
||||
begin
|
||||
x := 42 - (43 - i) * 2 + y;
|
||||
block.Read(color, SizeOf(Word));
|
||||
PWordArray(FGraphic.Bits + (43 - y) * 44 * 2)^[x] := color;
|
||||
if y > 0 then
|
||||
PWordArray(FGraphic.Bits + (44 - y) * 44 * 2)^[x] := color;
|
||||
end;
|
||||
end;
|
||||
for i := 0 to 44 * 44 - 1 do
|
||||
PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit
|
||||
end else if AArtType = atStatic then
|
||||
begin
|
||||
block.Read(FHeader, SizeOf(LongInt));
|
||||
block.Read(width, SizeOf(SmallInt));
|
||||
block.Read(height, SizeOf(SmallInt));
|
||||
FGraphic:= TSingleImage.CreateFromParams(width, height, ifA1R5G5B5);
|
||||
FillWord(FGraphic.Bits^, width * height, AArtColor);
|
||||
SetLength(lookup, height);
|
||||
start := block.Position + (height * 2);
|
||||
for i := 0 to height - 1 do
|
||||
begin
|
||||
block.Read(offset, SizeOf(Word));
|
||||
lookup[i] := start + (offset * 2);
|
||||
end;
|
||||
for iCurrentHeight := 0 to height - 1 do
|
||||
begin
|
||||
block.Position := lookup[iCurrentHeight];
|
||||
iCurrentWidth := 0;
|
||||
P := FGraphic.Bits + iCurrentHeight * width * 2;
|
||||
while (block.Read(offset, SizeOf(Word)) = SizeOf(Word)) and (block.Read(run, SizeOf(Word)) = SizeOf(Word)) and (offset + run <> 0) do
|
||||
begin
|
||||
inc(iCurrentWidth, offset);
|
||||
for i := 0 to run - 1 do
|
||||
begin
|
||||
block.Read(color, SizeOf(Word));
|
||||
P^[iCurrentWidth + i] := color;
|
||||
end;
|
||||
inc(iCurrentWidth, run);
|
||||
end;
|
||||
end;
|
||||
|
||||
if AHue <> nil then
|
||||
begin
|
||||
for i := 0 to width * height - 1 do
|
||||
begin
|
||||
color := PWordArray(FGraphic.Bits)^[i];
|
||||
if color <> AArtColor then
|
||||
begin
|
||||
r := (color and $7C00) shr 10;
|
||||
if APartialHue then
|
||||
begin
|
||||
g := (color and $3E0) shr 5;
|
||||
b := color and $1F;
|
||||
if (r = g) and (g = b) then
|
||||
color := AHue.ColorTable[r];
|
||||
end else
|
||||
color := AHue.ColorTable[r];
|
||||
end;
|
||||
PWordArray(FGraphic.Bits)^[i] := color;
|
||||
end;
|
||||
end;
|
||||
|
||||
for i := 0 to width * height - 1 do
|
||||
PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit
|
||||
end else
|
||||
FGraphic:= TSingleImage.Create;
|
||||
if Assigned(block) then block.Free;
|
||||
end else
|
||||
begin
|
||||
FHeader := 1;
|
||||
FGraphic := TSingleImage.Create;
|
||||
end;
|
||||
FGraphic.Format := ifA8R8G8B8;
|
||||
end;
|
||||
|
||||
destructor TArt.Destroy;
|
||||
begin
|
||||
if assigned(FGraphic) then FGraphic.Free;
|
||||
if assigned(FBuffer) then FBuffer.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TArt.Clone: TArt;
|
||||
begin
|
||||
Result := TArt.Create(nil, nil, FArtType);
|
||||
Result.FHeader := FHeader;
|
||||
Result.FGraphic.Assign(FGraphic);
|
||||
end;
|
||||
|
||||
procedure TArt.Write(AData: TStream);
|
||||
begin
|
||||
FBuffer.Position := 0;
|
||||
AData.CopyFrom(FBuffer, FBuffer.Size);
|
||||
end;
|
||||
|
||||
function TArt.GetSize: Integer;
|
||||
begin
|
||||
RefreshBuffer;
|
||||
Result := FBuffer.Size
|
||||
end;
|
||||
|
||||
procedure TArt.RefreshBuffer;
|
||||
var
|
||||
argbGraphic: TSingleImage;
|
||||
i, j, x, y, lineWidth, start: Integer;
|
||||
iCurrentHeight, iCurrentWidth: Integer;
|
||||
width, height: SmallInt;
|
||||
color, run, offset: Word;
|
||||
lookup: array of SmallInt;
|
||||
begin
|
||||
argbGraphic := TSingleImage.CreateFromImage(FGraphic);
|
||||
argbGraphic.Format := ifA1R5G5B5;
|
||||
for i := 0 to argbGraphic.Width * argbGraphic.Height - 1 do
|
||||
PWordArray(argbGraphic.Bits)^[i] := PWordArray(argbGraphic.Bits)^[i] xor $8000; //invert alpha bit
|
||||
FBuffer.Size := 0;
|
||||
if FArtType = atLand then
|
||||
begin
|
||||
if (argbGraphic.Height <> 44) or (argbGraphic.Width <> 44) then Exit;
|
||||
x := 21;
|
||||
y := 0;
|
||||
lineWidth := 2;
|
||||
for i := 1 to 22 do
|
||||
begin
|
||||
Dec(x);
|
||||
FBuffer.Write(PWordArray(argbGraphic.Bits + y * 44 * 2)^[x + j], lineWidth);
|
||||
Inc(y);
|
||||
Inc(lineWidth, 2);
|
||||
end;
|
||||
for i := 1 to 22 do
|
||||
begin
|
||||
Dec(lineWidth, 2);
|
||||
FBuffer.Write(PWordArray(argbGraphic.Bits + y * 44 * 2)^[x + j], lineWidth);
|
||||
Inc(x);
|
||||
Inc(y);
|
||||
end;
|
||||
end else if FArtType = atStatic then
|
||||
begin
|
||||
if (argbGraphic.Height = 0) or (argbGraphic.Width = 0) then Exit;
|
||||
width := argbGraphic.Width;
|
||||
height := argbGraphic.Height;
|
||||
FBuffer.Write(FHeader, SizeOf(LongInt));
|
||||
FBuffer.Write(width, SizeOf(SmallInt));
|
||||
FBuffer.Write(height, SizeOf(SmallInt));
|
||||
SetLength(lookup, height);
|
||||
for i := 0 to height - 1 do
|
||||
FBuffer.Write(lookup[i], SizeOf(SmallInt)); //placeholders for the lookup table
|
||||
start := FBuffer.Position;
|
||||
for iCurrentHeight := 0 to height - 1 do
|
||||
begin
|
||||
lookup[iCurrentHeight] := SmallInt((FBuffer.Position - start) div 2); //remember the lookup offset for the current line
|
||||
offset := 0;
|
||||
run := 0;
|
||||
for iCurrentWidth := 0 to width - 1 do //process every pixel on the current line
|
||||
begin
|
||||
color := PWordArray(FGraphic.Bits + iCurrentHeight * width * 2)^[iCurrentWidth];
|
||||
if (color and $8000 = 0) and (run = 0) then //new visible pixel found
|
||||
begin
|
||||
FBuffer.Write(offset, SizeOf(Word));
|
||||
FBuffer.Write(offset, SizeOf(Word)); //just a placeholder for the "run length"
|
||||
run := 1;
|
||||
FBuffer.Write(color, SizeOf(Word));
|
||||
end else if (color and $8000 = 0) and (run > 0) then //another visible pixel found
|
||||
begin
|
||||
inc(run);
|
||||
FBuffer.Write(color, SizeOf(Word));
|
||||
end else if (color and $8000 = $8000) and (run > 0) then //after some visible pixels this one is invisible, so stop the current run
|
||||
begin
|
||||
FBuffer.Seek(Integer(-((run + 1) * 2)), soFromCurrent); //jump back ...
|
||||
FBuffer.Write(run, SizeOf(Word)); //... to write the actual "run length" ...
|
||||
FBuffer.Seek(Integer(run * 2), soFromCurrent); //... and jump forth again to proceed
|
||||
run := 0;
|
||||
offset := 1;
|
||||
end else
|
||||
inc(offset);
|
||||
end;
|
||||
if run > 0 then //no more pixels but the "run" didn't end yet ;-)
|
||||
begin
|
||||
FBuffer.Seek(Integer(-((run + 1) * 2)), soFromCurrent);
|
||||
FBuffer.Write(run, SizeOf(Word));
|
||||
FBuffer.Seek(Integer(run * 2), soFromCurrent);
|
||||
run := 0;
|
||||
end;
|
||||
FBuffer.Write(run, SizeOf(Word)); //just write "0"
|
||||
FBuffer.Write(run, SizeOf(Word)); //... two times, to indicate the end of that line
|
||||
end;
|
||||
FBuffer.Position := start - (height * 2); //now update the lookup table with our new values
|
||||
for i := 0 to height - 1 do
|
||||
FBuffer.Write(lookup[i], SizeOf(SmallInt));
|
||||
end;
|
||||
argbGraphic.Free;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2009 Andreas Schneider
|
||||
*)
|
||||
unit UArt;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, Imaging, ImagingTypes, ImagingCanvases, ImagingClasses,
|
||||
UMulBlock, UGenericIndex, UHue;
|
||||
|
||||
type
|
||||
TArtType = (atLand, atStatic, atLandFlat);
|
||||
TArt = class(TMulBlock)
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType); overload;
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AHue: THue; APartialHue: Boolean); overload;
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AArtColor: Word; AHue: THue; APartialHue: Boolean); overload;
|
||||
destructor Destroy; override;
|
||||
function Clone: TArt; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure Write(AData: TStream); override;
|
||||
procedure RefreshBuffer;
|
||||
protected
|
||||
FArtType: TArtType;
|
||||
FHeader: LongInt;
|
||||
FGraphic: TSingleImage;
|
||||
FBuffer: TStream;
|
||||
public
|
||||
property ArtType: TArtType read FArtType write FArtType;
|
||||
property Header: LongInt read FHeader write FHeader;
|
||||
property Graphic: TSingleImage read FGraphic;
|
||||
property Buffer: TStream read FBuffer;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
type
|
||||
PWordArray = ^TWordArray;
|
||||
TWordArray = array[0..16383] of Word;
|
||||
|
||||
constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType);
|
||||
begin
|
||||
Create(AData, AIndex, AArtType, 0, nil, False);
|
||||
end;
|
||||
|
||||
constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AHue: THue; APartialHue: Boolean);
|
||||
begin
|
||||
Create(AData, AIndex, AArtType, 0, AHue, APartialHue);
|
||||
end;
|
||||
|
||||
constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AArtColor: Word; AHue: THue; APartialHue: Boolean);
|
||||
var
|
||||
i, x, y, start: Integer;
|
||||
iCurrentHeight, iCurrentWidth: Integer;
|
||||
width, height: SmallInt;
|
||||
lookup: array of integer;
|
||||
color, run, offset: Word;
|
||||
block: TMemoryStream;
|
||||
P: PWordArray;
|
||||
r, g, b: Byte;
|
||||
|
||||
begin
|
||||
FBuffer := TMemoryStream.Create;
|
||||
FArtType := AArtType;
|
||||
AArtColor := AArtColor or $8000; //set alpha bit on background
|
||||
if Assigned(AData) and (AIndex.Lookup > -1) then
|
||||
begin
|
||||
AData.Position := AIndex.Lookup;
|
||||
block := TMemoryStream.Create;
|
||||
block.CopyFrom(AData, AIndex.Size);
|
||||
block.Position := 0;
|
||||
|
||||
if AArtType = atLand then
|
||||
begin
|
||||
FGraphic:= TSingleImage.CreateFromParams(44, 44, ifA1R5G5B5);
|
||||
FillWord(FGraphic.Bits^, 44 * 44, AArtColor);
|
||||
for y := 0 to 21 do
|
||||
begin
|
||||
P := FGraphic.Bits + y * 44 * 2;
|
||||
block.Read(P^[22 - (y + 1)], (y + 1) * 4);
|
||||
end;
|
||||
for y := 0 to 21 do
|
||||
begin
|
||||
P := FGraphic.Bits + (22 + y) * 44 * 2;
|
||||
block.Read(P^[y], (22 - y) * 4);
|
||||
end;
|
||||
for i := 0 to 44 * 44 - 1 do
|
||||
PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit
|
||||
end else if AArtType = atLandFlat then
|
||||
begin
|
||||
FGraphic:= TSingleImage.CreateFromParams(44, 44, ifA1R5G5B5);
|
||||
for i := 1 to 22 do
|
||||
begin
|
||||
for x := 0 to i * 2 - 1 do
|
||||
begin
|
||||
y := i * 2 - x - 1;
|
||||
block.Read(color, SizeOf(Word));
|
||||
PWordArray(FGraphic.Bits + y * 44 * 2)^[x] := color;
|
||||
if y > 0 then
|
||||
PWordArray(FGraphic.Bits + (y - 1) * 44 * 2)^[x] := color;
|
||||
end;
|
||||
end;
|
||||
for i := 22 to 43 do
|
||||
begin
|
||||
for y := 0 to (44 - i) * 2 - 1 do
|
||||
begin
|
||||
x := 42 - (43 - i) * 2 + y;
|
||||
block.Read(color, SizeOf(Word));
|
||||
PWordArray(FGraphic.Bits + (43 - y) * 44 * 2)^[x] := color;
|
||||
if y > 0 then
|
||||
PWordArray(FGraphic.Bits + (44 - y) * 44 * 2)^[x] := color;
|
||||
end;
|
||||
end;
|
||||
for i := 0 to 44 * 44 - 1 do
|
||||
PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit
|
||||
end else if AArtType = atStatic then
|
||||
begin
|
||||
block.Read(FHeader, SizeOf(LongInt));
|
||||
block.Read(width, SizeOf(SmallInt));
|
||||
block.Read(height, SizeOf(SmallInt));
|
||||
FGraphic:= TSingleImage.CreateFromParams(width, height, ifA1R5G5B5);
|
||||
FillWord(FGraphic.Bits^, width * height, AArtColor);
|
||||
SetLength(lookup, height);
|
||||
start := block.Position + (height * 2);
|
||||
for i := 0 to height - 1 do
|
||||
begin
|
||||
block.Read(offset, SizeOf(Word));
|
||||
lookup[i] := start + (offset * 2);
|
||||
end;
|
||||
for iCurrentHeight := 0 to height - 1 do
|
||||
begin
|
||||
block.Position := lookup[iCurrentHeight];
|
||||
iCurrentWidth := 0;
|
||||
P := FGraphic.Bits + iCurrentHeight * width * 2;
|
||||
while (block.Read(offset, SizeOf(Word)) = SizeOf(Word)) and
|
||||
(block.Read(run, SizeOf(Word)) = SizeOf(Word)) and
|
||||
(offset + run <> 0) do
|
||||
begin
|
||||
inc(iCurrentWidth, offset);
|
||||
for i := 0 to run - 1 do
|
||||
begin
|
||||
block.Read(color, SizeOf(Word));
|
||||
P^[iCurrentWidth + i] := color;
|
||||
end;
|
||||
inc(iCurrentWidth, run);
|
||||
end;
|
||||
end;
|
||||
|
||||
if AHue <> nil then
|
||||
begin
|
||||
for i := 0 to width * height - 1 do
|
||||
begin
|
||||
color := PWordArray(FGraphic.Bits)^[i];
|
||||
if color <> AArtColor then
|
||||
begin
|
||||
r := (color and $7C00) shr 10;
|
||||
if APartialHue then
|
||||
begin
|
||||
g := (color and $3E0) shr 5;
|
||||
b := color and $1F;
|
||||
if (r = g) and (g = b) then
|
||||
color := AHue.ColorTable[r];
|
||||
end else
|
||||
color := AHue.ColorTable[r];
|
||||
end;
|
||||
PWordArray(FGraphic.Bits)^[i] := color;
|
||||
end;
|
||||
end;
|
||||
|
||||
for i := 0 to width * height - 1 do
|
||||
PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit
|
||||
end else
|
||||
FGraphic:= TSingleImage.Create;
|
||||
if Assigned(block) then block.Free;
|
||||
end else
|
||||
begin
|
||||
FHeader := 1;
|
||||
FGraphic := TSingleImage.Create;
|
||||
end;
|
||||
FGraphic.Format := ifA8R8G8B8;
|
||||
end;
|
||||
|
||||
destructor TArt.Destroy;
|
||||
begin
|
||||
if assigned(FGraphic) then FGraphic.Free;
|
||||
if assigned(FBuffer) then FBuffer.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TArt.Clone: TArt;
|
||||
begin
|
||||
Result := TArt.Create(nil, nil, FArtType);
|
||||
Result.FHeader := FHeader;
|
||||
Result.FGraphic.Assign(FGraphic);
|
||||
end;
|
||||
|
||||
procedure TArt.Write(AData: TStream);
|
||||
begin
|
||||
FBuffer.Position := 0;
|
||||
AData.CopyFrom(FBuffer, FBuffer.Size);
|
||||
end;
|
||||
|
||||
function TArt.GetSize: Integer;
|
||||
begin
|
||||
RefreshBuffer;
|
||||
Result := FBuffer.Size
|
||||
end;
|
||||
|
||||
procedure TArt.RefreshBuffer;
|
||||
var
|
||||
argbGraphic: TSingleImage;
|
||||
i, x, y, lineWidth, start: Integer;
|
||||
iCurrentHeight, iCurrentWidth: Integer;
|
||||
width, height: SmallInt;
|
||||
color, run, offset: Word;
|
||||
lookup: array of SmallInt;
|
||||
begin
|
||||
argbGraphic := TSingleImage.CreateFromImage(FGraphic);
|
||||
argbGraphic.Format := ifA1R5G5B5;
|
||||
for i := 0 to argbGraphic.Width * argbGraphic.Height - 1 do
|
||||
PWordArray(argbGraphic.Bits)^[i] := PWordArray(argbGraphic.Bits)^[i] xor $8000; //invert alpha bit
|
||||
FBuffer.Size := 0;
|
||||
if FArtType = atLand then
|
||||
begin
|
||||
if (argbGraphic.Height <> 44) or (argbGraphic.Width <> 44) then Exit;
|
||||
x := 21;
|
||||
y := 0;
|
||||
lineWidth := 2;
|
||||
for i := 1 to 22 do
|
||||
begin
|
||||
Dec(x);
|
||||
FBuffer.Write(PWordArray(argbGraphic.Bits + y * 44 * 2)^[x + i], lineWidth);
|
||||
Inc(y);
|
||||
Inc(lineWidth, 2);
|
||||
end;
|
||||
for i := 1 to 22 do
|
||||
begin
|
||||
Dec(lineWidth, 2);
|
||||
FBuffer.Write(PWordArray(argbGraphic.Bits + y * 44 * 2)^[x + i], lineWidth);
|
||||
Inc(x);
|
||||
Inc(y);
|
||||
end;
|
||||
end else if FArtType = atStatic then
|
||||
begin
|
||||
if (argbGraphic.Height = 0) or (argbGraphic.Width = 0) then Exit;
|
||||
width := argbGraphic.Width;
|
||||
height := argbGraphic.Height;
|
||||
FBuffer.Write(FHeader, SizeOf(LongInt));
|
||||
FBuffer.Write(width, SizeOf(SmallInt));
|
||||
FBuffer.Write(height, SizeOf(SmallInt));
|
||||
SetLength(lookup, height);
|
||||
for i := 0 to height - 1 do
|
||||
FBuffer.Write(lookup[i], SizeOf(SmallInt)); //placeholders for the lookup table
|
||||
start := FBuffer.Position;
|
||||
for iCurrentHeight := 0 to height - 1 do
|
||||
begin
|
||||
lookup[iCurrentHeight] := SmallInt((FBuffer.Position - start) div 2); //remember the lookup offset for the current line
|
||||
offset := 0;
|
||||
run := 0;
|
||||
for iCurrentWidth := 0 to width - 1 do //process every pixel on the current line
|
||||
begin
|
||||
color := PWordArray(FGraphic.Bits + iCurrentHeight * width * 2)^[iCurrentWidth];
|
||||
if (color and $8000 = 0) and (run = 0) then //new visible pixel found
|
||||
begin
|
||||
FBuffer.Write(offset, SizeOf(Word));
|
||||
FBuffer.Write(offset, SizeOf(Word)); //just a placeholder for the "run length"
|
||||
run := 1;
|
||||
FBuffer.Write(color, SizeOf(Word));
|
||||
end else if (color and $8000 = 0) and (run > 0) then //another visible pixel found
|
||||
begin
|
||||
inc(run);
|
||||
FBuffer.Write(color, SizeOf(Word));
|
||||
end else if (color and $8000 = $8000) and (run > 0) then //after some visible pixels this one is invisible, so stop the current run
|
||||
begin
|
||||
FBuffer.Seek(Integer(-((run + 1) * 2)), soFromCurrent); //jump back ...
|
||||
FBuffer.Write(run, SizeOf(Word)); //... to write the actual "run length" ...
|
||||
FBuffer.Seek(Integer(run * 2), soFromCurrent); //... and jump forth again to proceed
|
||||
run := 0;
|
||||
offset := 1;
|
||||
end else
|
||||
inc(offset);
|
||||
end;
|
||||
if run > 0 then //no more pixels but the "run" didn't end yet ;-)
|
||||
begin
|
||||
FBuffer.Seek(Integer(-((run + 1) * 2)), soFromCurrent);
|
||||
FBuffer.Write(run, SizeOf(Word));
|
||||
FBuffer.Seek(Integer(run * 2), soFromCurrent);
|
||||
run := 0;
|
||||
end;
|
||||
FBuffer.Write(run, SizeOf(Word)); //just write "0"
|
||||
FBuffer.Write(run, SizeOf(Word)); //... two times, to indicate the end of that line
|
||||
end;
|
||||
FBuffer.Position := start - (height * 2); //now update the lookup table with our new values
|
||||
for i := 0 to height - 1 do
|
||||
FBuffer.Write(lookup[i], SizeOf(SmallInt));
|
||||
end;
|
||||
argbGraphic.Free;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2007 Andreas Schneider
|
||||
* Portions Copyright 2009 Andreas Schneider
|
||||
*)
|
||||
unit UHue;
|
||||
|
||||
@@ -30,10 +30,14 @@ unit UHue;
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, Classes, Graphics, UMulBlock, UGraphicHelper;
|
||||
SysUtils, Classes, Graphics, UMulBlock;
|
||||
|
||||
type
|
||||
|
||||
TColorTable = array[0..31] of Word;
|
||||
|
||||
{ THue }
|
||||
|
||||
THue = class(TMulBlock)
|
||||
constructor Create(AData: TStream);
|
||||
function Clone: THue; override;
|
||||
@@ -52,7 +56,11 @@ type
|
||||
property TableEnd: Word read FTableEnd write FTableEnd;
|
||||
property Name: string read GetName write SetName;
|
||||
end;
|
||||
|
||||
THueEntries = array[0..7] of THue;
|
||||
|
||||
{ THueGroup }
|
||||
|
||||
THueGroup = class(TMulBlock)
|
||||
constructor Create(AData: TStream);
|
||||
destructor Destroy; override;
|
||||
@@ -92,7 +100,7 @@ var
|
||||
color: Word;
|
||||
begin
|
||||
SetLength(FName, 20);
|
||||
if Assigned(AData) then
|
||||
if AData <> nil then
|
||||
begin
|
||||
buffer := TMemoryStream.Create;
|
||||
buffer.CopyFrom(AData, 88);
|
||||
@@ -158,7 +166,7 @@ var
|
||||
i: Integer;
|
||||
buffer: TMemoryStream;
|
||||
begin
|
||||
if Assigned(AData) then
|
||||
if AData <> nil then
|
||||
begin
|
||||
buffer := TMemoryStream.Create;
|
||||
buffer.CopyFrom(AData, 708);
|
||||
@@ -170,7 +178,7 @@ begin
|
||||
for i := 0 to 7 do
|
||||
FHueEntries[i] := THue.Create(buffer);
|
||||
|
||||
if Assigned(buffer) then FreeAndNil(buffer);
|
||||
buffer.Free;
|
||||
end;
|
||||
|
||||
destructor THueGroup.Destroy;
|
||||
@@ -178,9 +186,8 @@ var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := 0 to 7 do
|
||||
if Assigned(FHueEntries[i]) then
|
||||
FreeAndNil(FHueEntries[i]);
|
||||
inherited;
|
||||
FreeAndNil(FHueEntries[i]);
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
function THueGroup.GetHueEntry(AIndex: Integer): THue;
|
||||
@@ -195,7 +202,7 @@ end;
|
||||
|
||||
procedure THueGroup.SetHueEntry(AIndex: Integer; AValue: THue);
|
||||
begin
|
||||
if Assigned(FHueEntries[AIndex]) then FreeAndNil(FHueEntries[AIndex]);
|
||||
FreeAndNil(FHueEntries[AIndex]);
|
||||
FHueEntries[AIndex] := AValue;
|
||||
end;
|
||||
|
||||
|
||||
444
UOLib/UMap.pas
444
UOLib/UMap.pas
@@ -1,222 +1,222 @@
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2009 Andreas Schneider
|
||||
*)
|
||||
unit UMap;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, Classes, fgl, UMulBlock, UWorldItem;
|
||||
|
||||
const
|
||||
MapCellSize = 3;
|
||||
MapBlockSize = 4 + (64 * MapCellSize);
|
||||
|
||||
type
|
||||
|
||||
{ TMapCell }
|
||||
|
||||
TMapCell = class(TWorldItem)
|
||||
constructor Create(AOwner: TWorldBlock; AData: TStream; AX, AY: Word); overload;
|
||||
constructor Create(AOwner: TWorldBlock; AData: TStream); overload;
|
||||
protected
|
||||
FIsGhost: Boolean;
|
||||
FGhostZ: ShortInt;
|
||||
FGhostID: Word;
|
||||
function GetTileID: Word; override;
|
||||
function GetZ: ShortInt; override;
|
||||
public
|
||||
property Altitude: ShortInt read GetZ write SetZ;
|
||||
property IsGhost: Boolean read FIsGhost write FIsGhost;
|
||||
property GhostZ: ShortInt read FGhostZ write FGhostZ;
|
||||
property GhostID: Word write FGhostID;
|
||||
|
||||
function Clone: TMapCell; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure Write(AData: TStream); override;
|
||||
end;
|
||||
|
||||
TMapCellList = specialize TFPGObjectList<TMapCell>;
|
||||
|
||||
{ TMapBlock }
|
||||
|
||||
TMapBlock = class(TWorldBlock)
|
||||
constructor Create(AData: TStream; AX, AY: Word); overload;
|
||||
constructor Create(AData: TStream); overload;
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
FHeader: LongInt;
|
||||
public
|
||||
Cells: array[0..63] of TMapCell;
|
||||
property Header: LongInt read FHeader write FHeader;
|
||||
function Clone: TMapBlock; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure Write(AData: TStream); override;
|
||||
end;
|
||||
|
||||
function GetMapCellOffset(ABlock: Integer): Integer;
|
||||
|
||||
implementation
|
||||
|
||||
function GetMapCellOffset(ABlock: Integer): Integer;
|
||||
var
|
||||
group, tile: Integer;
|
||||
begin
|
||||
group := ABlock div 64;
|
||||
tile := ABlock mod 64;
|
||||
|
||||
Result := group * MapBlockSize + 4 + tile * MapCellSize;
|
||||
end;
|
||||
|
||||
{ TMapCell }
|
||||
|
||||
constructor TMapCell.Create(AOwner: TWorldBlock; AData: TStream; AX, AY: Word);
|
||||
begin
|
||||
inherited Create(AOwner);
|
||||
|
||||
FX := AX;
|
||||
FY := AY;
|
||||
if AData <> nil then
|
||||
begin
|
||||
AData.Read(FTileID, SizeOf(Word));
|
||||
AData.Read(FZ, SizeOf(ShortInt));
|
||||
end;
|
||||
|
||||
FIsGhost := False;
|
||||
|
||||
InitOriginalState;
|
||||
end;
|
||||
|
||||
constructor TMapCell.Create(AOwner: TWorldBlock; AData: TStream);
|
||||
begin
|
||||
Create(AOwner, AData, 0, 0);
|
||||
end;
|
||||
|
||||
function TMapCell.GetTileID: Word;
|
||||
begin
|
||||
if FIsGhost then
|
||||
Result := FGhostID
|
||||
else
|
||||
Result := FTileID;
|
||||
end;
|
||||
|
||||
function TMapCell.GetZ: ShortInt;
|
||||
begin
|
||||
if FIsGhost then
|
||||
Result := FGhostZ
|
||||
else
|
||||
Result := FZ;
|
||||
end;
|
||||
|
||||
function TMapCell.Clone: TMapCell;
|
||||
begin
|
||||
Result := TMapCell.Create(nil, nil);
|
||||
Result.FX := FX;
|
||||
Result.FY := FY;
|
||||
Result.FZ := FZ;
|
||||
Result.FTileID := FTileID;
|
||||
end;
|
||||
|
||||
procedure TMapCell.Write(AData: TStream);
|
||||
begin
|
||||
AData.Write(FTileID, SizeOf(Word));
|
||||
AData.Write(FZ, SizeOf(ShortInt));
|
||||
end;
|
||||
|
||||
function TMapCell.GetSize: Integer;
|
||||
begin
|
||||
Result := MapCellSize;
|
||||
end;
|
||||
|
||||
{ TMapBlock }
|
||||
|
||||
constructor TMapBlock.Create(AData: TStream; AX, AY: Word);
|
||||
var
|
||||
iX, iY: Integer;
|
||||
buffer: TMemoryStream;
|
||||
begin
|
||||
inherited Create;
|
||||
FX := AX;
|
||||
FY := AY;
|
||||
try
|
||||
buffer := nil;
|
||||
if Assigned(AData) then
|
||||
begin
|
||||
buffer := TMemoryStream.Create;
|
||||
buffer.CopyFrom(AData, 196);
|
||||
buffer.Position := 0;
|
||||
buffer.Read(FHeader, SizeOf(LongInt));
|
||||
end;
|
||||
for iY := 0 to 7 do
|
||||
for iX := 0 to 7 do
|
||||
Cells[iY * 8 + iX] := TMapCell.Create(Self, buffer, AX * 8 + iX, AY * 8 + iY);
|
||||
finally
|
||||
if Assigned(buffer) then FreeAndNil(buffer);
|
||||
end;
|
||||
end;
|
||||
|
||||
constructor TMapBlock.Create(AData: TStream);
|
||||
begin
|
||||
Create(AData, 0, 0);
|
||||
end;
|
||||
|
||||
destructor TMapBlock.Destroy;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := 0 to 63 do
|
||||
Cells[i].Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TMapBlock.Clone: TMapBlock;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
Result := TMapBlock.Create(nil);
|
||||
Result.FX := FX;
|
||||
Result.FY := FY;
|
||||
for i := 0 to 63 do
|
||||
Result.Cells[i] := Cells[i].Clone;
|
||||
end;
|
||||
|
||||
procedure TMapBlock.Write(AData: TStream);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
AData.Write(FHeader, SizeOf(LongInt));
|
||||
for i := 0 to 63 do
|
||||
Cells[i].Write(AData);
|
||||
end;
|
||||
|
||||
function TMapBlock.GetSize: Integer;
|
||||
begin
|
||||
Result := MapBlockSize;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2009 Andreas Schneider
|
||||
*)
|
||||
unit UMap;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, Classes, fgl, UWorldItem;
|
||||
|
||||
const
|
||||
MapCellSize = 3;
|
||||
MapBlockSize = 4 + (64 * MapCellSize);
|
||||
|
||||
type
|
||||
|
||||
{ TMapCell }
|
||||
|
||||
TMapCell = class(TWorldItem)
|
||||
constructor Create(AOwner: TWorldBlock; AData: TStream; AX, AY: Word); overload;
|
||||
constructor Create(AOwner: TWorldBlock; AData: TStream); overload;
|
||||
protected
|
||||
FIsGhost: Boolean;
|
||||
FGhostZ: ShortInt;
|
||||
FGhostID: Word;
|
||||
function GetTileID: Word; override;
|
||||
function GetZ: ShortInt; override;
|
||||
public
|
||||
property Altitude: ShortInt read GetZ write SetZ;
|
||||
property IsGhost: Boolean read FIsGhost write FIsGhost;
|
||||
property GhostZ: ShortInt read FGhostZ write FGhostZ;
|
||||
property GhostID: Word write FGhostID;
|
||||
|
||||
function Clone: TMapCell; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure Write(AData: TStream); override;
|
||||
end;
|
||||
|
||||
TMapCellList = specialize TFPGObjectList<TMapCell>;
|
||||
|
||||
{ TMapBlock }
|
||||
|
||||
TMapBlock = class(TWorldBlock)
|
||||
constructor Create(AData: TStream; AX, AY: Word); overload;
|
||||
constructor Create(AData: TStream); overload;
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
FHeader: LongInt;
|
||||
public
|
||||
Cells: array[0..63] of TMapCell;
|
||||
property Header: LongInt read FHeader write FHeader;
|
||||
function Clone: TMapBlock; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure Write(AData: TStream); override;
|
||||
end;
|
||||
|
||||
function GetMapCellOffset(ABlock: Integer): Integer;
|
||||
|
||||
implementation
|
||||
|
||||
function GetMapCellOffset(ABlock: Integer): Integer;
|
||||
var
|
||||
group, tile: Integer;
|
||||
begin
|
||||
group := ABlock div 64;
|
||||
tile := ABlock mod 64;
|
||||
|
||||
Result := group * MapBlockSize + 4 + tile * MapCellSize;
|
||||
end;
|
||||
|
||||
{ TMapCell }
|
||||
|
||||
constructor TMapCell.Create(AOwner: TWorldBlock; AData: TStream; AX, AY: Word);
|
||||
begin
|
||||
inherited Create(AOwner);
|
||||
|
||||
FX := AX;
|
||||
FY := AY;
|
||||
if AData <> nil then
|
||||
begin
|
||||
AData.Read(FTileID, SizeOf(Word));
|
||||
AData.Read(FZ, SizeOf(ShortInt));
|
||||
end;
|
||||
|
||||
FIsGhost := False;
|
||||
|
||||
InitOriginalState;
|
||||
end;
|
||||
|
||||
constructor TMapCell.Create(AOwner: TWorldBlock; AData: TStream);
|
||||
begin
|
||||
Create(AOwner, AData, 0, 0);
|
||||
end;
|
||||
|
||||
function TMapCell.GetTileID: Word;
|
||||
begin
|
||||
if FIsGhost then
|
||||
Result := FGhostID
|
||||
else
|
||||
Result := FTileID;
|
||||
end;
|
||||
|
||||
function TMapCell.GetZ: ShortInt;
|
||||
begin
|
||||
if FIsGhost then
|
||||
Result := FGhostZ
|
||||
else
|
||||
Result := FZ;
|
||||
end;
|
||||
|
||||
function TMapCell.Clone: TMapCell;
|
||||
begin
|
||||
Result := TMapCell.Create(nil, nil);
|
||||
Result.FX := FX;
|
||||
Result.FY := FY;
|
||||
Result.FZ := FZ;
|
||||
Result.FTileID := FTileID;
|
||||
end;
|
||||
|
||||
procedure TMapCell.Write(AData: TStream);
|
||||
begin
|
||||
AData.Write(FTileID, SizeOf(Word));
|
||||
AData.Write(FZ, SizeOf(ShortInt));
|
||||
end;
|
||||
|
||||
function TMapCell.GetSize: Integer;
|
||||
begin
|
||||
Result := MapCellSize;
|
||||
end;
|
||||
|
||||
{ TMapBlock }
|
||||
|
||||
constructor TMapBlock.Create(AData: TStream; AX, AY: Word);
|
||||
var
|
||||
iX, iY: Integer;
|
||||
buffer: TMemoryStream;
|
||||
begin
|
||||
inherited Create;
|
||||
FX := AX;
|
||||
FY := AY;
|
||||
try
|
||||
buffer := nil;
|
||||
if Assigned(AData) then
|
||||
begin
|
||||
buffer := TMemoryStream.Create;
|
||||
buffer.CopyFrom(AData, 196);
|
||||
buffer.Position := 0;
|
||||
buffer.Read(FHeader, SizeOf(LongInt));
|
||||
end;
|
||||
for iY := 0 to 7 do
|
||||
for iX := 0 to 7 do
|
||||
Cells[iY * 8 + iX] := TMapCell.Create(Self, buffer, AX * 8 + iX, AY * 8 + iY);
|
||||
finally
|
||||
if Assigned(buffer) then FreeAndNil(buffer);
|
||||
end;
|
||||
end;
|
||||
|
||||
constructor TMapBlock.Create(AData: TStream);
|
||||
begin
|
||||
Create(AData, 0, 0);
|
||||
end;
|
||||
|
||||
destructor TMapBlock.Destroy;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := 0 to 63 do
|
||||
Cells[i].Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TMapBlock.Clone: TMapBlock;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
Result := TMapBlock.Create(nil);
|
||||
Result.FX := FX;
|
||||
Result.FY := FY;
|
||||
for i := 0 to 63 do
|
||||
Result.Cells[i] := Cells[i].Clone;
|
||||
end;
|
||||
|
||||
procedure TMapBlock.Write(AData: TStream);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
AData.Write(FHeader, SizeOf(LongInt));
|
||||
for i := 0 to 63 do
|
||||
Cells[i].Write(AData);
|
||||
end;
|
||||
|
||||
function TMapBlock.GetSize: Integer;
|
||||
begin
|
||||
Result := MapBlockSize;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2007 Andreas Schneider
|
||||
* Portions Copyright 2009 Andreas Schneider
|
||||
*)
|
||||
unit UMulBlock;
|
||||
|
||||
@@ -38,7 +38,7 @@ type
|
||||
|
||||
{ TMulBlockEventHandler }
|
||||
|
||||
TMulBlockEventHandler = class(TObject)
|
||||
TMulBlockEventHandler = class
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
@@ -51,7 +51,7 @@ type
|
||||
|
||||
{ TMulBlock }
|
||||
|
||||
TMulBlock = class(TObject)
|
||||
TMulBlock = class
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
|
||||
@@ -1,256 +1,254 @@
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2009 Andreas Schneider
|
||||
*)
|
||||
unit UStatics;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, Classes, fgl, UGenericIndex, UWorldItem, UTiledata;
|
||||
|
||||
type
|
||||
{ TStaticItem }
|
||||
|
||||
TStaticItem = class(TWorldItem)
|
||||
constructor Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
|
||||
ABlockY: Word); overload;
|
||||
constructor Create(AOwner: TWorldBlock; AData: TStream); overload;
|
||||
protected
|
||||
{ Members }
|
||||
FHue: Word;
|
||||
FOrgHue: Word;
|
||||
|
||||
{ Methods }
|
||||
function HasChanged: Boolean; override;
|
||||
procedure SetHue(AHue: Word);
|
||||
public
|
||||
{ Fields }
|
||||
property Hue: Word read FHue write SetHue;
|
||||
|
||||
{ Methods }
|
||||
function Clone: TStaticItem; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure InitOriginalState; override;
|
||||
procedure UpdatePriorities(ATileData: TStaticTiledata; ASolver: Integer);
|
||||
procedure Write(AData: TStream); override;
|
||||
end;
|
||||
|
||||
TStaticItemList = specialize TFPGObjectList<TStaticItem>;
|
||||
|
||||
{ TStaticBlock}
|
||||
|
||||
TStaticBlock = class(TWorldBlock)
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word);
|
||||
overload;
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
{ Members }
|
||||
FItems: TStaticItemList;
|
||||
public
|
||||
{ Fields }
|
||||
property Items: TStaticItemList read FItems write FItems;
|
||||
|
||||
{ Methods }
|
||||
function Clone: TStaticBlock; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure ReverseWrite(AData: TStream);
|
||||
procedure Sort;
|
||||
procedure Write(AData: TStream); override;
|
||||
end;
|
||||
|
||||
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
|
||||
|
||||
implementation
|
||||
|
||||
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
|
||||
begin
|
||||
Result := CompareWorldItems(AStatic1, AStatic2);
|
||||
end;
|
||||
|
||||
{ TStaticItem }
|
||||
|
||||
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
|
||||
ABlockY: Word);
|
||||
var
|
||||
iX, iY: Byte;
|
||||
begin
|
||||
inherited Create(AOwner);
|
||||
|
||||
if AData <> nil then
|
||||
begin
|
||||
AData.Read(FTileID, SizeOf(SmallInt));
|
||||
AData.Read(iX, SizeOf(Byte));
|
||||
AData.Read(iY, SizeOf(Byte));
|
||||
AData.Read(FZ, SizeOf(ShortInt));
|
||||
AData.Read(FHue, SizeOf(SmallInt));
|
||||
|
||||
FX := ABlockX * 8 + iX;
|
||||
FY := ABlockY * 8 + iY;
|
||||
end;
|
||||
|
||||
InitOriginalState;
|
||||
end;
|
||||
|
||||
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream);
|
||||
begin
|
||||
Create(AOwner, AData, 0, 0);
|
||||
end;
|
||||
|
||||
function TStaticItem.HasChanged: Boolean;
|
||||
begin
|
||||
Result := (FHue <> FOrgHue) or inherited HasChanged;
|
||||
end;
|
||||
|
||||
procedure TStaticItem.SetHue(AHue: Word);
|
||||
begin
|
||||
FHue := AHue;
|
||||
DoChanged;
|
||||
end;
|
||||
|
||||
function TStaticItem.Clone: TStaticItem;
|
||||
begin
|
||||
Result := TStaticItem.Create(nil, nil);
|
||||
Result.FTileID := FTileID;
|
||||
Result.FX := FX;
|
||||
Result.FY := FY;
|
||||
Result.FZ := FZ;
|
||||
Result.FHue := FHue;
|
||||
end;
|
||||
|
||||
function TStaticItem.GetSize: Integer;
|
||||
begin
|
||||
Result := 7;
|
||||
end;
|
||||
|
||||
procedure TStaticItem.InitOriginalState;
|
||||
begin
|
||||
FOrgHue := FHue;
|
||||
inherited InitOriginalState;
|
||||
end;
|
||||
|
||||
procedure TStaticItem.UpdatePriorities(ATileData: TStaticTiledata;
|
||||
ASolver: Integer);
|
||||
begin
|
||||
FPriorityBonus := 0;
|
||||
if not (tdfBackground in ATileData.Flags) then
|
||||
Inc(FPriorityBonus);
|
||||
if ATileData.Height > 0 then
|
||||
Inc(FPriorityBonus);
|
||||
FPriority := Z + FPriorityBonus;
|
||||
FPrioritySolver := ASolver;
|
||||
end;
|
||||
|
||||
procedure TStaticItem.Write(AData: TStream);
|
||||
var
|
||||
iX, iY: Byte;
|
||||
begin
|
||||
iX := FX mod 8;
|
||||
iY := FY mod 8;
|
||||
|
||||
AData.Write(FTileID, SizeOf(SmallInt));
|
||||
AData.Write(iX, SizeOf(Byte));
|
||||
AData.Write(iY, SizeOf(Byte));
|
||||
AData.Write(FZ, SizeOf(ShortInt));
|
||||
AData.Write(FHue, SizeOf(SmallInt));
|
||||
end;
|
||||
|
||||
{ TStaticBlock }
|
||||
|
||||
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex;
|
||||
AX, AY: Word);
|
||||
var
|
||||
i: Integer;
|
||||
block: TMemoryStream;
|
||||
begin
|
||||
inherited Create;
|
||||
FX := AX;
|
||||
FY := AY;
|
||||
|
||||
FItems := TStaticItemList.Create(True);
|
||||
if (AData <> nil) and (AIndex.Lookup > 0) and (AIndex.Size > 0) then
|
||||
begin
|
||||
AData.Position := AIndex.Lookup;
|
||||
block := TMemoryStream.Create;
|
||||
block.CopyFrom(AData, AIndex.Size);
|
||||
block.Position := 0;
|
||||
for i := 1 to (AIndex.Size div 7) do
|
||||
FItems.Add(TStaticItem.Create(Self, block, AX, AY));
|
||||
block.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex);
|
||||
begin
|
||||
Create(AData, AIndex, 0, 0);
|
||||
end;
|
||||
|
||||
destructor TStaticBlock.Destroy;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
FreeAndNil(FItems);
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TStaticBlock.Clone: TStaticBlock;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
Result := TStaticBlock.Create(nil, nil, FX, FY);
|
||||
for i := 0 to FItems.Count - 1 do
|
||||
Result.FItems.Add(FItems.Items[i].Clone);
|
||||
end;
|
||||
|
||||
function TStaticBlock.GetSize: Integer;
|
||||
begin
|
||||
Result := FItems.Count * 7;
|
||||
end;
|
||||
|
||||
procedure TStaticBlock.ReverseWrite(AData: TStream);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := FItems.Count - 1 downto 0 do
|
||||
FItems[i].Write(AData);
|
||||
end;
|
||||
|
||||
procedure TStaticBlock.Sort;
|
||||
begin
|
||||
FItems.Sort(@CompareStaticItems);
|
||||
end;
|
||||
|
||||
procedure TStaticBlock.Write(AData: TStream);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := 0 to FItems.Count - 1 do
|
||||
FItems[i].Write(AData);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2009 Andreas Schneider
|
||||
*)
|
||||
unit UStatics;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, Classes, fgl, UGenericIndex, UWorldItem, UTiledata;
|
||||
|
||||
type
|
||||
{ TStaticItem }
|
||||
|
||||
TStaticItem = class(TWorldItem)
|
||||
constructor Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
|
||||
ABlockY: Word); overload;
|
||||
constructor Create(AOwner: TWorldBlock; AData: TStream); overload;
|
||||
protected
|
||||
{ Members }
|
||||
FHue: Word;
|
||||
FOrgHue: Word;
|
||||
|
||||
{ Methods }
|
||||
function HasChanged: Boolean; override;
|
||||
procedure SetHue(AHue: Word);
|
||||
public
|
||||
{ Fields }
|
||||
property Hue: Word read FHue write SetHue;
|
||||
|
||||
{ Methods }
|
||||
function Clone: TStaticItem; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure InitOriginalState; override;
|
||||
procedure UpdatePriorities(ATileData: TStaticTiledata; ASolver: Integer);
|
||||
procedure Write(AData: TStream); override;
|
||||
end;
|
||||
|
||||
TStaticItemList = specialize TFPGObjectList<TStaticItem>;
|
||||
|
||||
{ TStaticBlock}
|
||||
|
||||
TStaticBlock = class(TWorldBlock)
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word);
|
||||
overload;
|
||||
constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
{ Members }
|
||||
FItems: TStaticItemList;
|
||||
public
|
||||
{ Fields }
|
||||
property Items: TStaticItemList read FItems write FItems;
|
||||
|
||||
{ Methods }
|
||||
function Clone: TStaticBlock; override;
|
||||
function GetSize: Integer; override;
|
||||
procedure ReverseWrite(AData: TStream);
|
||||
procedure Sort;
|
||||
procedure Write(AData: TStream); override;
|
||||
end;
|
||||
|
||||
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
|
||||
|
||||
implementation
|
||||
|
||||
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
|
||||
begin
|
||||
Result := CompareWorldItems(AStatic1, AStatic2);
|
||||
end;
|
||||
|
||||
{ TStaticItem }
|
||||
|
||||
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
|
||||
ABlockY: Word);
|
||||
var
|
||||
iX, iY: Byte;
|
||||
begin
|
||||
inherited Create(AOwner);
|
||||
|
||||
if AData <> nil then
|
||||
begin
|
||||
AData.Read(FTileID, SizeOf(SmallInt));
|
||||
AData.Read(iX, SizeOf(Byte));
|
||||
AData.Read(iY, SizeOf(Byte));
|
||||
AData.Read(FZ, SizeOf(ShortInt));
|
||||
AData.Read(FHue, SizeOf(SmallInt));
|
||||
|
||||
FX := ABlockX * 8 + iX;
|
||||
FY := ABlockY * 8 + iY;
|
||||
end;
|
||||
|
||||
InitOriginalState;
|
||||
end;
|
||||
|
||||
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream);
|
||||
begin
|
||||
Create(AOwner, AData, 0, 0);
|
||||
end;
|
||||
|
||||
function TStaticItem.HasChanged: Boolean;
|
||||
begin
|
||||
Result := (FHue <> FOrgHue) or inherited HasChanged;
|
||||
end;
|
||||
|
||||
procedure TStaticItem.SetHue(AHue: Word);
|
||||
begin
|
||||
FHue := AHue;
|
||||
DoChanged;
|
||||
end;
|
||||
|
||||
function TStaticItem.Clone: TStaticItem;
|
||||
begin
|
||||
Result := TStaticItem.Create(nil, nil);
|
||||
Result.FTileID := FTileID;
|
||||
Result.FX := FX;
|
||||
Result.FY := FY;
|
||||
Result.FZ := FZ;
|
||||
Result.FHue := FHue;
|
||||
end;
|
||||
|
||||
function TStaticItem.GetSize: Integer;
|
||||
begin
|
||||
Result := 7;
|
||||
end;
|
||||
|
||||
procedure TStaticItem.InitOriginalState;
|
||||
begin
|
||||
FOrgHue := FHue;
|
||||
inherited InitOriginalState;
|
||||
end;
|
||||
|
||||
procedure TStaticItem.UpdatePriorities(ATileData: TStaticTiledata;
|
||||
ASolver: Integer);
|
||||
begin
|
||||
FPriorityBonus := 0;
|
||||
if not (tdfBackground in ATileData.Flags) then
|
||||
Inc(FPriorityBonus);
|
||||
if ATileData.Height > 0 then
|
||||
Inc(FPriorityBonus);
|
||||
FPriority := Z + FPriorityBonus;
|
||||
FPrioritySolver := ASolver;
|
||||
end;
|
||||
|
||||
procedure TStaticItem.Write(AData: TStream);
|
||||
var
|
||||
iX, iY: Byte;
|
||||
begin
|
||||
iX := FX mod 8;
|
||||
iY := FY mod 8;
|
||||
|
||||
AData.Write(FTileID, SizeOf(SmallInt));
|
||||
AData.Write(iX, SizeOf(Byte));
|
||||
AData.Write(iY, SizeOf(Byte));
|
||||
AData.Write(FZ, SizeOf(ShortInt));
|
||||
AData.Write(FHue, SizeOf(SmallInt));
|
||||
end;
|
||||
|
||||
{ TStaticBlock }
|
||||
|
||||
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex;
|
||||
AX, AY: Word);
|
||||
var
|
||||
i: Integer;
|
||||
block: TMemoryStream;
|
||||
begin
|
||||
inherited Create;
|
||||
FX := AX;
|
||||
FY := AY;
|
||||
|
||||
FItems := TStaticItemList.Create(True);
|
||||
if (AData <> nil) and (AIndex.Lookup > 0) and (AIndex.Size > 0) then
|
||||
begin
|
||||
AData.Position := AIndex.Lookup;
|
||||
block := TMemoryStream.Create;
|
||||
block.CopyFrom(AData, AIndex.Size);
|
||||
block.Position := 0;
|
||||
for i := 1 to (AIndex.Size div 7) do
|
||||
FItems.Add(TStaticItem.Create(Self, block, AX, AY));
|
||||
block.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex);
|
||||
begin
|
||||
Create(AData, AIndex, 0, 0);
|
||||
end;
|
||||
|
||||
destructor TStaticBlock.Destroy;
|
||||
begin
|
||||
FreeAndNil(FItems);
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TStaticBlock.Clone: TStaticBlock;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
Result := TStaticBlock.Create(nil, nil, FX, FY);
|
||||
for i := 0 to FItems.Count - 1 do
|
||||
Result.FItems.Add(FItems.Items[i].Clone);
|
||||
end;
|
||||
|
||||
function TStaticBlock.GetSize: Integer;
|
||||
begin
|
||||
Result := FItems.Count * 7;
|
||||
end;
|
||||
|
||||
procedure TStaticBlock.ReverseWrite(AData: TStream);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := FItems.Count - 1 downto 0 do
|
||||
FItems[i].Write(AData);
|
||||
end;
|
||||
|
||||
procedure TStaticBlock.Sort;
|
||||
begin
|
||||
FItems.Sort(@CompareStaticItems);
|
||||
end;
|
||||
|
||||
procedure TStaticBlock.Write(AData: TStream);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := 0 to FItems.Count - 1 do
|
||||
FItems[i].Write(AData);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user