Skip to content

Commit e245caf

Browse files
committed
Disable FPE while initializing SDL2
See: PascalGameDevelopment/SDL2-for-Pascal#56 According to this GitHub Issue, when initializing the software renderer on Linux, SDL2 triggers a crash in Mesa code. This happens because FPC and Delphi enable FPE checking by default. This commit disables those exceptions for the duration of SDL2 video initialization, which should prevent crashes while using the software renderer.
1 parent 05ff9c7 commit e245caf

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/ld25.pas

+21-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
{$INCLUDE defines.inc}
2020

2121
uses
22-
SysUtils, SDL2, SDL2_image, SDL2_mixer,
22+
SysUtils, Math,
23+
SDL2, SDL2_image, SDL2_mixer,
2324
Assets, Colours, ConfigFiles, FloatingText, Fonts, Game, Images, Objects, MathUtils, Rooms, Shared
2425
;
2526

@@ -576,7 +577,10 @@
576577

577578
Function Startup():Boolean;
578579
Var
579-
Title, S:AnsiString; Timu:Comp; GM:TGameMode;
580+
Title, ErrStr: AnsiString;
581+
Timu: Comp;
582+
GM: TGameMode;
583+
OldMask, NewMask: TFPUExceptionMask;
580584
Begin
581585
Timu:=GetMSecs(); Randomize();
582586

@@ -585,7 +589,15 @@
585589

586590
LoadConfig();
587591
For GM:=Low(GM) to High(GM) do SaveExists[GM]:=IHasGame(GM);
588-
592+
593+
// Disable Floating Poing Exception checking while initializing the video stack.
594+
// See: https://github.com/PascalGameDevelopment/SDL2-for-Pascal/issues/56
595+
OldMask := Math.GetExceptionMask();
596+
NewMask := OldMask;
597+
Include(NewMask, exInvalidOp);
598+
Include(NewMask, exZeroDivide);
599+
Math.SetExceptionMask(NewMask);
600+
589601
Write('Initializing SDL2 video... ');
590602
If (SDL_Init(SDL_Init_Video or SDL_Init_Timer)<>0) then begin
591603
Writeln('Failed!');
@@ -647,7 +659,10 @@
647659
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 'nearest');
648660
SDL_RenderSetLogicalSize(Shared.Renderer, RESOL_W, RESOL_H)
649661
end;
650-
662+
663+
// Restore the old mask after we disabled FPE checks.
664+
Math.SetExceptionMask(OldMask);
665+
651666
Write('Creating render target texture... ');
652667
Shared.Display := SDL_CreateTexture(Shared.Renderer, SDL_GetWindowPixelFormat(Shared.Window), SDL_TEXTUREACCESS_TARGET, RESOL_W, RESOL_H);
653668
if(Shared.Display = NIL) then begin
@@ -658,8 +673,8 @@
658673

659674
Write('Loading assets... ');
660675
RegisterAllAssets();
661-
If Not LoadAssets(S, @LoadUpdate) then begin
662-
Writeln('ERROR'); Writeln(S); Exit(False);
676+
If Not LoadAssets(ErrStr, @LoadUpdate) then begin
677+
Writeln('ERROR'); Writeln(ErrStr); Exit(False);
663678
end else
664679
Writeln('Success!');
665680

0 commit comments

Comments
 (0)