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
59 constructor Create(PanelRec
: TPanelRec_1
;
60 AddTextures
: TAddTextureArray
;
62 var Textures
: TLevelTextureArray
);
63 destructor Destroy(); override;
66 procedure DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
68 procedure SetFrame(Frame
: Integer; Count
: Byte);
69 procedure NextTexture(AnimLoop
: Byte = 0);
70 procedure SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
71 function GetTextureID(): Cardinal;
72 function GetTextureCount(): Integer;
74 procedure SaveState(var Mem
: TBinMemoryWriter
);
75 procedure LoadState(var Mem
: TBinMemoryReader
);
79 TPanelArray
= Array of TPanel
;
84 SysUtils
, g_basic
, g_map
, MAPDEF
, g_game
, e_graphics
,
85 g_console
, g_language
, e_log
, GL
;
88 PANEL_SIGNATURE
= $4C4E4150; // 'PANL'
92 constructor TPanel
.Create(PanelRec
: TPanelRec_1
;
93 AddTextures
: TAddTextureArray
;
95 var Textures
: TLevelTextureArray
);
101 Width
:= PanelRec
.Width
;
102 Height
:= PanelRec
.Height
;
111 PanelType
:= PanelRec
.PanelType
;
149 if ByteBool(PanelRec
.Flags
and PANEL_FLAG_HIDE
) then
151 SetLength(FTextureIDs
, 0);
155 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
156 if ByteBool(PanelType
and
161 PANEL_BLOCKMON
)) then
163 SetLength(FTextureIDs
, 0);
168 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
169 if WordBool(PanelType
and (PANEL_WATER
or PANEL_ACID1
or PANEL_ACID2
)) and
170 (not ByteBool(PanelRec
.Flags
and PANEL_FLAG_WATERTEXTURES
)) then
172 SetLength(FTextureIDs
, 1);
173 FTextureIDs
[0].Anim
:= False;
175 case PanelRec
.PanelType
of
177 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_WATER
;
179 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_ACID1
;
181 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_ACID2
;
188 SetLength(FTextureIDs
, Length(AddTextures
));
193 if CurTex
>= Length(FTextureIDs
) then
194 FCurTexture
:= Length(FTextureIDs
) - 1
196 FCurTexture
:= CurTex
;
198 for i
:= 0 to Length(FTextureIDs
)-1 do
200 FTextureIDs
[i
].Anim
:= AddTextures
[i
].Anim
;
201 if FTextureIDs
[i
].Anim
then
202 begin // Àíèìèðîâàííàÿ òåêñòóðà
203 FTextureIDs
[i
].AnTex
:=
204 TAnimation
.Create(Textures
[AddTextures
[i
].Texture
].FramesID
,
205 True, Textures
[AddTextures
[i
].Texture
].Speed
);
206 FTextureIDs
[i
].AnTex
.Blending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
207 FTextureIDs
[i
].AnTex
.Alpha
:= PanelRec
.Alpha
;
211 begin // Îáû÷íàÿ òåêñòóðà
212 FTextureIDs
[i
].Tex
:= Textures
[AddTextures
[i
].Texture
].TextureID
;
216 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
217 if Length(FTextureIDs
) > 1 then
220 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
221 if PanelRec
.TextureNum
> High(Textures
) then
223 e_WriteLog(Format('WTF?! PanelRec.TextureNum is out of limits! (%d : %d)', [PanelRec
.TextureNum
, High(Textures
)]), MSG_FATALERROR
);
227 FBlending
:= ByteBool(0);
229 else if not g_Map_IsSpecialTexture(Textures
[PanelRec
.TextureNum
].TextureName
) then
231 FTextureWidth
:= Textures
[PanelRec
.TextureNum
].Width
;
232 FTextureHeight
:= Textures
[PanelRec
.TextureNum
].Height
;
233 FAlpha
:= PanelRec
.Alpha
;
234 FBlending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
238 destructor TPanel
.Destroy();
242 for i
:= 0 to High(FTextureIDs
) do
243 if FTextureIDs
[i
].Anim
then
244 FTextureIDs
[i
].AnTex
.Free();
245 SetLength(FTextureIDs
, 0);
250 procedure TPanel
.Draw();
256 if Enabled
and (FCurTexture
>= 0) and
257 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) and
258 g_Collide(X
, Y
, Width
, Height
,
259 sX
, sY
, sWidth
, sHeight
) then
261 if FTextureIDs
[FCurTexture
].Anim
then
262 begin // Àíèìèðîâàííàÿ òåêñòóðà
263 if FTextureIDs
[FCurTexture
].AnTex
= nil then
266 for xx
:= 0 to (Width
div FTextureWidth
)-1 do
267 for yy
:= 0 to (Height
div FTextureHeight
)-1 do
268 FTextureIDs
[FCurTexture
].AnTex
.Draw(
269 X
+ xx
*FTextureWidth
,
270 Y
+ yy
*FTextureHeight
, M_NONE
);
273 begin // Îáû÷íàÿ òåêñòóðà
274 case FTextureIDs
[FCurTexture
].Tex
of
275 TEXTURE_SPECIAL_WATER
:
276 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
277 0, 0, 255, 0, B_FILTER
);
278 TEXTURE_SPECIAL_ACID1
:
279 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
280 0, 128, 0, 0, B_FILTER
);
281 TEXTURE_SPECIAL_ACID2
:
282 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
283 128, 0, 0, 0, B_FILTER
);
285 if g_Texture_Get('NOTEXTURE', NoTextureID
) then
287 e_GetTextureSize(NoTextureID
, @NW
, @NH
);
288 e_DrawFill(NoTextureID
, X
, Y
, Width
div NW
, Height
div NH
,
292 xx
:= X
+ (Width
div 2);
293 yy
:= Y
+ (Height
div 2);
294 e_DrawFillQuad(X
, Y
, xx
, yy
,
296 e_DrawFillQuad(xx
, Y
, X
+Width
-1, yy
,
298 e_DrawFillQuad(X
, yy
, xx
, Y
+Height
-1,
300 e_DrawFillQuad(xx
, yy
, X
+Width
-1, Y
+Height
-1,
305 e_DrawFill(FTextureIDs
[FCurTexture
].Tex
, X
, Y
,
306 Width
div FTextureWidth
,
307 Height
div FTextureHeight
,
308 FAlpha
, True, FBlending
);
314 procedure TPanel
.DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
315 procedure extrude (x
: Integer; y
: Integer);
317 glVertex2i(x
+(x
-lightX
)*500, y
+(y
-lightY
)*500);
318 //e_WriteLog(Format(' : (%d,%d)', [x+(x-lightX)*300, y+(y-lightY)*300]), MSG_WARNING);
321 procedure drawLine (x0
: Integer; y0
: Integer; x1
: Integer; y1
: Integer);
323 // does this side facing the light?
324 if ((x1
-x0
)*(lightY
-y0
)-(lightX
-x0
)*(y1
-y0
) >= 0) then exit
;
325 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
326 // this edge is facing the light, extrude and draw it
334 if radius
< 4 then exit
;
335 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
337 if not FTextureIDs
[FCurTexture
].Anim
then
339 case FTextureIDs
[FCurTexture
].Tex
of
340 TEXTURE_SPECIAL_WATER
: exit
;
341 TEXTURE_SPECIAL_ACID1
: exit
;
342 TEXTURE_SPECIAL_ACID2
: exit
;
346 if (X
+Width
< lightX
-radius
) then exit
;
347 if (Y
+Height
< lightY
-radius
) then exit
;
348 if (X
> lightX
+radius
) then exit
;
349 if (Y
> lightY
+radius
) then exit
;
350 //e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending);
353 drawLine(x
, y
, x
+width
, y
); // top
354 drawLine(x
+width
, y
, x
+width
, y
+height
); // right
355 drawLine(x
+width
, y
+height
, x
, y
+height
); // bottom
356 drawLine(x
, y
+height
, x
, y
); // left
361 procedure TPanel
.Update();
363 if Enabled
and (FCurTexture
>= 0) and
364 (FTextureIDs
[FCurTexture
].Anim
) and
365 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
366 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
368 FTextureIDs
[FCurTexture
].AnTex
.Update();
369 FCurFrame
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
;
370 FCurFrameCount
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
;
374 procedure TPanel
.SetFrame(Frame
: Integer; Count
: Byte);
376 function ClampInt(X
, A
, B
: Integer): Integer;
379 if X
< A
then Result
:= A
else if X
> B
then Result
:= B
;
383 if Enabled
and (FCurTexture
>= 0) and
384 (FTextureIDs
[FCurTexture
].Anim
) and
385 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
386 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
388 FCurFrame
:= ClampInt(Frame
, 0, FTextureIDs
[FCurTexture
].AnTex
.TotalFrames
);
389 FCurFrameCount
:= Count
;
390 FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
:= FCurFrame
;
391 FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
:= FCurFrameCount
;
395 procedure TPanel
.NextTexture(AnimLoop
: Byte = 0);
397 Assert(FCurTexture
>= -1, 'FCurTexture < -1');
400 if Length(FTextureIDs
) = 0 then
403 // Òîëüêî îäíà òåêñòóðà:
404 if Length(FTextureIDs
) = 1 then
406 if FCurTexture
= 0 then
412 // Áîëüøå îäíîé òåêñòóðû:
416 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
417 if FCurTexture
>= Length(FTextureIDs
) then
421 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
422 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
424 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
426 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
431 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
434 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
436 FTextureIDs
[FCurTexture
].AnTex
.Reset();
439 LastAnimLoop
:= AnimLoop
;
442 procedure TPanel
.SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
445 if Length(FTextureIDs
) = 0 then
448 // Òîëüêî îäíà òåêñòóðà:
449 if Length(FTextureIDs
) = 1 then
451 if (ID
= 0) or (ID
= -1) then
455 // Áîëüøå îäíîé òåêñòóðû:
457 if (ID
>= -1) and (ID
<= High(FTextureIDs
)) then
461 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
462 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
464 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
466 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
471 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
474 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
476 FTextureIDs
[FCurTexture
].AnTex
.Reset();
479 LastAnimLoop
:= AnimLoop
;
482 function TPanel
.GetTextureID(): DWORD
;
484 Result
:= TEXTURE_NONE
;
486 if (FCurTexture
>= 0) then
488 if FTextureIDs
[FCurTexture
].Anim
then
489 Result
:= FTextureIDs
[FCurTexture
].AnTex
.FramesID
491 Result
:= FTextureIDs
[FCurTexture
].Tex
;
495 function TPanel
.GetTextureCount(): Integer;
497 Result
:= Length(FTextureIDs
);
498 if Enabled
and (FCurTexture
>= 0) then
499 if (FTextureIDs
[FCurTexture
].Anim
) and
500 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
501 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
502 Result
:= Result
+ 100;
505 procedure TPanel
.SaveState(Var Mem
: TBinMemoryWriter
);
510 if (not SaveIt
) or (Mem
= nil) then
514 sig
:= PANEL_SIGNATURE
; // 'PANL'
516 // Îòêðûòà/çàêðûòà, åñëè äâåðü:
517 Mem
.WriteBoolean(Enabled
);
518 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò:
519 Mem
.WriteByte(LiftType
);
520 // Íîìåð òåêóùåé òåêñòóðû:
521 Mem
.WriteInt(FCurTexture
);
525 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà:
526 if (FCurTexture
>= 0) and (FTextureIDs
[FCurTexture
].Anim
) then
528 Assert(FTextureIDs
[FCurTexture
].AnTex
<> nil,
529 'TPanel.SaveState: No animation object');
534 Mem
.WriteBoolean(anim
);
535 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ:
537 FTextureIDs
[FCurTexture
].AnTex
.SaveState(Mem
);
540 procedure TPanel
.LoadState(var Mem
: TBinMemoryReader
);
545 if (not SaveIt
) or (Mem
= nil) then
550 if sig
<> PANEL_SIGNATURE
then // 'PANL'
552 raise EBinSizeError
.Create('TPanel.LoadState: Wrong Panel Signature');
554 // Îòêðûòà/çàêðûòà, åñëè äâåðü:
555 Mem
.ReadBoolean(Enabled
);
556 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò:
557 Mem
.ReadByte(LiftType
);
558 // Íîìåð òåêóùåé òåêñòóðû:
559 Mem
.ReadInt(FCurTexture
);
563 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà:
564 Mem
.ReadBoolean(anim
);
565 // Åñëè äà - çàãðóæàåì àíèìàöèþ:
568 Assert((FCurTexture
>= 0) and
569 (FTextureIDs
[FCurTexture
].Anim
) and
570 (FTextureIDs
[FCurTexture
].AnTex
<> nil),
571 'TPanel.LoadState: No animation object');
572 FTextureIDs
[FCurTexture
].AnTex
.LoadState(Mem
);