From d182fd7f46e7569067b952752dd7efa2fef6582f Mon Sep 17 00:00:00 2001 From: Kieron Morris Date: Sun, 6 Feb 2022 13:25:04 +0000 Subject: [PATCH] TARGA & Texture Units Added TARGA and Texture units to represent & parse TARGA into and provide a standard texture format for use when drawing. --- src/include/color.pas | 4 +++ src/include/targa.pas | 67 +++++++++++++++++++++++++++++++++++++++++ src/include/texture.pas | 34 +++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/include/targa.pas create mode 100644 src/include/texture.pas diff --git a/src/include/color.pas b/src/include/color.pas index cd22dd05..42039b2a 100644 --- a/src/include/color.pas +++ b/src/include/color.pas @@ -28,24 +28,28 @@ type R : uint8; A : uint8; end; + PRGB32 = ^TRGB32; TRGB24 = bitpacked record B : uint8; G : uint8; R : uint8; end; + PRGB23 = ^TRGB24; TRGB16 = bitpacked record B : UBit5; G : UBit6; R : UBit5; end; + PRGB16 = ^TRGB16; TRGB8 = bitpacked record B : UBit2; G : UBit4; R : UBit2; end; + PRGB8 = ^TRGB8; const black : TRGB32 = (B: 000; G: 000; R: 000; A: 000); diff --git a/src/include/targa.pas b/src/include/targa.pas new file mode 100644 index 00000000..6a2804aa --- /dev/null +++ b/src/include/targa.pas @@ -0,0 +1,67 @@ +unit targa; + +interface + +uses + lmemorymanager, texture; + +type + TTARGAColor = packed record + b, g, r, a: uint8; + end; + PTARGAColor = ^TTARGAColor; + + TTARGAHeader = packed record + Magic: uint8; + ColorMapType: uint8; + ImageType: uint8; + ColorMapOrigin: uint16; + ColorMapLength: uint16; + ColorMapDepth: uint8; + XOrigin: uint16; + YOrigin: uint16; + Width: uint16; + Height: uint16; + PixelDepth: uint8; + ImageDescriptor: uint8; + Data : PTARGAColor; + end; + PTARGAHeader = ^TTARGAHeader; + +Function Parse(buffer : puint8; len : uint32) : PTexture; + +implementation + +Function Parse(buffer : puint8; len : uint32) : PTexture; +var + header : PTARGAHeader; + i, j : uint32; + data : PTARGAColor; + tex : PTexture; + +begin + if (len < sizeof(TTARGAHeader)) then exit; + + header := PTARGAHeader(buffer); + if (header^.Magic <> $0) or (header^.ImageType <> 2) or (header^.PixelDepth <> 32) then exit; + + //Create a new texture + tex:= texture.newTexture(header^.Width, header^.Height); + tex^.Width:= header^.Width; + tex^.Height:= header^.Height; + + //Copy the data + data := PTARGAColor(header^.Data); + for i := 0 to header^.Height - 1 do begin + for j := 0 to header^.Width - 1 do begin + tex^.Pixels[i * header^.Width + j].r := data^.r; + tex^.Pixels[i * header^.Width + j].g := data^.g; + tex^.Pixels[i * header^.Width + j].b := data^.b; + tex^.Pixels[i * header^.Width + j].a := data^.a; + end; + end; + + Parse := tex; +end; + +end. \ No newline at end of file diff --git a/src/include/texture.pas b/src/include/texture.pas new file mode 100644 index 00000000..7c3957f2 --- /dev/null +++ b/src/include/texture.pas @@ -0,0 +1,34 @@ +unit texture; + +interface + +uses + lmemorymanager, color; + +type + TTexture = packed record + Width : uint32; + Height : uint32; + Size : uint32; + Pixels : PRGB32; + end; + PTexture = ^TTexture; + +Function newTexture(width, height: uint32): PTexture; + +implementation + +Function newTexture(width, height: uint32): PTexture; +var + texture: PTexture; + +begin + texture:= PTexture(kalloc(sizeof(TTexture))); + texture^.Pixels:= PRGB32(kalloc(width * height * sizeof(TRGB32))); + texture^.Width:= width; + texture^.Height:= height; + texture^.Size:= width * height; + newTexture:= texture; +end; + +end. \ No newline at end of file