1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
22 MAPSTRUCT
, BinEditor
, g_textures
;
25 TAddTextureArray
= Array of
31 TPanel
= Class (TObject
)
40 False: (Tex
: Cardinal);
41 True: (AnTex
: TAnimation
);
45 FCurTexture
: Integer; // Íîìåð òåêóùåé òåêñòóðû
51 SaveIt
: Boolean; // Ñîõðàíÿòü ïðè SaveState?
57 ArrIdx
: Integer; // index in one of internal arrays; sorry
58 tag
: Integer; // used in coldets and such; sorry
60 constructor Create(PanelRec
: TPanelRec_1
;
61 AddTextures
: TAddTextureArray
;
63 var Textures
: TLevelTextureArray
);
64 destructor Destroy(); override;
67 procedure DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
69 procedure SetFrame(Frame
: Integer; Count
: Byte);
70 procedure NextTexture(AnimLoop
: Byte = 0);
71 procedure SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
72 function GetTextureID(): Cardinal;
73 function GetTextureCount(): Integer;
75 procedure SaveState(var Mem
: TBinMemoryWriter
);
76 procedure LoadState(var Mem
: TBinMemoryReader
);
80 TPanelArray
= Array of TPanel
;
85 SysUtils
, g_basic
, g_map
, MAPDEF
, g_game
, e_graphics
,
86 g_console
, g_language
, e_log
, GL
;
89 PANEL_SIGNATURE
= $4C4E4150; // 'PANL'
93 constructor TPanel
.Create(PanelRec
: TPanelRec_1
;
94 AddTextures
: TAddTextureArray
;
96 var Textures
: TLevelTextureArray
);
102 Width
:= PanelRec
.Width
;
103 Height
:= PanelRec
.Height
;
112 PanelType
:= PanelRec
.PanelType
;
150 if ByteBool(PanelRec
.Flags
and PANEL_FLAG_HIDE
) then
152 SetLength(FTextureIDs
, 0);
156 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
157 if ByteBool(PanelType
and
162 PANEL_BLOCKMON
)) then
164 SetLength(FTextureIDs
, 0);
169 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
170 if WordBool(PanelType
and (PANEL_WATER
or PANEL_ACID1
or PANEL_ACID2
)) and
171 (not ByteBool(PanelRec
.Flags
and PANEL_FLAG_WATERTEXTURES
)) then
173 SetLength(FTextureIDs
, 1);
174 FTextureIDs
[0].Anim
:= False;
176 case PanelRec
.PanelType
of
178 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_WATER
;
180 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_ACID1
;
182 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_ACID2
;
189 SetLength(FTextureIDs
, Length(AddTextures
));
194 if CurTex
>= Length(FTextureIDs
) then
195 FCurTexture
:= Length(FTextureIDs
) - 1
197 FCurTexture
:= CurTex
;
199 for i
:= 0 to Length(FTextureIDs
)-1 do
201 FTextureIDs
[i
].Anim
:= AddTextures
[i
].Anim
;
202 if FTextureIDs
[i
].Anim
then
203 begin // Àíèìèðîâàííàÿ òåêñòóðà
204 FTextureIDs
[i
].AnTex
:=
205 TAnimation
.Create(Textures
[AddTextures
[i
].Texture
].FramesID
,
206 True, Textures
[AddTextures
[i
].Texture
].Speed
);
207 FTextureIDs
[i
].AnTex
.Blending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
208 FTextureIDs
[i
].AnTex
.Alpha
:= PanelRec
.Alpha
;
212 begin // Îáû÷íàÿ òåêñòóðà
213 FTextureIDs
[i
].Tex
:= Textures
[AddTextures
[i
].Texture
].TextureID
;
217 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
218 if Length(FTextureIDs
) > 1 then
221 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
222 if PanelRec
.TextureNum
> High(Textures
) then
224 e_WriteLog(Format('WTF?! PanelRec.TextureNum is out of limits! (%d : %d)', [PanelRec
.TextureNum
, High(Textures
)]), MSG_FATALERROR
);
228 FBlending
:= ByteBool(0);
230 else if not g_Map_IsSpecialTexture(Textures
[PanelRec
.TextureNum
].TextureName
) then
232 FTextureWidth
:= Textures
[PanelRec
.TextureNum
].Width
;
233 FTextureHeight
:= Textures
[PanelRec
.TextureNum
].Height
;
234 FAlpha
:= PanelRec
.Alpha
;
235 FBlending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
239 destructor TPanel
.Destroy();
243 for i
:= 0 to High(FTextureIDs
) do
244 if FTextureIDs
[i
].Anim
then
245 FTextureIDs
[i
].AnTex
.Free();
246 SetLength(FTextureIDs
, 0);
251 procedure TPanel
.Draw();
257 if Enabled
and (FCurTexture
>= 0) and
258 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) and
259 g_Collide(X
, Y
, Width
, Height
,
260 sX
, sY
, sWidth
, sHeight
) then
262 if FTextureIDs
[FCurTexture
].Anim
then
263 begin // Àíèìèðîâàííàÿ òåêñòóðà
264 if FTextureIDs
[FCurTexture
].AnTex
= nil then
267 for xx
:= 0 to (Width
div FTextureWidth
)-1 do
268 for yy
:= 0 to (Height
div FTextureHeight
)-1 do
269 FTextureIDs
[FCurTexture
].AnTex
.Draw(
270 X
+ xx
*FTextureWidth
,
271 Y
+ yy
*FTextureHeight
, M_NONE
);
274 begin // Îáû÷íàÿ òåêñòóðà
275 case FTextureIDs
[FCurTexture
].Tex
of
276 TEXTURE_SPECIAL_WATER
:
277 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
278 0, 0, 255, 0, B_FILTER
);
279 TEXTURE_SPECIAL_ACID1
:
280 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
281 0, 128, 0, 0, B_FILTER
);
282 TEXTURE_SPECIAL_ACID2
:
283 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
284 128, 0, 0, 0, B_FILTER
);
286 if g_Texture_Get('NOTEXTURE', NoTextureID
) then
288 e_GetTextureSize(NoTextureID
, @NW
, @NH
);
289 e_DrawFill(NoTextureID
, X
, Y
, Width
div NW
, Height
div NH
,
293 xx
:= X
+ (Width
div 2);
294 yy
:= Y
+ (Height
div 2);
295 e_DrawFillQuad(X
, Y
, xx
, yy
,
297 e_DrawFillQuad(xx
, Y
, X
+Width
-1, yy
,
299 e_DrawFillQuad(X
, yy
, xx
, Y
+Height
-1,
301 e_DrawFillQuad(xx
, yy
, X
+Width
-1, Y
+Height
-1,
306 e_DrawFill(FTextureIDs
[FCurTexture
].Tex
, X
, Y
,
307 Width
div FTextureWidth
,
308 Height
div FTextureHeight
,
309 FAlpha
, True, FBlending
);
315 procedure TPanel
.DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
316 procedure extrude (x
: Integer; y
: Integer);
318 glVertex2i(x
+(x
-lightX
)*500, y
+(y
-lightY
)*500);
319 //e_WriteLog(Format(' : (%d,%d)', [x+(x-lightX)*300, y+(y-lightY)*300]), MSG_WARNING);
322 procedure drawLine (x0
: Integer; y0
: Integer; x1
: Integer; y1
: Integer);
324 // does this side facing the light?
325 if ((x1
-x0
)*(lightY
-y0
)-(lightX
-x0
)*(y1
-y0
) >= 0) then exit
;
326 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
327 // this edge is facing the light, extrude and draw it
335 if radius
< 4 then exit
;
336 if Enabled
and (FCurTexture
>= 0) and (Width
> 0) and (Height
> 0) and (FAlpha
< 255) and g_Collide(X
, Y
, Width
, Height
, sX
, sY
, sWidth
, sHeight
) then
338 if not FTextureIDs
[FCurTexture
].Anim
then
340 case FTextureIDs
[FCurTexture
].Tex
of
341 TEXTURE_SPECIAL_WATER
: exit
;
342 TEXTURE_SPECIAL_ACID1
: exit
;
343 TEXTURE_SPECIAL_ACID2
: exit
;
347 if (X
+Width
< lightX
-radius
) then exit
;
348 if (Y
+Height
< lightY
-radius
) then exit
;
349 if (X
> lightX
+radius
) then exit
;
350 if (Y
> lightY
+radius
) then exit
;
351 //e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending);
354 drawLine(x
, y
, x
+width
, y
); // top
355 drawLine(x
+width
, y
, x
+width
, y
+height
); // right
356 drawLine(x
+width
, y
+height
, x
, y
+height
); // bottom
357 drawLine(x
, y
+height
, x
, y
); // left
362 procedure TPanel
.Update();
364 if Enabled
and (FCurTexture
>= 0) and
365 (FTextureIDs
[FCurTexture
].Anim
) and
366 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
367 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
369 FTextureIDs
[FCurTexture
].AnTex
.Update();
370 FCurFrame
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
;
371 FCurFrameCount
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
;
375 procedure TPanel
.SetFrame(Frame
: Integer; Count
: Byte);
377 function ClampInt(X
, A
, B
: Integer): Integer;
380 if X
< A
then Result
:= A
else if X
> B
then Result
:= B
;
384 if Enabled
and (FCurTexture
>= 0) and
385 (FTextureIDs
[FCurTexture
].Anim
) and
386 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
387 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
389 FCurFrame
:= ClampInt(Frame
, 0, FTextureIDs
[FCurTexture
].AnTex
.TotalFrames
);
390 FCurFrameCount
:= Count
;
391 FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
:= FCurFrame
;
392 FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
:= FCurFrameCount
;
396 procedure TPanel
.NextTexture(AnimLoop
: Byte = 0);
398 Assert(FCurTexture
>= -1, 'FCurTexture < -1');
401 if Length(FTextureIDs
) = 0 then
404 // Òîëüêî îäíà òåêñòóðà:
405 if Length(FTextureIDs
) = 1 then
407 if FCurTexture
= 0 then
413 // Áîëüøå îäíîé òåêñòóðû:
417 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
418 if FCurTexture
>= Length(FTextureIDs
) then
422 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
423 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
425 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
427 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
432 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
435 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
437 FTextureIDs
[FCurTexture
].AnTex
.Reset();
440 LastAnimLoop
:= AnimLoop
;
443 procedure TPanel
.SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
446 if Length(FTextureIDs
) = 0 then
449 // Òîëüêî îäíà òåêñòóðà:
450 if Length(FTextureIDs
) = 1 then
452 if (ID
= 0) or (ID
= -1) then
456 // Áîëüøå îäíîé òåêñòóðû:
458 if (ID
>= -1) and (ID
<= High(FTextureIDs
)) then
462 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
463 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
465 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
467 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
472 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
475 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
477 FTextureIDs
[FCurTexture
].AnTex
.Reset();
480 LastAnimLoop
:= AnimLoop
;
483 function TPanel
.GetTextureID(): DWORD
;
485 Result
:= TEXTURE_NONE
;
487 if (FCurTexture
>= 0) then
489 if FTextureIDs
[FCurTexture
].Anim
then
490 Result
:= FTextureIDs
[FCurTexture
].AnTex
.FramesID
492 Result
:= FTextureIDs
[FCurTexture
].Tex
;
496 function TPanel
.GetTextureCount(): Integer;
498 Result
:= Length(FTextureIDs
);
499 if Enabled
and (FCurTexture
>= 0) then
500 if (FTextureIDs
[FCurTexture
].Anim
) and
501 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
502 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
503 Result
:= Result
+ 100;
506 procedure TPanel
.SaveState(Var Mem
: TBinMemoryWriter
);
511 if (not SaveIt
) or (Mem
= nil) then
515 sig
:= PANEL_SIGNATURE
; // 'PANL'
517 // Îòêðûòà/çàêðûòà, åñëè äâåðü:
518 Mem
.WriteBoolean(Enabled
);
519 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò:
520 Mem
.WriteByte(LiftType
);
521 // Íîìåð òåêóùåé òåêñòóðû:
522 Mem
.WriteInt(FCurTexture
);
526 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà:
527 if (FCurTexture
>= 0) and (FTextureIDs
[FCurTexture
].Anim
) then
529 Assert(FTextureIDs
[FCurTexture
].AnTex
<> nil,
530 'TPanel.SaveState: No animation object');
535 Mem
.WriteBoolean(anim
);
536 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ:
538 FTextureIDs
[FCurTexture
].AnTex
.SaveState(Mem
);
541 procedure TPanel
.LoadState(var Mem
: TBinMemoryReader
);
546 if (not SaveIt
) or (Mem
= nil) then
551 if sig
<> PANEL_SIGNATURE
then // 'PANL'
553 raise EBinSizeError
.Create('TPanel.LoadState: Wrong Panel Signature');
555 // Îòêðûòà/çàêðûòà, åñëè äâåðü:
556 Mem
.ReadBoolean(Enabled
);
557 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò:
558 Mem
.ReadByte(LiftType
);
559 // Íîìåð òåêóùåé òåêñòóðû:
560 Mem
.ReadInt(FCurTexture
);
564 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà:
565 Mem
.ReadBoolean(anim
);
566 // Åñëè äà - çàãðóæàåì àíèìàöèþ:
569 Assert((FCurTexture
>= 0) and
570 (FTextureIDs
[FCurTexture
].Anim
) and
571 (FTextureIDs
[FCurTexture
].AnTex
<> nil),
572 'TPanel.LoadState: No animation object');
573 FTextureIDs
[FCurTexture
].AnTex
.LoadState(Mem
);