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
,
50 TTGAHeader
= packed record
54 ColorMapSpec
: Array [0..4] of Byte;
55 OrigX
: Array [0..1] of Byte;
56 OrigY
: Array [0..1] of Byte;
57 Width
: Array [0..1] of Byte;
58 Height
: Array [0..1] of Byte;
65 function IsAnim(Res
: String): Boolean;
73 Sign
: Array [0..4] of Char;
82 // Читаем файл и ресурс в нем:
83 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
85 WAD
:= TWADEditor_1
.Create();
87 if (not WAD
.ReadFile(WADName
)) or
88 (not WAD
.GetResource(SectionName
, ResourceName
, Data
, Size
)) then
96 // Проверка сигнатуры. Если есть - это WAD внутри WAD:
97 CopyMemory(@Sign
[0], Data
, 5);
99 if not (Sign
= DFWAD_SIGNATURE
) then
106 // Пробуем прочитать данные:
107 if not WAD
.ReadMemory(Data
, Size
) then
117 Sections
:= WAD
.GetSectionList();
119 if Sections
= nil then
125 // Ищем в секциях "TEXT":
127 for a
:= 0 to High(Sections
) do
128 if Sections
[a
] = 'TEXT' then
134 // Ищем в секциях лист текстур - "TEXTURES":
135 for a
:= 0 to High(Sections
) do
136 if Sections
[a
] = 'TEXTURES' then
148 // Получаем ресурсы секции "TEXT":
149 Resources
:= WAD
.GetResourcesList('TEXT');
151 if Resources
= nil then
157 // Ищем в них описание анимации - "AINM":
159 for a
:= 0 to High(Resources
) do
160 if Resources
[a
] = 'ANIM' then
168 // Если все получилось, то это аним. текстура:
172 function GetFrame(Res
: String; var Data
: Pointer; var DataLen
: Integer;
173 var Width
, Height
: Word): Boolean;
179 ResourceName
: String;
188 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
190 WAD
:= TWADEditor_1
.Create();
192 if not WAD
.ReadFile(WADName
) then
198 // Читаем WAD-ресурс из WAD:
199 if not WAD
.GetResource(SectionName
, ResourceName
, AnimWAD
, Len
) then
207 // Читаем WAD в WAD'е:
208 if not WAD
.ReadMemory(AnimWAD
, Len
) then
215 // Читаем описание анимации:
216 if not WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
) then
224 config
:= TConfig
.CreateMem(TextData
, Len
);
226 // Читаем ресурс - лист текстур:
227 if not WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), Data
, Len
) then
237 Height
:= config
.ReadInt('', 'frameheight', 0);
238 Width
:= config
.ReadInt('', 'framewidth', 0);
249 function CreateBitMap(Data
: Pointer; DataSize
: Cardinal): TBitMap
;
269 if not LoadImageFromMemory(Data
, DataSize
, img
) then
273 Height
:= img
.height
;
275 ImageSize
:= Width
*Height
*(ColorDepth
div 8);
277 BitMap
:= TBitMap
.Create();
278 BitMap
.PixelFormat
:= pf24bit
;
280 BitMap
.Width
:= Width
;
281 BitMap
.Height
:= Height
;
283 // Копируем в BitMap:
284 ii
:= BitMap
.RawImage
.Data
;
285 for y
:= 0 to height
-1 do
287 for x
:= 0 to width
-1 do
289 clr
:= GetPixel32(img
, x
, y
);
290 // HACK: Lazarus's TBitMap doesn't seem to have a working 32 bit mode, so
291 // mix color with pink background. FUCK!
292 clr
.r
:= ClampToByte(((255 - clr
.a
) * BG_R
+ clr
.a
* clr
.r
) div 255);
293 clr
.g
:= ClampToByte(((255 - clr
.a
) * BG_G
+ clr
.a
* clr
.g
) div 255);
294 clr
.b
:= ClampToByte(((255 - clr
.a
) * BG_B
+ clr
.a
* clr
.b
) div 255);
295 // TODO: check for ARGB/RGBA/BGRA/ABGR somehow?
296 ii
^ := clr
.b
; Inc(ii
);
297 ii
^ := clr
.g
; Inc(ii
);
298 ii
^ := clr
.r
; Inc(ii
);
299 // ii^ := clr.a; Inc(ii);
306 function ShowAnim(Res
: String): TBitMap
;
312 ResourceName
: String;
316 TextureData
: Pointer;
321 // Читаем WAD файл и ресурс в нем:
322 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
324 WAD
:= TWADEditor_1
.Create();
325 WAD
.ReadFile(WADName
);
326 WAD
.GetResource(SectionName
, ResourceName
, AnimWAD
, Len
);
329 // Читаем описание анимации:
330 WAD
.ReadMemory(AnimWAD
, Len
);
331 WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
);
333 config
:= TConfig
.CreateMem(TextData
, Len
);
335 // Читаем лист текстур:
336 WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), TextureData
, Len
);
338 if (TextureData
<> nil) and
339 (WAD
.GetLastError
= DFWAD_NOERROR
) then
341 // Создаем BitMap из листа текстур:
342 Result
:= CreateBitMap(TextureData
, Len
);
344 // Размеры одного кадра - виден только первый кадр:
345 Result
.Height
:= config
.ReadInt('', 'frameheight', 0);
346 Result
.Width
:= config
.ReadInt('', 'framewidth', 0);
352 FreeMem(TextureData
);
357 function ShowTGATexture(ResourceStr
: String): TBitMap
;
359 TextureData
: Pointer;
363 ResourceName
: String;
370 g_ProcessResourceStr(ResourceStr
, WADName
, SectionName
, ResourceName
);
372 WAD
:= TWADEditor_1
.Create();
373 if not WAD
.ReadFile(WADName
) then
379 // Читаем ресурс текстуры в нем:
380 WAD
.GetResource(SectionName
, ResourceName
, TextureData
, Len
);
384 // Создаем на его основе BitMap:
385 Result
:= CreateBitMap(TextureData
, Len
);
387 FreeMem(TextureData
);
390 procedure TAddTextureForm
.FormActivate(Sender
: TObject
);
394 cbWADList
.Items
.Add(_lc
[I_WAD_SPECIAL_TEXS
]);
396 eTextureName
.Text := '';
397 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
399 bOK
.Visible
:= False;
400 bCancel
.Visible
:= False;
403 procedure TAddTextureForm
.lbResourcesListClick(Sender
: TObject
);
411 if lbResourcesList
.ItemIndex
= -1 then
413 if FResourceName
= '' then
415 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
418 g_ProcessResourceStr(FFullResourceName
, @wad
, nil, nil);
419 if wad
= _lc
[I_WAD_SPECIAL_TEXS
] then
422 if IsAnim(FFullResourceName
) then
423 Texture
:= ShowAnim(FFullResourceName
)
425 Texture
:= ShowTGATexture(FFullResourceName
);
427 if Texture
= nil then
429 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
430 iPreview
.Canvas
.CopyRect(Texture
.Canvas
.ClipRect
, Texture
.Canvas
, Texture
.Canvas
.ClipRect
);
434 procedure TAddTextureForm
.eTextureNameChange(Sender
: TObject
);
440 // Убираем старые выделения:
441 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
442 lbResourcesList
.Selected
[a
] := False;
445 if (lbResourcesList
.Items
.Count
= 0) or
446 (eTextureName
.Text = '') then
451 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
452 if LowerCase(Copy(lbResourcesList
.Items
[a
], 1,
453 Length(eTextureName
.Text))) =
454 LowerCase(eTextureName
.Text) then
456 lbResourcesList
.Selected
[a
] := True;
460 // Показываем первую текстуру из найденных:
461 lbResourcesList
.TopIndex
:= a
;
462 lbResourcesList
.OnClick(nil);
469 procedure TAddTextureForm
.cbWADListChange(Sender
: TObject
);
471 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
473 cbSectionsList
.Clear();
474 cbSectionsList
.Items
.Add('..');
481 procedure TAddTextureForm
.cbSectionsListChange(Sender
: TObject
);
483 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
485 lbResourcesList
.Clear();
486 lbResourcesList
.Items
.Add(TEXTURE_NAME_WATER
);
487 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID1
);
488 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID2
);
495 procedure TAddTextureForm
.bCloseClick(Sender
: TObject
);
500 procedure TAddTextureForm
.bAddTextureClick(Sender
: TObject
);
505 for i
:= 0 to lbResourcesList
.Count
-1 do
506 if lbResourcesList
.Selected
[i
] then
508 AddTexture(utf2win(cbWADlist
.Text), utf2win(cbSectionsList
.Text),
509 utf2win(lbResourcesList
.Items
[i
]), False);
510 lbResourcesList
.Selected
[i
] := False;
514 procedure TAddTextureForm
.bAddCloseClick(Sender
: TObject
);
516 bAddTextureClick(bAddTexture
);