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
;
252 if not LoadImageFromMemory(Data
, DataSize
, img
) then
256 Height
:= img
.height
;
258 ImageSize
:= Width
*Height
*(ColorDepth
div 8);
260 BitMap
:= TBitMap
.Create();
261 BitMap
.PixelFormat
:= pf24bit
;
263 BitMap
.Width
:= Width
;
264 BitMap
.Height
:= Height
;
266 // Копируем в BitMap:
267 ii
:= BitMap
.RawImage
.Data
;
268 for y
:= 0 to height
-1 do
270 for x
:= 0 to width
-1 do
272 clr
:= GetPixel32(img
, x
, y
);
273 // HACK: Lazarus's TBitMap doesn't seem to have a working 32 bit mode, so
274 // mix color with checkered background. Also, can't really read
275 // CHECKERS.tga from here. FUCK!
276 if (((x
shr 3) and 1) = 0) xor (((y
shr 3) and 1) = 0) then
280 clr
.r
:= ClampToByte(((255 - clr
.a
) * bgc
+ clr
.a
* clr
.r
) div 255);
281 clr
.g
:= ClampToByte(((255 - clr
.a
) * bgc
+ clr
.a
* clr
.g
) div 255);
282 clr
.b
:= ClampToByte(((255 - clr
.a
) * bgc
+ clr
.a
* clr
.b
) div 255);
283 // TODO: check for RGB/BGR somehow?
284 ii
^ := clr
.b
; Inc(ii
);
285 ii
^ := clr
.g
; Inc(ii
);
286 ii
^ := clr
.r
; Inc(ii
);
293 function ShowAnim(Res
: String): TBitMap
;
299 ResourceName
: String;
303 TextureData
: Pointer;
308 // Читаем WAD файл и ресурс в нем:
309 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
311 WAD
:= TWADEditor_1
.Create();
312 WAD
.ReadFile(WADName
);
313 WAD
.GetResource(SectionName
, ResourceName
, AnimWAD
, Len
);
316 // Читаем описание анимации:
317 WAD
.ReadMemory(AnimWAD
, Len
);
318 WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
);
320 config
:= TConfig
.CreateMem(TextData
, Len
);
322 // Читаем лист текстур:
323 WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), TextureData
, Len
);
325 if (TextureData
<> nil) and
326 (WAD
.GetLastError
= DFWAD_NOERROR
) then
328 // Создаем BitMap из листа текстур:
329 Result
:= CreateBitMap(TextureData
, Len
);
331 // Размеры одного кадра - виден только первый кадр:
332 Result
.Height
:= config
.ReadInt('', 'frameheight', 0);
333 Result
.Width
:= config
.ReadInt('', 'framewidth', 0);
339 FreeMem(TextureData
);
344 function ShowTGATexture(ResourceStr
: String): TBitMap
;
346 TextureData
: Pointer;
350 ResourceName
: String;
357 g_ProcessResourceStr(ResourceStr
, WADName
, SectionName
, ResourceName
);
359 WAD
:= TWADEditor_1
.Create();
360 if not WAD
.ReadFile(WADName
) then
366 // Читаем ресурс текстуры в нем:
367 WAD
.GetResource(SectionName
, ResourceName
, TextureData
, Len
);
371 // Создаем на его основе BitMap:
372 Result
:= CreateBitMap(TextureData
, Len
);
374 FreeMem(TextureData
);
377 procedure TAddTextureForm
.FormActivate(Sender
: TObject
);
381 cbWADList
.Items
.Add(_lc
[I_WAD_SPECIAL_TEXS
]);
383 eTextureName
.Text := '';
384 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
386 bOK
.Visible
:= False;
387 bCancel
.Visible
:= False;
390 procedure TAddTextureForm
.lbResourcesListClick(Sender
: TObject
);
398 if lbResourcesList
.ItemIndex
= -1 then
400 if FResourceName
= '' then
402 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
405 g_ProcessResourceStr(FFullResourceName
, @wad
, nil, nil);
406 if wad
= _lc
[I_WAD_SPECIAL_TEXS
] then
409 if IsAnim(FFullResourceName
) then
410 Texture
:= ShowAnim(FFullResourceName
)
412 Texture
:= ShowTGATexture(FFullResourceName
);
414 if Texture
= nil then
416 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
417 iPreview
.Canvas
.CopyRect(Texture
.Canvas
.ClipRect
, Texture
.Canvas
, Texture
.Canvas
.ClipRect
);
421 procedure TAddTextureForm
.eTextureNameChange(Sender
: TObject
);
427 // Убираем старые выделения:
428 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
429 lbResourcesList
.Selected
[a
] := False;
432 if (lbResourcesList
.Items
.Count
= 0) or
433 (eTextureName
.Text = '') then
438 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
439 if LowerCase(Copy(lbResourcesList
.Items
[a
], 1,
440 Length(eTextureName
.Text))) =
441 LowerCase(eTextureName
.Text) then
443 lbResourcesList
.Selected
[a
] := True;
447 // Показываем первую текстуру из найденных:
448 lbResourcesList
.TopIndex
:= a
;
449 lbResourcesList
.OnClick(nil);
456 procedure TAddTextureForm
.cbWADListChange(Sender
: TObject
);
458 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
460 cbSectionsList
.Clear();
461 cbSectionsList
.Items
.Add('..');
468 procedure TAddTextureForm
.cbSectionsListChange(Sender
: TObject
);
470 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
472 lbResourcesList
.Clear();
473 lbResourcesList
.Items
.Add(TEXTURE_NAME_WATER
);
474 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID1
);
475 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID2
);
482 procedure TAddTextureForm
.bCloseClick(Sender
: TObject
);
487 procedure TAddTextureForm
.bAddTextureClick(Sender
: TObject
);
492 for i
:= 0 to lbResourcesList
.Count
-1 do
493 if lbResourcesList
.Selected
[i
] then
495 AddTexture(utf2win(cbWADlist
.Text), utf2win(cbSectionsList
.Text),
496 utf2win(lbResourcesList
.Items
[i
]), False);
497 lbResourcesList
.Selected
[i
] := False;
501 procedure TAddTextureForm
.bAddCloseClick(Sender
: TObject
);
503 bAddTextureClick(bAddTexture
);