1 unit f_addresource_texture
;
3 {$INCLUDE ../shared/a_modes.inc}
8 LCLIntf
, LCLType
, LMessages
, SysUtils
, Variants
, Classes
,
9 Graphics
, Controls
, Forms
, Dialogs
, f_addresource
,
10 StdCtrls
, ExtCtrls
, utils
, Imaging
, ImagingTypes
, ImagingUtility
;
13 TAddTextureForm
= class (TAddResourceForm
)
14 PanelTexPreview
: TPanel
;
21 procedure FormActivate(Sender
: TObject
);
22 procedure lbResourcesListClick(Sender
: TObject
);
23 procedure eTextureNameChange(Sender
: TObject
);
24 procedure cbWADListChange(Sender
: TObject
);
25 procedure cbSectionsListChange(Sender
: TObject
);
26 procedure bCloseClick(Sender
: TObject
);
27 procedure bAddTextureClick(Sender
: TObject
);
28 procedure bAddCloseClick(Sender
: TObject
);
37 AddTextureForm
: TAddTextureForm
;
39 function IsAnim(Res
: String): Boolean;
40 function GetFrame(Res
: String; var Data
: Pointer; var DataLen
: Integer;
41 var Width
, Height
: Word): Boolean;
46 BinEditor
, WADEDITOR
, WADSTRUCT
, f_main
, g_textures
, CONFIG
, g_map
,
51 function IsAnim(Res
: String): Boolean;
59 Sign
: Array [0..4] of Char;
68 // Читаем файл и ресурс в нем:
69 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
71 WAD
:= TWADEditor_1
.Create();
73 if (not WAD
.ReadFile(WADName
)) or
74 (not WAD
.GetResource(SectionName
, ResourceName
, Data
, Size
)) then
82 // Проверка сигнатуры. Если есть - это WAD внутри WAD:
83 CopyMemory(@Sign
[0], Data
, 5);
85 if not (Sign
= DFWAD_SIGNATURE
) then
92 // Пробуем прочитать данные:
93 if not WAD
.ReadMemory(Data
, Size
) then
103 Sections
:= WAD
.GetSectionList();
105 if Sections
= nil then
111 // Ищем в секциях "TEXT":
113 for a
:= 0 to High(Sections
) do
114 if Sections
[a
] = 'TEXT' then
120 // Ищем в секциях лист текстур - "TEXTURES":
121 for a
:= 0 to High(Sections
) do
122 if Sections
[a
] = 'TEXTURES' then
134 // Получаем ресурсы секции "TEXT":
135 Resources
:= WAD
.GetResourcesList('TEXT');
137 if Resources
= nil then
143 // Ищем в них описание анимации - "AINM":
145 for a
:= 0 to High(Resources
) do
146 if Resources
[a
] = 'ANIM' then
154 // Если все получилось, то это аним. текстура:
158 function GetFrame(Res
: String; var Data
: Pointer; var DataLen
: Integer;
159 var Width
, Height
: Word): Boolean;
165 ResourceName
: String;
174 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
176 WAD
:= TWADEditor_1
.Create();
178 if not WAD
.ReadFile(WADName
) then
184 // Читаем WAD-ресурс из WAD:
185 if not WAD
.GetResource(SectionName
, ResourceName
, AnimWAD
, Len
) then
193 // Читаем WAD в WAD'е:
194 if not WAD
.ReadMemory(AnimWAD
, Len
) then
201 // Читаем описание анимации:
202 if not WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
) then
210 config
:= TConfig
.CreateMem(TextData
, Len
);
212 // Читаем ресурс - лист текстур:
213 if not WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), Data
, Len
) then
223 Height
:= config
.ReadInt('', 'frameheight', 0);
224 Width
:= config
.ReadInt('', 'framewidth', 0);
235 function CreateBitMap(Data
: Pointer; DataSize
: Cardinal): TBitMap
;
250 if not LoadImageFromMemory(Data
, DataSize
, img
) then
254 Height
:= img
.height
;
256 BitMap
:= TBitMap
.Create();
257 BitMap
.PixelFormat
:= pf24bit
;
259 BitMap
.Width
:= Width
;
260 BitMap
.Height
:= Height
;
262 // Копируем в BitMap:
263 ii
:= BitMap
.RawImage
.Data
;
264 for y
:= 0 to height
-1 do
266 for x
:= 0 to width
-1 do
268 clr
:= GetPixel32(img
, x
, y
);
269 // HACK: Lazarus's TBitMap doesn't seem to have a working 32 bit mode, so
270 // mix color with checkered background. Also, can't really read
271 // CHECKERS.tga from here. FUCK!
272 if (((x
shr 3) and 1) = 0) xor (((y
shr 3) and 1) = 0) then
276 clr
.r
:= ClampToByte(((255 - clr
.a
) * bgc
+ clr
.a
* clr
.r
) div 255);
277 clr
.g
:= ClampToByte(((255 - clr
.a
) * bgc
+ clr
.a
* clr
.g
) div 255);
278 clr
.b
:= ClampToByte(((255 - clr
.a
) * bgc
+ clr
.a
* clr
.b
) div 255);
279 // TODO: check for RGB/BGR somehow?
280 ii
^ := clr
.b
; Inc(ii
);
281 ii
^ := clr
.g
; Inc(ii
);
282 ii
^ := clr
.r
; Inc(ii
);
289 function ShowAnim(Res
: String): TBitMap
;
295 ResourceName
: String;
299 TextureData
: Pointer;
304 // Читаем WAD файл и ресурс в нем:
305 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
307 WAD
:= TWADEditor_1
.Create();
308 WAD
.ReadFile(WADName
);
309 WAD
.GetResource(SectionName
, ResourceName
, AnimWAD
, Len
);
312 // Читаем описание анимации:
313 WAD
.ReadMemory(AnimWAD
, Len
);
314 WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
);
316 config
:= TConfig
.CreateMem(TextData
, Len
);
318 // Читаем лист текстур:
319 WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), TextureData
, Len
);
321 if (TextureData
<> nil) and
322 (WAD
.GetLastError
= DFWAD_NOERROR
) then
324 // Создаем BitMap из листа текстур:
325 Result
:= CreateBitMap(TextureData
, Len
);
327 // Размеры одного кадра - виден только первый кадр:
328 Result
.Height
:= config
.ReadInt('', 'frameheight', 0);
329 Result
.Width
:= config
.ReadInt('', 'framewidth', 0);
335 FreeMem(TextureData
);
340 function ShowTGATexture(ResourceStr
: String): TBitMap
;
342 TextureData
: Pointer;
346 ResourceName
: String;
353 g_ProcessResourceStr(ResourceStr
, WADName
, SectionName
, ResourceName
);
355 WAD
:= TWADEditor_1
.Create();
356 if not WAD
.ReadFile(WADName
) then
362 // Читаем ресурс текстуры в нем:
363 WAD
.GetResource(SectionName
, ResourceName
, TextureData
, Len
);
367 // Создаем на его основе BitMap:
368 Result
:= CreateBitMap(TextureData
, Len
);
370 FreeMem(TextureData
);
373 procedure TAddTextureForm
.FormActivate(Sender
: TObject
);
377 cbWADList
.Items
.Add(_lc
[I_WAD_SPECIAL_TEXS
]);
379 eTextureName
.Text := '';
380 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
382 bOK
.Visible
:= False;
383 bCancel
.Visible
:= False;
386 procedure TAddTextureForm
.lbResourcesListClick(Sender
: TObject
);
394 if lbResourcesList
.ItemIndex
= -1 then
396 if FResourceName
= '' then
398 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
401 g_ProcessResourceStr(FFullResourceName
, @wad
, nil, nil);
402 if wad
= _lc
[I_WAD_SPECIAL_TEXS
] then
405 if IsAnim(FFullResourceName
) then
406 Texture
:= ShowAnim(FFullResourceName
)
408 Texture
:= ShowTGATexture(FFullResourceName
);
410 if Texture
= nil then
412 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
413 iPreview
.Canvas
.CopyRect(Texture
.Canvas
.ClipRect
, Texture
.Canvas
, Texture
.Canvas
.ClipRect
);
417 procedure TAddTextureForm
.eTextureNameChange(Sender
: TObject
);
423 // Убираем старые выделения:
424 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
425 lbResourcesList
.Selected
[a
] := False;
428 if (lbResourcesList
.Items
.Count
= 0) or
429 (eTextureName
.Text = '') then
434 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
435 if LowerCase(Copy(lbResourcesList
.Items
[a
], 1,
436 Length(eTextureName
.Text))) =
437 LowerCase(eTextureName
.Text) then
439 lbResourcesList
.Selected
[a
] := True;
443 // Показываем первую текстуру из найденных:
444 lbResourcesList
.TopIndex
:= a
;
445 lbResourcesList
.OnClick(nil);
452 procedure TAddTextureForm
.cbWADListChange(Sender
: TObject
);
454 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
456 cbSectionsList
.Clear();
457 cbSectionsList
.Items
.Add('..');
464 procedure TAddTextureForm
.cbSectionsListChange(Sender
: TObject
);
466 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
468 lbResourcesList
.Clear();
469 lbResourcesList
.Items
.Add(TEXTURE_NAME_WATER
);
470 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID1
);
471 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID2
);
478 procedure TAddTextureForm
.bCloseClick(Sender
: TObject
);
483 procedure TAddTextureForm
.bAddTextureClick(Sender
: TObject
);
488 for i
:= 0 to lbResourcesList
.Count
-1 do
489 if lbResourcesList
.Selected
[i
] then
491 AddTexture(utf2win(cbWADlist
.Text), utf2win(cbSectionsList
.Text),
492 utf2win(lbResourcesList
.Items
[i
]), False);
493 lbResourcesList
.Selected
[i
] := False;
497 procedure TAddTextureForm
.bAddCloseClick(Sender
: TObject
);
499 bAddTextureClick(bAddTexture
);