df0b55559f2aa34550dd653363cd36041499de83
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;
166 var Width
, Height
: Word): Boolean;
172 ResourceName
: String;
184 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
186 WAD
:= TWADEditor_1
.Create();
188 if not WAD
.ReadFile(WADName
) then
194 // Читаем WAD-ресурс из WAD:
195 if not WAD
.GetResource(utf2win(SectionName
), utf2win(ResourceName
), AnimWAD
, Len
) then
203 // Читаем WAD в WAD'е:
204 if not WAD
.ReadMemory(AnimWAD
, Len
) then
211 // Читаем описание анимации:
212 if not WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
) then
220 config
:= TConfig
.CreateMem(TextData
, Len
);
222 // Читаем ресурс - лист текстур:
223 if not WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), Data
, Len
) then
233 Height
:= config
.ReadInt('', 'frameheight', 0);
234 Width
:= config
.ReadInt('', 'framewidth', 0);
245 function CreateBitMap(Data
: Pointer; DataSize
: Cardinal): TBitMap
;
260 if not LoadImageFromMemory(Data
, DataSize
, img
) then
264 Height
:= img
.height
;
266 BitMap
:= TBitMap
.Create();
267 BitMap
.PixelFormat
:= pf24bit
;
269 BitMap
.Width
:= Width
;
270 BitMap
.Height
:= Height
;
272 // Копируем в BitMap:
273 ii
:= BitMap
.RawImage
.Data
;
274 for y
:= 0 to height
-1 do
276 for x
:= 0 to width
-1 do
278 clr
:= GetPixel32(img
, x
, y
);
279 // HACK: Lazarus's TBitMap doesn't seem to have a working 32 bit mode, so
280 // mix color with checkered background. Also, can't really read
281 // CHECKERS.tga from here. FUCK!
282 if UseCheckerboard
then
284 if (((x
shr 3) and 1) = 0) xor (((y
shr 3) and 1) = 0) then
287 bgc
.Color
:= $CBCBCB;
291 bgc
.r
:= GetRValue(PreviewColor
);
292 bgc
.g
:= GetGValue(PreviewColor
);
293 bgc
.b
:= GetBValue(PreviewColor
);
295 clr
.r
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.r
+ clr
.a
* clr
.r
) div 255);
296 clr
.g
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.g
+ clr
.a
* clr
.g
) div 255);
297 clr
.b
:= ClampToByte((Byte(255 - clr
.a
) * bgc
.b
+ clr
.a
* clr
.b
) div 255);
298 // TODO: check for RGB/BGR somehow?
299 ii
^ := clr
.b
; Inc(ii
);
300 ii
^ := clr
.g
; Inc(ii
);
301 ii
^ := clr
.r
; Inc(ii
);
308 function ShowAnim(Res
: String): TBitMap
;
314 ResourceName
: String;
318 TextureData
: Pointer;
327 // Читаем WAD файл и ресурс в нем:
328 g_ProcessResourceStr(Res
, WADName
, SectionName
, ResourceName
);
330 WAD
:= TWADEditor_1
.Create();
331 WAD
.ReadFile(WADName
);
332 WAD
.GetResource(utf2win(SectionName
), utf2win(ResourceName
), AnimWAD
, Len
);
335 // Читаем описание анимации:
336 WAD
.ReadMemory(AnimWAD
, Len
);
337 WAD
.GetResource('TEXT', 'ANIM', TextData
, Len
);
339 config
:= TConfig
.CreateMem(TextData
, Len
);
341 // Читаем лист текстур:
342 WAD
.GetResource('TEXTURES', config
.ReadStr('', 'resource', ''), TextureData
, Len
);
343 NumFrames
:= config
.ReadInt('', 'framecount', 0);
345 if (TextureData
<> nil) and
346 (WAD
.GetLastError
= DFWAD_NOERROR
) then
348 // Создаем BitMap из листа текстур:
349 Result
:= CreateBitMap(TextureData
, Len
);
351 // Размеры одного кадра - виден только первый кадр:
352 Result
.Height
:= config
.ReadInt('', 'frameheight', 0);
353 Result
.Width
:= config
.ReadInt('', 'framewidth', 0);
359 FreeMem(TextureData
);
364 function ShowTGATexture(ResourceStr
: String): TBitMap
;
366 TextureData
: Pointer;
370 ResourceName
: String;
379 g_ProcessResourceStr(ResourceStr
, WADName
, SectionName
, ResourceName
);
381 WAD
:= TWADEditor_1
.Create();
382 if not WAD
.ReadFile(WADName
) then
388 // Читаем ресурс текстуры в нем:
389 WAD
.GetResource(utf2win(SectionName
), utf2win(ResourceName
), TextureData
, Len
);
393 // Создаем на его основе BitMap:
394 Result
:= CreateBitMap(TextureData
, Len
);
396 FreeMem(TextureData
);
399 procedure TAddTextureForm
.FormActivate(Sender
: TObject
);
403 lStats
.Caption
:= '';
404 cbWADList
.Items
.Add(_lc
[I_WAD_SPECIAL_TEXS
]);
406 eTextureName
.Text := '';
407 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
409 bOK
.Visible
:= False;
410 bCancel
.Visible
:= False;
413 procedure TAddTextureForm
.lbResourcesListClick(Sender
: TObject
);
422 lStats
.Caption
:= '';
423 if lbResourcesList
.ItemIndex
= -1 then
425 if FResourceName
= '' then
427 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
430 g_ProcessResourceStr(FFullResourceName
, @wad
, nil, nil);
431 if wad
= _lc
[I_WAD_SPECIAL_TEXS
] then
434 Anim
:= IsAnim(FFullResourceName
);
436 Texture
:= ShowAnim(FFullResourceName
)
438 Texture
:= ShowTGATexture(FFullResourceName
);
440 if Texture
= nil then
444 lStats
.Caption
:= Format(_lc
[I_CAP_ANIMATION
], [Texture
.Width
, Texture
.Height
, NumFrames
])
446 lStats
.Caption
:= Format(_lc
[I_CAP_TEXTURE
], [Texture
.Width
, Texture
.Height
]);
448 iPreview
.Canvas
.FillRect(iPreview
.Canvas
.ClipRect
);
449 iPreview
.Canvas
.CopyRect(Texture
.Canvas
.ClipRect
, Texture
.Canvas
, Texture
.Canvas
.ClipRect
);
453 procedure TAddTextureForm
.eTextureNameChange(Sender
: TObject
);
459 // Убираем старые выделения:
460 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
461 lbResourcesList
.Selected
[a
] := False;
464 if (lbResourcesList
.Items
.Count
= 0) or
465 (eTextureName
.Text = '') then
470 for a
:= 0 to lbResourcesList
.Items
.Count
-1 do
471 if LowerCase(Copy(lbResourcesList
.Items
[a
], 1,
472 Length(eTextureName
.Text))) =
473 LowerCase(eTextureName
.Text) then
475 lbResourcesList
.Selected
[a
] := True;
479 // Показываем первую текстуру из найденных:
480 lbResourcesList
.TopIndex
:= a
;
481 lbResourcesList
.OnClick(nil);
488 procedure TAddTextureForm
.cbWADListChange(Sender
: TObject
);
490 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
492 cbSectionsList
.Clear();
493 cbSectionsList
.Items
.Add('..');
500 procedure TAddTextureForm
.cbSectionsListChange(Sender
: TObject
);
502 if cbWADList
.Text = _lc
[I_WAD_SPECIAL_TEXS
] then
504 lbResourcesList
.Clear();
505 lbResourcesList
.Items
.Add(TEXTURE_NAME_WATER
);
506 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID1
);
507 lbResourcesList
.Items
.Add(TEXTURE_NAME_ACID2
);
514 procedure TAddTextureForm
.bCloseClick(Sender
: TObject
);
519 procedure TAddTextureForm
.bAddTextureClick(Sender
: TObject
);
524 for i
:= 0 to lbResourcesList
.Count
-1 do
525 if lbResourcesList
.Selected
[i
] then
527 AddTexture(cbWADlist
.Text, cbSectionsList
.Text,
528 lbResourcesList
.Items
[i
], False);
529 lbResourcesList
.Selected
[i
] := False;
533 procedure TAddTextureForm
.bAddCloseClick(Sender
: TObject
);
535 bAddTextureClick(bAddTexture
);