hedgewars/uStore.pas
branchhedgeroid
changeset 5725 e27100a0e2d0
parent 5667 24f4d7a37038
parent 5714 efd8b227cc71
child 5932 5164d17b6374
--- a/hedgewars/uStore.pas	Thu Sep 01 14:55:31 2011 +0200
+++ b/hedgewars/uStore.pas	Thu Sep 01 17:03:19 2011 +0200
@@ -44,10 +44,10 @@
 implementation
 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands, uDebug;
 
-type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
+//type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
 
 var MaxTextureSize: LongInt;
-    cGPUVendor: TGPUVendor;
+//    cGPUVendor: TGPUVendor;
 
 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
 var w, h: LongInt;
@@ -259,18 +259,20 @@
     tmpsurf: PSDL_Surface;
     i: LongInt;
 begin
+AddFileLog('StoreLoad()');
 
-for fi:= Low(THWFont) to High(THWFont) do
-    with Fontz[fi] do
-        begin
-        s:= UserPathz[ptFonts] + '/' + Name;
-        if not FileExists(s) then s:= Pathz[ptFonts] + '/' + Name;
-        WriteToConsole(msgLoading + s + ' (' + inttostr(Height) + 'pt)... ');
-        Handle:= TTF_OpenFont(Str2PChar(s), Height);
-        SDLTry(Handle <> nil, true);
-        TTF_SetFontStyle(Handle, style);
-        WriteLnToConsole(msgOK)
-        end;
+if not reload then
+    for fi:= Low(THWFont) to High(THWFont) do
+        with Fontz[fi] do
+            begin
+            s:= UserPathz[ptFonts] + '/' + Name;
+            if not FileExists(s) then s:= Pathz[ptFonts] + '/' + Name;
+            WriteToConsole(msgLoading + s + ' (' + inttostr(Height) + 'pt)... ');
+            Handle:= TTF_OpenFont(Str2PChar(s), Height);
+            SDLTry(Handle <> nil, true);
+            TTF_SetFontStyle(Handle, style);
+            WriteLnToConsole(msgOK)
+            end;
 
 WriteNames(fnt16);
 MakeCrossHairs;
@@ -345,10 +347,10 @@
 // This should maybe be flagged. It wastes quite a bit of memory.
                 if not reload then
                     begin
-{$IFNDEF DARWIN & WIN32}
+{$IF DEFINED(DARWIN) OR DEFINED(WIN32)}
+                    Surface:= tmpsurf 
+{$ELSE}
                     if saveSurf then Surface:= tmpsurf else SDL_FreeSurface(tmpsurf)
-{$ELSE}
-                    Surface:= tmpsurf 
 {$ENDIF}
                     end
                 end
@@ -563,11 +565,28 @@
 {$ENDIF}
 end;
 
+procedure SetupOpenGLAttributes;
+begin
+{$IFDEF IPHONEOS}
+    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
+    SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
+{$ELSE}
+    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+{$IFNDEF SDL13} // vsync is default in 1.3
+    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
+end;
+
 procedure SetupOpenGL;
-var vendor: shortstring = '';
-{$IFDEF DARWIN}
-const one : LongInt = 1;
-{$ENDIF}
+//var vendor: shortstring = '';
 begin
 {$IFDEF SDL13}
     // this function creates an opengles1.1 context by default on mobile devices
@@ -577,39 +596,8 @@
     SDL_GL_SetSwapInterval(1);
 {$ENDIF}
 
-{$IFDEF IPHONEOS}
-    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
-    SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
-    vendor:= vendor; // avoid hint
-    one:= one; // avoid hint
-{$ELSE}
-    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-    vendor:= LowerCase(shortstring(pchar(glGetString(GL_VENDOR))));
-{$IFNDEF SDL13}
-// this attribute is default in 1.3 and must be enabled in MacOSX
-    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0));
-
-{$IFDEF DARWIN}
-// fixes vsync in Snow Leopard
-    CGLSetParameter(CGLGetCurrentContext(), 222, @one);
-{$ENDIF}
-{$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
-
+    // get the max (horizontal and vertical) size for textures that the gpu can support
     glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
-
-    AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
-    AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
-    AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
-    AddFileLog('  \----- GL_MAX_TEXTURE_SIZE: ' + inttostr(MaxTextureSize));
-
     if MaxTextureSize <= 0 then
         begin
         MaxTextureSize:= 1024;
@@ -621,17 +609,30 @@
         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;
+        cGPUVendor:= gvIntel
+    else
+        AddFileLog('OpenGL Warning - unknown hardware vendor; please report');
 {$ENDIF}
 //SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two');
+*)
+
+    // everyone love debugging
+    AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
+    AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
+    AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
+    AddFileLog('  \----- Texture Size: ' + inttostr(MaxTextureSize));
+
 {$IFNDEF S3D_DISABLED}
     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
     begin
@@ -674,9 +675,6 @@
     end;
 {$ENDIF}
 
-    if cGPUVendor = gvUnknown then
-        AddFileLog('OpenGL Warning - unknown hardware vendor; please report');
-
     // set view port to whole window
     if (rotationQt = 0) or (rotationQt = 180) then
         glViewport(0, 0, cScreenWidth, cScreenHeight)
@@ -760,7 +758,6 @@
     SDL_GL_SwapBuffers();
 {$ENDIF}
     inc(Step);
-
 end;
 
 procedure FinishProgress;
@@ -768,6 +765,7 @@
     WriteLnToConsole('Freeing progress surface... ');
     FreeTexture(ProgrTex);
     uMobile.GameLoaded();
+    Step:= 0
 end;
 
 function RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
@@ -952,50 +950,48 @@
     reinit: boolean;
     {$IFDEF SDL13}x, y: LongInt;{$ENDIF}
 begin
-    s:= s; // avoid compiler hint
     if Length(s) = 0 then cFullScreen:= not cFullScreen
     else cFullScreen:= s = '1';
 
     buf[0]:= char(0); // avoid compiler hint
-    AddFileLog('Prepare to change video parameters...');
-
-    flags:= SDL_OPENGL;// or SDL_RESIZABLE;
-
-    if cFullScreen then
-        flags:= flags or SDL_FULLSCREEN;
+    AddFileLog('Preparing to change video parameters...');
 
+    reinit:= false;
+    if SDLPrimSurface = nil then
+        begin
+        // set window title
+        SDL_WM_SetCaption('Hedgewars', nil);
 {$IFDEF SDL_IMAGE_NEWER}
-    WriteToConsole('Init SDL_image... ');
-    SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
-    WriteLnToConsole(msgOK);
+        WriteToConsole('Init SDL_image... ');
+        SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
+        WriteLnToConsole(msgOK);
 {$ENDIF}
-    // load engine icon
+        // load engine icon
 {$IFDEF DARWIN}
-    ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps);
-    if ico = nil then ico:= LoadImage(Pathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps);
+        ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps);
+        if ico = nil then ico:= LoadImage(Pathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps);
 {$ELSE}
-    ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
-    if ico = nil then ico:= LoadImage(Pathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
+        ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
+        if ico = nil then ico:= LoadImage(Pathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
 {$ENDIF}
-    if ico <> nil then
-    begin
-        SDL_WM_SetIcon(ico, 0);
-        SDL_FreeSurface(ico)
-    end;
-
-    // set window title
-    SDL_WM_SetCaption('Hedgewars', nil);
-    reinit:= false;
-    if SDLPrimSurface <> nil then
-    begin
-{$IFDEF DARWIN | WIN32}
+        if ico <> nil then
+            begin
+            SDL_WM_SetIcon(ico, 0);
+            SDL_FreeSurface(ico)
+            end;
+        end
+    else
+        begin
+{$IF DEFINED(DARWIN) OR DEFINED(WIN32)}
         reinit:= true;
 {$ENDIF}
         AddFileLog('Freeing old primary surface...');
         SDL_FreeSurface(SDLPrimSurface);
         SDLPrimSurface:= nil;
-    end;
+        end;
 
+    // these attributes must be set up before creating the sdl window
+    SetupOpenGLAttributes();
 {$IFDEF SDL13}
     // these values in x and y make the window appear in the center
     x:= SDL_WINDOWPOS_CENTERED_MASK;
@@ -1007,12 +1003,18 @@
     x:= x or (SDL_GetNumVideoDisplays() - 1);
     y:= y or (SDL_GetNumVideoDisplays() - 1);
 
-    flags:= flags or SDL_WINDOW_BORDERLESS; // do not use SDL_WINDOW_RESIZABLE on ios (yet)
+    SDL_SetHint('SDL_IOS_ORIENTATIONS','LandscapeLeft LandscapeRight');
+    flags:= flags or SDL_WINDOW_BORDERLESS or SDL_WINDOW_RESIZABLE;
 {$ENDIF}
 
-    SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
+    if cFullScreen then SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cOrigScreenWidth, cOrigScreenHeight, flags or SDL_WINDOW_FULLSCREEN)
+    else SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
     SDLTry(SDLwindow <> nil, true);
 {$ELSE}
+    flags:= SDL_OPENGL or SDL_RESIZABLE;
+    if cFullScreen then
+        flags:= flags or SDL_FULLSCREEN;
+
     if not cOnlyStats then
         begin
 {$IFDEF WIN32}
@@ -1029,8 +1031,17 @@
     SetupOpenGL();
     if reinit then
         begin
+        if SuddenDeathDmg then
+             glClearColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255, 0.99)
+        else if ((cReducedQuality and rqNoBackground) = 0) then 
+             glClearColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255, 0.99)
+        else glClearColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255, 0.99);
+
         StoreRelease(true);
         StoreLoad(true);
+
+        ResetLand;
+
         UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT)
         end;
 end;
@@ -1043,15 +1054,12 @@
 
     SDLPrimSurface:= nil;
 
-{$IFNDEF IPHONEOS}
     rotationQt:= 0;
-    cGPUVendor:= gvUnknown;
-{$ENDIF}
-
     cScaleFactor:= 2.0;
-    SupportNPOTT:= false;
     Step:= 0;
     ProgrTex:= nil;
+    SupportNPOTT:= false;
+//    cGPUVendor:= gvUnknown;
 
     // init all ammo name texture pointers
     for ai:= Low(TAmmoType) to High(TAmmoType) do