af9d35f89cb78dd58e2f60395e06aeb7fd00de01
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/>.
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?
58 constructor Create(PanelRec
: TPanelRec_1
;
59 AddTextures
: TAddTextureArray
;
61 var Textures
: TLevelTextureArray
);
62 destructor Destroy(); override;
65 procedure DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
67 procedure SetFrame(Frame
: Integer; Count
: Byte);
68 procedure NextTexture(AnimLoop
: Byte = 0);
69 procedure SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
70 function GetTextureID(): Cardinal;
71 function GetTextureCount(): Integer;
73 procedure SaveState(var Mem
: TBinMemoryWriter
);
74 procedure LoadState(var Mem
: TBinMemoryReader
);
78 TPanelArray
= Array of TPanel
;
83 SysUtils
, g_basic
, g_map
, MAPDEF
, g_game
, e_graphics
,
84 g_console
, g_language
, e_log
, GL
;
87 PANEL_SIGNATURE
= $4C4E4150; // 'PANL'
91 constructor TPanel
.Create(PanelRec
: TPanelRec_1
;
92 AddTextures
: TAddTextureArray
;
94 var Textures
: TLevelTextureArray
);
100 Width
:= PanelRec
.Width
;
101 Height
:= PanelRec
.Height
;
110 PanelType
:= PanelRec
.PanelType
;
148 if ByteBool(PanelRec
.Flags
and PANEL_FLAG_HIDE
) then
150 SetLength(FTextureIDs
, 0);
154 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
155 if ByteBool(PanelType
and
160 PANEL_BLOCKMON
)) then
162 SetLength(FTextureIDs
, 0);
167 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
168 if WordBool(PanelType
and (PANEL_WATER
or PANEL_ACID1
or PANEL_ACID2
)) and
169 (not ByteBool(PanelRec
.Flags
and PANEL_FLAG_WATERTEXTURES
)) then
171 SetLength(FTextureIDs
, 1);
172 FTextureIDs
[0].Anim
:= False;
174 case PanelRec
.PanelType
of
176 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_WATER
;
178 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_ACID1
;
180 FTextureIDs
[0].Tex
:= TEXTURE_SPECIAL_ACID2
;
187 SetLength(FTextureIDs
, Length(AddTextures
));
192 if CurTex
>= Length(FTextureIDs
) then
193 FCurTexture
:= Length(FTextureIDs
) - 1
195 FCurTexture
:= CurTex
;
197 for i
:= 0 to Length(FTextureIDs
)-1 do
199 FTextureIDs
[i
].Anim
:= AddTextures
[i
].Anim
;
200 if FTextureIDs
[i
].Anim
then
201 begin // Àíèìèðîâàííàÿ òåêñòóðà
202 FTextureIDs
[i
].AnTex
:=
203 TAnimation
.Create(Textures
[AddTextures
[i
].Texture
].FramesID
,
204 True, Textures
[AddTextures
[i
].Texture
].Speed
);
205 FTextureIDs
[i
].AnTex
.Blending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
206 FTextureIDs
[i
].AnTex
.Alpha
:= PanelRec
.Alpha
;
210 begin // Îáû÷íàÿ òåêñòóðà
211 FTextureIDs
[i
].Tex
:= Textures
[AddTextures
[i
].Texture
].TextureID
;
215 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
216 if Length(FTextureIDs
) > 1 then
219 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
220 if PanelRec
.TextureNum
> High(Textures
) then
222 e_WriteLog(Format('WTF?! PanelRec.TextureNum is out of limits! (%d : %d)', [PanelRec
.TextureNum
, High(Textures
)]), MSG_FATALERROR
);
226 FBlending
:= ByteBool(0);
228 else if not g_Map_IsSpecialTexture(Textures
[PanelRec
.TextureNum
].TextureName
) then
230 FTextureWidth
:= Textures
[PanelRec
.TextureNum
].Width
;
231 FTextureHeight
:= Textures
[PanelRec
.TextureNum
].Height
;
232 FAlpha
:= PanelRec
.Alpha
;
233 FBlending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
237 destructor TPanel
.Destroy();
241 for i
:= 0 to High(FTextureIDs
) do
242 if FTextureIDs
[i
].Anim
then
243 FTextureIDs
[i
].AnTex
.Free();
244 SetLength(FTextureIDs
, 0);
249 procedure TPanel
.Draw();
255 if Enabled
and (FCurTexture
>= 0) and
256 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) and
257 g_Collide(X
, Y
, Width
, Height
,
258 sX
, sY
, sWidth
, sHeight
) then
260 if FTextureIDs
[FCurTexture
].Anim
then
261 begin // Àíèìèðîâàííàÿ òåêñòóðà
262 if FTextureIDs
[FCurTexture
].AnTex
= nil then
265 for xx
:= 0 to (Width
div FTextureWidth
)-1 do
266 for yy
:= 0 to (Height
div FTextureHeight
)-1 do
267 FTextureIDs
[FCurTexture
].AnTex
.Draw(
268 X
+ xx
*FTextureWidth
,
269 Y
+ yy
*FTextureHeight
, M_NONE
);
272 begin // Îáû÷íàÿ òåêñòóðà
273 case FTextureIDs
[FCurTexture
].Tex
of
274 TEXTURE_SPECIAL_WATER
:
275 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
276 0, 0, 255, 0, B_FILTER
);
277 TEXTURE_SPECIAL_ACID1
:
278 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
279 0, 128, 0, 0, B_FILTER
);
280 TEXTURE_SPECIAL_ACID2
:
281 e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1,
282 128, 0, 0, 0, B_FILTER
);
284 if g_Texture_Get('NOTEXTURE', NoTextureID
) then
286 e_GetTextureSize(NoTextureID
, @NW
, @NH
);
287 e_DrawFill(NoTextureID
, X
, Y
, Width
div NW
, Height
div NH
,
291 xx
:= X
+ (Width
div 2);
292 yy
:= Y
+ (Height
div 2);
293 e_DrawFillQuad(X
, Y
, xx
, yy
,
295 e_DrawFillQuad(xx
, Y
, X
+Width
-1, yy
,
297 e_DrawFillQuad(X
, yy
, xx
, Y
+Height
-1,
299 e_DrawFillQuad(xx
, yy
, X
+Width
-1, Y
+Height
-1,
304 e_DrawFill(FTextureIDs
[FCurTexture
].Tex
, X
, Y
,
305 Width
div FTextureWidth
,
306 Height
div FTextureHeight
,
307 FAlpha
, True, FBlending
);
313 procedure TPanel
.DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
314 procedure extrude (x
: Integer; y
: Integer);
316 glVertex2i(x
+(x
-lightX
)*500, y
+(y
-lightY
)*500);
317 //e_WriteLog(Format(' : (%d,%d)', [x+(x-lightX)*300, y+(y-lightY)*300]), MSG_WARNING);
320 procedure drawLine (x0
: Integer; y0
: Integer; x1
: Integer; y1
: Integer);
322 // does this side facing the light?
323 if ((x1
-x0
)*(lightY
-y0
)-(lightX
-x0
)*(y1
-y0
) >= 0) then exit
;
324 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
325 // this edge is facing the light, extrude and draw it
333 if radius
< 4 then exit
;
334 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
336 if not FTextureIDs
[FCurTexture
].Anim
then
338 case FTextureIDs
[FCurTexture
].Tex
of
339 TEXTURE_SPECIAL_WATER
: exit
;
340 TEXTURE_SPECIAL_ACID1
: exit
;
341 TEXTURE_SPECIAL_ACID2
: exit
;
345 if (X
+Width
< lightX
-radius
) then exit
;
346 if (Y
+Height
< lightY
-radius
) then exit
;
347 if (X
> lightX
+radius
) then exit
;
348 if (Y
> lightY
+radius
) then exit
;
349 //e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending);
352 drawLine(x
, y
, x
+width
, y
); // top
353 drawLine(x
+width
, y
, x
+width
, y
+height
); // right
354 drawLine(x
+width
, y
+height
, x
, y
+height
); // bottom
355 drawLine(x
, y
+height
, x
, y
); // left
360 procedure TPanel
.Update();
362 if Enabled
and (FCurTexture
>= 0) and
363 (FTextureIDs
[FCurTexture
].Anim
) and
364 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
365 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
367 FTextureIDs
[FCurTexture
].AnTex
.Update();
368 FCurFrame
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
;
369 FCurFrameCount
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
;
373 procedure TPanel
.SetFrame(Frame
: Integer; Count
: Byte);
375 function ClampInt(X
, A
, B
: Integer): Integer;
378 if X
< A
then Result
:= A
else if X
> B
then Result
:= B
;
382 if Enabled
and (FCurTexture
>= 0) and
383 (FTextureIDs
[FCurTexture
].Anim
) and
384 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
385 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
387 FCurFrame
:= ClampInt(Frame
, 0, FTextureIDs
[FCurTexture
].AnTex
.TotalFrames
);
388 FCurFrameCount
:= Count
;
389 FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
:= FCurFrame
;
390 FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
:= FCurFrameCount
;
394 procedure TPanel
.NextTexture(AnimLoop
: Byte = 0);
396 Assert(FCurTexture
>= -1, 'FCurTexture < -1');
399 if Length(FTextureIDs
) = 0 then
402 // Òîëüêî îäíà òåêñòóðà:
403 if Length(FTextureIDs
) = 1 then
405 if FCurTexture
= 0 then
411 // Áîëüøå îäíîé òåêñòóðû:
415 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
416 if FCurTexture
>= Length(FTextureIDs
) then
420 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
421 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
423 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
425 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
430 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
433 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
435 FTextureIDs
[FCurTexture
].AnTex
.Reset();
438 LastAnimLoop
:= AnimLoop
;
441 procedure TPanel
.SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
444 if Length(FTextureIDs
) = 0 then
447 // Òîëüêî îäíà òåêñòóðà:
448 if Length(FTextureIDs
) = 1 then
450 if (ID
= 0) or (ID
= -1) then
454 // Áîëüøå îäíîé òåêñòóðû:
456 if (ID
>= -1) and (ID
<= High(FTextureIDs
)) then
460 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
461 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
463 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
465 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
470 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
473 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
475 FTextureIDs
[FCurTexture
].AnTex
.Reset();
478 LastAnimLoop
:= AnimLoop
;
481 function TPanel
.GetTextureID(): DWORD
;
483 Result
:= TEXTURE_NONE
;
485 if (FCurTexture
>= 0) then
487 if FTextureIDs
[FCurTexture
].Anim
then
488 Result
:= FTextureIDs
[FCurTexture
].AnTex
.FramesID
490 Result
:= FTextureIDs
[FCurTexture
].Tex
;
494 function TPanel
.GetTextureCount(): Integer;
496 Result
:= Length(FTextureIDs
);
497 if Enabled
and (FCurTexture
>= 0) then
498 if (FTextureIDs
[FCurTexture
].Anim
) and
499 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
500 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
501 Result
:= Result
+ 100;
504 procedure TPanel
.SaveState(Var Mem
: TBinMemoryWriter
);
509 if (not SaveIt
) or (Mem
= nil) then
513 sig
:= PANEL_SIGNATURE
; // 'PANL'
515 // Îòêðûòà/çàêðûòà, åñëè äâåðü:
516 Mem
.WriteBoolean(Enabled
);
517 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò:
518 Mem
.WriteByte(LiftType
);
519 // Íîìåð òåêóùåé òåêñòóðû:
520 Mem
.WriteInt(FCurTexture
);
524 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà:
525 if (FCurTexture
>= 0) and (FTextureIDs
[FCurTexture
].Anim
) then
527 Assert(FTextureIDs
[FCurTexture
].AnTex
<> nil,
528 'TPanel.SaveState: No animation object');
533 Mem
.WriteBoolean(anim
);
534 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ:
536 FTextureIDs
[FCurTexture
].AnTex
.SaveState(Mem
);
539 procedure TPanel
.LoadState(var Mem
: TBinMemoryReader
);
544 if (not SaveIt
) or (Mem
= nil) then
549 if sig
<> PANEL_SIGNATURE
then // 'PANL'
551 raise EBinSizeError
.Create('TPanel.LoadState: Wrong Panel Signature');
553 // Îòêðûòà/çàêðûòà, åñëè äâåðü:
554 Mem
.ReadBoolean(Enabled
);
555 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò:
556 Mem
.ReadByte(LiftType
);
557 // Íîìåð òåêóùåé òåêñòóðû:
558 Mem
.ReadInt(FCurTexture
);
562 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà:
563 Mem
.ReadBoolean(anim
);
564 // Åñëè äà - çàãðóæàåì àíèìàöèþ:
567 Assert((FCurTexture
>= 0) and
568 (FTextureIDs
[FCurTexture
].Anim
) and
569 (FTextureIDs
[FCurTexture
].AnTex
<> nil),
570 'TPanel.LoadState: No animation object');
571 FTextureIDs
[FCurTexture
].AnTex
.LoadState(Mem
);