- 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 } //TODO : add ref counting
TMaterial = class TMaterial = class
constructor Create(AWidth, AHeight: Integer; AGraphic: TSingleImage);
destructor Destroy; override; destructor Destroy; override;
protected protected
FWidth: Integer; FWidth: Integer;
FHeight: Integer; FHeight: Integer;
FRealWidth: Integer; FRealWidth: Integer;
FRealHeight: Integer; FRealHeight: Integer;
FTexture: GLuint; FTexture: TGLuint;
FGraphic: TSingleImage; FGraphic: TMultiImage;
function GetTexture: GLuint; virtual;
procedure UpdateTexture;
public public
property Width: Integer read FWidth; property Width: Integer read FWidth;
property Height: Integer read FHeight; property Height: Integer read FHeight;
property RealWidth: Integer read FRealWidth; property RealWidth: Integer read FRealWidth;
property RealHeight: Integer read FRealHeight; property RealHeight: Integer read FRealHeight;
property Texture: GLuint read FTexture; property Texture: GLuint read GetTexture;
property Graphic: TSingleImage read FGraphic;
function HitTest(AX, AY: Integer): Boolean; function HitTest(AX, AY: Integer): Boolean;
procedure UpdateTexture; end;
{ TSimpleMaterial }
TSimpleMaterial = class(TMaterial)
constructor Create(AGraphic: TBaseImage);
end; end;
TMaterialCache = specialize TCacheManager<TMaterial>; TMaterialCache = specialize TCacheManager<TMaterial>;
@ -302,8 +307,7 @@ begin
begin begin
artEntry := TArt(ResMan.Art.Block[ATileID]); artEntry := TArt(ResMan.Art.Block[ATileID]);
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height, Result := TSimpleMaterial.Create(artEntry.Graphic);
artEntry.Graphic);
FArtCache.StoreID(ATileID, Result); FArtCache.StoreID(ATileID, Result);
artEntry.Free; artEntry.Free;
@ -326,8 +330,7 @@ begin
begin begin
artEntry := ResMan.Art.GetArt(ATileID, 0, AHue, APartialHue); artEntry := ResMan.Art.GetArt(ATileID, 0, AHue, APartialHue);
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height, Result := TSimpleMaterial.Create(artEntry.Graphic);
artEntry.Graphic);
FArtCache.StoreID(id, Result); FArtCache.StoreID(id, Result);
artEntry.Free; artEntry.Free;
@ -343,8 +346,7 @@ begin
begin begin
artEntry := ResMan.Art.GetFlatLand(ATileID); artEntry := ResMan.Art.GetFlatLand(ATileID);
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height, Result := TSimpleMaterial.Create(artEntry.Graphic);
artEntry.Graphic);
FFlatLandArtCache.StoreID(ATileID, Result); FFlatLandArtCache.StoreID(ATileID, Result);
artEntry.Free; artEntry.Free;
@ -382,8 +384,7 @@ begin
begin begin
texEntry := TTexture(ResMan.Texmaps.Block[texID]); texEntry := TTexture(ResMan.Texmaps.Block[texID]);
Result := TMaterial.Create(texEntry.Graphic.Width, texEntry.Graphic.Height, Result := TSimpleMaterial.Create(texEntry.Graphic);
texEntry.Graphic);
FTexCache.StoreID(ATileID, Result); FTexCache.StoreID(ATileID, Result);
texEntry.Free; texEntry.Free;
@ -1143,36 +1144,31 @@ end;
{ TMaterial } { 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; destructor TMaterial.Destroy;
begin begin
if FGraphic <> nil then FreeAndNil(FGraphic);
if FTexture <> 0 then glDeleteTextures(1, @FTexture); if FTexture <> 0 then glDeleteTextures(1, @FTexture);
FreeAndNil(FGraphic);
inherited Destroy; inherited Destroy;
end; 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; function TMaterial.HitTest(AX, AY: Integer): Boolean;
var var
pixel: TColor32Rec; pixel: TColor32Rec;
@ -1187,18 +1183,6 @@ begin
end; end;
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 } { TScreenBuffer }
constructor TScreenBuffer.Create; constructor TScreenBuffer.Create;
@ -1530,5 +1514,16 @@ begin
FFont.DrawText(x, y, FText); FFont.DrawText(x, y, FText);
end; end;
{ TSimpleMaterial }
constructor TSimpleMaterial.Create(AGraphic: TBaseImage);
begin
inherited Create;
FGraphic := TMultiImage.CreateFromImage(AGraphic);
FRealWidth := FGraphic.Width;
FRealHeight := FGraphic.Height;
UpdateTexture;
end;
end. end.

View File

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