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 function getx1 (): Integer; inline;
46 function gety1 (): Integer; inline;
47 function getvisvalid (): Boolean; inline;
50 FCurTexture
: Integer; // Íîìåð òåêóùåé òåêñòóðû
56 SaveIt
: Boolean; // Ñîõðàíÿòü ïðè SaveState?
62 arrIdx
: Integer; // index in one of internal arrays; sorry
63 tag
: Integer; // used in coldets and such; sorry
64 proxyId
: Integer; // proxy id in map grid (DO NOT USE!)
66 constructor Create(PanelRec
: TPanelRec_1
;
67 AddTextures
: TAddTextureArray
;
69 var Textures
: TLevelTextureArray
);
70 destructor Destroy(); override;
73 procedure DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
75 procedure SetFrame(Frame
: Integer; Count
: Byte);
76 procedure NextTexture(AnimLoop
: Byte = 0);
77 procedure SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
78 function GetTextureID(): Cardinal;
79 function GetTextureCount(): Integer;
81 procedure SaveState(var Mem
: TBinMemoryWriter
);
82 procedure LoadState(var Mem
: TBinMemoryReader
);
84 property x0
: Integer read X
;
85 property y0
: Integer read Y
;
86 property x1
: Integer read getx1
; // inclusive!
87 property y1
: Integer read gety1
; // inclusive!
88 property visvalid
: Boolean read getvisvalid
; // panel is "visvalid" when it's width and height are positive
92 TPanelArray
= Array of TPanel
;
97 SysUtils
, g_basic
, g_map
, MAPDEF
, g_game
, e_graphics
,
98 g_console
, g_language
, e_log
, GL
;
101 PANEL_SIGNATURE
= $4C4E4150; // 'PANL'
105 constructor TPanel
.Create(PanelRec
: TPanelRec_1
;
106 AddTextures
: TAddTextureArray
;
108 var Textures
: TLevelTextureArray
);
114 Width
:= PanelRec
.Width
;
115 Height
:= PanelRec
.Height
;
124 PanelType
:= PanelRec
.PanelType
;
162 if ByteBool(PanelRec
.Flags
and PANEL_FLAG_HIDE
) then
164 SetLength(FTextureIDs
, 0);
168 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
169 if ByteBool(PanelType
and
174 PANEL_BLOCKMON
)) then
176 SetLength(FTextureIDs
, 0);
181 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
182 if WordBool(PanelType
and (PANEL_WATER
or PANEL_ACID1
or PANEL_ACID2
)) and
183 (not ByteBool(PanelRec
.Flags
and PANEL_FLAG_WATERTEXTURES
)) then
185 SetLength(FTextureIDs
, 1);
186 FTextureIDs
[0].Anim
:= False;
188 case PanelRec
.PanelType
of
190 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_WATER
;
192 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_ACID1
;
194 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_ACID2
;
201 SetLength(FTextureIDs
, Length(AddTextures
));
206 if CurTex
>= Length(FTextureIDs
) then
207 FCurTexture
:= Length(FTextureIDs
) - 1
209 FCurTexture
:= CurTex
;
211 for i
:= 0 to Length(FTextureIDs
)-1 do
213 FTextureIDs
[i
].Anim
:= AddTextures
[i
].Anim
;
214 if FTextureIDs
[i
].Anim
then
215 begin // Àíèìèðîâàííàÿ òåêñòóðà
216 FTextureIDs
[i
].AnTex
:=
217 TAnimation
.Create(Textures
[AddTextures
[i
].Texture
].FramesID
,
218 True, Textures
[AddTextures
[i
].Texture
].Speed
);
219 FTextureIDs
[i
].AnTex
.Blending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
220 FTextureIDs
[i
].AnTex
.Alpha
:= PanelRec
.Alpha
;
224 begin // Îáû÷íàÿ òåêñòóðà
225 FTextureIDs
[i
].Tex
:= Textures
[AddTextures
[i
].Texture
].TextureID
;
229 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
230 if Length(FTextureIDs
) > 1 then
233 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
234 if PanelRec
.TextureNum
> High(Textures
) then
236 e_WriteLog(Format('WTF?! PanelRec.TextureNum is out of limits! (%d : %d)', [PanelRec
.TextureNum
, High(Textures
)]), MSG_FATALERROR
);
240 FBlending
:= ByteBool(0);
242 else if not g_Map_IsSpecialTexture(Textures
[PanelRec
.TextureNum
].TextureName
) then
244 FTextureWidth
:= Textures
[PanelRec
.TextureNum
].Width
;
245 FTextureHeight
:= Textures
[PanelRec
.TextureNum
].Height
;
246 FAlpha
:= PanelRec
.Alpha
;
247 FBlending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
251 destructor TPanel
.Destroy();
255 for i
:= 0 to High(FTextureIDs
) do
256 if FTextureIDs
[i
].Anim
then
257 FTextureIDs
[i
].AnTex
.Free();
258 SetLength(FTextureIDs
, 0);
263 function TPanel
.getx1 (): Integer; inline; begin result
:= X
+Width
-1; end;
264 function TPanel
.gety1 (): Integer; inline; begin result
:= Y
+Height
-1; end;
265 function TPanel
.getvisvalid (): Boolean; inline; begin result
:= (Width
> 0) and (Height
> 0); end;
267 procedure TPanel
.Draw();
273 if {Enabled and} (FCurTexture
>= 0) and
274 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) and
275 g_Collide(X
, Y
, Width
, Height
,
276 sX
, sY
, sWidth
, sHeight
) then
278 if FTextureIDs
[FCurTexture
].Anim
then
279 begin // Àíèìèðîâàííàÿ òåêñòóðà
280 if FTextureIDs
[FCurTexture
].AnTex
= nil then
283 for xx
:= 0 to (Width
div FTextureWidth
)-1 do
284 for yy
:= 0 to (Height
div FTextureHeight
)-1 do
285 FTextureIDs
[FCurTexture
].AnTex
.Draw(
286 X
+ xx
*FTextureWidth
,
287 Y
+ yy
*FTextureHeight
, M_NONE
);
290 begin // Îáû÷íàÿ òåêñòóðà
291 case FTextureIDs
[FCurTexture
].Tex
of
292 TEXTURE_SPECIAL_WATER
:
293 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
294 0, 0, 255, 0, B_FILTER
);
295 TEXTURE_SPECIAL_ACID1
:
296 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
297 0, 128, 0, 0, B_FILTER
);
298 TEXTURE_SPECIAL_ACID2
:
299 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
300 128, 0, 0, 0, B_FILTER
);
302 if g_Texture_Get('NOTEXTURE', NoTextureID
) then
304 e_GetTextureSize(NoTextureID
, @NW
, @NH
);
305 e_DrawFill(NoTextureID
, X
, Y
, Width
div NW
, Height
div NH
,
309 xx
:= X
+ (Width
div 2);
310 yy
:= Y
+ (Height
div 2);
311 e_DrawFillQuad(X
, Y
, xx
, yy
,
313 e_DrawFillQuad(xx
, Y
, X
+Width
-1, yy
,
315 e_DrawFillQuad(X
, yy
, xx
, Y
+Height
-1,
317 e_DrawFillQuad(xx
, yy
, X
+Width
-1, Y
+Height
-1,
322 e_DrawFill(FTextureIDs
[FCurTexture
].Tex
, X
, Y
,
323 Width
div FTextureWidth
,
324 Height
div FTextureHeight
,
325 FAlpha
, True, FBlending
);
331 procedure TPanel
.DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
332 procedure extrude (x
: Integer; y
: Integer);
334 glVertex2i(x
+(x
-lightX
)*500, y
+(y
-lightY
)*500);
335 //e_WriteLog(Format(' : (%d,%d)', [x+(x-lightX)*300, y+(y-lightY)*300]), MSG_WARNING);
338 procedure drawLine (x0
: Integer; y0
: Integer; x1
: Integer; y1
: Integer);
340 // does this side facing the light?
341 if ((x1
-x0
)*(lightY
-y0
)-(lightX
-x0
)*(y1
-y0
) >= 0) then exit
;
342 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
343 // this edge is facing the light, extrude and draw it
351 if radius
< 4 then exit
;
352 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
354 if not FTextureIDs
[FCurTexture
].Anim
then
356 case FTextureIDs
[FCurTexture
].Tex
of
357 TEXTURE_SPECIAL_WATER
: exit
;
358 TEXTURE_SPECIAL_ACID1
: exit
;
359 TEXTURE_SPECIAL_ACID2
: exit
;
363 if (X
+Width
< lightX
-radius
) then exit
;
364 if (Y
+Height
< lightY
-radius
) then exit
;
365 if (X
> lightX
+radius
) then exit
;
366 if (Y
> lightY
+radius
) then exit
;
367 //e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending);
370 drawLine(x
, y
, x
+width
, y
); // top
371 drawLine(x
+width
, y
, x
+width
, y
+height
); // right
372 drawLine(x
+width
, y
+height
, x
, y
+height
); // bottom
373 drawLine(x
, y
+height
, x
, y
); // left
378 procedure TPanel
.Update();
380 if Enabled
and (FCurTexture
>= 0) and
381 (FTextureIDs
[FCurTexture
].Anim
) and
382 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
383 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
385 FTextureIDs
[FCurTexture
].AnTex
.Update();
386 FCurFrame
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
;
387 FCurFrameCount
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
;
391 procedure TPanel
.SetFrame(Frame
: Integer; Count
: Byte);
393 function ClampInt(X
, A
, B
: Integer): Integer;
396 if X
< A
then Result
:= A
else if X
> B
then Result
:= B
;
400 if Enabled
and (FCurTexture
>= 0) and
401 (FTextureIDs
[FCurTexture
].Anim
) and
402 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
403 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
405 FCurFrame
:= ClampInt(Frame
, 0, FTextureIDs
[FCurTexture
].AnTex
.TotalFrames
);
406 FCurFrameCount
:= Count
;
407 FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
:= FCurFrame
;
408 FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
:= FCurFrameCount
;
412 procedure TPanel
.NextTexture(AnimLoop
: Byte = 0);
414 Assert(FCurTexture
>= -1, 'FCurTexture < -1');
417 if Length(FTextureIDs
) = 0 then
420 // Òîëüêî îäíà òåêñòóðà:
421 if Length(FTextureIDs
) = 1 then
423 if FCurTexture
= 0 then
429 // Áîëüøå îäíîé òåêñòóðû:
433 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
434 if FCurTexture
>= Length(FTextureIDs
) then
438 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
439 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
441 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
443 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
448 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
451 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
453 FTextureIDs
[FCurTexture
].AnTex
.Reset();
456 LastAnimLoop
:= AnimLoop
;
459 procedure TPanel
.SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
462 if Length(FTextureIDs
) = 0 then
465 // Òîëüêî îäíà òåêñòóðà:
466 if Length(FTextureIDs
) = 1 then
468 if (ID
= 0) or (ID
= -1) then
472 // Áîëüøå îäíîé òåêñòóðû:
474 if (ID
>= -1) and (ID
<= High(FTextureIDs
)) then
478 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
479 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
481 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
483 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
488 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
491 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
493 FTextureIDs
[FCurTexture
].AnTex
.Reset();
496 LastAnimLoop
:= AnimLoop
;
499 function TPanel
.GetTextureID(): DWORD
;
501 Result
:= TEXTURE_NONE
;
503 if (FCurTexture
>= 0) then
505 if FTextureIDs
[FCurTexture
].Anim
then
506 Result
:= FTextureIDs
[FCurTexture
].AnTex
.FramesID
508 Result
:= FTextureIDs
[FCurTexture
].Tex
;
512 function TPanel
.GetTextureCount(): Integer;
514 Result
:= Length(FTextureIDs
);
515 if Enabled
and (FCurTexture
>= 0) then
516 if (FTextureIDs
[FCurTexture
].Anim
) and
517 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
518 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
519 Result
:= Result
+ 100;
522 procedure TPanel
.SaveState(Var Mem
: TBinMemoryWriter
);
527 if (not SaveIt
) or (Mem
= nil) then
531 sig
:= PANEL_SIGNATURE
; // 'PANL'
533 // Îòêðûòà/çàêðûòà, åñëè äâåðü:
534 Mem
.WriteBoolean(Enabled
);
535 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò:
536 Mem
.WriteByte(LiftType
);
537 // Íîìåð òåêóùåé òåêñòóðû:
538 Mem
.WriteInt(FCurTexture
);
542 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà:
543 if (FCurTexture
>= 0) and (FTextureIDs
[FCurTexture
].Anim
) then
545 Assert(FTextureIDs
[FCurTexture
].AnTex
<> nil,
546 'TPanel.SaveState: No animation object');
551 Mem
.WriteBoolean(anim
);
552 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ:
554 FTextureIDs
[FCurTexture
].AnTex
.SaveState(Mem
);
557 procedure TPanel
.LoadState(var Mem
: TBinMemoryReader
);
562 if (not SaveIt
) or (Mem
= nil) then
567 if sig
<> PANEL_SIGNATURE
then // 'PANL'
569 raise EBinSizeError
.Create('TPanel.LoadState: Wrong Panel Signature');
571 // Îòêðûòà/çàêðûòà, åñëè äâåðü:
572 Mem
.ReadBoolean(Enabled
);
573 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò:
574 Mem
.ReadByte(LiftType
);
575 // Íîìåð òåêóùåé òåêñòóðû:
576 Mem
.ReadInt(FCurTexture
);
580 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà:
581 Mem
.ReadBoolean(anim
);
582 // Åñëè äà - çàãðóæàåì àíèìàöèþ:
585 Assert((FCurTexture
>= 0) and
586 (FTextureIDs
[FCurTexture
].Anim
) and
587 (FTextureIDs
[FCurTexture
].AnTex
<> nil),
588 'TPanel.LoadState: No animation object');
589 FTextureIDs
[FCurTexture
].AnTex
.LoadState(Mem
);