Skip to content

Commit

Permalink
Graceful shutdown with saves when using SDL window close button
Browse files Browse the repository at this point in the history
  • Loading branch information
veikkos committed Nov 22, 2020
1 parent 0b74015 commit 9aad000
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
15 changes: 14 additions & 1 deletion SDLPORT.PAS
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ unit SDLPort;
interface

type TPalette = Array[0..255,0..2] of byte;
type TCloseCallback = procedure;

function GetVersion : string;

procedure Init;
procedure DeinitGraphics;
procedure Deinit;

procedure SetCloseCallback(callback : TCloseCallback);
procedure SetPalette(input : TPalette);
procedure Render(buffer : array of byte);
procedure WaitRaster;
Expand Down Expand Up @@ -42,6 +44,7 @@ implementation

timer: TSDL_TimerID;
frameCount, lastFrameCount, subFrameCount, lastFrameTick: LongInt;
closeCallback: TCloseCallback;

function TimerCallback(interval: UInt32; param: Pointer): UInt32; cdecl;
var frameIntervalMs, nowMs : LongInt;
Expand Down Expand Up @@ -142,6 +145,8 @@ begin
lastFrameTick := SDL_GetTicks();

timer := SDL_AddTimer(1, TimerCallback, nil);

closeCallback := nil;
end;

procedure DeinitGraphics;
Expand Down Expand Up @@ -172,6 +177,11 @@ begin
SDL_SetPaletteColors(originalSurface^.format^.palette, @palette, 0, 256);
end;

procedure SetCloseCallback(callback : TCloseCallback);
begin
closeCallback:=callback;
end;

procedure Render(buffer : array of byte);
var pixels : Pointer;
pitch : LongInt;
Expand Down Expand Up @@ -233,7 +243,10 @@ begin

if (event.type_= SDL_QUITEV) then
begin
Halt;
if Assigned(closeCallback) then
begin
closeCallback;
end;
end;

if event.type_= SDL_KEYDOWN then
Expand Down
19 changes: 14 additions & 5 deletions SJ3.PAS
Original file line number Diff line number Diff line change
Expand Up @@ -5816,8 +5816,20 @@ begin

end;

procedure Close;
begin
SDLPort.DeinitGraphics;

Loppu;

SDLPort.Deinit;
end;

procedure CloseAndHalt;
begin
Close;
Halt;
end;

begin { MAIN }

Expand Down Expand Up @@ -5852,6 +5864,7 @@ begin { MAIN }
regnumber:='1234A'; }

SDLPort.Init;
SDLPort.SetCloseCallback(CloseAndHalt);

alku;

Expand All @@ -5868,11 +5881,7 @@ begin { MAIN }

{ Valikko; }

SDLPort.DeinitGraphics;

Loppu;

SDLPort.Deinit;
Close;

(*
CloseFont; { vapautetaan niiden k„ytt„m„ muisti }
Expand Down

0 comments on commit 9aad000

Please sign in to comment.