DEADSOFTWARE

SDL: separated GL loading code
[d2df-sdl.git] / src / game / sdl2 / g_system.pas
index 89cebaf02b5e84d292118595e878a4a17e680f52..31c5b1cde5c4147282cd52211f24446cd9a47e12 100644 (file)
@@ -74,6 +74,28 @@ implementation
 
   (* --------- Graphics --------- *)
 
+  function LoadGL: Boolean;
+  begin
+    result := true;
+    {$IFDEF NOGL_INIT}
+    nogl_Init;
+    if glRenderToFBO and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
+    {$ELSE}
+    if glRenderToFBO and (not Load_GL_ARB_framebuffer_object) then
+    {$ENDIF}
+    begin
+      e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
+      glRenderToFBO := false;
+    end;
+  end;
+
+  procedure FreeGL;
+  begin
+    {$IFDEF NOGL_INIT}
+    nogl_Quit();
+    {$ENDIF}
+  end;
+
   procedure UpdateSize (w, h: Integer);
   begin
     gWinSizeX := w;
@@ -156,18 +178,12 @@ implementation
         context := SDL_GL_CreateContext(window);
         if context <> nil then
         begin
-          {$IFDEF NOGL_INIT}
-            nogl_Init;
-            glRenderToFBO := False; // TODO: check for GL_OES_framebuffer_object
-          {$ELSE}
-            if glRenderToFBO then
-              if not Load_GL_ARB_framebuffer_object() then
-                if not Load_GL_EXT_framebuffer_object() then
-                begin
-                  e_LogWriteln('SDL: no framebuffer support detected');
-                  glRenderToFBO := False
-                end;
-          {$ENDIF}
+          if not LoadGL then
+          begin
+            e_LogWriteln('GL: unable to load OpenGL functions', TMsgType.Fatal);
+            SDL_GL_DeleteContext(context); context := nil;
+            exit;
+          end;
           if (fullscreen = false) and (maximized = false) and (wc = false) then
           begin
             SDL_GetWindowPosition(window, @x, @y);
@@ -193,8 +209,12 @@ implementation
     end
     else
     begin
+      if fullScreen then flags := SDL_WINDOW_FULLSCREEN else flags := 0;
+      SDL_SetWindowFullscreen(window, flags);
       SDL_SetWindowSize(window, w, h);
-      if wc then
+      if maximized then SDL_MaximizeWindow(window);
+      // always reset to center when changing fullscreen->windowed for safety purposes
+      if wc or (gFullscreen and not fullscreen) or (gWinMaximized and not maximized) then
       begin
         x := SDL_WINDOWPOS_CENTERED;
         y := SDL_WINDOWPOS_CENTERED
@@ -210,10 +230,6 @@ implementation
         SDL_GetWindowPosition(window, @x, @y);
         wx := x; wy := y
       end;
-      if maximized then
-        SDL_MaximizeWindow(window);
-      if fullScreen then flags := SDL_WINDOW_FULLSCREEN else flags := 0;
-      SDL_SetWindowFullscreen(window, flags);
       gFullScreen := fullscreen;
       gWinMaximized := maximized;
       gRC_FullScreen := fullscreen;
@@ -566,9 +582,7 @@ implementation
     e_WriteLog('Releasing SDL2', TMsgType.Notify);
     if context <> nil then
     begin
-      {$IFDEF NOGL_INIT}
-        nogl_Quit;
-      {$ENDIF}
+      FreeGL;
       SDL_GL_DeleteContext(context);
       context := nil;
     end;