SDLTry doesn't halt engine no more
authorunc0rr
Sun, 10 Jan 2016 00:45:13 +0300
changeset 11507 bd9a2f1b0080
parent 11506 24bef86e3f3a
child 11508 a4ad8a9e0f69
SDLTry doesn't halt engine no more
hedgewars/hwengine.pas
hedgewars/uDebug.pas
hedgewars/uIO.pas
hedgewars/uLand.pas
hedgewars/uLandGraphics.pas
hedgewars/uLandObjects.pas
hedgewars/uRenderUtils.pas
hedgewars/uSound.pas
hedgewars/uStore.pas
hedgewars/uTextures.pas
--- a/hedgewars/hwengine.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/hwengine.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -342,18 +342,23 @@
         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
 
     WriteToConsole('Init SDL... ');
-    if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, 'SDL_Init', true);
+    if not cOnlyStats then SDLCheck(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, 'SDL_Init', true);
     WriteLnToConsole(msgOK);
+    if not cOnlyStats then
+        begin
+        WriteToConsole('Init SDL_ttf... ');
+        SDLCheck(TTF_Init() <> -1, 'TTF_Init', true);
+        WriteLnToConsole(msgOK);
+        end;
 
+    if not allOK then
+    begin
+        freeEverything(true);
+        exit
+    end;
     //SDL_StartTextInput();
     SDL_ShowCursor(0);
 
-    if not cOnlyStats then
-        begin
-        WriteToConsole('Init SDL_ttf... ');
-        SDLTry(TTF_Init() <> -1, 'TTF_Init', true);
-        WriteLnToConsole(msgOK);
-        end;
 
 {$IFDEF USE_VIDEO_RECORDING}
     if GameType = gmtRecord then
@@ -401,32 +406,35 @@
             LoadRecordFromFile(recordFileName);
         end;
 
-    ScriptOnGameInit;
-    s:= 'eproto ' + inttostr(cNetProtoVersion);
-    SendIPCRaw(@s[0], Length(s) + 1); // send proto version
+    if allOK then
+    begin
+        ScriptOnGameInit;
+        s:= 'eproto ' + inttostr(cNetProtoVersion);
+        SendIPCRaw(@s[0], Length(s) + 1); // send proto version
 
-    InitTeams();
-    AssignStores();
+        InitTeams();
+        AssignStores();
 
-    if GameType = gmtRecord then
-        SetSound(false);
+        if GameType = gmtRecord then
+            SetSound(false);
 
-    InitSound();
+        InitSound();
 
-    isDeveloperMode:= false;
-    TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
-    //ParseCommand('rotmask', true);
+        isDeveloperMode:= false;
+        TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
+        //ParseCommand('rotmask', true);
 
-{$IFDEF USE_VIDEO_RECORDING}
-    if GameType = gmtRecord then
-    begin
-        RecorderMainLoop();
-        freeEverything(true);
-        exit;
+    {$IFDEF USE_VIDEO_RECORDING}
+        if GameType = gmtRecord then
+        begin
+            RecorderMainLoop();
+            freeEverything(true);
+            exit;
+        end;
+    {$ENDIF}
+
+        MainLoop;
     end;
-{$ENDIF}
-
-    MainLoop;
     // clean up all the memory allocated
     freeEverything(true);
 end;
@@ -437,6 +445,7 @@
 // freeEverything - free above. Pay attention to the init/free order!
 procedure preInitEverything;
 begin
+    allOK:= true;
     Randomize();
 
     uVariables.preInitModule;
@@ -537,19 +546,23 @@
     initEverything(false);
 
     InitIPC;
-    IPCWaitPongEvent;
-    TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
+    if allOK then
+    begin
+        IPCWaitPongEvent;
+        TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
 
-    ScriptOnPreviewInit;
-{$IFDEF MOBILE}
-    GenPreview(Preview);
-{$ELSE}
-    GenPreviewAlpha(Preview);
-{$ENDIF}
-    WriteLnToConsole('Sending preview...');
-    SendIPCRaw(@Preview, sizeof(Preview));
-    SendIPCRaw(@MaxHedgehogs, sizeof(byte));
-    WriteLnToConsole('Preview sent, disconnect');
+        ScriptOnPreviewInit;
+    {$IFDEF MOBILE}
+        GenPreview(Preview);
+    {$ELSE}
+        GenPreviewAlpha(Preview);
+    {$ENDIF}
+        WriteLnToConsole('Sending preview...');
+        SendIPCRaw(@Preview, sizeof(Preview));
+        SendIPCRaw(@MaxHedgehogs, sizeof(byte));
+        WriteLnToConsole('Preview sent, disconnect');
+    end;
+
     freeEverything(false);
 end;
 
--- a/hedgewars/uDebug.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uDebug.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -24,7 +24,10 @@
 
 procedure OutError(Msg: shortstring; isFatalError: boolean);
 procedure TryDo(Assert: boolean; Msg: shortstring; isFatal: boolean); inline;
-procedure SDLTry(Assert: boolean; Msg: shortstring; isFatal: boolean);
+function SDLCheck(Assert: boolean; Msg: shortstring; isFatal: boolean): boolean;
+
+var
+    allOK: boolean;
 
 implementation
 uses SDLh, uConsole, uCommands, uConsts;
@@ -47,14 +50,17 @@
     OutError(Msg, isFatal)
 end;
 
-procedure SDLTry(Assert: boolean; Msg: shortstring; isFatal: boolean);
+function SDLCheck(Assert: boolean; Msg: shortstring; isFatal: boolean): boolean;
 var s: shortstring;
 begin
-if not Assert then
+    if not Assert then
     begin
-    s:= SDL_GetError();
-    OutError(Msg + ': ' + s, isFatal)
-    end
+        s:= SDL_GetError();
+        OutError(Msg + ': ' + s, false)
+    end;
+
+    allOK:= allOK and (Assert or (not isFatal));
+    SDLCheck:= (not Assert) and isFatal
 end;
 
 end.
--- a/hedgewars/uIO.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uIO.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -114,16 +114,16 @@
 var ipaddr: TIPAddress;
 begin
     WriteToConsole('Init SDL_Net... ');
-    SDLTry(SDLNet_Init = 0, 'SDLNet_Init', true);
+    SDLCheck(SDLNet_Init = 0, 'SDLNet_Init', true);
     fds:= SDLNet_AllocSocketSet(1);
-    SDLTry(fds <> nil, 'SDLNet_AllocSocketSet', true);
+    SDLCheck(fds <> nil, 'SDLNet_AllocSocketSet', true);
     WriteLnToConsole(msgOK);
     WriteToConsole('Establishing IPC connection to tcp 127.0.0.1:' + IntToStr(ipcPort) + ' ');
     {$HINTS OFF}
-    SDLTry(SDLNet_ResolveHost(ipaddr, PChar('127.0.0.1'), ipcPort) = 0, 'SDLNet_ResolveHost', true);
+    SDLCheck(SDLNet_ResolveHost(ipaddr, PChar('127.0.0.1'), ipcPort) = 0, 'SDLNet_ResolveHost', true);
     {$HINTS ON}
     IPCSock:= SDLNet_TCP_Open(ipaddr);
-    SDLTry(IPCSock <> nil, 'SDLNet_TCP_Open', true);
+    SDLCheck(IPCSock <> nil, 'SDLNet_TCP_Open', true);
     WriteLnToConsole(msgOK)
 end;
 
@@ -512,6 +512,7 @@
     RegisterVariable('fatal', @chFatalError, true );
 
     IPCSock:= nil;
+    fds:= nil;
 
     headcmd:= nil;
     lastcmd:= nil;
--- a/hedgewars/uLand.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uLand.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -263,7 +263,7 @@
 TryDo(Surface <> nil, 'Assert (LandSurface <> nil) failed', true);
 
 if SDL_MustLock(Surface) then
-    SDLTry(SDL_LockSurface(Surface) >= 0, 'SDL_LockSurface', true);
+    if SDLCheck(SDL_LockSurface(Surface) >= 0, 'SDL_LockSurface', true) then exit;
 
 p:= Surface^.pixels;
 for y:= 0 to LAND_HEIGHT - 1 do
@@ -451,8 +451,10 @@
     cpX:= (LAND_WIDTH - tmpsurf^.w) div 2;
     cpY:= LAND_HEIGHT - tmpsurf^.h;
     if SDL_MustLock(tmpsurf) then
-        SDLTry(SDL_LockSurface(tmpsurf) >= 0, 'SDL_LockSurface', true);
+        SDLCheck(SDL_LockSurface(tmpsurf) >= 0, 'SDL_LockSurface', true);
 
+    if allOK then
+    begin
         p:= tmpsurf^.pixels;
         for y:= 0 to Pred(tmpsurf^.h) do
             begin
@@ -461,15 +463,16 @@
             p:= PLongwordArray(@(p^[tmpsurf^.pitch div 4]));
             end;
 
-    if SDL_MustLock(tmpsurf) then
-        SDL_UnlockSurface(tmpsurf);
-    if not disableLandBack then
-        begin
-        // freed in freeModule() below
-        LandBackSurface:= LoadDataImage(ptCurrTheme, 'LandBackTex', ifIgnoreCaps or ifTransparent);
-        if (LandBackSurface <> nil) and GrayScale then
-            Surface2GrayScale(LandBackSurface)
-        end;
+        if SDL_MustLock(tmpsurf) then
+            SDL_UnlockSurface(tmpsurf);
+        if not disableLandBack then
+            begin
+            // freed in freeModule() below
+            LandBackSurface:= LoadDataImage(ptCurrTheme, 'LandBackTex', ifIgnoreCaps or ifTransparent);
+            if (LandBackSurface <> nil) and GrayScale then
+                Surface2GrayScale(LandBackSurface)
+            end;
+    end;
 end;
 if (tmpsurf <> nil) then
     SDL_FreeSurface(tmpsurf);
--- a/hedgewars/uLandGraphics.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uLandGraphics.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -716,7 +716,7 @@
 col:= Frame div numFramesFirstCol;
 
 if SDL_MustLock(Image) then
-    SDLTry(SDL_LockSurface(Image) >= 0, 'TryPlaceOnLand', true);
+    if SDLCheck(SDL_LockSurface(Image) >= 0, 'TryPlaceOnLand', true) then exit;
 
 bpp:= Image^.format^.BytesPerPixel;
 TryDo(bpp = 4, 'It should be 32 bpp sprite', true);
@@ -835,7 +835,7 @@
 col:= Frame div numFramesFirstCol;
 
 if SDL_MustLock(Image) then
-    SDLTry(SDL_LockSurface(Image) >= 0, 'EraseLand', true);
+    if SDLCheck(SDL_LockSurface(Image) >= 0, 'EraseLand', true) then exit;
 
 bpp:= Image^.format^.BytesPerPixel;
 TryDo(bpp = 4, 'It should be 32 bpp sprite', true);
@@ -918,7 +918,8 @@
 col:= Frame div numFramesFirstCol;
 
 if SDL_MustLock(Image) then
-    SDLTry(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true);
+    if SDLCheck(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true) then
+        exit;
 
 bpp:= Image^.format^.BytesPerPixel;
 TryDo(bpp = 4, 'It should be 32 bpp sprite', true);
@@ -930,7 +931,8 @@
 TryDo(finalSurface <> nil, 'GetPlaceCollisionTex: fail to create surface', true);
 
 if SDL_MustLock(finalSurface) then
-    SDLTry(SDL_LockSurface(finalSurface) >= 0, 'GetPlaceCollisionTex', true);
+    if SDLCheck(SDL_LockSurface(finalSurface) >= 0, 'GetPlaceCollisionTex', true) then
+        exit;
 
 p:= PLongWordArray(@(PLongWordArray(Image^.pixels)^[ (Image^.pitch div 4) * row * h + col * w ]));
 pt:= PLongWordArray(finalSurface^.pixels);
--- a/hedgewars/uLandObjects.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uLandObjects.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -109,7 +109,7 @@
 WriteToConsole('Generating collision info... ');
 
 if SDL_MustLock(Image) then
-    SDLTry(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true);
+    if SDLCheck(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true) then exit;
 
 bpp:= Image^.format^.BytesPerPixel;
 TryDo(bpp = 4, 'Land object should be 32bit', true);
@@ -160,7 +160,7 @@
 WriteToConsole('Generating collision info... ');
 
 if SDL_MustLock(Image) then
-    SDLTry(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true);
+    if SDLCheck(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true) then exit;
 
 bpp:= Image^.format^.BytesPerPixel;
 TryDo(bpp = 4, 'Land object should be 32bit', true);
--- a/hedgewars/uRenderUtils.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uRenderUtils.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -99,7 +99,8 @@
     tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, PChar(s), clr);
     finalRect.x:= X + cFontBorder + 2;
     finalRect.y:= Y + cFontBorder;
-    SDLTry(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true);
+    if SDLCheck(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true) then
+        exit;
     SDL_UpperBlit(tmpsurf, @textRect, Surface, @finalRect);
     SDL_FreeSurface(tmpsurf);
     finalRect.x:= X;
@@ -522,9 +523,11 @@
         rect.x:= edgeHeight + 1 + ((i - w) div 2);
         // trying to more evenly position the text, vertically
         rect.y:= edgeHeight + ((j-(numLines*h)) div 2) + line * h;
-        SDLTry(tmpsurf <> nil, 'TTF_Init', true);
-        SDL_UpperBlit(tmpsurf, nil, finalSurface, @rect);
-        SDL_FreeSurface(tmpsurf);
+        if not SDLCheck(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true) then
+        begin
+            SDL_UpperBlit(tmpsurf, nil, finalSurface, @rect);
+            SDL_FreeSurface(tmpsurf);
+        end;
         inc(line);
         end;
 
--- a/hedgewars/uSound.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uSound.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -322,7 +322,7 @@
     end;
 
     WriteToConsole('Init SDL_mixer... ');
-    SDLTry(Mix_Init(MIX_INIT_OGG) <> 0, 'Mix_Init', true);
+    if SDLCheck(Mix_Init(MIX_INIT_OGG) <> 0, 'Mix_Init', true) then exit;
     WriteLnToConsole(msgOK);
 
     Mix_AllocateChannels(Succ(chanTPU));
@@ -425,7 +425,7 @@
             s:= cPathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
             WriteToConsole(msgLoading + s + ' ');
             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(rwopsOpenRead(s), 1);
-            SDLTry(defVoicepack^.chunks[snd] <> nil, 'Mix_LoadWAV_RW', true);
+            if not SDLCheck(defVoicepack^.chunks[snd] <> nil, 'Mix_LoadWAV_RW', true) then exit;
             WriteLnToConsole(msgOK);
             end;
         lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], 0, -1)
@@ -524,7 +524,7 @@
             s:= cPathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
             WriteToConsole(msgLoading + s + ' ');
             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(rwopsOpenRead(s), 1);
-            SDLTry(defVoicepack^.chunks[snd] <> nil, 'Mix_LoadWAV_RW', true);
+            if SDLCheck(defVoicepack^.chunks[snd] <> nil, 'Mix_LoadWAV_RW', true) then exit;
             WriteLnToConsole(msgOK);
             end;
         if fadems > 0 then
@@ -575,10 +575,10 @@
     WriteToConsole(msgLoading + s + ' ');
 
     Mus:= Mix_LoadMUS_RW(rwopsOpenRead(s));
-    SDLTry(Mus <> nil, 'Mix_LoadMUS_RW', false);
+    SDLCheck(Mus <> nil, 'Mix_LoadMUS_RW', false);
     WriteLnToConsole(msgOK);
 
-    SDLTry(Mix_FadeInMusic(Mus, -1, 3000) <> -1, 'Mix_FadeInMusic', false)
+    SDLCheck(Mix_FadeInMusic(Mus, -1, 3000) <> -1, 'Mix_FadeInMusic', false)
 end;
 
 procedure SetVolume(vol: LongInt);
--- a/hedgewars/uStore.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uStore.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -112,15 +112,19 @@
 clr.g:= (Color shr 8) and $FF;
 clr.b:= Color and $FF;
 tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, s, clr);
-SDLTry(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true);
+if tmpsurf = nil then exit;
 tmpsurf:= doSurfaceConversion(tmpsurf);
-SDLTry(tmpsurf <> nil, 'TTF_RenderUTF8_Blended, doSurfaceConversion', true);
-SDL_UpperBlit(tmpsurf, nil, Surface, @finalRect);
-SDL_FreeSurface(tmpsurf);
-finalRect.x:= X;
-finalRect.y:= Y;
-finalRect.w:= w + cFontBorder * 2 + 4;
-finalRect.h:= h + cFontBorder * 2;
+
+if tmpsurf <> nil then
+begin
+    SDL_UpperBlit(tmpsurf, nil, Surface, @finalRect);
+    SDL_FreeSurface(tmpsurf);
+    finalRect.x:= X;
+    finalRect.y:= Y;
+    finalRect.w:= w + cFontBorder * 2 + 4;
+    finalRect.h:= h + cFontBorder * 2;
+end;
+
 WriteInRect:= finalRect
 end;
 
@@ -356,7 +360,7 @@
             s:= cPathz[ptFonts] + '/' + Name;
             WriteToConsole(msgLoading + s + ' (' + inttostr(Height) + 'pt)... ');
             Handle:= TTF_OpenFontRW(rwopsOpenRead(s), true, Height);
-            SDLTry(Handle <> nil, 'TTF_OpenFontRW', true);
+            if SDLCheck(Handle <> nil, 'TTF_OpenFontRW', true) then exit;
             TTF_SetFontStyle(Handle, style);
             WriteLnToConsole(msgOK)
             end;
@@ -605,7 +609,7 @@
             // anounce that loading failed
             OutError(msgFailed, false);
 
-            SDLTry(false, 'LoadImage', (imageFlags and ifCritical) <> 0);
+            if SDLCheck(false, 'LoadImage', (imageFlags and ifCritical) <> 0) then exit;
             // rwops was already freed by IMG_Load_RW
             rwops:= nil;
             end else
@@ -734,7 +738,6 @@
 end;
 
 procedure SetupOpenGL;
-var buf: array[byte] of char;
 begin
     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_GetCurrentVideoDriver()) + ')');
 
@@ -744,7 +747,7 @@
     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
     if SDLGLcontext = nil then
         SDLGLcontext:= SDL_GL_CreateContext(SDLwindow);
-    SDLTry(SDLGLcontext <> nil, 'SDLGLcontext', true);
+    if SDLCheck(SDLGLcontext <> nil, 'SDLGLcontext', true) then exit;
     SDL_GL_SetSwapInterval(1);
 
     RendererSetup();
@@ -988,7 +991,7 @@
                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
                                  cScreenWidth, cScreenHeight,
                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
-    SDLTry(SDLwindow <> nil, 'SDL_CreateWindow', true);
+    if SDLCheck(SDLwindow <> nil, 'SDL_CreateWindow', true) then exit;
     SetupOpenGL();
 end;
 {$ENDIF} // USE_VIDEO_RECORDING
@@ -1024,7 +1027,7 @@
         begin
         // set window title
         WriteToConsole('Init SDL_image... ');
-        SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, 'IMG_Init', true);
+        if SDLCheck(IMG_Init(IMG_INIT_PNG) <> 0, 'IMG_Init', true) then exit;
         WriteLnToConsole(msgOK);
         end
     else
@@ -1082,7 +1085,7 @@
 
     if SDLwindow = nil then
         SDLwindow:= SDL_CreateWindow(PChar('Hedgewars'), x, y, cScreenWidth, cScreenHeight, flags);
-    SDLTry(SDLwindow <> nil, 'SDL_CreateWindow', true);
+    if SDLCheck(SDLwindow <> nil, 'SDL_CreateWindow', true) then exit;
 
     // load engine ico
     {$IFNDEF DARWIN}
--- a/hedgewars/uTextures.pas	Wed Jan 13 16:19:50 2016 +0100
+++ b/hedgewars/uTextures.pas	Sun Jan 10 00:45:13 2016 +0300
@@ -239,7 +239,8 @@
 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
 
 if SDL_MustLock(surf) then
-    SDLTry(SDL_LockSurface(surf) >= 0, 'Lock surface', true);
+    if SDLCheck(SDL_LockSurface(surf) >= 0, 'Lock surface', true) then
+        exit(nil);
 
 fromP4:= Surf^.pixels;