hedgewars/uStore.pas
branchwebgl
changeset 9521 8054d9d775fd
parent 9127 e350500c4edb
parent 9317 a04c30940d2d
child 9950 2759212a27de
equal deleted inserted replaced
9282:92af50454cf2 9521:8054d9d775fd
    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};
       
    59 procedure SetSkyColor(r, g, b: real);
    57 
    60 
    58 {$IFDEF GL2}
    61 {$IFDEF GL2}
    59 procedure UpdateModelviewProjection;
    62 procedure UpdateModelviewProjection;
    60 procedure EnableTexture(enable:Boolean);
    63 procedure EnableTexture(enable:Boolean);
    61 {$ENDIF}
    64 {$ENDIF}
    69 implementation
    72 implementation
    70 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    73 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    71     , uPhysFSLayer
    74     , uPhysFSLayer
    72     , uDebug
    75     , uDebug
    73     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    76     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    74     {$IF NOT DEFINED(SDL13) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    77     {$IF NOT DEFINED(SDL2) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    75 
    78 
    76 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
    79 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
    77 
    80 
    78 var MaxTextureSize: LongInt;
    81 var MaxTextureSize: LongInt;
    79 {$IFDEF SDL13}
    82 {$IFDEF SDL2}
    80     SDLwindow: PSDL_Window;
    83     SDLwindow: PSDL_Window;
    81     SDLGLcontext: PSDL_GLContext;
    84     SDLGLcontext: PSDL_GLContext;
    82 {$ELSE}
    85 {$ELSE}
    83     SDLPrimSurface: PSDL_Surface;
    86     SDLPrimSurface: PSDL_Surface;
    84 {$ENDIF}
    87 {$ENDIF}
   631 function LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
   634 function LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
   632 var tmpsurf: PSDL_Surface;
   635 var tmpsurf: PSDL_Surface;
   633 begin
   636 begin
   634     // check for file in user dir (never critical)
   637     // check for file in user dir (never critical)
   635     tmpsurf:= LoadImage(cPathz[path] + '/' + filename, imageFlags);
   638     tmpsurf:= LoadImage(cPathz[path] + '/' + filename, imageFlags);
   636     
   639 
   637     LoadDataImage:= tmpsurf;
   640     LoadDataImage:= tmpsurf;
   638 end;
   641 end;
   639 
   642 
   640 
   643 
   641 function LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
   644 function LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
   692         end;
   695         end;
   693 end;
   696 end;
   694 
   697 
   695 function glLoadExtension(extension : shortstring) : boolean;
   698 function glLoadExtension(extension : shortstring) : boolean;
   696 begin
   699 begin
   697 //TODO: pas2c doesn't handle {$IF (GLunit = gles11) OR DEFINED(PAS2C)}
   700 //TODO: pas2c does not handle {$IF (GLunit = gles11) OR DEFINED(PAS2C)}
   698 {$IFNDEF PAS2C}
   701 {$IFNDEF PAS2C}
   699 {$IF GLunit = gles11}
   702 {$IF GLunit = gles11}
   700     // FreePascal doesnt come with OpenGL ES 1.1 Extension headers
   703     // FreePascal doesnt come with OpenGL ES 1.1 Extension headers
   701     extension:= extension; // avoid hint
   704     extension:= extension; // avoid hint
   702     glLoadExtension:= false;
   705     glLoadExtension:= false;
   716 {$IFDEF IPHONEOS}
   719 {$IFDEF IPHONEOS}
   717     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
   720     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
   718     SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
   721     SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
   719 {$ELSE}
   722 {$ELSE}
   720     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   723     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   721 {$IFNDEF SDL13} // vsync is default in 1.3
   724 {$ENDIF}
       
   725 {$IFNDEF SDL2} // vsync is default in SDL2
   722     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0));
   726     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0));
   723 {$ENDIF}
   727 {$ENDIF}
   724 {$ENDIF}
       
   725     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); // no depth buffer
       
   726     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   728     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   727     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
   729     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
   728     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   730     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   729     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required
   731     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);         // no depth buffer
   730     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only
   732     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);         // no alpha channel
   731     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering
   733     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);       // buffer should be 16
       
   734     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // prefer hw rendering
   732 end;
   735 end;
   733 
   736 
   734 {$IFDEF GL2}
   737 {$IFDEF GL2}
   735 function CompileShader(shaderFile: string; shaderType: GLenum): GLuint;
   738 function CompileShader(shaderFile: string; shaderType: GLenum): GLuint;
   736 var
   739 var
   836 end;
   839 end;
   837 
   840 
   838 {$ENDIF}
   841 {$ENDIF}
   839 
   842 
   840 procedure SetupOpenGL;
   843 procedure SetupOpenGL;
   841 //var vendor: shortstring = '';
   844 var name: array[byte] of char;
   842 var buf: array[byte] of char;
   845     AuxBufNum: LongInt = 0;
   843 {$IFDEF PAS2C}err: GLenum;{$ENDIF}
       
   844 {$IFDEF USE_VIDEO_RECORDING}AuxBufNum: LongInt;{$ENDIF}
       
   845     tmpstr: AnsiString;
   846     tmpstr: AnsiString;
   846     tmpint: LongInt;
   847     tmpint: LongInt;
   847     tmpn: LongInt;
   848     tmpn: LongInt;
   848 begin
   849 begin
   849     buf[0]:= char(0); // avoid compiler hint
   850 
   850     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   851 {$IFDEF SDL2}
   851 
   852     name:= SDL_GetCurrentVideoDriver();
       
   853 {$ELSE}
       
   854     name:= SDL_VideoDriverName(name, sizeof(name));
       
   855 {$ENDIF}
       
   856 
       
   857     AuxBufNum:= AuxBufNum;
       
   858     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(name) + ')');
       
   859 
       
   860 {$IFDEF MOBILE}
       
   861     // TODO: this function creates an opengles1.1 context
       
   862     // un-comment below and add proper logic to support opengles2.0
       
   863     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
       
   864     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
       
   865     if SDLGLcontext = nil then
       
   866         SDLGLcontext:= SDL_GL_CreateContext(SDLwindow);
       
   867     SDLTry(SDLGLcontext <> nil, true);
       
   868     SDL_GL_SetSwapInterval(1);
       
   869 {$ENDIF}
       
   870 
       
   871     // get the max (h and v) size for textures that the gpu can support
       
   872     glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
       
   873     if MaxTextureSize <= 0 then
       
   874         begin
       
   875         MaxTextureSize:= 1024;
       
   876         AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
       
   877         end
       
   878     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
       
   879         begin
       
   880         cReducedQuality := cReducedQuality or rqNoBackground;
       
   881         AddFileLog('Texture size too small for backgrounds, disabling.');
       
   882         end;
   852 {$IFDEF WEBGL}
   883 {$IFDEF WEBGL}
   853     if OpenGLSetupedBefore then
   884     if OpenGLSetupedBefore then
   854         begin
   885         begin
   855         glViewport(0, 0, cScreenWidth, cScreenHeight);
   886         glViewport(0, 0, cScreenWidth, cScreenHeight);
   856         hglMatrixMode(MATRIX_MODELVIEW);
   887         hglMatrixMode(MATRIX_MODELVIEW);
   858         hglScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
   889         hglScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
   859         hglTranslatef(0, -cScreenHeight / 2, 0);
   890         hglTranslatef(0, -cScreenHeight / 2, 0);
   860         exit;
   891         exit;
   861         end
   892         end
   862     OpenGLSetupedBefore := true;
   893     OpenGLSetupedBefore := true;
   863 {$ENDIF}
   894 {$ELSE}
   864 
   895     // everyone loves debugging
   865 {$IFDEF SDL13}
       
   866     // this function creates an opengles1.1 context by default on mobile devices
       
   867     // unless you un-comment this two attributes
       
   868     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
       
   869     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
       
   870     if SDLGLcontext = nil then
       
   871         SDLGLcontext:= SDL_GL_CreateContext(SDLwindow);
       
   872     SDLTry(SDLGLcontext <> nil, true);
       
   873     SDL_GL_SetSwapInterval(1);
       
   874 {$ENDIF}
       
   875 
       
   876     // get the max (horizontal and vertical) size for textures that the gpu can support
       
   877     glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
       
   878     if MaxTextureSize <= 0 then
       
   879         begin
       
   880         MaxTextureSize:= 1024;
       
   881         AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
       
   882         end
       
   883     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
       
   884         begin
       
   885         cReducedQuality := cReducedQuality or rqNoBackground;
       
   886         AddFileLog('Texture size too small for backgrounds, disabling.');
       
   887         end;
       
   888 
       
   889 (*
       
   890     // find out which gpu we are using (for extension compatibility maybe?)
   896     // find out which gpu we are using (for extension compatibility maybe?)
   891 {$IFDEF IPHONEOS}
       
   892     vendor:= vendor; // avoid hint
       
   893     cGPUVendor:= gvApple;
       
   894 {$ELSE}
       
   895     vendor:= LowerCase(shortstring(pchar(glGetString(GL_VENDOR))));
       
   896     if StrPos(Str2PChar(vendor), Str2PChar('nvidia')) <> nil then
       
   897         cGPUVendor:= gvNVIDIA
       
   898     else if StrPos(Str2PChar(vendor), Str2PChar('intel')) <> nil then
       
   899         cGPUVendor:= gvATI
       
   900     else if StrPos(Str2PChar(vendor), Str2PChar('ati')) <> nil then
       
   901         cGPUVendor:= gvIntel
       
   902     else
       
   903         AddFileLog('OpenGL Warning - unknown hardware vendor; please report');
       
   904 {$ENDIF}
       
   905 //SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two');
       
   906 *)
       
   907 
       
   908     // everyone love debugging
       
   909     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
   897     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
   910     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   898     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   911     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   899     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   912     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   900     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   913 {$IFDEF USE_VIDEO_RECORDING}
   901 {$IFDEF USE_VIDEO_RECORDING}
   914     glGetIntegerv(GL_AUX_BUFFERS, @AuxBufNum);
   902     glGetIntegerv(GL_AUX_BUFFERS, @AuxBufNum);
   915     AddFileLog('  |----- Number of auxiliary buffers: ' + inttostr(AuxBufNum));
   903     AddFileLog('  |----- Number of auxiliary buffers: ' + inttostr(AuxBufNum));
   916 {$ENDIF}
   904 {$ENDIF}
   917 {$IFDEF PAS2C}
   905 {$IFNDEF PAS2C}
   918 
       
   919     // doesn't seem to print >256 chars
       
   920     AddFileLogRaw(PChar(glGetString(GL_EXTENSIONS)));
       
   921 {$ELSE}
       
   922     AddFileLog('  \----- Extensions: ');
   906     AddFileLog('  \----- Extensions: ');
   923 
   907 
   924     // fetch extentions and store them in string
   908     // fetch extentions and store them in string
   925     tmpstr := StrPas(PChar(glGetString(GL_EXTENSIONS)));
   909     tmpstr := StrPas(PChar(glGetString(GL_EXTENSIONS)));
   926     tmpn := WordCount(tmpstr, [' ']);
   910     tmpn := WordCount(tmpstr, [' ']);
   936             ExtractWord(tmpint+2, tmpstr, [' '])
   920             ExtractWord(tmpint+2, tmpstr, [' '])
   937         ));
   921         ));
   938         tmpint := tmpint + 3;
   922         tmpint := tmpint + 3;
   939     end;
   923     end;
   940     until (tmpint > tmpn);
   924     until (tmpint > tmpn);
       
   925 {$ENDIF}
   941 {$ENDIF}
   926 {$ENDIF}
   942     AddFileLog('');
   927     AddFileLog('');
   943 
   928 
   944     defaultFrame:= 0;
   929     defaultFrame:= 0;
   945 {$IFDEF USE_VIDEO_RECORDING}
   930 {$IFDEF USE_VIDEO_RECORDING}
  1432 FreeTexture(WeaponTooltipTex);
  1417 FreeTexture(WeaponTooltipTex);
  1433 WeaponTooltipTex:= nil
  1418 WeaponTooltipTex:= nil
  1434 end;
  1419 end;
  1435 
  1420 
  1436 {$IFDEF USE_VIDEO_RECORDING}
  1421 {$IFDEF USE_VIDEO_RECORDING}
  1437 {$IFDEF SDL13}
  1422 {$IFDEF SDL2}
  1438 procedure InitOffscreenOpenGL;
  1423 procedure InitOffscreenOpenGL;
  1439 begin
  1424 begin
  1440     // create hidden window
  1425     // create hidden window
  1441     SDLwindow:= SDL_CreateWindow('hedgewars (you don''t see this)',
  1426     SDLwindow:= SDL_CreateWindow('hedgewars video rendering (SDL2 hidden window)',
  1442                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
  1427                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
  1443                                  cScreenWidth, cScreenHeight,
  1428                                  cScreenWidth, cScreenHeight,
  1444                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
  1429                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
  1445     SDLTry(SDLwindow <> nil, true);
  1430     SDLTry(SDLwindow <> nil, true);
  1446     SetupOpenGL();
  1431     SetupOpenGL();
  1452 begin
  1437 begin
  1453     ArgCount:= 1;
  1438     ArgCount:= 1;
  1454     PrgName:= 'hwengine';
  1439     PrgName:= 'hwengine';
  1455     glutInit(@ArgCount, @PrgName);
  1440     glutInit(@ArgCount, @PrgName);
  1456     glutInitWindowSize(cScreenWidth, cScreenHeight);
  1441     glutInitWindowSize(cScreenWidth, cScreenHeight);
  1457     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
  1442     // we do not need a window, but without this call OpenGL will not initialize
       
  1443     glutCreateWindow('hedgewars video rendering (glut hidden window)');
  1458     glutHideWindow();
  1444     glutHideWindow();
  1459     glutDisplayFunc(@SwapBuffers); // we don't need a callback, but it's required for GLUT3
  1445     // we do not need to set this callback, but it is required for GLUT3 compat
       
  1446     glutDisplayFunc(@SwapBuffers);
  1460     SetupOpenGL();
  1447     SetupOpenGL();
  1461 end;
  1448 end;
  1462 {$ENDIF} // SDL13
  1449 {$ENDIF} // SDL2
  1463 {$ENDIF} // USE_VIDEO_RECORDING
  1450 {$ENDIF} // USE_VIDEO_RECORDING
  1464 
  1451 
  1465 procedure chFullScr(var s: shortstring);
  1452 procedure chFullScr(var s: shortstring);
  1466 var flags: Longword = 0;
  1453 var flags: Longword = 0;
  1467     reinit: boolean = false;
  1454     reinit: boolean = false;
  1468     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
  1455     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
  1469     {$IFDEF SDL13}x, y: LongInt;{$ENDIF}
  1456     {$IFDEF SDL2}x, y: LongInt;{$ENDIF}
  1470 begin
  1457 begin
  1471     if cOnlyStats then
  1458     if cOnlyStats then
  1472         begin
  1459         begin
  1473         MaxTextureSize:= 1024;
  1460         MaxTextureSize:= 1024;
  1474         exit
  1461         exit
  1475         end;
  1462         end;
  1476     if Length(s) = 0 then
  1463     if Length(s) = 0 then
  1477          cFullScreen:= (not cFullScreen)
  1464          cFullScreen:= (not cFullScreen)
  1478     else cFullScreen:= s = '1';
  1465     else cFullScreen:= s = '1';
  1479     
  1466 
  1480     if cFullScreen then
  1467     if cFullScreen then
  1481         begin
  1468         begin
  1482         cScreenWidth:= cFullscreenWidth;
  1469         cScreenWidth:= cFullscreenWidth;
  1483         cScreenHeight:= cFullscreenHeight;
  1470         cScreenHeight:= cFullscreenHeight;
  1484         end
  1471         end
  1487         cScreenWidth:= cWindowedWidth;
  1474         cScreenWidth:= cWindowedWidth;
  1488         cScreenHeight:= cWindowedHeight;
  1475         cScreenHeight:= cWindowedHeight;
  1489         end;
  1476         end;
  1490 
  1477 
  1491     AddFileLog('Preparing to change video parameters...');
  1478     AddFileLog('Preparing to change video parameters...');
  1492 {$IFDEF SDL13}
  1479 {$IFDEF SDL2}
  1493     if SDLwindow = nil then
  1480     if SDLwindow = nil then
  1494 {$ELSE}
  1481 {$ELSE}
  1495     if SDLPrimSurface = nil then
  1482     if SDLPrimSurface = nil then
  1496 {$ENDIF}
  1483 {$ENDIF}
  1497         begin
  1484         begin
  1498         // set window title
  1485         // set window title
  1499         {$IFNDEF SDL13}SDL_WM_SetCaption(_P'Hedgewars', nil);{$ENDIF}
  1486     {$IFNDEF SDL2}
       
  1487         SDL_WM_SetCaption(_P'Hedgewars', nil);
       
  1488     {$ENDIF}
  1500         WriteToConsole('Init SDL_image... ');
  1489         WriteToConsole('Init SDL_image... ');
  1501         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
  1490         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
  1502         WriteLnToConsole(msgOK);
  1491         WriteLnToConsole(msgOK);
  1503         // load engine icon
  1492         // load engine icon
  1504     {$IFNDEF DARWIN}
  1493     {$IFNDEF DARWIN}
  1523         glTranslatef(0, -cScreenHeight / 2, 0);
  1512         glTranslatef(0, -cScreenHeight / 2, 0);
  1524         glViewport(0, 0, cScreenWidth, cScreenHeight);
  1513         glViewport(0, 0, cScreenWidth, cScreenHeight);
  1525         exit;
  1514         exit;
  1526 {$ELSE}
  1515 {$ELSE}
  1527         SetScale(cDefaultZoomLevel);
  1516         SetScale(cDefaultZoomLevel);
  1528      {$IFDEF USE_CONTEXT_RESTORE}
  1517     {$IFDEF USE_CONTEXT_RESTORE}
  1529         reinit:= true;
  1518         reinit:= true;
  1530         StoreRelease(true);
  1519         StoreRelease(true);
  1531         ResetLand;
  1520         ResetLand;
  1532         ResetWorldTex;
  1521         ResetWorldTex;
  1533         //uTextures.freeModule; //DEBUG ONLY
  1522         //uTextures.freeModule; //DEBUG ONLY
  1534     {$ENDIF}
  1523     {$ENDIF}
  1535         AddFileLog('Freeing old primary surface...');
  1524         AddFileLog('Freeing old primary surface...');
  1536     {$IFNDEF SDL13}
  1525     {$IFNDEF SDL2}
  1537     {$IFNDEF WEBGL}
  1526     {$IFNDEF WEBGL}
  1538         SDL_FreeSurface(SDLPrimSurface);
  1527         SDL_FreeSurface(SDLPrimSurface);
  1539         SDLPrimSurface:= nil;
  1528         SDLPrimSurface:= nil;
  1540     {$ENDIF}
  1529     {$ENDIF}
  1541     {$ENDIF}
  1530     {$ENDIF}
  1542 {$ENDIF}
  1531 {$ENDIF}
  1543         end;
  1532         end;
  1544 
  1533 
  1545     // these attributes must be set up before creating the sdl window
  1534     // these attributes must be set up before creating the sdl window
  1546 {$IFNDEF WIN32}
  1535 {$IFNDEF WIN32}
  1547 (* On a large number of testers machines, SDL default to software rendering when opengl attributes were set.
  1536 (* On a large number of testers machines, SDL default to software rendering
  1548    These attributes were "set" after CreateWindow in .15, which probably did nothing.
  1537    when opengl attributes were set. These attributes were "set" after
  1549    IMO we should rely on the gl_config defaults from SDL, and use SDL_GL_GetAttribute to possibly post warnings if any
  1538    CreateWindow in .15, which probably did nothing.
  1550    bad values are set.  *)
  1539    IMO we should rely on the gl_config defaults from SDL, and use
       
  1540    SDL_GL_GetAttribute to possibly post warnings if any bad values are set.
       
  1541  *)
  1551     SetupOpenGLAttributes();
  1542     SetupOpenGLAttributes();
  1552 {$ENDIF}
  1543 {$ENDIF}
  1553 {$IFDEF SDL13}
  1544 {$IFDEF SDL2}
  1554     // these values in x and y make the window appear in the center
  1545     // these values in x and y make the window appear in the center
  1555     x:= SDL_WINDOWPOS_CENTERED_MASK;
  1546     x:= SDL_WINDOWPOS_CENTERED_MASK;
  1556     y:= SDL_WINDOWPOS_CENTERED_MASK;
  1547     y:= SDL_WINDOWPOS_CENTERED_MASK;
  1557     // SDL_WINDOW_RESIZABLE makes the window respond to rotation events on mobile devices
  1548     // SDL_WINDOW_RESIZABLE makes the window resizable and
       
  1549     //  respond to rotation events on mobile devices
  1558     flags:= SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN or SDL_WINDOW_RESIZABLE;
  1550     flags:= SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN or SDL_WINDOW_RESIZABLE;
  1559 
  1551 
  1560     {$IFDEF MOBILE}
  1552     {$IFDEF MOBILE}
  1561     if isPhone() then
  1553     if isPhone() then
  1562         SDL_SetHint('SDL_IOS_ORIENTATIONS','LandscapeLeft LandscapeRight');
  1554         SDL_SetHint('SDL_IOS_ORIENTATIONS','LandscapeLeft LandscapeRight');
  1563     // no need for borders on mobile devices
  1555     // no need for borders on mobile devices
  1564     flags:= flags or SDL_WINDOW_BORDERLESS;
  1556     flags:= flags or SDL_WINDOW_BORDERLESS;
  1565     {$ENDIF}
  1557     {$ENDIF}
  1566 
  1558 
       
  1559     if cFullScreen then
       
  1560         flags:= flags or SDL_WINDOW_FULLSCREEN;
       
  1561 
  1567     if SDLwindow = nil then
  1562     if SDLwindow = nil then
  1568         if cFullScreen then
  1563         SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
  1569             SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags or SDL_WINDOW_FULLSCREEN)
       
  1570         else
       
  1571             begin
       
  1572             SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
       
  1573             end;
       
  1574     SDLTry(SDLwindow <> nil, true);
  1564     SDLTry(SDLwindow <> nil, true);
  1575 {$ELSE}
  1565 {$ELSE}
  1576     flags:= SDL_OPENGL or SDL_RESIZABLE;
  1566     flags:= SDL_OPENGL or SDL_RESIZABLE;
  1577     if cFullScreen then
  1567     if cFullScreen then
  1578         begin
       
  1579         flags:= flags or SDL_FULLSCREEN;
  1568         flags:= flags or SDL_FULLSCREEN;
  1580         end;
       
  1581     if not cOnlyStats then
  1569     if not cOnlyStats then
  1582         begin
  1570         begin
  1583     {$IFDEF WIN32}
  1571     {$IFDEF WIN32}
  1584         s:= SDL_getenv('SDL_VIDEO_CENTERED');
  1572         s:= SDL_getenv('SDL_VIDEO_CENTERED');
  1585         SDL_putenv('SDL_VIDEO_CENTERED=1');
  1573         SDL_putenv('SDL_VIDEO_CENTERED=1');
  1586     {$ENDIF}
  1574     {$ENDIF}
  1587         SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, 0, flags);
  1575         SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, 0, flags);
  1588         SDLTry(SDLPrimSurface <> nil, true);
  1576         SDLTry(SDLPrimSurface <> nil, true);
  1589     {$IFDEF WIN32}SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));{$ENDIF}
  1577     {$IFDEF WIN32}
       
  1578         SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));
       
  1579     {$ENDIF}
  1590         end;
  1580         end;
  1591 {$ENDIF}
  1581 {$ENDIF}
  1592 
  1582 
  1593     SetupOpenGL();
  1583     SetupOpenGL();
  1594 
  1584 
  1595     if reinit then
  1585     if reinit then
  1596         begin
  1586         begin
  1597         // clean the window from any previous content
  1587         // clean the window from any previous content
  1598         glClear(GL_COLOR_BUFFER_BIT);
  1588         glClear(GL_COLOR_BUFFER_BIT);
  1599         if SuddenDeathDmg then
  1589         if SuddenDeathDmg then
  1600             glClearColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255, 0.99)
  1590             SetSkyColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255)
  1601         else if ((cReducedQuality and rqNoBackground) = 0) then
  1591         else if ((cReducedQuality and rqNoBackground) = 0) then
  1602             glClearColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255, 0.99)
  1592             SetSkyColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255)
  1603         else
  1593         else
  1604             glClearColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255, 0.99);
  1594             SetSkyColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255);
  1605 
  1595 
  1606         // reload everything we had before
  1596         // reload everything we had before
  1607         ReloadCaptions(false);
  1597         ReloadCaptions(false);
  1608         ReloadLines;
  1598         ReloadLines;
  1609         StoreLoad(true);
  1599         StoreLoad(true);
  1610         // redraw all land
  1600         // redraw all land
  1611         UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false);
  1601         UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false);
  1612         end;
  1602         end;
  1613 end;
  1603 end;
  1614 
  1604 
       
  1605 {$IFDEF SDL2}
       
  1606 // for sdl1.2 we directly call SDL_WarpMouse()
       
  1607 // for sdl2 we provide a SDL_WarpMouse() which just calls this function
       
  1608 // this has the advantage of reducing 'uses' and 'ifdef' statements
       
  1609 // (SDLwindow is a private member of this module)
       
  1610 procedure WarpMouse(x, y: Word); inline;
       
  1611 begin
       
  1612     SDL_WarpMouseInWindow(SDLwindow, x, y);
       
  1613 end;
       
  1614 {$ENDIF}
       
  1615 
       
  1616 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
       
  1617 begin
       
  1618     if GameType = gmtRecord then
       
  1619         exit;
       
  1620 {$IFDEF SDL2}
       
  1621     SDL_GL_SwapWindow(SDLwindow);
       
  1622 {$ELSE}
       
  1623     SDL_GL_SwapBuffers();
       
  1624 {$ENDIF}
       
  1625 end;
       
  1626 
       
  1627 procedure SetSkyColor(r, g, b: real);
       
  1628 begin
       
  1629     glClearColor(r, g, b, 0.99)
       
  1630 end;
       
  1631 
  1615 procedure initModule;
  1632 procedure initModule;
  1616 var ai: TAmmoType;
  1633 var ai: TAmmoType;
  1617     i: LongInt;
  1634     i: LongInt;
  1618 begin
  1635 begin
  1619     RegisterVariable('fullscr', @chFullScr, true);
  1636     RegisterVariable('fullscr', @chFullScr, true);
  1633         Ammoz[ai].NameTex := nil;
  1650         Ammoz[ai].NameTex := nil;
  1634     end;
  1651     end;
  1635     // init all count texture pointers
  1652     // init all count texture pointers
  1636     for i:= Low(CountTexz) to High(CountTexz) do
  1653     for i:= Low(CountTexz) to High(CountTexz) do
  1637         CountTexz[i] := nil;
  1654         CountTexz[i] := nil;
  1638 {$IFDEF SDL13}
  1655 {$IFDEF SDL2}
  1639     SDLwindow:= nil;
  1656     SDLwindow:= nil;
  1640     SDLGLcontext:= nil;
  1657     SDLGLcontext:= nil;
  1641 {$ELSE}
  1658 {$ELSE}
  1642     SDLPrimSurface:= nil;
  1659     SDLPrimSurface:= nil;
  1643 {$ENDIF}
  1660 {$ENDIF}
  1652     glDeleteBuffers(1, @tBuffer);
  1669     glDeleteBuffers(1, @tBuffer);
  1653     glDeleteBuffers(1, @cBuffer);
  1670     glDeleteBuffers(1, @cBuffer);
  1654 {$ENDIF}
  1671 {$ENDIF}
  1655     StoreRelease(false);
  1672     StoreRelease(false);
  1656     TTF_Quit();
  1673     TTF_Quit();
  1657 {$IFDEF SDL13}
  1674 {$IFDEF SDL2}
  1658     SDL_GL_DeleteContext(SDLGLcontext);
  1675     SDL_GL_DeleteContext(SDLGLcontext);
  1659     SDL_DestroyWindow(SDLwindow);
  1676     SDL_DestroyWindow(SDLwindow);
  1660 {$ENDIF}
  1677 {$ENDIF}
  1661     SDL_Quit();
  1678     SDL_Quit();
  1662 end;
  1679 end;
  1663 
       
  1664 procedure WarpMouse(x, y: Word); inline;
       
  1665 begin
       
  1666 {$IFDEF SDL13}
       
  1667     SDL_WarpMouseInWindow(SDLwindow, x, y);
       
  1668 {$ELSE}
       
  1669     x:= x; y:= y; // avoid hints
       
  1670 {$ENDIF}
       
  1671 end;
       
  1672 
       
  1673 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
       
  1674 begin
       
  1675     if GameType = gmtRecord then
       
  1676         exit;
       
  1677 {$IFDEF SDL13}
       
  1678     SDL_GL_SwapWindow(SDLwindow);
       
  1679 {$ELSE}
       
  1680     SDL_GL_SwapBuffers();
       
  1681 {$ENDIF}
       
  1682 end;
       
  1683 
       
  1684 end.
  1680 end.