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
,
56 function IsAnim(Res
: String): Boolean;
64 Sign
: Array [0..4] of Char;
75 // Читаем файл и ресурс в нем:
76 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
78 WAD
:= TWADEditor_1
.Create();
80 if (not WAD
.ReadFile(WADName
)) or
81 (not WAD
.GetResource(utf2win(SectionName
), utf2win(ResourceName
), Data
, Size
)) then
89 // Проверка сигнатуры. Если есть - это WAD внутри WAD:
90 CopyMemory(@Sign
[0], Data
, 5);
92 if not (Sign
= DFWAD_SIGNATURE
) then
99 // Пробуем прочитать данные:
100 if not WAD
.ReadMemory(Data
, Size
) then
110 Sections
:= WAD
.GetSectionList();
112 if Sections
= nil then
118 // Ищем в секциях "TEXT":
120 for a
:= 0 to High(Sections
) do
121 if Sections
[a
] = 'TEXT' then
127 // Ищем в секциях лист текстур - "TEXTURES":
128 for a
:= 0 to High(Sections
) do
129 if Sections
[a
] = 'TEXTURES' then
141 // Получаем ресурсы секции "TEXT":
142 Resources
:= WAD
.GetResourcesList('TEXT');
144 if Resources
= nil then
150 // Ищем в них описание анимации - "ANIM":
152 for a
:= 0 to High(Resources
) do
153 if Resources
[a
] = 'ANIM' then
161 // Если все получилось, то это аним. текстура:
165 function GetFrame(Res
: String; var Data
: Pointer; var DataLen
: Integer; var Width
, Height
: Word): Boolean;
171 ResourceName
: String;
183 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
185 WAD
:= TWADEditor_1
.Create();
187 if not WAD
.ReadFile(WADName
) then
193 // Читаем WAD-ресурс из WAD:
194 if not WAD
.GetResource(utf2win(SectionName
), utf2win(ResourceName
), AnimWAD
, Len
) then
202 // Читаем WAD в WAD'е:
203 if not WAD
.ReadMemory(AnimWAD
, Len
) then
210 // Читаем описание анимации:
211 if not WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
) then
219 config
:= TConfig
.CreateMem(TextData
, Len
);
221 // Читаем ресурс - лист текстур:
222 if not WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), Data
, Len
) then
232 Height
:= config
.ReadInt('', 'frameheight', 0);
233 Width
:= config
.ReadInt('', 'framewidth', 0);
244 function CreateBitMap (Data
: Pointer; DataSize
: Cardinal): TBitMap
;
247 clr
, bgc
: TColor32Rec
;
248 Width
, Height
: Integer;
254 if not LoadImageFromMemory(Data
, DataSize
, img
) then
258 Height
:= img
.height
;
259 BitMap
:= TBitMap
.Create();
260 BitMap
.PixelFormat
:= pf24bit
;
261 BitMap
.Width
:= Width
;
262 BitMap
.Height
:= Height
;
263 for y
:= 0 to Height
- 1 do
265 for x
:= 0 to Width
- 1 do
267 clr
:= GetPixel32(img
, x
, y
);
268 // HACK: Lazarus's TBitMap doesn't seem to have a working 32 bit mode, so
269 // mix color with checkered background. Also, can't really read
270 // CHECKERS.tga from here. FUCK!
271 if UseCheckerboard
then
273 if (((x
shr 3) and 1) = 0) xor (((y
shr 3) and 1) = 0) then
280 bgc
.r
:= GetRValue(PreviewColor
);
281 bgc
.g
:= GetGValue(PreviewColor
);
282 bgc
.b
:= GetBValue(PreviewColor
)
284 clr
.r
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.r
+ clr
.a
* clr
.r
) div 255);
285 clr
.g
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.g
+ clr
.a
* clr
.g
) div 255);
286 clr
.b
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.b
+ clr
.a
* clr
.b
) div 255);
287 BitMap
.Canvas
.Pixels
[x
, y
] := RGBToColor(clr
.r
, clr
.g
, clr
.b
)
294 function ShowAnim(Res
: String): TBitMap
;
300 ResourceName
: String;
304 TextureData
: Pointer;
313 // Читаем WAD файл и ресурс в нем:
314 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
316 WAD
:= TWADEditor_1
.Create();
317 WAD
.ReadFile(WADName
);
318 WAD
.GetResource(utf2win(SectionName
), utf2win(ResourceName
), AnimWAD
, Len
);
321 // Читаем описание анимации:
322 WAD
.ReadMemory(AnimWAD
, Len
);
323 WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
);
325 config
:= TConfig
.CreateMem(TextData
, Len
);
327 // Читаем лист текстур:
328 WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), TextureData
, Len
);
329 NumFrames
:= config
.ReadInt('', 'framecount', 0);
331 if (TextureData
<> nil) and
332 (WAD
.GetLastError
= DFWAD_NOERROR
) then
334 // Создаем BitMap из листа текстур:
335 Result
:= CreateBitMap(TextureData
, Len
);
337 // Размеры одного кадра - виден только первый кадр:
338 Result
.Height
:= config
.ReadInt('', 'frameheight', 0);
339 Result
.Width
:= config
.ReadInt('', 'framewidth', 0);
345 FreeMem(TextureData
);
350 function ShowTGATexture(ResourceStr
: String): TBitMap
;
352 TextureData
: Pointer;
356 ResourceName
: String;
365 g_ProcessResourceStr(ResourceStr
, WADName
, SectionName
, ResourceName
);
367 WAD
:= TWADEditor_1
.Create();
368 if not WAD
.ReadFile(WADName
) then
374 // Читаем ресурс текстуры в нем:
375 WAD
.GetResource(utf2win(SectionName
), utf2win(ResourceName
), TextureData
, Len
);
379 // Создаем на его основе BitMap:
380 Result
:= CreateBitMap(TextureData
, Len
);
382 FreeMem(TextureData
);
385 procedure TAddTextureForm
.FormActivate(Sender
: TObject
);
389 lStats
.Caption
:= '';
390 cbWADList
.Items
.Add(MsgWadSpecialTexs
);
392 eTextureName
.Text := '';
393 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
395 bOK
.Visible
:= False;
396 bCancel
.Visible
:= False;
399 procedure TAddTextureForm
.lbResourcesListClick(Sender
: TObject
);
408 lStats
.Caption
:= '';
409 if lbResourcesList
.ItemIndex
= -1 then
411 if FResourceName
= '' then
413 if cbWADList
.Text = MsgWadSpecialTexs
then
416 g_ProcessResourceStr(FFullResourceName
, @wad
, nil, nil);
417 if wad
= MsgWadSpecialTexs
then
420 Anim
:= IsAnim(FFullResourceName
);
422 Texture
:= ShowAnim(FFullResourceName
)
424 Texture
:= ShowTGATexture(FFullResourceName
);
426 if Texture
= nil then
430 lStats
.Caption
:= Format(MsgCapAnimation
, [Texture
.Width
, Texture
.Height
, NumFrames
])
432 lStats
.Caption
:= Format(MsgCapTexture
, [Texture
.Width
, Texture
.Height
]);
434 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
435 iPreview
.Canvas
.CopyRect(Texture
.Canvas
.ClipRect
, Texture
.Canvas
, Texture
.Canvas
.ClipRect
);
439 procedure TAddTextureForm
.eTextureNameChange(Sender
: TObject
);
445 // Убираем старые выделения:
446 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
447 lbResourcesList
.Selected
[a
] := False;
450 if (lbResourcesList
.Items
.Count
= 0) or
451 (eTextureName
.Text = '') then
456 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
457 if LowerCase(Copy(lbResourcesList
.Items
[a
], 1,
458 Length(eTextureName
.Text))) =
459 LowerCase(eTextureName
.Text) then
461 lbResourcesList
.Selected
[a
] := True;
465 // Показываем первую текстуру из найденных:
466 lbResourcesList
.TopIndex
:= a
;
467 lbResourcesList
.OnClick(nil);
474 procedure TAddTextureForm
.cbWADListChange(Sender
: TObject
);
476 if cbWADList
.Text = MsgWadSpecialTexs
then
478 cbSectionsList
.Clear();
479 cbSectionsList
.Items
.Add('..');
486 procedure TAddTextureForm
.cbSectionsListChange(Sender
: TObject
);
488 if cbWADList
.Text = MsgWadSpecialTexs
then
490 lbResourcesList
.Clear();
491 lbResourcesList
.Items
.Add(TEXTURE_NAME_WATER
);
492 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID1
);
493 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID2
);
500 procedure TAddTextureForm
.bCloseClick(Sender
: TObject
);
505 procedure TAddTextureForm
.bAddTextureClick(Sender
: TObject
);
510 for i
:= 0 to lbResourcesList
.Count
-1 do
511 if lbResourcesList
.Selected
[i
] then
513 AddTexture(cbWADlist
.Text, cbSectionsList
.Text,
514 lbResourcesList
.Items
[i
], False);
515 lbResourcesList
.Selected
[i
] := False;
519 procedure TAddTextureForm
.bAddCloseClick(Sender
: TObject
);
521 bAddTextureClick(bAddTexture
);