- Abstracted TMaterial

- Removed unnecessary width/height processing from TMaterial
- Added TSimpleMaterial for one-graphic-textures
This commit is contained in:
Andreas Schneider 2009-12-09 14:36:53 +01:00
parent 3df7b446f5
commit 2a3e17fa57
2 changed files with 53 additions and 58 deletions

View File

@ -46,25 +46,30 @@ type
{ TMaterial } //TODO : add ref counting
TMaterial = class
constructor Create(AWidth, AHeight: Integer; AGraphic: TSingleImage);
destructor Destroy; override;
protected
FWidth: Integer;
FHeight: Integer;
FRealWidth: Integer;
FRealHeight: Integer;
FTexture: GLuint;
FGraphic: TSingleImage;
FTexture: TGLuint;
FGraphic: TMultiImage;
function GetTexture: GLuint; virtual;
procedure UpdateTexture;
public
property Width: Integer read FWidth;
property Height: Integer read FHeight;
property RealWidth: Integer read FRealWidth;
property RealHeight: Integer read FRealHeight;
property Texture: GLuint read FTexture;
property Graphic: TSingleImage read FGraphic;
property Texture: GLuint read GetTexture;
function HitTest(AX, AY: Integer): Boolean;
procedure UpdateTexture;
end;
{ TSimpleMaterial }
TSimpleMaterial = class(TMaterial)
constructor Create(AGraphic: TBaseImage);
end;
TMaterialCache = specialize TCacheManager<TMaterial>;
@ -302,8 +307,7 @@ begin
begin
artEntry := TArt(ResMan.Art.Block[ATileID]);
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height,
artEntry.Graphic);
Result := TSimpleMaterial.Create(artEntry.Graphic);
FArtCache.StoreID(ATileID, Result);
artEntry.Free;
@ -326,8 +330,7 @@ begin
begin
artEntry := ResMan.Art.GetArt(ATileID, 0, AHue, APartialHue);
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height,
artEntry.Graphic);
Result := TSimpleMaterial.Create(artEntry.Graphic);
FArtCache.StoreID(id, Result);
artEntry.Free;
@ -343,8 +346,7 @@ begin
begin
artEntry := ResMan.Art.GetFlatLand(ATileID);
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height,
artEntry.Graphic);
Result := TSimpleMaterial.Create(artEntry.Graphic);
FFlatLandArtCache.StoreID(ATileID, Result);
artEntry.Free;
@ -382,8 +384,7 @@ begin
begin
texEntry := TTexture(ResMan.Texmaps.Block[texID]);
Result := TMaterial.Create(texEntry.Graphic.Width, texEntry.Graphic.Height,
texEntry.Graphic);
Result := TSimpleMaterial.Create(texEntry.Graphic);
FTexCache.StoreID(ATileID, Result);
texEntry.Free;
@ -1143,36 +1144,31 @@ end;
{ TMaterial }
constructor TMaterial.Create(AWidth, AHeight: Integer;
AGraphic: TSingleImage);
var
caps: TGLTextureCaps;
begin
inherited Create;
FRealWidth := AWidth;
FRealHeight := AHeight;
GetGLTextureCaps(caps);
if caps.NonPowerOfTwo then
begin
FWidth := AWidth;
FHeight := AHeight;
end else
begin
if IsPow2(AWidth) then FWidth := AWidth else FWidth := NextPow2(AWidth);
if IsPow2(AHeight) then FHeight := AHeight else FHeight := NextPow2(AHeight);
end;
FGraphic := TSingleImage.CreateFromParams(FWidth, FHeight, ifA8R8G8B8);
AGraphic.CopyTo(0, 0, AWidth, AHeight, FGraphic, 0, 0);
UpdateTexture;
end;
destructor TMaterial.Destroy;
begin
if FGraphic <> nil then FreeAndNil(FGraphic);
if FTexture <> 0 then glDeleteTextures(1, @FTexture);
FreeAndNil(FGraphic);
inherited Destroy;
end;
function TMaterial.GetTexture: GLuint;
begin
Result := FTexture;
end;
procedure TMaterial.UpdateTexture;
begin
if FTexture <> 0 then glDeleteTextures(1, @FTexture);
FTexture := CreateGLTextureFromImage(FGraphic.ImageDataPointer^, 0, 0, False,
ifUnknown, @FWidth, @FHeight);
glBindTexture(GL_TEXTURE_2D, FTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
end;
function TMaterial.HitTest(AX, AY: Integer): Boolean;
var
pixel: TColor32Rec;
@ -1187,18 +1183,6 @@ begin
end;
end;
procedure TMaterial.UpdateTexture;
begin
if FTexture <> 0 then glDeleteTextures(1, @FTexture);
FTexture := CreateGLTextureFromImage(FGraphic.ImageDataPointer^, 0, 0, False, ifUnknown, @FWidth, @FHeight);
glBindTexture(GL_TEXTURE_2D, FTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
end;
{ TScreenBuffer }
constructor TScreenBuffer.Create;
@ -1530,5 +1514,16 @@ begin
FFont.DrawText(x, y, FText);
end;
{ TSimpleMaterial }
constructor TSimpleMaterial.Create(AGraphic: TBaseImage);
begin
inherited Create;
FGraphic := TMultiImage.CreateFromImage(AGraphic);
FRealWidth := FGraphic.Width;
FRealHeight := FGraphic.Height;
UpdateTexture;
end;
end.

View File

@ -292,6 +292,7 @@ type
FCurrentTile: TWorldItem;
FSelectedTile: TWorldItem;
FVirtualTiles: TWorldItemList;
FVLayerImage: TSingleImage;
FVLayerMaterial: TMaterial;
FOverlayUI: TOverlayUI;
FLocationsFile: string;
@ -781,6 +782,7 @@ begin
begin
frmVirtualLayer.seZ.Value := EnsureRange(frmVirtualLayer.seZ.Value +
WheelDelta, -128, 127);
frmVirtualLayer.seZChange(frmVirtualLayer.seZ);
cursorNeedsUpdate := True;
Handled := True;
end else if not (ssCtrl in Shift) then
@ -818,7 +820,6 @@ end;
procedure TfrmMain.FormCreate(Sender: TObject);
var
virtualLayerGraphic: TSingleImage;
searchRec: TSearchRec;
begin
FAppDir := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName));
@ -870,10 +871,7 @@ begin
RegisterPacketHandler($0C, TPacketHandler.Create(0, @OnClientHandlingPacket));
virtualLayerGraphic := TSingleImage.CreateFromStream(ResourceManager.GetResource(2));
FVLayerMaterial := TMaterial.Create(virtualLayerGraphic.Width,
virtualLayerGraphic.Height, virtualLayerGraphic);
virtualLayerGraphic.Free;
FVLayerImage := TSingleImage.CreateFromStream(ResourceManager.GetResource(2));
FGLFont := TGLFont.Create;
FGLFont.LoadImage(ResourceManager.GetResource(3));
@ -1182,6 +1180,7 @@ begin
FreeAndNil(FTextureManager);
FreeAndNil(FScreenBuffer);
FreeAndNil(FOverlayUI);
FreeAndNil(FVLayerImage);
FreeAndNil(FVLayerMaterial);
FreeAndNil(FVirtualTiles);
FreeAndNil(FUndoList);
@ -1321,9 +1320,6 @@ begin
InitRender;
InitSize;
if FVLayerMaterial.Texture = 0 then
FVLayerMaterial.UpdateTexture;
glDisable(GL_DEPTH_TEST);
Render;
@ -2352,6 +2348,10 @@ begin
if frmVirtualLayer.cbShowLayer.Checked then
begin
Logger.Send([lcClient, lcDebug], 'Preparing Virtual Layer');
if FVLayerMaterial = nil then
FVLayerMaterial := TSimpleMaterial.Create(FVLayerImage);
i := 0;
for tileX := FX + FLowOffsetX to FX + FHighOffsetX do
begin