hedgewars/uStore.pas
changeset 7687 c73fd8cfa7c0
parent 7640 e9e6b4d740f6
child 7716 95117607b81a
equal deleted inserted replaced
7613:ce6ead3327b2 7687:c73fd8cfa7c0
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 {$IF GLunit = GL}{$DEFINE GLunit:=GL,GLext}{$ENDIF}
    20 {$IF GLunit = GL}{$DEFINE GLunit:=GL,GLext}{$ENDIF}
    21 
    21 
    22 unit uStore;
    22 unit uStore;
    23 interface
    23 interface
    24 uses SysUtils, uConsts, SDLh, GLunit, uTypes, uLandTexture, uCaptions, uChat;
    24 uses {$IFNDEF PAS2C} StrUtils, {$ENDIF}SysUtils, uConsts, SDLh, GLunit, uTypes, uLandTexture, uCaptions, uChat;
    25 
    25 
    26 procedure initModule;
    26 procedure initModule;
    27 procedure freeModule;
    27 procedure freeModule;
    28 
    28 
    29 procedure StoreLoad(reload: boolean);
    29 procedure StoreLoad(reload: boolean);
    30 procedure StoreRelease(reload: boolean);
    30 procedure StoreRelease(reload: boolean);
    31 procedure RenderHealth(var Hedgehog: THedgehog);
    31 procedure RenderHealth(var Hedgehog: THedgehog);
    32 procedure AddProgress;
    32 procedure AddProgress;
    33 procedure FinishProgress;
    33 procedure FinishProgress;
    34 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    34 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
       
    35 
       
    36 // loads an image from the game's data files
       
    37 function  LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
       
    38 // like LoadDataImage but uses altPath as fallback-path if file not found/loadable in path
       
    39 function  LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
       
    40 // like LoadDataImage but uses altFile as fallback-filename if file cannot be loaded
       
    41 function  LoadDataImageAltFile(const path: TPathType; const filename, altFile: shortstring; imageFlags: LongInt): PSDL_Surface;
       
    42 
    35 procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
    43 procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
    36 procedure SetupOpenGL;
    44 procedure SetupOpenGL;
    37 procedure SetScale(f: GLfloat);
    45 procedure SetScale(f: GLfloat);
    38 function  RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
    46 function  RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
    39 procedure RenderWeaponTooltip(atype: TAmmoType);
    47 procedure RenderWeaponTooltip(atype: TAmmoType);
    40 procedure ShowWeaponTooltip(x, y: LongInt);
    48 procedure ShowWeaponTooltip(x, y: LongInt);
    41 procedure FreeWeaponTooltip;
    49 procedure FreeWeaponTooltip;
    42 procedure MakeCrossHairs;
    50 procedure MakeCrossHairs;
       
    51 {$IFDEF USE_VIDEO_RECORDING}
       
    52 procedure InitOffscreenOpenGL;
       
    53 {$ENDIF}
    43 
    54 
    44 procedure WarpMouse(x, y: Word); inline;
    55 procedure WarpMouse(x, y: Word); inline;
    45 procedure SwapBuffers; inline;
    56 procedure SwapBuffers; inline;
    46 
    57 
    47 implementation
    58 implementation
    48 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands,
    59 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands,
    49      uDebug{$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF};
    60      uDebug{$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
       
    61      {$IF NOT DEFINED(SDL13) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    50 
    62 
    51 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
    63 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
    52 
    64 
    53 var MaxTextureSize: LongInt;
    65 var MaxTextureSize: LongInt;
    54 {$IFDEF SDL13}
    66 {$IFDEF SDL13}
    89 var t: LongInt;
   101 var t: LongInt;
    90     tmpsurf, texsurf: PSDL_Surface;
   102     tmpsurf, texsurf: PSDL_Surface;
    91     Color, i: Longword;
   103     Color, i: Longword;
    92     s: shortstring;
   104     s: shortstring;
    93 begin
   105 begin
    94 s:= UserPathz[ptGraphics] + '/' + cCHFileName;
   106 tmpsurf:= LoadDataImage(ptGraphics, cCHFileName, ifAlpha or ifCritical);
    95 if not FileExists(s+'.png') then s:= Pathz[ptGraphics] + '/' + cCHFileName;
       
    96 tmpsurf:= LoadImage(s, ifAlpha or ifCritical);
       
    97 
   107 
    98 for t:= 0 to Pred(TeamsCount) do
   108 for t:= 0 to Pred(TeamsCount) do
    99     with TeamsArray[t]^ do
   109     with TeamsArray[t]^ do
   100     begin
   110     begin
   101     texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, tmpsurf^.w, tmpsurf^.h, 32, RMask, GMask, BMask, AMask);
   111     texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, tmpsurf^.w, tmpsurf^.h, 32, RMask, GMask, BMask, AMask);
   194             Flag:= 'cpu'
   204             Flag:= 'cpu'
   195             end
   205             end
   196         else if (Flag = 'cpu') or (Flag = 'cpu_plain') then
   206         else if (Flag = 'cpu') or (Flag = 'cpu_plain') then
   197                 Flag:= 'hedgewars';
   207                 Flag:= 'hedgewars';
   198 
   208 
   199         flagsurf:= LoadImage(UserPathz[ptFlags] + '/' + Flag, ifNone);
   209         flagsurf:= LoadDataImageAltFile(ptFlags, Flag, 'hedgewars', ifNone);
   200         if flagsurf = nil then
       
   201             flagsurf:= LoadImage(Pathz[ptFlags] + '/' + Flag, ifNone);
       
   202         if flagsurf = nil then
       
   203             flagsurf:= LoadImage(UserPathz[ptFlags] + '/hedgewars', ifNone);
       
   204         if flagsurf = nil then
       
   205             flagsurf:= LoadImage(Pathz[ptFlags] + '/hedgewars', ifNone);
       
   206         TryDo(flagsurf <> nil, 'Failed to load flag "' + Flag + '" as well as the default flag', true);
   210         TryDo(flagsurf <> nil, 'Failed to load flag "' + Flag + '" as well as the default flag', true);
   207 
   211 
   208         case maxLevel of
   212         case maxLevel of
   209             1: copyToXY(SpritesData[sprBotlevels].Surface, flagsurf, 0, 0); 
   213             1: copyToXY(SpritesData[sprBotlevels].Surface, flagsurf, 0, 0); 
   210             2: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 5, 2, 17, 13, 5, 2); 
   214             2: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 5, 2, 17, 13, 5, 2); 
   245                         else
   249                         else
   246                             LoadHedgehogHat(Gear, Hat);
   250                             LoadHedgehogHat(Gear, Hat);
   247                         end
   251                         end
   248                     end;
   252                     end;
   249         end;
   253         end;
   250     MissionIcons:= LoadImage(UserPathz[ptGraphics] + '/missions', ifNone);
   254     MissionIcons:= LoadDataImage(ptGraphics, 'missions', ifCritical);
   251     if MissionIcons = nil then
       
   252         MissionIcons:= LoadImage(Pathz[ptGraphics] + '/missions', ifCritical);
       
   253     iconsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, 28, 28, 32, RMask, GMask, BMask, AMask);
   255     iconsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, 28, 28, 32, RMask, GMask, BMask, AMask);
   254     if iconsurf <> nil then
   256     if iconsurf <> nil then
   255         begin
   257         begin
   256         r.x:= 0;
   258         r.x:= 0;
   257         r.y:= 0;
   259         r.y:= 0;
   285     if TeamsArray[t] <> nil then
   287     if TeamsArray[t] <> nil then
   286         with TeamsArray[t]^ do
   288         with TeamsArray[t]^ do
   287             begin
   289             begin
   288             if GraveName = '' then
   290             if GraveName = '' then
   289                 GraveName:= 'Statue';
   291                 GraveName:= 'Statue';
   290             texsurf:= LoadImage(UserPathz[ptGraves] + '/' + GraveName, ifTransparent);
   292             texsurf:= LoadDataImageAltFile(ptGraves, GraveName, 'Statue', ifCritical or ifTransparent);
   291             if texsurf = nil then
       
   292                 texsurf:= LoadImage(Pathz[ptGraves] + '/' + GraveName, ifTransparent);
       
   293             if texsurf = nil then
       
   294                 texsurf:= LoadImage(UserPathz[ptGraves] + '/Statue', ifTransparent);
       
   295             if texsurf = nil then
       
   296                 texsurf:= LoadImage(Pathz[ptGraves] + '/Statue', ifCritical or ifTransparent);
       
   297             GraveTex:= Surface2Tex(texsurf, false);
   293             GraveTex:= Surface2Tex(texsurf, false);
   298             SDL_FreeSurface(texsurf)
   294             SDL_FreeSurface(texsurf)
   299             end
   295             end
   300 end;
   296 end;
   301 
   297 
   303 var s: shortstring;
   299 var s: shortstring;
   304     ii: TSprite;
   300     ii: TSprite;
   305     fi: THWFont;
   301     fi: THWFont;
   306     ai: TAmmoType;
   302     ai: TAmmoType;
   307     tmpsurf: PSDL_Surface;
   303     tmpsurf: PSDL_Surface;
   308     i: LongInt;
   304     i, imflags: LongInt;
   309 begin
   305 begin
   310 AddFileLog('StoreLoad()');
   306 AddFileLog('StoreLoad()');
   311 
   307 
   312 if not reload then
   308 if not reload then
   313     for fi:= Low(THWFont) to High(THWFont) do
   309     for fi:= Low(THWFont) to High(THWFont) do
   336            (((cReducedQuality and rqPlainSplash) = 0) or ((not (ii in [sprSplash, sprDroplet, sprSDSplash, sprSDDroplet])))) and
   332            (((cReducedQuality and rqPlainSplash) = 0) or ((not (ii in [sprSplash, sprDroplet, sprSDSplash, sprSDDroplet])))) and
   337            (((cReducedQuality and rqKillFlakes) = 0) or (Theme = 'Snow') or (Theme = 'Christmas') or ((not (ii in [sprFlake, sprSDFlake])))) and
   333            (((cReducedQuality and rqKillFlakes) = 0) or (Theme = 'Snow') or (Theme = 'Christmas') or ((not (ii in [sprFlake, sprSDFlake])))) and
   338            ((cCloudsNumber > 0) or (ii <> sprCloud)) and
   334            ((cCloudsNumber > 0) or (ii <> sprCloud)) and
   339            ((vobCount > 0) or (ii <> sprFlake)) then
   335            ((vobCount > 0) or (ii <> sprFlake)) then
   340             begin
   336             begin
   341             if AltPath = ptNone then
   337             if reload then
   342                 if ii in [sprHorizont, sprHorizontL, sprHorizontR, sprSky, sprSkyL, sprSkyR, sprChunk] then // FIXME: hack
   338                 tmpsurf:= Surface
   343                     begin
       
   344                     if not reload then
       
   345                         begin
       
   346                         tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
       
   347                         if tmpsurf = nil then
       
   348                            tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent)
       
   349                         end
       
   350                     else
       
   351                            tmpsurf:= Surface
       
   352                     end
       
   353                 else
       
   354                     begin
       
   355                     if not reload then
       
   356                         begin
       
   357                            tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
       
   358                         if tmpsurf = nil then
       
   359                            tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent or ifCritical)
       
   360                         end
       
   361                     else
       
   362                            tmpsurf:= Surface
       
   363                     end
       
   364             else
   339             else
   365                 begin
   340                 begin
   366                 if not reload then
   341                 imflags := (ifAlpha or ifTransparent);
   367                     begin
   342 
   368                     tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
   343                 // these sprites are optional
   369                     if tmpsurf = nil then
   344                 if not (ii in [sprHorizont, sprHorizontL, sprHorizontR, sprSky, sprSkyL, sprSkyR, sprChunk]) then // FIXME: hack
   370                         tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
   345                     imflags := (imflags or ifCritical);
   371                     if tmpsurf = nil then
   346 
   372                         tmpsurf:= LoadImage(UserPathz[AltPath] + '/' + FileName, ifAlpha or ifTransparent);
   347                 // load the image
   373                     if tmpsurf = nil then
   348                 tmpsurf := LoadDataImageAltPath(Path, AltPath, FileName, imflags)
   374                         tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, ifAlpha or ifCritical or ifTransparent)
       
   375                     end
       
   376                 else
       
   377                         tmpsurf:= Surface
       
   378                 end;
   349                 end;
   379 
   350 
   380             if tmpsurf <> nil then
   351             if tmpsurf <> nil then
   381                 begin
   352                 begin
   382                 if getImageDimensions then
   353                 if getImageDimensions then
   422 WriteNames(fnt16);
   393 WriteNames(fnt16);
   423 
   394 
   424 if not reload then
   395 if not reload then
   425     AddProgress;
   396     AddProgress;
   426 
   397 
   427     tmpsurf:= LoadImage(UserPathz[ptGraphics] + '/' + cHHFileName, ifAlpha or ifTransparent);
   398 tmpsurf:= LoadDataImage(ptGraphics, cHHFileName, ifAlpha or ifCritical or ifTransparent);
   428 if tmpsurf = nil then
   399 
   429     tmpsurf:= LoadImage(Pathz[ptGraphics] + '/' + cHHFileName, ifAlpha or ifCritical or ifTransparent);
       
   430     
       
   431 HHTexture:= Surface2Tex(tmpsurf, false);
   400 HHTexture:= Surface2Tex(tmpsurf, false);
   432 SDL_FreeSurface(tmpsurf);
   401 SDL_FreeSurface(tmpsurf);
   433 
   402 
   434 InitHealth;
   403 InitHealth;
   435 
   404 
   465 
   434 
   466 if not reload then
   435 if not reload then
   467     AddProgress;
   436     AddProgress;
   468 IMG_Quit();
   437 IMG_Quit();
   469 end;
   438 end;
       
   439 
       
   440 {$IF NOT DEFINED(S3D_DISABLED) OR DEFINED(USE_VIDEO_RECORDING)}
       
   441 procedure CreateFramebuffer(var frame, depth, tex: GLuint);
       
   442 begin
       
   443     glGenFramebuffersEXT(1, @frame);
       
   444     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frame);
       
   445     glGenRenderbuffersEXT(1, @depth);
       
   446     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth);
       
   447     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, cScreenWidth, cScreenHeight);
       
   448     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth);
       
   449     glGenTextures(1, @tex);
       
   450     glBindTexture(GL_TEXTURE_2D, tex);
       
   451     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  cScreenWidth, cScreenHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
       
   452     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
   453     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       
   454     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);
       
   455 end;
       
   456 
       
   457 procedure DeleteFramebuffer(var frame, depth, tex: GLuint);
       
   458 begin
       
   459     glDeleteTextures(1, @tex);
       
   460     glDeleteRenderbuffersEXT(1, @depth);
       
   461     glDeleteFramebuffersEXT(1, @frame);
       
   462 end;
       
   463 {$ENDIF}
   470 
   464 
   471 procedure StoreRelease(reload: boolean);
   465 procedure StoreRelease(reload: boolean);
   472 var ii: TSprite;
   466 var ii: TSprite;
   473     ai: TAmmoType;
   467     ai: TAmmoType;
   474     i, t: LongInt;
   468     i, t: LongInt;
   539                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].HatTex);
   533                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].HatTex);
   540                 TeamsArray[t]^.Hedgehogs[i].HatTex:= nil;
   534                 TeamsArray[t]^.Hedgehogs[i].HatTex:= nil;
   541                 end;
   535                 end;
   542             end;
   536             end;
   543         end;
   537         end;
       
   538 {$IFDEF USE_VIDEO_RECORDING}
       
   539     if defaultFrame <> 0 then
       
   540         DeleteFramebuffer(defaultFrame, depthv, texv);
       
   541 {$ENDIF}
   544 {$IFNDEF S3D_DISABLED}
   542 {$IFNDEF S3D_DISABLED}
   545     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   543     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   546         begin
   544         begin
   547         glDeleteTextures(1, @texl);
   545         DeleteFramebuffer(framel, depthl, texl);
   548         glDeleteRenderbuffersEXT(1, @depthl);
   546         DeleteFramebuffer(framer, depthr, texr);
   549         glDeleteFramebuffersEXT(1, @framel);
       
   550         glDeleteTextures(1, @texr);
       
   551         glDeleteRenderbuffersEXT(1, @depthr);
       
   552         glDeleteFramebuffersEXT(1, @framer)
       
   553         end
   547         end
   554 {$ENDIF}
   548 {$ENDIF}
   555 end;
   549 end;
   556 
   550 
   557 
   551 
   596     WriteLnToConsole(msgOK + ' (' + inttostr(tmpsurf^.w) + 'x' + inttostr(tmpsurf^.h) + ')');
   590     WriteLnToConsole(msgOK + ' (' + inttostr(tmpsurf^.w) + 'x' + inttostr(tmpsurf^.h) + ')');
   597 
   591 
   598     LoadImage:= tmpsurf //Result
   592     LoadImage:= tmpsurf //Result
   599 end;
   593 end;
   600 
   594 
       
   595 
       
   596 function LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
       
   597 var tmpsurf: PSDL_Surface;
       
   598 begin
       
   599     // check for file in user dir (never critical)
       
   600     tmpsurf:= LoadImage(UserPathz[path] + '/' + filename, imageFlags and (not ifCritical));
       
   601 
       
   602     // if unsuccessful check data dir
       
   603     if (tmpsurf = nil) then
       
   604         tmpsurf:= LoadImage(Pathz[path] + '/' + filename, imageFlags);
       
   605 
       
   606     LoadDataImage:= tmpsurf;
       
   607 end;
       
   608 
       
   609 
       
   610 function LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
       
   611 var tmpsurf: PSDL_Surface;
       
   612 begin
       
   613     // if there is no alternative path, just forward and return result
       
   614     if (altPath = ptNone) then
       
   615         exit(LoadDataImage(path, filename, imageFlags));
       
   616 
       
   617     // since we have a fallback path this search isn't critical yet
       
   618     tmpsurf:= LoadDataImage(path, filename, imageFlags and (not ifCritical));
       
   619 
       
   620     // if image still not found try alternative path
       
   621     if (tmpsurf = nil) then
       
   622         tmpsurf:= LoadDataImage(altPath, filename, imageFlags);
       
   623 
       
   624     LoadDataImageAltPath:= tmpsurf;
       
   625 end;
       
   626 
       
   627 function LoadDataImageAltFile(const path: TPathType; const filename, altFile: shortstring; imageFlags: LongInt): PSDL_Surface;
       
   628 var tmpsurf: PSDL_Surface;
       
   629 begin
       
   630     // if there is no alternative filename, just forward and return result
       
   631     if (altFile = '') then
       
   632         exit(LoadDataImage(path, filename, imageFlags));
       
   633 
       
   634     // since we have a fallback filename this search isn't critical yet
       
   635     tmpsurf:= LoadDataImage(path, filename, imageFlags and (not ifCritical));
       
   636 
       
   637     // if image still not found try alternative filename
       
   638     if (tmpsurf = nil) then
       
   639         tmpsurf:= LoadDataImage(path, altFile, imageFlags);
       
   640 
       
   641     LoadDataImageAltFile:= tmpsurf;
       
   642 end;
       
   643 
   601 procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
   644 procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
   602 var texsurf: PSDL_Surface;
   645 var texsurf: PSDL_Surface;
   603 begin
   646 begin
   604 texsurf:= LoadImage(UserPathz[ptHats] + '/' + newHat, ifNone);
   647     texsurf:= LoadDataImage(ptHats, newHat, ifNone);
   605     if texsurf = nil then
       
   606         texsurf:= LoadImage(Pathz[ptHats] + '/' + newHat, ifNone);
       
   607 
   648 
   608     // only do something if the hat could be loaded
   649     // only do something if the hat could be loaded
   609     if texsurf <> nil then
   650     if texsurf <> nil then
   610         begin
   651         begin
   611         // free the mem of any previously assigned texture
   652         // free the mem of any previously assigned texture
   656 end;
   697 end;
   657 
   698 
   658 procedure SetupOpenGL;
   699 procedure SetupOpenGL;
   659 //var vendor: shortstring = '';
   700 //var vendor: shortstring = '';
   660 var buf: array[byte] of char;
   701 var buf: array[byte] of char;
       
   702 {$IFDEF USE_VIDEO_RECORDING}
       
   703     AuxBufNum: LongInt;
       
   704 {$ENDIF}
       
   705     tmpstr: AnsiString;
       
   706     tmpint: LongInt;
       
   707     tmpn: LongInt;
   661 begin
   708 begin
   662     buf[0]:= char(0); // avoid compiler hint
   709     buf[0]:= char(0); // avoid compiler hint
   663     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   710     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   664 
   711 
   665 {$IFDEF SDL13}
   712 {$IFDEF SDL13}
   707     // everyone love debugging
   754     // everyone love debugging
   708     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
   755     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
   709     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   756     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   710     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   757     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   711     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   758     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   712     AddFileLog('  \----- Extensions: ' + shortstring(pchar(glGetString(GL_EXTENSIONS))));
   759 {$IFDEF USE_VIDEO_RECORDING}
   713     //TODO: don't have the Extensions line trimmed but slipt it into multiple lines
   760     glGetIntegerv(GL_AUX_BUFFERS, @AuxBufNum);
       
   761     AddFileLog('  |----- Number of auxiliary buffers: ' + inttostr(AuxBufNum));
       
   762 {$ENDIF}
       
   763     AddFileLog('  \----- Extensions: ');
       
   764 {$IFNDEF PAS2C}
       
   765     // fetch extentions and store them in string
       
   766     tmpstr := StrPas(PChar(glGetString(GL_EXTENSIONS)));
       
   767     tmpn := WordCount(tmpstr, [' ']);
       
   768     tmpint := 1;
       
   769 
       
   770     repeat
       
   771     begin
       
   772         // print up to 3 extentions per row
       
   773         // ExtractWord will return empty string if index out of range
       
   774         AddFileLog(TrimRight(
       
   775             ExtractWord(tmpint, tmpstr, [' ']) + ' ' +
       
   776             ExtractWord(tmpint+1, tmpstr, [' ']) + ' ' +
       
   777             ExtractWord(tmpint+2, tmpstr, [' '])
       
   778         ));
       
   779         tmpint := tmpint + 3;
       
   780     end;
       
   781     until (tmpint > tmpn);
       
   782 {$ELSE}
       
   783     // doesn't seem to print >256 chars
       
   784     AddFileLogRaw(PChar(glGetString(GL_EXTENSIONS)));
       
   785 {$ENDIF}
       
   786     AddFileLog('');
       
   787 
       
   788     defaultFrame:= 0;
       
   789 {$IFDEF USE_VIDEO_RECORDING}
       
   790     if GameType = gmtRecord then
       
   791     begin
       
   792         if glLoadExtension('GL_EXT_framebuffer_object') then
       
   793         begin
       
   794             CreateFramebuffer(defaultFrame, depthv, texv);
       
   795             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, defaultFrame);
       
   796             AddFileLog('Using framebuffer for video recording.');
       
   797         end
       
   798         else if AuxBufNum > 0 then
       
   799         begin
       
   800             glDrawBuffer(GL_AUX0);
       
   801             glReadBuffer(GL_AUX0);
       
   802             AddFileLog('Using auxiliary buffer for video recording.');
       
   803         end
       
   804         else
       
   805         begin
       
   806             glDrawBuffer(GL_BACK);
       
   807             glReadBuffer(GL_BACK);
       
   808             AddFileLog('Warning: off-screen rendering is not supported; using back buffer but it may not work.');
       
   809         end;
       
   810     end;
       
   811 {$ENDIF}
   714 
   812 
   715 {$IFNDEF S3D_DISABLED}
   813 {$IFNDEF S3D_DISABLED}
   716     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   814     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   717     begin
   815     begin
   718         // prepare left and right frame buffers and associated textures
   816         // prepare left and right frame buffers and associated textures
   719         if glLoadExtension('GL_EXT_framebuffer_object') then
   817         if glLoadExtension('GL_EXT_framebuffer_object') then
   720             begin
   818             begin
   721             // left
   819             CreateFramebuffer(framel, depthl, texl);
   722             glGenFramebuffersEXT(1, @framel);
   820             CreateFramebuffer(framer, depthr, texr);
   723             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framel);
       
   724             glGenRenderbuffersEXT(1, @depthl);
       
   725             glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthl);
       
   726             glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, cScreenWidth, cScreenHeight);
       
   727             glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthl);
       
   728             glGenTextures(1, @texl);
       
   729             glBindTexture(GL_TEXTURE_2D, texl);
       
   730             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  cScreenWidth, cScreenHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
       
   731             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
   732             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       
   733             glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texl, 0);
       
   734 
       
   735             // right
       
   736             glGenFramebuffersEXT(1, @framer);
       
   737             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framer);
       
   738             glGenRenderbuffersEXT(1, @depthr);
       
   739             glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthr);
       
   740             glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, cScreenWidth, cScreenHeight);
       
   741             glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthr);
       
   742             glGenTextures(1, @texr);
       
   743             glBindTexture(GL_TEXTURE_2D, texr);
       
   744             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  cScreenWidth, cScreenHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
       
   745             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
   746             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       
   747             glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texr, 0);
       
   748 
   821 
   749             // reset
   822             // reset
   750             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
   823             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, defaultFrame)
   751             end
   824             end
   752         else
   825         else
   753             cStereoMode:= smNone;
   826             cStereoMode:= smNone;
   754     end;
   827     end;
   755 {$ENDIF}
   828 {$ENDIF}
   801     texsurf: PSDL_Surface;
   874     texsurf: PSDL_Surface;
   802 begin
   875 begin
   803     if Step = 0 then
   876     if Step = 0 then
   804     begin
   877     begin
   805         WriteToConsole(msgLoading + 'progress sprite: ');
   878         WriteToConsole(msgLoading + 'progress sprite: ');
   806         texsurf:= LoadImage(UserPathz[ptGraphics] + '/Progress', ifTransparent);
   879         texsurf:= LoadDataImage(ptGraphics, 'Progress', ifCritical or ifTransparent);
   807         if texsurf = nil then
       
   808             texsurf:= LoadImage(Pathz[ptGraphics] + '/Progress', ifCritical or ifTransparent);
       
   809 
   880 
   810         ProgrTex:= Surface2Tex(texsurf, false);
   881         ProgrTex:= Surface2Tex(texsurf, false);
   811 
   882 
   812         squaresize:= texsurf^.w shr 1;
   883         squaresize:= texsurf^.w shr 1;
   813         numsquares:= texsurf^.h div squaresize;
   884         numsquares:= texsurf^.h div squaresize;
  1019 // free the existing texture (if there is any)
  1090 // free the existing texture (if there is any)
  1020 FreeTexture(WeaponTooltipTex);
  1091 FreeTexture(WeaponTooltipTex);
  1021 WeaponTooltipTex:= nil
  1092 WeaponTooltipTex:= nil
  1022 end;
  1093 end;
  1023 
  1094 
       
  1095 {$IFDEF USE_VIDEO_RECORDING}
       
  1096 {$IFDEF SDL13}
       
  1097 procedure InitOffscreenOpenGL;
       
  1098 begin
       
  1099     // create hidden window
       
  1100     SDLwindow:= SDL_CreateWindow('hedgewars (you don''t see this)',
       
  1101                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
       
  1102                                  cScreenWidth, cScreenHeight,
       
  1103                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
       
  1104     SDLTry(SDLwindow <> nil, true);
       
  1105     SetupOpenGL();
       
  1106 end;
       
  1107 {$ELSE}
       
  1108 procedure InitOffscreenOpenGL;
       
  1109 var ArgCount: LongInt;
       
  1110     PrgName: pchar;
       
  1111 begin
       
  1112     ArgCount:= 1;
       
  1113     PrgName:= 'hwengine';
       
  1114     glutInit(@ArgCount, @PrgName);
       
  1115     glutInitWindowSize(cScreenWidth, cScreenHeight);
       
  1116     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
       
  1117     glutHideWindow();
       
  1118     SetupOpenGL();
       
  1119 end;
       
  1120 {$ENDIF} // SDL13
       
  1121 {$ENDIF} // USE_VIDEO_RECORDING
       
  1122 
  1024 procedure chFullScr(var s: shortstring);
  1123 procedure chFullScr(var s: shortstring);
  1025 var flags: Longword = 0;
  1124 var flags: Longword = 0;
  1026     reinit: boolean = false;
  1125     reinit: boolean = false;
  1027     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
  1126     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
  1028     {$IFDEF SDL13}x, y: LongInt;{$ENDIF}
  1127     {$IFDEF SDL13}x, y: LongInt;{$ENDIF}
  1044         WriteToConsole('Init SDL_image... ');
  1143         WriteToConsole('Init SDL_image... ');
  1045         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
  1144         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
  1046         WriteLnToConsole(msgOK);
  1145         WriteLnToConsole(msgOK);
  1047         // load engine icon
  1146         // load engine icon
  1048     {$IFNDEF DARWIN}
  1147     {$IFNDEF DARWIN}
  1049         ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
  1148         ico:= LoadDataImage(ptGraphics, 'hwengine', ifIgnoreCaps);
  1050         if ico = nil then
       
  1051             ico:= LoadImage(Pathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
       
  1052         if ico <> nil then
  1149         if ico <> nil then
  1053             begin
  1150             begin
  1054             SDL_WM_SetIcon(ico, 0);
  1151             SDL_WM_SetIcon(ico, 0);
  1055             SDL_FreeSurface(ico)
  1152             SDL_FreeSurface(ico)
  1056             end;
  1153             end;
  1199 {$ENDIF}
  1296 {$ENDIF}
  1200 end;
  1297 end;
  1201 
  1298 
  1202 procedure SwapBuffers; inline;
  1299 procedure SwapBuffers; inline;
  1203 begin
  1300 begin
       
  1301     if GameType = gmtRecord then
       
  1302         exit;
  1204 {$IFDEF SDL13}
  1303 {$IFDEF SDL13}
  1205     SDL_GL_SwapWindow(SDLwindow);
  1304     SDL_GL_SwapWindow(SDLwindow);
  1206 {$ELSE}
  1305 {$ELSE}
  1207     SDL_GL_SwapBuffers();
  1306     SDL_GL_SwapBuffers();
  1208 {$ENDIF}
  1307 {$ENDIF}