1 unit f_addresource_texture
;
3 {$INCLUDE ../shared/a_modes.inc}
8 LCLIntf
, LCLType
, SysUtils
, Variants
, Classes
,
9 Graphics
, Controls
, Forms
, Dialogs
, f_addresource
,
10 StdCtrls
, ExtCtrls
, utils
, Imaging
, ImagingTypes
, ImagingUtility
;
16 TAddTextureForm
= class (TAddResourceForm
)
18 PanelTexPreview
: TPanel
;
25 procedure FormActivate(Sender
: TObject
);
26 procedure lbResourcesListClick(Sender
: TObject
);
27 procedure eTextureNameChange(Sender
: TObject
);
28 procedure cbWADListChange(Sender
: TObject
);
29 procedure cbSectionsListChange(Sender
: TObject
);
30 procedure bCloseClick(Sender
: TObject
);
31 procedure bAddTextureClick(Sender
: TObject
);
32 procedure bAddCloseClick(Sender
: TObject
);
41 AddTextureForm
: TAddTextureForm
;
42 NumFrames
: Integer = 0;
44 function IsAnim(Res
: String): Boolean;
45 function GetFrame(Res
: String; var Data
: Pointer; var DataLen
: Integer;
46 var Width
, Height
: Word): Boolean;
51 BinEditor
, WADEDITOR
, WADSTRUCT
, f_main
, g_textures
, CONFIG
, g_map
,
52 g_language
, e_Log
, g_resources
;
56 function IsAnim(Res
: String): Boolean;
60 WADName
, SectionName
, ResourceName
: String;
62 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
63 (* just check file existance *)
64 g_ReadSubResource(WADName
, SectionName
, ResourceName
, 'TEXT', 'ANIM', data
, len
);
65 (* TODO check section TEXTURES *)
66 Result
:= data
<> nil;
71 function GetFrame (Res
: String; var Data
: Pointer; var DataLen
: Integer; var Width
, Height
: Word): Boolean;
75 WADName
, SectionName
, ResourceName
: String;
78 Result
:= False; Data
:= nil; DataLen
:= 0; Width
:= 0; Height
:= 0;
79 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
80 g_ReadSubResource(WADName
, SectionName
, ResourceName
, 'TEXT', 'ANIM', TextData
, Len
);
81 if TextData
<> nil then
83 config
:= TConfig
.CreateMem(TextData
, Len
);
84 g_ReadSubResource(WADName
, SectionName
, ResourceName
, 'TEXTURES', config
.ReadStr('', 'resource', ''), Data
, DataLen
);
87 Height
:= config
.ReadInt('', 'frameheight', 0);
88 Width
:= config
.ReadInt('', 'framewidth', 0);
96 function CreateBitMap(Data
: Pointer; DataSize
: Cardinal): TBitMap
;
111 if not LoadImageFromMemory(Data
, DataSize
, img
) then
115 Height
:= img
.height
;
117 BitMap
:= TBitMap
.Create();
118 BitMap
.PixelFormat
:= pf24bit
;
120 BitMap
.Width
:= Width
;
121 BitMap
.Height
:= Height
;
123 // Копируем в BitMap:
124 ii
:= BitMap
.RawImage
.Data
;
125 for y
:= 0 to height
-1 do
127 for x
:= 0 to width
-1 do
129 clr
:= GetPixel32(img
, x
, y
);
130 // HACK: Lazarus's TBitMap doesn't seem to have a working 32 bit mode, so
131 // mix color with checkered background. Also, can't really read
132 // CHECKERS.tga from here. FUCK!
133 if UseCheckerboard
then
135 if (((x
shr 3) and 1) = 0) xor (((y
shr 3) and 1) = 0) then
138 bgc
.Color
:= $CBCBCB;
142 bgc
.r
:= GetRValue(PreviewColor
);
143 bgc
.g
:= GetGValue(PreviewColor
);
144 bgc
.b
:= GetBValue(PreviewColor
);
146 clr
.r
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.r
+ clr
.a
* clr
.r
) div 255);
147 clr
.g
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.g
+ clr
.a
* clr
.g
) div 255);
148 clr
.b
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.b
+ clr
.a
* clr
.b
) div 255);
149 // TODO: check for RGB/BGR somehow?
150 ii
^ := clr
.b
; Inc(ii
);
151 ii
^ := clr
.g
; Inc(ii
);
152 ii
^ := clr
.r
; Inc(ii
);
154 (* Why this works in linux? *)
155 {$IFNDEF WINDOWS}Inc(ii
){$ENDIF}
162 function ShowAnim(Res
: String): TBitMap
;
165 TextData
, TextureData
: Pointer;
166 WADName
, SectionName
, ResourceName
: String;
170 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
171 g_ReadSubResource(WADName
, SectionName
, ResourceName
, 'TEXT', 'ANIM', TextData
, Len
);
172 if TextData
<> nil then
174 config
:= TConfig
.CreateMem(TextData
, Len
);
175 g_ReadSubResource(WADName
, SectionName
, ResourceName
, 'TEXTURES', config
.ReadStr('', 'resource', ''), TextureData
, Len
);
176 if TextureData
<> nil then
178 Result
:= CreateBitMap(TextureData
, Len
);
179 (* view only first frame *)
180 NumFrames
:= config
.ReadInt('', 'framecount', 0);
181 Result
.Height
:= config
.ReadInt('', 'frameheight', 0);
182 Result
.Width
:= config
.ReadInt('', 'framewidth', 0);
190 function ShowTGATexture(ResourceStr
: String): TBitMap
;
193 TextureData
: Pointer;
194 WADName
, SectionName
, ResourceName
: String;
197 g_ProcessResourceStr(ResourceStr
, WADName
, SectionName
, ResourceName
);
198 g_ReadResource(WADName
, SectionName
, ResourceName
, TextureData
, Len
);
199 if TextureData
<> nil then
200 Result
:= CreateBitMap(TextureData
, Len
)
203 procedure TAddTextureForm
.FormActivate(Sender
: TObject
);
207 lStats
.Caption
:= '';
208 cbWADList
.Items
.Add(_lc
[I_WAD_SPECIAL_TEXS
]);
210 eTextureName
.Text := '';
211 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
213 bOK
.Visible
:= False;
214 bCancel
.Visible
:= False;
217 procedure TAddTextureForm
.lbResourcesListClick(Sender
: TObject
);
226 lStats
.Caption
:= '';
227 if lbResourcesList
.ItemIndex
= -1 then
229 if FResourceName
= '' then
231 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
234 g_ProcessResourceStr(FFullResourceName
, @wad
, nil, nil);
235 if wad
= _lc
[I_WAD_SPECIAL_TEXS
] then
238 Anim
:= IsAnim(FFullResourceName
);
240 Texture
:= ShowAnim(FFullResourceName
)
242 Texture
:= ShowTGATexture(FFullResourceName
);
244 if Texture
= nil then
248 lStats
.Caption
:= Format(_lc
[I_CAP_ANIMATION
], [Texture
.Width
, Texture
.Height
, NumFrames
])
250 lStats
.Caption
:= Format(_lc
[I_CAP_TEXTURE
], [Texture
.Width
, Texture
.Height
]);
252 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
253 iPreview
.Canvas
.CopyRect(Texture
.Canvas
.ClipRect
, Texture
.Canvas
, Texture
.Canvas
.ClipRect
);
257 procedure TAddTextureForm
.eTextureNameChange(Sender
: TObject
);
263 // Убираем старые выделения:
264 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
265 lbResourcesList
.Selected
[a
] := False;
268 if (lbResourcesList
.Items
.Count
= 0) or
269 (eTextureName
.Text = '') then
274 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
275 if LowerCase(Copy(lbResourcesList
.Items
[a
], 1,
276 Length(eTextureName
.Text))) =
277 LowerCase(eTextureName
.Text) then
279 lbResourcesList
.Selected
[a
] := True;
283 // Показываем первую текстуру из найденных:
284 lbResourcesList
.TopIndex
:= a
;
285 lbResourcesList
.OnClick(nil);
292 procedure TAddTextureForm
.cbWADListChange(Sender
: TObject
);
294 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
296 cbSectionsList
.Clear();
297 cbSectionsList
.Items
.Add('..');
304 procedure TAddTextureForm
.cbSectionsListChange(Sender
: TObject
);
306 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
308 lbResourcesList
.Clear();
309 lbResourcesList
.Items
.Add(TEXTURE_NAME_WATER
);
310 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID1
);
311 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID2
);
318 procedure TAddTextureForm
.bCloseClick(Sender
: TObject
);
323 procedure TAddTextureForm
.bAddTextureClick(Sender
: TObject
);
328 for i
:= 0 to lbResourcesList
.Count
-1 do
329 if lbResourcesList
.Selected
[i
] then
331 AddTexture(cbWADlist
.Text, cbSectionsList
.Text,
332 lbResourcesList
.Items
[i
], False);
333 lbResourcesList
.Selected
[i
] := False;
337 procedure TAddTextureForm
.bAddCloseClick(Sender
: TObject
);
339 bAddTextureClick(bAddTexture
);