DEADSOFTWARE

render: system: move gl related code from system driver
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Sat, 19 Jun 2021 11:13:42 +0000 (14:13 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 9 Jun 2023 07:49:25 +0000 (10:49 +0300)
src/game/opengl/r_render.pas
src/game/sdl/g_system.pas
src/game/sdl2/g_system.pas

index 668fb901f4e850dcd843a09ce3342daaecdccda2..d547054e8e90103fccecc511949cc62b8e538caf 100644 (file)
@@ -23,13 +23,54 @@ interface
 
 implementation
 
-  uses SysUtils, Classes, e_log, g_system, g_game, g_options, r_window, r_graphics, r_console, r_playermodel;
+  uses
+    {$INCLUDE ../../nogl/noGLuses.inc}
+    SysUtils, Classes,
+    e_log, g_system,
+    g_game, g_options, r_window, r_graphics, r_console, r_playermodel
+  ;
+
+  var
+    LoadedGL: Boolean = false;
+
+  procedure LoadGL;
+  begin
+    if LoadedGL = false then
+    begin
+      {$IFDEF NOGL_INIT}
+        nogl_Init;
+        if glRenderToFBO and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
+        begin
+          e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
+          glRenderToFBO := false
+        end;
+      {$ELSE}
+        if glRenderToFBO and (not Load_GL_ARB_framebuffer_object) then
+        begin
+          e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
+          glRenderToFBO := false
+        end;
+      {$ENDIF}
+      LoadedGL := true
+    end
+  end;
+
+  procedure FreeGL;
+  begin
+    if LoadedGL = true then
+    begin
+      {$IFDEF NOGL_INIT}
+        nogl_Quit;
+      {$ENDIF}
+      LoadedGL := false
+    end
+  end;
 
   procedure r_Render_Initialize;
   begin
     if sys_SetDisplayMode(gRC_Width, gRC_Height, gBPP, gRC_FullScreen, gRC_Maximized) = False then
       raise Exception.Create('Failed to set videomode on startup.');
-    
+    LoadGL;
     r_Window_Initialize;
     r_Console_Init;
     r_PlayerModel_Initialize;
@@ -37,12 +78,14 @@ implementation
 
   procedure r_Render_Finalize;
   begin
+    FreeGL;
     r_PlayerModel_Finalize;
     e_ReleaseEngine
   end;
 
   procedure r_Render_Resize (w, h: Integer);
   begin
+    LoadGL;
     gWinSizeX := w;
     gWinSizeY := h;
     gRC_Width := w;
index bb8c66ae3e28a1a1ffa8a68d642da52ac0ad4f6c..c7eab2cd9750d273bcb424ce2bcfff4cf0d07d20 100644 (file)
@@ -51,8 +51,7 @@ implementation
       {$ENDIF}
     {$ENDIF}
     SysUtils, SDL, Math,
-    {$INCLUDE ../nogl/noGLuses.inc}
-    e_log, r_graphics, e_input, e_sound,
+    e_log, e_input, e_sound,
     g_options, g_console, g_game, g_menu, g_gui, g_basic;
 
   const
@@ -81,38 +80,6 @@ implementation
 
   (* --------- Graphics --------- *)
 
-  function LoadGL: Boolean;
-    var ltmp: Integer;
-  begin
-    result := true;
-    {$IFDEF NOGL_INIT}
-      nogl_Init;
-      if glRenderToFBO and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
-      begin
-        e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
-        glRenderToFBO := false;
-      end;
-    {$ELSE}
-      if glRenderToFBO and (not Load_GL_ARB_framebuffer_object) then
-      begin
-        e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
-        glRenderToFBO := false;
-      end;
-    {$ENDIF}
-    if SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, ltmp) = 0 then
-    begin
-      e_LogWritefln('stencil buffer size: %s', [ltmp]);
-      gwin_has_stencil := (ltmp > 0);
-    end;
-  end;
-
-  procedure FreeGL;
-  begin
-    {$IFDEF NOGL_INIT}
-    nogl_Quit();
-    {$ENDIF}
-  end;
-
   function GetDriver (): AnsiString;
     var buf: array [0..31] of AnsiChar;
   begin
@@ -150,11 +117,6 @@ implementation
       screen := SDL_SetVideoMode(w, h, bpp, flags);
       if screen <> nil then
       begin
-        if not LoadGL then
-        begin
-          e_LogWriteln('GL: unable to load OpenGL functions', TMsgType.Fatal);
-          exit;
-        end;
         title := GetTitle();
         SDL_WM_SetCaption(PChar(title), nil);
         gFullScreen := fullscreen;
@@ -689,10 +651,7 @@ implementation
     for i := 0 to e_MaxJoys - 1 do
       RemoveJoystick(i);
     if screen <> nil then
-    begin
-      FreeGL;
-      SDL_FreeSurface(screen)
-    end;
+      SDL_FreeSurface(screen);
     SDL_Quit
   end;
 
index 5e91cc0f285e0dfbeb08bea4a3e2e4227bbc6f18..44fa1c8b972c2a49767bf6ebbc4e35b7f0d4f01c 100644 (file)
@@ -45,8 +45,7 @@ implementation
 
   uses
     SysUtils, SDL2, Math, ctypes,
-    e_log, r_graphics, e_input, e_sound,
-    {$INCLUDE ../nogl/noGLuses.inc}
+    e_log, e_input, e_sound,
     {$IFDEF ENABLE_HOLMES}
       g_holmes, sdlcarcass, fui_ctls,
     {$ENDIF}
@@ -78,38 +77,6 @@ implementation
 
   (* --------- Graphics --------- *)
 
-  function LoadGL: Boolean;
-    var ltmp: Integer;
-  begin
-    result := true;
-    {$IFDEF NOGL_INIT}
-      nogl_Init;
-      if glRenderToFBO and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
-      begin
-        e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
-        glRenderToFBO := false;
-      end;
-    {$ELSE}
-      if glRenderToFBO and (not Load_GL_ARB_framebuffer_object) then
-      begin
-        e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
-        glRenderToFBO := false;
-      end;
-    {$ENDIF}
-    if SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp) = 0 then
-    begin
-      e_LogWritefln('stencil buffer size: %s', [ltmp]);
-      gwin_has_stencil := (ltmp > 0);
-    end;
-  end;
-
-  procedure FreeGL;
-  begin
-    {$IFDEF NOGL_INIT}
-    nogl_Quit();
-    {$ENDIF}
-  end;
-
   function GetTitle (): AnsiString;
     var info: AnsiString;
   begin
@@ -161,12 +128,6 @@ implementation
         context := SDL_GL_CreateContext(window);
         if context <> nil then
         begin
-          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);
@@ -570,7 +531,6 @@ implementation
     e_WriteLog('Releasing SDL2', TMsgType.Notify);
     if context <> nil then
     begin
-      FreeGL;
       SDL_GL_DeleteContext(context);
       context := nil;
     end;