hedgewars/uStore.pas
changeset 9317 a04c30940d2d
parent 9295 f8819c3dde54
child 9521 8054d9d775fd
child 9530 0dc3b8f94b0b
--- a/hedgewars/uStore.pas	Thu Jul 04 11:58:40 2013 +0200
+++ b/hedgewars/uStore.pas	Sat Jul 06 19:29:18 2013 +0200
@@ -52,7 +52,9 @@
 procedure InitOffscreenOpenGL;
 {$ENDIF}
 
+{$IFDEF SDL2}
 procedure WarpMouse(x, y: Word); inline;
+{$ENDIF}
 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
 procedure SetSkyColor(r, g, b: real);
 
@@ -61,12 +63,12 @@
     , uPhysFSLayer
     , uDebug
     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
-    {$IF NOT DEFINED(SDL13) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
+    {$IF NOT DEFINED(SDL2) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
 
 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
 
 var MaxTextureSize: LongInt;
-{$IFDEF SDL13}
+{$IFDEF SDL2}
     SDLwindow: PSDL_Window;
     SDLGLcontext: PSDL_GLContext;
 {$ELSE}
@@ -76,7 +78,7 @@
     numsquares : LongInt;
     ProgrTex: PTexture;
 
-const 
+const
     cHHFileName = 'Hedgehog';
     cCHFileName = 'Crosshair';
 
@@ -202,7 +204,7 @@
                     foundBot:= true;
                     // initially was going to do the highest botlevel of the team, but for now, just apply if entire team has same bot level
                     if maxLevel = -1 then maxLevel:= BotLevel
-                    else if (maxLevel > 0) and (maxLevel <> BotLevel) then maxLevel:= 0; 
+                    else if (maxLevel > 0) and (maxLevel <> BotLevel) then maxLevel:= 0;
                     //if (maxLevel > 0) and (BotLevel < maxLevel) then maxLevel:= BotLevel
                     end
                 else if Gear <> nil then  maxLevel:= 0;
@@ -210,7 +212,7 @@
         if foundBot then
             begin
             // disabled the plain flag - I think it looks ok even w/ full bars obscuring CPU
-            //if (maxLevel > 0) and (maxLevel < 3) then Flag:= 'cpu_plain' else 
+            //if (maxLevel > 0) and (maxLevel < 3) then Flag:= 'cpu_plain' else
             Flag:= 'cpu'
             end
         else if (Flag = 'cpu') or (Flag = 'cpu_plain') then
@@ -220,10 +222,10 @@
         TryDo(flagsurf <> nil, 'Failed to load flag "' + Flag + '" as well as the default flag', true);
 
         case maxLevel of
-            1: copyToXY(SpritesData[sprBotlevels].Surface, flagsurf, 0, 0); 
-            2: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 5, 2, 17, 13, 5, 2); 
-            3: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 9, 5, 13, 10, 9, 5); 
-            4: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 13, 9, 9, 6, 13, 9); 
+            1: copyToXY(SpritesData[sprBotlevels].Surface, flagsurf, 0, 0);
+            2: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 5, 2, 17, 13, 5, 2);
+            3: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 9, 5, 13, 10, 9, 5);
+            4: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 13, 9, 9, 6, 13, 9);
             5: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 17, 11, 5, 4, 17, 11)
             end;
 
@@ -385,7 +387,7 @@
                 if not reload then
                     begin
 {$IFDEF USE_CONTEXT_RESTORE}
-                    Surface:= tmpsurf 
+                    Surface:= tmpsurf
 {$ELSE}
                     if saveSurf then
                         Surface:= tmpsurf
@@ -606,7 +608,7 @@
 begin
     // check for file in user dir (never critical)
     tmpsurf:= LoadImage(cPathz[path] + '/' + filename, imageFlags);
-    
+
     LoadDataImage:= tmpsurf;
 end;
 
@@ -688,35 +690,38 @@
     SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
 {$ELSE}
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-{$IFNDEF SDL13} // vsync is default in 1.3
+{$ENDIF}
+{$IFNDEF SDL2} // vsync is default in SDL2
     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0));
 {$ENDIF}
-{$ENDIF}
-    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); // no depth buffer
     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
-    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required
-    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only
-    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering
+    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);         // no depth buffer
+    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);         // no alpha channel
+    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);       // buffer should be 16
+    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // prefer hw rendering
 end;
 
 procedure SetupOpenGL;
-//var vendor: shortstring = '';
-var buf: array[byte] of char;
-{$IFDEF USE_VIDEO_RECORDING}
-    AuxBufNum: LongInt;
-{$ENDIF}
+var name: array[byte] of char;
+    AuxBufNum: LongInt = 0;
     tmpstr: AnsiString;
     tmpint: LongInt;
     tmpn: LongInt;
 begin
-    buf[0]:= char(0); // avoid compiler hint
-    AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
+{$IFDEF SDL2}
+    name:= SDL_GetCurrentVideoDriver();
+{$ELSE}
+    name:= SDL_VideoDriverName(name, sizeof(name));
+{$ENDIF}
 
-{$IFDEF SDL13}
-    // this function creates an opengles1.1 context by default on mobile devices
-    // unless you un-comment this two attributes
+    AuxBufNum:= AuxBufNum;
+    AddFileLog('Setting up OpenGL (using driver: ' + shortstring(name) + ')');
+
+{$IFDEF MOBILE}
+    // TODO: this function creates an opengles1.1 context
+    // un-comment below and add proper logic to support opengles2.0
     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
     if SDLGLcontext = nil then
@@ -725,7 +730,7 @@
     SDL_GL_SetSwapInterval(1);
 {$ENDIF}
 
-    // get the max (horizontal and vertical) size for textures that the gpu can support
+    // get the max (h and v) size for textures that the gpu can support
     glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
     if MaxTextureSize <= 0 then
         begin
@@ -734,29 +739,11 @@
         end
     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
         begin
-        cReducedQuality := cReducedQuality or rqNoBackground;  
+        cReducedQuality := cReducedQuality or rqNoBackground;
         AddFileLog('Texture size too small for backgrounds, disabling.');
         end;
 
-(*  // find out which gpu we are using (for extension compatibility maybe?)
-{$IFDEF IPHONEOS}
-    vendor:= vendor; // avoid hint
-    cGPUVendor:= gvApple;
-{$ELSE}
-    vendor:= LowerCase(shortstring(pchar(glGetString(GL_VENDOR))));
-    if StrPos(Str2PChar(vendor), Str2PChar('nvidia')) <> nil then
-        cGPUVendor:= gvNVIDIA
-    else if StrPos(Str2PChar(vendor), Str2PChar('intel')) <> nil then
-        cGPUVendor:= gvATI
-    else if StrPos(Str2PChar(vendor), Str2PChar('ati')) <> nil then
-        cGPUVendor:= gvIntel
-    else
-        AddFileLog('OpenGL Warning - unknown hardware vendor; please report');
-{$ENDIF}
-//SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two');
-*)
-
-    // everyone love debugging
+    // everyone loves debugging
     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
@@ -857,7 +844,7 @@
         exit;
 
     if f = cDefaultZoomLevel then
-        glPopMatrix         // "return" to default scaling
+        glPopMatrix         // return to default scaling
     else                    // other scaling
         begin
         glPushMatrix;       // save default scaling
@@ -888,7 +875,7 @@
         with mobileRecord do
             if GameLoading <> nil then
                 GameLoading();
-        
+
         end;
 
     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
@@ -1099,11 +1086,11 @@
 end;
 
 {$IFDEF USE_VIDEO_RECORDING}
-{$IFDEF SDL13}
+{$IFDEF SDL2}
 procedure InitOffscreenOpenGL;
 begin
     // create hidden window
-    SDLwindow:= SDL_CreateWindow('hedgewars (you don''t see this)',
+    SDLwindow:= SDL_CreateWindow('hedgewars video rendering (SDL2 hidden window)',
                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
                                  cScreenWidth, cScreenHeight,
                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
@@ -1119,19 +1106,21 @@
     PrgName:= 'hwengine';
     glutInit(@ArgCount, @PrgName);
     glutInitWindowSize(cScreenWidth, cScreenHeight);
-    glutCreateWindow('hedgewars (you don''t see this)'); // we don't need a window, but if this function is not called then OpenGL will not be initialized
+    // we do not need a window, but without this call OpenGL will not initialize
+    glutCreateWindow('hedgewars video rendering (glut hidden window)');
     glutHideWindow();
-    glutDisplayFunc(@SwapBuffers); // we don't need a callback, but it's required for GLUT3
+    // we do not need to set this callback, but it is required for GLUT3 compat
+    glutDisplayFunc(@SwapBuffers);
     SetupOpenGL();
 end;
-{$ENDIF} // SDL13
+{$ENDIF} // SDL2
 {$ENDIF} // USE_VIDEO_RECORDING
 
 procedure chFullScr(var s: shortstring);
 var flags: Longword = 0;
     reinit: boolean = false;
     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
-    {$IFDEF SDL13}x, y: LongInt;{$ENDIF}
+    {$IFDEF SDL2}x, y: LongInt;{$ENDIF}
 begin
     if cOnlyStats then
         begin
@@ -1141,7 +1130,7 @@
     if Length(s) = 0 then
          cFullScreen:= (not cFullScreen)
     else cFullScreen:= s = '1';
-    
+
     if cFullScreen then
         begin
         cScreenWidth:= cFullscreenWidth;
@@ -1154,14 +1143,16 @@
         end;
 
     AddFileLog('Preparing to change video parameters...');
-{$IFDEF SDL13}
+{$IFDEF SDL2}
     if SDLwindow = nil then
 {$ELSE}
     if SDLPrimSurface = nil then
 {$ENDIF}
         begin
         // set window title
-        {$IFNDEF SDL13}SDL_WM_SetCaption(_P'Hedgewars', nil);{$ENDIF}
+    {$IFNDEF SDL2}
+        SDL_WM_SetCaption(_P'Hedgewars', nil);
+    {$ENDIF}
         WriteToConsole('Init SDL_image... ');
         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
         WriteLnToConsole(msgOK);
@@ -1190,7 +1181,7 @@
         exit;
 {$ELSE}
         SetScale(cDefaultZoomLevel);
-     {$IFDEF USE_CONTEXT_RESTORE}
+    {$IFDEF USE_CONTEXT_RESTORE}
         reinit:= true;
         StoreRelease(true);
         ResetLand;
@@ -1198,7 +1189,7 @@
         //uTextures.freeModule; //DEBUG ONLY
     {$ENDIF}
         AddFileLog('Freeing old primary surface...');
-    {$IFNDEF SDL13}        
+    {$IFNDEF SDL2}
         SDL_FreeSurface(SDLPrimSurface);
         SDLPrimSurface:= nil;
     {$ENDIF}
@@ -1207,17 +1198,20 @@
 
     // these attributes must be set up before creating the sdl window
 {$IFNDEF WIN32}
-(* On a large number of testers machines, SDL default to software rendering when opengl attributes were set.
-   These attributes were "set" after CreateWindow in .15, which probably did nothing.
-   IMO we should rely on the gl_config defaults from SDL, and use SDL_GL_GetAttribute to possibly post warnings if any
-   bad values are set.  *)
+(* On a large number of testers machines, SDL default to software rendering
+   when opengl attributes were set. These attributes were "set" after
+   CreateWindow in .15, which probably did nothing.
+   IMO we should rely on the gl_config defaults from SDL, and use
+   SDL_GL_GetAttribute to possibly post warnings if any bad values are set.
+ *)
     SetupOpenGLAttributes();
 {$ENDIF}
-{$IFDEF SDL13}
+{$IFDEF SDL2}
     // these values in x and y make the window appear in the center
     x:= SDL_WINDOWPOS_CENTERED_MASK;
     y:= SDL_WINDOWPOS_CENTERED_MASK;
-    // SDL_WINDOW_RESIZABLE makes the window respond to rotation events on mobile devices
+    // SDL_WINDOW_RESIZABLE makes the window resizable and
+    //  respond to rotation events on mobile devices
     flags:= SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN or SDL_WINDOW_RESIZABLE;
 
     {$IFDEF MOBILE}
@@ -1227,20 +1221,16 @@
     flags:= flags or SDL_WINDOW_BORDERLESS;
     {$ENDIF}
 
+    if cFullScreen then
+        flags:= flags or SDL_WINDOW_FULLSCREEN;
+
     if SDLwindow = nil then
-        if cFullScreen then
-            SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags or SDL_WINDOW_FULLSCREEN)
-        else
-            begin
-            SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
-            end;
+        SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
     SDLTry(SDLwindow <> nil, true);
 {$ELSE}
     flags:= SDL_OPENGL or SDL_RESIZABLE;
     if cFullScreen then
-        begin
         flags:= flags or SDL_FULLSCREEN;
-        end;
     if not cOnlyStats then
         begin
     {$IFDEF WIN32}
@@ -1249,7 +1239,9 @@
     {$ENDIF}
         SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, 0, flags);
         SDLTry(SDLPrimSurface <> nil, true);
-    {$IFDEF WIN32}SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));{$ENDIF}
+    {$IFDEF WIN32}
+        SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));
+    {$ENDIF}
         end;
 {$ENDIF}
 
@@ -1260,7 +1252,7 @@
         glClear(GL_COLOR_BUFFER_BIT);
         if SuddenDeathDmg then
             SetSkyColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255)
-        else if ((cReducedQuality and rqNoBackground) = 0) then 
+        else if ((cReducedQuality and rqNoBackground) = 0) then
             SetSkyColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255)
         else
             SetSkyColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255);
@@ -1274,20 +1266,22 @@
         end;
 end;
 
+{$IFDEF SDL2}
+// for sdl1.2 we directly call SDL_WarpMouse()
+// for sdl2 we provide a SDL_WarpMouse() which just calls this function
+// this has the advantage of reducing 'uses' and 'ifdef' statements
+// (SDLwindow is a private member of this module)
 procedure WarpMouse(x, y: Word); inline;
 begin
-{$IFDEF SDL13}
     SDL_WarpMouseInWindow(SDLwindow, x, y);
-{$ELSE}
-    x:= x; y:= y; // avoid hints
+end;
 {$ENDIF}
-end;
 
 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
 begin
     if GameType = gmtRecord then
         exit;
-{$IFDEF SDL13}
+{$IFDEF SDL2}
     SDL_GL_SwapWindow(SDLwindow);
 {$ELSE}
     SDL_GL_SwapBuffers();
@@ -1318,7 +1312,7 @@
     // init all count texture pointers
     for i:= Low(CountTexz) to High(CountTexz) do
         CountTexz[i] := nil;
-{$IFDEF SDL13}
+{$IFDEF SDL2}
     SDLwindow:= nil;
     SDLGLcontext:= nil;
 {$ELSE}
@@ -1330,7 +1324,7 @@
 begin
     StoreRelease(false);
     TTF_Quit();
-{$IFDEF SDL13}
+{$IFDEF SDL2}
     SDL_GL_DeleteContext(SDLGLcontext);
     SDL_DestroyWindow(SDLwindow);
 {$ENDIF}