hedgewars/uStore.pas
changeset 9317 a04c30940d2d
parent 9295 f8819c3dde54
child 9521 8054d9d775fd
child 9530 0dc3b8f94b0b
equal deleted inserted replaced
9315:15487f7fed42 9317:a04c30940d2d
    50 procedure MakeCrossHairs;
    50 procedure MakeCrossHairs;
    51 {$IFDEF USE_VIDEO_RECORDING}
    51 {$IFDEF USE_VIDEO_RECORDING}
    52 procedure InitOffscreenOpenGL;
    52 procedure InitOffscreenOpenGL;
    53 {$ENDIF}
    53 {$ENDIF}
    54 
    54 
       
    55 {$IFDEF SDL2}
    55 procedure WarpMouse(x, y: Word); inline;
    56 procedure WarpMouse(x, y: Word); inline;
       
    57 {$ENDIF}
    56 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
    58 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
    57 procedure SetSkyColor(r, g, b: real);
    59 procedure SetSkyColor(r, g, b: real);
    58 
    60 
    59 implementation
    61 implementation
    60 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    62 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    61     , uPhysFSLayer
    63     , uPhysFSLayer
    62     , uDebug
    64     , uDebug
    63     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    65     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    64     {$IF NOT DEFINED(SDL13) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    66     {$IF NOT DEFINED(SDL2) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    65 
    67 
    66 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
    68 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
    67 
    69 
    68 var MaxTextureSize: LongInt;
    70 var MaxTextureSize: LongInt;
    69 {$IFDEF SDL13}
    71 {$IFDEF SDL2}
    70     SDLwindow: PSDL_Window;
    72     SDLwindow: PSDL_Window;
    71     SDLGLcontext: PSDL_GLContext;
    73     SDLGLcontext: PSDL_GLContext;
    72 {$ELSE}
    74 {$ELSE}
    73     SDLPrimSurface: PSDL_Surface;
    75     SDLPrimSurface: PSDL_Surface;
    74 {$ENDIF}
    76 {$ENDIF}
    75     squaresize : LongInt;
    77     squaresize : LongInt;
    76     numsquares : LongInt;
    78     numsquares : LongInt;
    77     ProgrTex: PTexture;
    79     ProgrTex: PTexture;
    78 
    80 
    79 const 
    81 const
    80     cHHFileName = 'Hedgehog';
    82     cHHFileName = 'Hedgehog';
    81     cCHFileName = 'Crosshair';
    83     cCHFileName = 'Crosshair';
    82 
    84 
    83 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    85 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    84 var w, h: LongInt;
    86 var w, h: LongInt;
   200                 if (Gear <> nil) and (BotLevel > 0) then
   202                 if (Gear <> nil) and (BotLevel > 0) then
   201                     begin
   203                     begin
   202                     foundBot:= true;
   204                     foundBot:= true;
   203                     // initially was going to do the highest botlevel of the team, but for now, just apply if entire team has same bot level
   205                     // initially was going to do the highest botlevel of the team, but for now, just apply if entire team has same bot level
   204                     if maxLevel = -1 then maxLevel:= BotLevel
   206                     if maxLevel = -1 then maxLevel:= BotLevel
   205                     else if (maxLevel > 0) and (maxLevel <> BotLevel) then maxLevel:= 0; 
   207                     else if (maxLevel > 0) and (maxLevel <> BotLevel) then maxLevel:= 0;
   206                     //if (maxLevel > 0) and (BotLevel < maxLevel) then maxLevel:= BotLevel
   208                     //if (maxLevel > 0) and (BotLevel < maxLevel) then maxLevel:= BotLevel
   207                     end
   209                     end
   208                 else if Gear <> nil then  maxLevel:= 0;
   210                 else if Gear <> nil then  maxLevel:= 0;
   209 
   211 
   210         if foundBot then
   212         if foundBot then
   211             begin
   213             begin
   212             // disabled the plain flag - I think it looks ok even w/ full bars obscuring CPU
   214             // disabled the plain flag - I think it looks ok even w/ full bars obscuring CPU
   213             //if (maxLevel > 0) and (maxLevel < 3) then Flag:= 'cpu_plain' else 
   215             //if (maxLevel > 0) and (maxLevel < 3) then Flag:= 'cpu_plain' else
   214             Flag:= 'cpu'
   216             Flag:= 'cpu'
   215             end
   217             end
   216         else if (Flag = 'cpu') or (Flag = 'cpu_plain') then
   218         else if (Flag = 'cpu') or (Flag = 'cpu_plain') then
   217                 Flag:= 'hedgewars';
   219                 Flag:= 'hedgewars';
   218 
   220 
   219         flagsurf:= LoadDataImageAltFile(ptFlags, Flag, 'hedgewars', ifNone);
   221         flagsurf:= LoadDataImageAltFile(ptFlags, Flag, 'hedgewars', ifNone);
   220         TryDo(flagsurf <> nil, 'Failed to load flag "' + Flag + '" as well as the default flag', true);
   222         TryDo(flagsurf <> nil, 'Failed to load flag "' + Flag + '" as well as the default flag', true);
   221 
   223 
   222         case maxLevel of
   224         case maxLevel of
   223             1: copyToXY(SpritesData[sprBotlevels].Surface, flagsurf, 0, 0); 
   225             1: copyToXY(SpritesData[sprBotlevels].Surface, flagsurf, 0, 0);
   224             2: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 5, 2, 17, 13, 5, 2); 
   226             2: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 5, 2, 17, 13, 5, 2);
   225             3: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 9, 5, 13, 10, 9, 5); 
   227             3: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 9, 5, 13, 10, 9, 5);
   226             4: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 13, 9, 9, 6, 13, 9); 
   228             4: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 13, 9, 9, 6, 13, 9);
   227             5: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 17, 11, 5, 4, 17, 11)
   229             5: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 17, 11, 5, 4, 17, 11)
   228             end;
   230             end;
   229 
   231 
   230         copyToXY(flagsurf, texsurf, 2, 2);
   232         copyToXY(flagsurf, texsurf, 2, 2);
   231         SDL_FreeSurface(flagsurf);
   233         SDL_FreeSurface(flagsurf);
   383                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, priority);
   385                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, priority);
   384 // This should maybe be flagged. It wastes quite a bit of memory.
   386 // This should maybe be flagged. It wastes quite a bit of memory.
   385                 if not reload then
   387                 if not reload then
   386                     begin
   388                     begin
   387 {$IFDEF USE_CONTEXT_RESTORE}
   389 {$IFDEF USE_CONTEXT_RESTORE}
   388                     Surface:= tmpsurf 
   390                     Surface:= tmpsurf
   389 {$ELSE}
   391 {$ELSE}
   390                     if saveSurf then
   392                     if saveSurf then
   391                         Surface:= tmpsurf
   393                         Surface:= tmpsurf
   392                     else
   394                     else
   393                         SDL_FreeSurface(tmpsurf)
   395                         SDL_FreeSurface(tmpsurf)
   604 function LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
   606 function LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
   605 var tmpsurf: PSDL_Surface;
   607 var tmpsurf: PSDL_Surface;
   606 begin
   608 begin
   607     // check for file in user dir (never critical)
   609     // check for file in user dir (never critical)
   608     tmpsurf:= LoadImage(cPathz[path] + '/' + filename, imageFlags);
   610     tmpsurf:= LoadImage(cPathz[path] + '/' + filename, imageFlags);
   609     
   611 
   610     LoadDataImage:= tmpsurf;
   612     LoadDataImage:= tmpsurf;
   611 end;
   613 end;
   612 
   614 
   613 
   615 
   614 function LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
   616 function LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
   686 {$IFDEF IPHONEOS}
   688 {$IFDEF IPHONEOS}
   687     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
   689     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
   688     SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
   690     SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
   689 {$ELSE}
   691 {$ELSE}
   690     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   692     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   691 {$IFNDEF SDL13} // vsync is default in 1.3
   693 {$ENDIF}
       
   694 {$IFNDEF SDL2} // vsync is default in SDL2
   692     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0));
   695     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0));
   693 {$ENDIF}
   696 {$ENDIF}
   694 {$ENDIF}
       
   695     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); // no depth buffer
       
   696     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   697     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   697     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
   698     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
   698     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   699     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   699     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required
   700     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);         // no depth buffer
   700     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only
   701     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);         // no alpha channel
   701     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering
   702     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);       // buffer should be 16
       
   703     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // prefer hw rendering
   702 end;
   704 end;
   703 
   705 
   704 procedure SetupOpenGL;
   706 procedure SetupOpenGL;
   705 //var vendor: shortstring = '';
   707 var name: array[byte] of char;
   706 var buf: array[byte] of char;
   708     AuxBufNum: LongInt = 0;
   707 {$IFDEF USE_VIDEO_RECORDING}
       
   708     AuxBufNum: LongInt;
       
   709 {$ENDIF}
       
   710     tmpstr: AnsiString;
   709     tmpstr: AnsiString;
   711     tmpint: LongInt;
   710     tmpint: LongInt;
   712     tmpn: LongInt;
   711     tmpn: LongInt;
   713 begin
   712 begin
   714     buf[0]:= char(0); // avoid compiler hint
   713 {$IFDEF SDL2}
   715     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   714     name:= SDL_GetCurrentVideoDriver();
   716 
   715 {$ELSE}
   717 {$IFDEF SDL13}
   716     name:= SDL_VideoDriverName(name, sizeof(name));
   718     // this function creates an opengles1.1 context by default on mobile devices
   717 {$ENDIF}
   719     // unless you un-comment this two attributes
   718 
       
   719     AuxBufNum:= AuxBufNum;
       
   720     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(name) + ')');
       
   721 
       
   722 {$IFDEF MOBILE}
       
   723     // TODO: this function creates an opengles1.1 context
       
   724     // un-comment below and add proper logic to support opengles2.0
   720     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
   725     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
   721     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
   726     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
   722     if SDLGLcontext = nil then
   727     if SDLGLcontext = nil then
   723         SDLGLcontext:= SDL_GL_CreateContext(SDLwindow);
   728         SDLGLcontext:= SDL_GL_CreateContext(SDLwindow);
   724     SDLTry(SDLGLcontext <> nil, true);
   729     SDLTry(SDLGLcontext <> nil, true);
   725     SDL_GL_SetSwapInterval(1);
   730     SDL_GL_SetSwapInterval(1);
   726 {$ENDIF}
   731 {$ENDIF}
   727 
   732 
   728     // get the max (horizontal and vertical) size for textures that the gpu can support
   733     // get the max (h and v) size for textures that the gpu can support
   729     glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
   734     glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
   730     if MaxTextureSize <= 0 then
   735     if MaxTextureSize <= 0 then
   731         begin
   736         begin
   732         MaxTextureSize:= 1024;
   737         MaxTextureSize:= 1024;
   733         AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
   738         AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
   734         end
   739         end
   735     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
   740     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
   736         begin
   741         begin
   737         cReducedQuality := cReducedQuality or rqNoBackground;  
   742         cReducedQuality := cReducedQuality or rqNoBackground;
   738         AddFileLog('Texture size too small for backgrounds, disabling.');
   743         AddFileLog('Texture size too small for backgrounds, disabling.');
   739         end;
   744         end;
   740 
   745 
   741 (*  // find out which gpu we are using (for extension compatibility maybe?)
   746     // everyone loves debugging
   742 {$IFDEF IPHONEOS}
       
   743     vendor:= vendor; // avoid hint
       
   744     cGPUVendor:= gvApple;
       
   745 {$ELSE}
       
   746     vendor:= LowerCase(shortstring(pchar(glGetString(GL_VENDOR))));
       
   747     if StrPos(Str2PChar(vendor), Str2PChar('nvidia')) <> nil then
       
   748         cGPUVendor:= gvNVIDIA
       
   749     else if StrPos(Str2PChar(vendor), Str2PChar('intel')) <> nil then
       
   750         cGPUVendor:= gvATI
       
   751     else if StrPos(Str2PChar(vendor), Str2PChar('ati')) <> nil then
       
   752         cGPUVendor:= gvIntel
       
   753     else
       
   754         AddFileLog('OpenGL Warning - unknown hardware vendor; please report');
       
   755 {$ENDIF}
       
   756 //SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two');
       
   757 *)
       
   758 
       
   759     // everyone love debugging
       
   760     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
   747     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
   761     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   748     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   762     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   749     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   763     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   750     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   764 {$IFDEF USE_VIDEO_RECORDING}
   751 {$IFDEF USE_VIDEO_RECORDING}
   855 // leave immediately if scale factor did not change
   842 // leave immediately if scale factor did not change
   856     if f = cScaleFactor then
   843     if f = cScaleFactor then
   857         exit;
   844         exit;
   858 
   845 
   859     if f = cDefaultZoomLevel then
   846     if f = cDefaultZoomLevel then
   860         glPopMatrix         // "return" to default scaling
   847         glPopMatrix         // return to default scaling
   861     else                    // other scaling
   848     else                    // other scaling
   862         begin
   849         begin
   863         glPushMatrix;       // save default scaling
   850         glPushMatrix;       // save default scaling
   864         glLoadIdentity;
   851         glLoadIdentity;
   865         glScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
   852         glScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
   886         numsquares:= texsurf^.h div squaresize;
   873         numsquares:= texsurf^.h div squaresize;
   887         SDL_FreeSurface(texsurf);
   874         SDL_FreeSurface(texsurf);
   888         with mobileRecord do
   875         with mobileRecord do
   889             if GameLoading <> nil then
   876             if GameLoading <> nil then
   890                 GameLoading();
   877                 GameLoading();
   891         
   878 
   892         end;
   879         end;
   893 
   880 
   894     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
   881     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
   895 
   882 
   896     glClear(GL_COLOR_BUFFER_BIT);
   883     glClear(GL_COLOR_BUFFER_BIT);
  1097 FreeTexture(WeaponTooltipTex);
  1084 FreeTexture(WeaponTooltipTex);
  1098 WeaponTooltipTex:= nil
  1085 WeaponTooltipTex:= nil
  1099 end;
  1086 end;
  1100 
  1087 
  1101 {$IFDEF USE_VIDEO_RECORDING}
  1088 {$IFDEF USE_VIDEO_RECORDING}
  1102 {$IFDEF SDL13}
  1089 {$IFDEF SDL2}
  1103 procedure InitOffscreenOpenGL;
  1090 procedure InitOffscreenOpenGL;
  1104 begin
  1091 begin
  1105     // create hidden window
  1092     // create hidden window
  1106     SDLwindow:= SDL_CreateWindow('hedgewars (you don''t see this)',
  1093     SDLwindow:= SDL_CreateWindow('hedgewars video rendering (SDL2 hidden window)',
  1107                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
  1094                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
  1108                                  cScreenWidth, cScreenHeight,
  1095                                  cScreenWidth, cScreenHeight,
  1109                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
  1096                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
  1110     SDLTry(SDLwindow <> nil, true);
  1097     SDLTry(SDLwindow <> nil, true);
  1111     SetupOpenGL();
  1098     SetupOpenGL();
  1117 begin
  1104 begin
  1118     ArgCount:= 1;
  1105     ArgCount:= 1;
  1119     PrgName:= 'hwengine';
  1106     PrgName:= 'hwengine';
  1120     glutInit(@ArgCount, @PrgName);
  1107     glutInit(@ArgCount, @PrgName);
  1121     glutInitWindowSize(cScreenWidth, cScreenHeight);
  1108     glutInitWindowSize(cScreenWidth, cScreenHeight);
  1122     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
  1109     // we do not need a window, but without this call OpenGL will not initialize
       
  1110     glutCreateWindow('hedgewars video rendering (glut hidden window)');
  1123     glutHideWindow();
  1111     glutHideWindow();
  1124     glutDisplayFunc(@SwapBuffers); // we don't need a callback, but it's required for GLUT3
  1112     // we do not need to set this callback, but it is required for GLUT3 compat
       
  1113     glutDisplayFunc(@SwapBuffers);
  1125     SetupOpenGL();
  1114     SetupOpenGL();
  1126 end;
  1115 end;
  1127 {$ENDIF} // SDL13
  1116 {$ENDIF} // SDL2
  1128 {$ENDIF} // USE_VIDEO_RECORDING
  1117 {$ENDIF} // USE_VIDEO_RECORDING
  1129 
  1118 
  1130 procedure chFullScr(var s: shortstring);
  1119 procedure chFullScr(var s: shortstring);
  1131 var flags: Longword = 0;
  1120 var flags: Longword = 0;
  1132     reinit: boolean = false;
  1121     reinit: boolean = false;
  1133     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
  1122     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
  1134     {$IFDEF SDL13}x, y: LongInt;{$ENDIF}
  1123     {$IFDEF SDL2}x, y: LongInt;{$ENDIF}
  1135 begin
  1124 begin
  1136     if cOnlyStats then
  1125     if cOnlyStats then
  1137         begin
  1126         begin
  1138         MaxTextureSize:= 1024;
  1127         MaxTextureSize:= 1024;
  1139         exit
  1128         exit
  1140         end;
  1129         end;
  1141     if Length(s) = 0 then
  1130     if Length(s) = 0 then
  1142          cFullScreen:= (not cFullScreen)
  1131          cFullScreen:= (not cFullScreen)
  1143     else cFullScreen:= s = '1';
  1132     else cFullScreen:= s = '1';
  1144     
  1133 
  1145     if cFullScreen then
  1134     if cFullScreen then
  1146         begin
  1135         begin
  1147         cScreenWidth:= cFullscreenWidth;
  1136         cScreenWidth:= cFullscreenWidth;
  1148         cScreenHeight:= cFullscreenHeight;
  1137         cScreenHeight:= cFullscreenHeight;
  1149         end
  1138         end
  1152         cScreenWidth:= cWindowedWidth;
  1141         cScreenWidth:= cWindowedWidth;
  1153         cScreenHeight:= cWindowedHeight;
  1142         cScreenHeight:= cWindowedHeight;
  1154         end;
  1143         end;
  1155 
  1144 
  1156     AddFileLog('Preparing to change video parameters...');
  1145     AddFileLog('Preparing to change video parameters...');
  1157 {$IFDEF SDL13}
  1146 {$IFDEF SDL2}
  1158     if SDLwindow = nil then
  1147     if SDLwindow = nil then
  1159 {$ELSE}
  1148 {$ELSE}
  1160     if SDLPrimSurface = nil then
  1149     if SDLPrimSurface = nil then
  1161 {$ENDIF}
  1150 {$ENDIF}
  1162         begin
  1151         begin
  1163         // set window title
  1152         // set window title
  1164         {$IFNDEF SDL13}SDL_WM_SetCaption(_P'Hedgewars', nil);{$ENDIF}
  1153     {$IFNDEF SDL2}
       
  1154         SDL_WM_SetCaption(_P'Hedgewars', nil);
       
  1155     {$ENDIF}
  1165         WriteToConsole('Init SDL_image... ');
  1156         WriteToConsole('Init SDL_image... ');
  1166         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
  1157         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
  1167         WriteLnToConsole(msgOK);
  1158         WriteLnToConsole(msgOK);
  1168         // load engine icon
  1159         // load engine icon
  1169     {$IFNDEF DARWIN}
  1160     {$IFNDEF DARWIN}
  1188         glTranslatef(0, -cScreenHeight / 2, 0);
  1179         glTranslatef(0, -cScreenHeight / 2, 0);
  1189         glViewport(0, 0, cScreenWidth, cScreenHeight);
  1180         glViewport(0, 0, cScreenWidth, cScreenHeight);
  1190         exit;
  1181         exit;
  1191 {$ELSE}
  1182 {$ELSE}
  1192         SetScale(cDefaultZoomLevel);
  1183         SetScale(cDefaultZoomLevel);
  1193      {$IFDEF USE_CONTEXT_RESTORE}
  1184     {$IFDEF USE_CONTEXT_RESTORE}
  1194         reinit:= true;
  1185         reinit:= true;
  1195         StoreRelease(true);
  1186         StoreRelease(true);
  1196         ResetLand;
  1187         ResetLand;
  1197         ResetWorldTex;
  1188         ResetWorldTex;
  1198         //uTextures.freeModule; //DEBUG ONLY
  1189         //uTextures.freeModule; //DEBUG ONLY
  1199     {$ENDIF}
  1190     {$ENDIF}
  1200         AddFileLog('Freeing old primary surface...');
  1191         AddFileLog('Freeing old primary surface...');
  1201     {$IFNDEF SDL13}        
  1192     {$IFNDEF SDL2}
  1202         SDL_FreeSurface(SDLPrimSurface);
  1193         SDL_FreeSurface(SDLPrimSurface);
  1203         SDLPrimSurface:= nil;
  1194         SDLPrimSurface:= nil;
  1204     {$ENDIF}
  1195     {$ENDIF}
  1205 {$ENDIF}
  1196 {$ENDIF}
  1206         end;
  1197         end;
  1207 
  1198 
  1208     // these attributes must be set up before creating the sdl window
  1199     // these attributes must be set up before creating the sdl window
  1209 {$IFNDEF WIN32}
  1200 {$IFNDEF WIN32}
  1210 (* On a large number of testers machines, SDL default to software rendering when opengl attributes were set.
  1201 (* On a large number of testers machines, SDL default to software rendering
  1211    These attributes were "set" after CreateWindow in .15, which probably did nothing.
  1202    when opengl attributes were set. These attributes were "set" after
  1212    IMO we should rely on the gl_config defaults from SDL, and use SDL_GL_GetAttribute to possibly post warnings if any
  1203    CreateWindow in .15, which probably did nothing.
  1213    bad values are set.  *)
  1204    IMO we should rely on the gl_config defaults from SDL, and use
       
  1205    SDL_GL_GetAttribute to possibly post warnings if any bad values are set.
       
  1206  *)
  1214     SetupOpenGLAttributes();
  1207     SetupOpenGLAttributes();
  1215 {$ENDIF}
  1208 {$ENDIF}
  1216 {$IFDEF SDL13}
  1209 {$IFDEF SDL2}
  1217     // these values in x and y make the window appear in the center
  1210     // these values in x and y make the window appear in the center
  1218     x:= SDL_WINDOWPOS_CENTERED_MASK;
  1211     x:= SDL_WINDOWPOS_CENTERED_MASK;
  1219     y:= SDL_WINDOWPOS_CENTERED_MASK;
  1212     y:= SDL_WINDOWPOS_CENTERED_MASK;
  1220     // SDL_WINDOW_RESIZABLE makes the window respond to rotation events on mobile devices
  1213     // SDL_WINDOW_RESIZABLE makes the window resizable and
       
  1214     //  respond to rotation events on mobile devices
  1221     flags:= SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN or SDL_WINDOW_RESIZABLE;
  1215     flags:= SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN or SDL_WINDOW_RESIZABLE;
  1222 
  1216 
  1223     {$IFDEF MOBILE}
  1217     {$IFDEF MOBILE}
  1224     if isPhone() then
  1218     if isPhone() then
  1225         SDL_SetHint('SDL_IOS_ORIENTATIONS','LandscapeLeft LandscapeRight');
  1219         SDL_SetHint('SDL_IOS_ORIENTATIONS','LandscapeLeft LandscapeRight');
  1226     // no need for borders on mobile devices
  1220     // no need for borders on mobile devices
  1227     flags:= flags or SDL_WINDOW_BORDERLESS;
  1221     flags:= flags or SDL_WINDOW_BORDERLESS;
  1228     {$ENDIF}
  1222     {$ENDIF}
  1229 
  1223 
       
  1224     if cFullScreen then
       
  1225         flags:= flags or SDL_WINDOW_FULLSCREEN;
       
  1226 
  1230     if SDLwindow = nil then
  1227     if SDLwindow = nil then
  1231         if cFullScreen then
  1228         SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
  1232             SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags or SDL_WINDOW_FULLSCREEN)
       
  1233         else
       
  1234             begin
       
  1235             SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
       
  1236             end;
       
  1237     SDLTry(SDLwindow <> nil, true);
  1229     SDLTry(SDLwindow <> nil, true);
  1238 {$ELSE}
  1230 {$ELSE}
  1239     flags:= SDL_OPENGL or SDL_RESIZABLE;
  1231     flags:= SDL_OPENGL or SDL_RESIZABLE;
  1240     if cFullScreen then
  1232     if cFullScreen then
  1241         begin
       
  1242         flags:= flags or SDL_FULLSCREEN;
  1233         flags:= flags or SDL_FULLSCREEN;
  1243         end;
       
  1244     if not cOnlyStats then
  1234     if not cOnlyStats then
  1245         begin
  1235         begin
  1246     {$IFDEF WIN32}
  1236     {$IFDEF WIN32}
  1247         s:= SDL_getenv('SDL_VIDEO_CENTERED');
  1237         s:= SDL_getenv('SDL_VIDEO_CENTERED');
  1248         SDL_putenv('SDL_VIDEO_CENTERED=1');
  1238         SDL_putenv('SDL_VIDEO_CENTERED=1');
  1249     {$ENDIF}
  1239     {$ENDIF}
  1250         SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, 0, flags);
  1240         SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, 0, flags);
  1251         SDLTry(SDLPrimSurface <> nil, true);
  1241         SDLTry(SDLPrimSurface <> nil, true);
  1252     {$IFDEF WIN32}SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));{$ENDIF}
  1242     {$IFDEF WIN32}
       
  1243         SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));
       
  1244     {$ENDIF}
  1253         end;
  1245         end;
  1254 {$ENDIF}
  1246 {$ENDIF}
  1255 
  1247 
  1256     SetupOpenGL();
  1248     SetupOpenGL();
  1257     if reinit then
  1249     if reinit then
  1258         begin
  1250         begin
  1259         // clean the window from any previous content
  1251         // clean the window from any previous content
  1260         glClear(GL_COLOR_BUFFER_BIT);
  1252         glClear(GL_COLOR_BUFFER_BIT);
  1261         if SuddenDeathDmg then
  1253         if SuddenDeathDmg then
  1262             SetSkyColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255)
  1254             SetSkyColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255)
  1263         else if ((cReducedQuality and rqNoBackground) = 0) then 
  1255         else if ((cReducedQuality and rqNoBackground) = 0) then
  1264             SetSkyColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255)
  1256             SetSkyColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255)
  1265         else
  1257         else
  1266             SetSkyColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255);
  1258             SetSkyColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255);
  1267 
  1259 
  1268         // reload everything we had before
  1260         // reload everything we had before
  1272         // redraw all land
  1264         // redraw all land
  1273         UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false);
  1265         UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false);
  1274         end;
  1266         end;
  1275 end;
  1267 end;
  1276 
  1268 
       
  1269 {$IFDEF SDL2}
       
  1270 // for sdl1.2 we directly call SDL_WarpMouse()
       
  1271 // for sdl2 we provide a SDL_WarpMouse() which just calls this function
       
  1272 // this has the advantage of reducing 'uses' and 'ifdef' statements
       
  1273 // (SDLwindow is a private member of this module)
  1277 procedure WarpMouse(x, y: Word); inline;
  1274 procedure WarpMouse(x, y: Word); inline;
  1278 begin
  1275 begin
  1279 {$IFDEF SDL13}
       
  1280     SDL_WarpMouseInWindow(SDLwindow, x, y);
  1276     SDL_WarpMouseInWindow(SDLwindow, x, y);
  1281 {$ELSE}
  1277 end;
  1282     x:= x; y:= y; // avoid hints
  1278 {$ENDIF}
  1283 {$ENDIF}
       
  1284 end;
       
  1285 
  1279 
  1286 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
  1280 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
  1287 begin
  1281 begin
  1288     if GameType = gmtRecord then
  1282     if GameType = gmtRecord then
  1289         exit;
  1283         exit;
  1290 {$IFDEF SDL13}
  1284 {$IFDEF SDL2}
  1291     SDL_GL_SwapWindow(SDLwindow);
  1285     SDL_GL_SwapWindow(SDLwindow);
  1292 {$ELSE}
  1286 {$ELSE}
  1293     SDL_GL_SwapBuffers();
  1287     SDL_GL_SwapBuffers();
  1294 {$ENDIF}
  1288 {$ENDIF}
  1295 end;
  1289 end;
  1316         Ammoz[ai].NameTex := nil;
  1310         Ammoz[ai].NameTex := nil;
  1317     end;
  1311     end;
  1318     // init all count texture pointers
  1312     // init all count texture pointers
  1319     for i:= Low(CountTexz) to High(CountTexz) do
  1313     for i:= Low(CountTexz) to High(CountTexz) do
  1320         CountTexz[i] := nil;
  1314         CountTexz[i] := nil;
  1321 {$IFDEF SDL13}
  1315 {$IFDEF SDL2}
  1322     SDLwindow:= nil;
  1316     SDLwindow:= nil;
  1323     SDLGLcontext:= nil;
  1317     SDLGLcontext:= nil;
  1324 {$ELSE}
  1318 {$ELSE}
  1325     SDLPrimSurface:= nil;
  1319     SDLPrimSurface:= nil;
  1326 {$ENDIF}
  1320 {$ENDIF}
  1328 
  1322 
  1329 procedure freeModule;
  1323 procedure freeModule;
  1330 begin
  1324 begin
  1331     StoreRelease(false);
  1325     StoreRelease(false);
  1332     TTF_Quit();
  1326     TTF_Quit();
  1333 {$IFDEF SDL13}
  1327 {$IFDEF SDL2}
  1334     SDL_GL_DeleteContext(SDLGLcontext);
  1328     SDL_GL_DeleteContext(SDLGLcontext);
  1335     SDL_DestroyWindow(SDLwindow);
  1329     SDL_DestroyWindow(SDLwindow);
  1336 {$ENDIF}
  1330 {$ENDIF}
  1337     SDL_Quit();
  1331     SDL_Quit();
  1338 end;
  1332 end;