hedgewars/uStore.pas
changeset 10015 4feced261c68
parent 10009 88929358d2e1
parent 9954 bf51bc7e2808
child 10102 bab8a3cccdf8
equal deleted inserted replaced
10014:56d2f2d5aad8 10015:4feced261c68
    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 StrUtils, 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);
    57 procedure WarpMouse(x, y: Word); inline;
    57 procedure WarpMouse(x, y: Word); inline;
    58 {$ENDIF}
    58 {$ENDIF}
    59 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
    59 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
    60 procedure SetSkyColor(r, g, b: real);
    60 procedure SetSkyColor(r, g, b: real);
    61 
    61 
       
    62 {$IFDEF GL2}
       
    63 procedure UpdateModelviewProjection;
       
    64 procedure EnableTexture(enable:Boolean);
       
    65 {$ENDIF}
       
    66 
       
    67 procedure SetTexCoordPointer(p: Pointer;n: Integer);
       
    68 procedure SetVertexPointer(p: Pointer;n: Integer);
       
    69 procedure SetColorPointer(p: Pointer;n: Integer);
       
    70 procedure BeginWater;
       
    71 procedure EndWater;
       
    72 
    62 implementation
    73 implementation
    63 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    74 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils,
    64     , uPhysFSLayer
    75      uCommands, uPhysFSLayer, uDebug
    65     , uDebug
    76     {$IFDEF GL2}, uMatrix{$ENDIF}
    66     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    77     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    67     {$IF NOT DEFINED(SDL2) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    78     {$IF NOT DEFINED(SDL2) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    68 
       
    69 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
       
    70 
    79 
    71 var MaxTextureSize: LongInt;
    80 var MaxTextureSize: LongInt;
    72 {$IFDEF SDL2}
    81 {$IFDEF SDL2}
    73     SDLwindow: PSDL_Window;
    82     SDLwindow: PSDL_Window;
    74     SDLGLcontext: PSDL_GLContext;
    83     SDLGLcontext: PSDL_GLContext;
    76     SDLPrimSurface: PSDL_Surface;
    85     SDLPrimSurface: PSDL_Surface;
    77 {$ENDIF}
    86 {$ENDIF}
    78     squaresize : LongInt;
    87     squaresize : LongInt;
    79     numsquares : LongInt;
    88     numsquares : LongInt;
    80     ProgrTex: PTexture;
    89     ProgrTex: PTexture;
       
    90 
       
    91 {$IFDEF GL2}
       
    92     shaderMain: GLuint;
       
    93     shaderWater: GLuint;
       
    94 
       
    95     // attributes
       
    96 {$ENDIF}
    81 
    97 
    82 const
    98 const
    83     cHHFileName = 'Hedgehog';
    99     cHHFileName = 'Hedgehog';
    84     cCHFileName = 'Crosshair';
   100     cCHFileName = 'Crosshair';
    85 
   101 
   157 begin
   173 begin
   158     if cOnlyStats then exit;
   174     if cOnlyStats then exit;
   159 r.x:= 0;
   175 r.x:= 0;
   160 r.y:= 0;
   176 r.y:= 0;
   161 drY:= - 4;
   177 drY:= - 4;
       
   178 {$IFNDEF PAS2C}
   162 DecodeDate(Date, year, month, md);
   179 DecodeDate(Date, year, month, md);
       
   180 {$ELSE}
       
   181 year:= 0;
       
   182 month:= 0;
       
   183 md:= 0;
       
   184 {$ENDIF}
   163 for t:= 0 to Pred(TeamsCount) do
   185 for t:= 0 to Pred(TeamsCount) do
   164     with TeamsArray[t]^ do
   186     with TeamsArray[t]^ do
   165         begin
   187         begin
   166         NameTagTex:= RenderStringTexLim(TeamName, Clan^.Color, Font, cTeamHealthWidth);
   188         NameTagTex:= RenderStringTexLim(TeamName, Clan^.Color, Font, cTeamHealthWidth);
   167         if length(Owner) > 0 then
   189         if length(Owner) > 0 then
   247                         else if (month = 12) and ((md = 24) or (md = 25) or (md = 26)) then
   269                         else if (month = 12) and ((md = 24) or (md = 25) or (md = 26)) then
   248                             Hat := 'Santa'       // Christmas Eve/Christmas/Boxing Day
   270                             Hat := 'Santa'       // Christmas Eve/Christmas/Boxing Day
   249                         else if (month = 10) and (md = 31) then
   271                         else if (month = 10) and (md = 31) then
   250                             Hat := 'fr_pumpkin'; // Halloween/Hedgewars' birthday
   272                             Hat := 'fr_pumpkin'; // Halloween/Hedgewars' birthday
   251                         end;
   273                         end;
   252                     
   274 
   253                     if Hat <> 'NoHat' then
   275                     if Hat <> 'NoHat' then
   254                         begin
   276                         begin
   255                         if (Length(Hat) > 39) and (Copy(Hat,1,8) = 'Reserved') and (Copy(Hat,9,32) = PlayerHash) then
   277                         if (Length(Hat) > 39) and (Copy(Hat,1,8) = 'Reserved') and (Copy(Hat,9,32) = PlayerHash) then
   256                             LoadHedgehogHat(Hedgehogs[i], 'Reserved/' + Copy(Hat,9,Length(Hat)-8))
   278                             LoadHedgehogHat(Hedgehogs[i], 'Reserved/' + Copy(Hat,9,Length(Hat)-8))
   257                         else
   279                         else
   448     end;
   470     end;
   449 
   471 
   450 if not reload then
   472 if not reload then
   451     AddProgress;
   473     AddProgress;
   452 IMG_Quit();
   474 IMG_Quit();
   453 end;
   475 
   454 
   476 end;
       
   477 
       
   478 {$IFNDEF PAS2C}
   455 {$IF DEFINED(USE_S3D_RENDERING) OR DEFINED(USE_VIDEO_RECORDING)}
   479 {$IF DEFINED(USE_S3D_RENDERING) OR DEFINED(USE_VIDEO_RECORDING)}
   456 procedure CreateFramebuffer(var frame, depth, tex: GLuint);
   480 procedure CreateFramebuffer(var frame, depth, tex: GLuint);
   457 begin
   481 begin
   458     glGenFramebuffersEXT(1, @frame);
   482     glGenFramebuffersEXT(1, @frame);
   459     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frame);
   483     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frame);
   473 begin
   497 begin
   474     glDeleteTextures(1, @tex);
   498     glDeleteTextures(1, @tex);
   475     glDeleteRenderbuffersEXT(1, @depth);
   499     glDeleteRenderbuffersEXT(1, @depth);
   476     glDeleteFramebuffersEXT(1, @frame);
   500     glDeleteFramebuffersEXT(1, @frame);
   477 end;
   501 end;
       
   502 {$ENDIF}
   478 {$ENDIF}
   503 {$ENDIF}
   479 
   504 
   480 procedure StoreRelease(reload: boolean);
   505 procedure StoreRelease(reload: boolean);
   481 var ii: TSprite;
   506 var ii: TSprite;
   482     ai: TAmmoType;
   507     ai: TAmmoType;
   537                 FreeAndNilTexture(TeamsArray[t]^.Hedgehogs[i].HealthTagTex);
   562                 FreeAndNilTexture(TeamsArray[t]^.Hedgehogs[i].HealthTagTex);
   538                 FreeAndNilTexture(TeamsArray[t]^.Hedgehogs[i].HatTex);
   563                 FreeAndNilTexture(TeamsArray[t]^.Hedgehogs[i].HatTex);
   539                 end;
   564                 end;
   540             end;
   565             end;
   541         end;
   566         end;
       
   567 {$IFNDEF PAS2C}
   542 {$IFDEF USE_VIDEO_RECORDING}
   568 {$IFDEF USE_VIDEO_RECORDING}
   543     if defaultFrame <> 0 then
   569     if defaultFrame <> 0 then
   544         DeleteFramebuffer(defaultFrame, depthv, texv);
   570         DeleteFramebuffer(defaultFrame, depthv, texv);
   545 {$ENDIF}
   571 {$ENDIF}
   546 {$IFDEF USE_S3D_RENDERING}
   572 {$IFDEF USE_S3D_RENDERING}
   547     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) then
   573     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) then
   548         begin
   574         begin
   549         DeleteFramebuffer(framel, depthl, texl);
   575         DeleteFramebuffer(framel, depthl, texl);
   550         DeleteFramebuffer(framer, depthr, texr);
   576         DeleteFramebuffer(framer, depthr, texr);
   551         end
   577         end
       
   578 {$ENDIF}
   552 {$ENDIF}
   579 {$ENDIF}
   553 end;
   580 end;
   554 
   581 
   555 
   582 
   556 procedure RenderHealth(var Hedgehog: THedgehog);
   583 procedure RenderHealth(var Hedgehog: THedgehog);
   665         end;
   692         end;
   666 end;
   693 end;
   667 
   694 
   668 function glLoadExtension(extension : shortstring) : boolean;
   695 function glLoadExtension(extension : shortstring) : boolean;
   669 begin
   696 begin
       
   697 //TODO: pas2c does not handle {$IF (GLunit = gles11) OR DEFINED(PAS2C)}
       
   698 {$IFNDEF PAS2C}
   670 {$IF GLunit = gles11}
   699 {$IF GLunit = gles11}
   671     // FreePascal doesnt come with OpenGL ES 1.1 Extension headers
   700     // FreePascal doesnt come with OpenGL ES 1.1 Extension headers
   672     extension:= extension; // avoid hint
   701     extension:= extension; // avoid hint
   673     glLoadExtension:= false;
   702     glLoadExtension:= false;
   674     AddFileLog('OpenGL - "' + extension + '" skipped')
   703     AddFileLog('OpenGL - "' + extension + '" skipped')
   676     glLoadExtension:= glext_LoadExtension(extension);
   705     glLoadExtension:= glext_LoadExtension(extension);
   677     if glLoadExtension then
   706     if glLoadExtension then
   678         AddFileLog('OpenGL - "' + extension + '" loaded')
   707         AddFileLog('OpenGL - "' + extension + '" loaded')
   679     else
   708     else
   680         AddFileLog('OpenGL - "' + extension + '" failed to load');
   709         AddFileLog('OpenGL - "' + extension + '" failed to load');
       
   710 {$ENDIF}
   681 {$ENDIF}
   711 {$ENDIF}
   682 end;
   712 end;
   683 
   713 
   684 procedure SetupOpenGLAttributes;
   714 procedure SetupOpenGLAttributes;
   685 begin
   715 begin
   699     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);         // no alpha channel
   729     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);         // no alpha channel
   700     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);       // buffer should be 16
   730     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);       // buffer should be 16
   701     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // prefer hw rendering
   731     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // prefer hw rendering
   702 end;
   732 end;
   703 
   733 
       
   734 {$IFDEF GL2}
       
   735 function CompileShader(shaderFile: string; shaderType: GLenum): GLuint;
       
   736 var
       
   737     shader: GLuint;
       
   738     f: Textfile;
       
   739     source, line: AnsiString;
       
   740     sourceA: Pchar;
       
   741     lengthA: GLint;
       
   742     compileResult: GLint;
       
   743     logLength: GLint;
       
   744     log: PChar;
       
   745 begin
       
   746     Assign(f, PathPrefix + cPathz[ptShaders] + '/' + shaderFile);
       
   747     filemode:= 0; // readonly
       
   748     Reset(f);
       
   749     if IOResult <> 0 then
       
   750     begin
       
   751         AddFileLog('Unable to load ' + shaderFile);
       
   752         halt(-1);
       
   753     end;
       
   754 
       
   755     source:='';
       
   756     while not eof(f) do
       
   757     begin
       
   758         ReadLn(f, line);
       
   759         source:= source + line + #10;
       
   760     end;
       
   761 
       
   762     Close(f);
       
   763 
       
   764     WriteLnToConsole('Compiling shader: ' + PathPrefix + cPathz[ptShaders] + '/' + shaderFile);
       
   765 
       
   766     sourceA:=PChar(source);
       
   767     lengthA:=Length(source);
       
   768 
       
   769     shader:=glCreateShader(shaderType);
       
   770     glShaderSource(shader, 1, @sourceA, @lengthA);
       
   771     glCompileShader(shader);
       
   772     glGetShaderiv(shader, GL_COMPILE_STATUS, @compileResult);
       
   773     glGetShaderiv(shader, GL_INFO_LOG_LENGTH, @logLength);
       
   774 
       
   775     if logLength > 1 then
       
   776     begin
       
   777         log := GetMem(logLength);
       
   778         glGetShaderInfoLog(shader, logLength, nil, log);
       
   779         WriteLnToConsole('========== Compiler log  ==========');
       
   780         WriteLnToConsole(shortstring(log));
       
   781         WriteLnToConsole('===================================');
       
   782         FreeMem(log, logLength);
       
   783     end;
       
   784 
       
   785     if compileResult <> GL_TRUE then
       
   786     begin
       
   787         WriteLnToConsole('Shader compilation failed, halting');
       
   788         halt(-1);
       
   789     end;
       
   790 
       
   791     CompileShader:= shader;
       
   792 end;
       
   793 
       
   794 function CompileProgram(shaderName: string): GLuint;
       
   795 var
       
   796     program_: GLuint;
       
   797     vs, fs: GLuint;
       
   798     linkResult: GLint;
       
   799     logLength: GLint;
       
   800     log: PChar;
       
   801 begin
       
   802     program_:= glCreateProgram();
       
   803     vs:= CompileShader(shaderName + '.vs', GL_VERTEX_SHADER);
       
   804     fs:= CompileShader(shaderName + '.fs', GL_FRAGMENT_SHADER);
       
   805     glAttachShader(program_, vs);
       
   806     glAttachShader(program_, fs);
       
   807 
       
   808     glBindAttribLocation(program_, aVertex, PChar('vertex'));
       
   809     glBindAttribLocation(program_, aTexCoord, PChar('texcoord'));
       
   810     glBindAttribLocation(program_, aColor, PChar('color'));
       
   811 
       
   812     glLinkProgram(program_);
       
   813     glDeleteShader(vs);
       
   814     glDeleteShader(fs);
       
   815 
       
   816     glGetProgramiv(program_, GL_LINK_STATUS, @linkResult);
       
   817     glGetProgramiv(program_, GL_INFO_LOG_LENGTH, @logLength);
       
   818 
       
   819     if logLength > 1 then
       
   820     begin
       
   821         log := GetMem(logLength);
       
   822         glGetProgramInfoLog(program_, logLength, nil, log);
       
   823         WriteLnToConsole('========== Compiler log  ==========');
       
   824         WriteLnToConsole(shortstring(log));
       
   825         WriteLnToConsole('===================================');
       
   826         FreeMem(log, logLength);
       
   827     end;
       
   828 
       
   829     if linkResult <> GL_TRUE then
       
   830     begin
       
   831         WriteLnToConsole('Linking program failed, halting');
       
   832         halt(-1);
       
   833     end;
       
   834 
       
   835     CompileProgram:= program_;
       
   836 end;
       
   837 
       
   838 {$ENDIF}
       
   839 
   704 procedure SetupOpenGL;
   840 procedure SetupOpenGL;
   705 var buf: array[byte] of char;
   841 var buf: array[byte] of char;
   706     AuxBufNum: LongInt = 0;
   842     AuxBufNum: LongInt = 0;
   707     tmpstr: AnsiString;
   843     tmpstr: AnsiString;
   708     tmpint: LongInt;
   844     tmpint: LongInt;
   709     tmpn: LongInt;
   845     tmpn: LongInt;
   710 begin
   846 begin
       
   847 
   711 {$IFDEF SDL2}
   848 {$IFDEF SDL2}
   712     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_GetCurrentVideoDriver()) + ')');
   849     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_GetCurrentVideoDriver()) + ')');
   713 {$ELSE}
   850 {$ELSE}
   714     buf[0]:= char(0); // avoid compiler hint
   851     buf[0]:= char(0); // avoid compiler hint
   715     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   852     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   738     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
   875     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
   739         begin
   876         begin
   740         cReducedQuality := cReducedQuality or rqNoBackground;
   877         cReducedQuality := cReducedQuality or rqNoBackground;
   741         AddFileLog('Texture size too small for backgrounds, disabling.');
   878         AddFileLog('Texture size too small for backgrounds, disabling.');
   742         end;
   879         end;
   743 
       
   744     // everyone loves debugging
   880     // everyone loves debugging
       
   881     // find out which gpu we are using (for extension compatibility maybe?)
   745     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
   882     AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER))));
   746     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   883     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   747     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   884     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   748     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   885     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   749 {$IFDEF USE_VIDEO_RECORDING}
   886 {$IFDEF USE_VIDEO_RECORDING}
   750     glGetIntegerv(GL_AUX_BUFFERS, @AuxBufNum);
   887     glGetIntegerv(GL_AUX_BUFFERS, @AuxBufNum);
   751     AddFileLog('  |----- Number of auxiliary buffers: ' + inttostr(AuxBufNum));
   888     AddFileLog('  |----- Number of auxiliary buffers: ' + inttostr(AuxBufNum));
   752 {$ENDIF}
   889 {$ENDIF}
       
   890 {$IFNDEF PAS2C}
   753     AddFileLog('  \----- Extensions: ');
   891     AddFileLog('  \----- Extensions: ');
   754 
   892 
   755     // fetch extentions and store them in string
   893     // fetch extentions and store them in string
   756     tmpstr := StrPas(PChar(glGetString(GL_EXTENSIONS)));
   894     tmpstr := StrPas(PChar(glGetString(GL_EXTENSIONS)));
   757     tmpn := WordCount(tmpstr, [' ']);
   895     tmpn := WordCount(tmpstr, [' ']);
   767             ExtractWord(tmpint+2, tmpstr, [' '])
   905             ExtractWord(tmpint+2, tmpstr, [' '])
   768         ));
   906         ));
   769         tmpint := tmpint + 3;
   907         tmpint := tmpint + 3;
   770     end;
   908     end;
   771     until (tmpint > tmpn);
   909     until (tmpint > tmpn);
       
   910 {$ENDIF}
   772     AddFileLog('');
   911     AddFileLog('');
   773 
   912 
   774     defaultFrame:= 0;
   913     defaultFrame:= 0;
   775 {$IFDEF USE_VIDEO_RECORDING}
   914 {$IFDEF USE_VIDEO_RECORDING}
   776     if GameType = gmtRecord then
   915     if GameType = gmtRecord then
   794             AddFileLog('Warning: off-screen rendering is not supported; using back buffer but it may not work.');
   933             AddFileLog('Warning: off-screen rendering is not supported; using back buffer but it may not work.');
   795         end;
   934         end;
   796     end;
   935     end;
   797 {$ENDIF}
   936 {$ENDIF}
   798 
   937 
   799 {$IFDEF USE_S3D_RENDERING}
   938 {$IFDEF GL2}
   800     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) then
   939 
       
   940 {$IFDEF PAS2C}
       
   941     err := glewInit();
       
   942     if err <> GLEW_OK then
       
   943     begin
       
   944         WriteLnToConsole('Failed to initialize GLEW.');
       
   945         halt;
       
   946     end;
       
   947 {$ENDIF}
       
   948 
       
   949 {$IFNDEF PAS2C}
       
   950     if not Load_GL_VERSION_2_0 then
       
   951         halt;
       
   952 {$ENDIF}
       
   953 
       
   954     shaderWater:= CompileProgram('water');
       
   955     glUseProgram(shaderWater);
       
   956     glUniform1i(glGetUniformLocation(shaderWater, pchar('tex0')), 0);
       
   957     uWaterMVPLocation:= glGetUniformLocation(shaderWater, pchar('mvp'));
       
   958 
       
   959     shaderMain:= CompileProgram('default');
       
   960     glUseProgram(shaderMain);
       
   961     glUniform1i(glGetUniformLocation(shaderMain, pchar('tex0')), 0);
       
   962     uMainMVPLocation:= glGetUniformLocation(shaderMain, pchar('mvp'));
       
   963     uMainTintLocation:= glGetUniformLocation(shaderMain, pchar('tint'));
       
   964 
       
   965     uCurrentMVPLocation:= uMainMVPLocation;
       
   966 
       
   967     Tint(255, 255, 255, 255);
       
   968     UpdateModelviewProjection;
       
   969 {$ENDIF}
       
   970 
       
   971 {$IFNDEF PAS2C}
       
   972 {$IFNDEF USE_S3D_RENDERING}
       
   973     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   801     begin
   974     begin
   802         // prepare left and right frame buffers and associated textures
   975         // prepare left and right frame buffers and associated textures
   803         if glLoadExtension('GL_EXT_framebuffer_object') then
   976         if glLoadExtension('GL_EXT_framebuffer_object') then
   804             begin
   977             begin
   805             CreateFramebuffer(framel, depthl, texl);
   978             CreateFramebuffer(framel, depthl, texl);
   810             end
   983             end
   811         else
   984         else
   812             cStereoMode:= smNone;
   985             cStereoMode:= smNone;
   813     end;
   986     end;
   814 {$ENDIF}
   987 {$ENDIF}
   815 
   988 {$ENDIF}
   816     // set view port to whole window
   989 
   817     glViewport(0, 0, cScreenWidth, cScreenHeight);
   990 // set view port to whole window
   818 
   991 glViewport(0, 0, cScreenWidth, cScreenHeight);
       
   992 
       
   993 {$IFDEF GL2}
       
   994     uMatrix.initModule;
       
   995     hglMatrixMode(MATRIX_MODELVIEW);
       
   996     // prepare default translation/scaling
       
   997     hglLoadIdentity();
       
   998     hglScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
       
   999     hglTranslatef(0, -cScreenHeight / 2, 0);
       
  1000 
       
  1001     EnableTexture(True);
       
  1002 
       
  1003     glEnableVertexAttribArray(aVertex);
       
  1004     glEnableVertexAttribArray(aTexCoord);
       
  1005     glGenBuffers(1, @vBuffer);
       
  1006     glGenBuffers(1, @tBuffer);
       
  1007     glGenBuffers(1, @cBuffer);
       
  1008 {$ELSE}
   819     glMatrixMode(GL_MODELVIEW);
  1009     glMatrixMode(GL_MODELVIEW);
   820     // prepare default translation/scaling
  1010     // prepare default translation/scaling
   821     glLoadIdentity();
  1011     glLoadIdentity();
   822     glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
  1012     glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
   823     glTranslatef(0, -cScreenHeight / 2, 0);
  1013     glTranslatef(0, -cScreenHeight / 2, 0);
   824 
  1014 
   825     // enable alpha blending
       
   826     glEnable(GL_BLEND);
       
   827     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       
   828     // disable/lower perspective correction (will not need it anyway)
  1015     // disable/lower perspective correction (will not need it anyway)
   829     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  1016     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
   830     // disable dithering
  1017     // disable dithering
   831     glDisable(GL_DITHER);
  1018     glDisable(GL_DITHER);
   832     // enable common states by default as they save a lot
  1019     // enable common states by default as they save a lot
   833     glEnable(GL_TEXTURE_2D);
  1020     glEnable(GL_TEXTURE_2D);
   834     glEnableClientState(GL_VERTEX_ARRAY);
  1021     glEnableClientState(GL_VERTEX_ARRAY);
   835     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  1022     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   836 end;
  1023 {$ENDIF}
       
  1024 
       
  1025     // enable alpha blending
       
  1026     glEnable(GL_BLEND);
       
  1027     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       
  1028     // disable/lower perspective correction (will not need it anyway)
       
  1029 end;
       
  1030 
       
  1031 {$IFDEF GL2}
       
  1032 procedure EnableTexture(enable:Boolean);
       
  1033 begin
       
  1034     if enable then
       
  1035         glUniform1i(glGetUniformLocation(shaderMain, pchar('enableTexture')), 1)
       
  1036     else
       
  1037         glUniform1i(glGetUniformLocation(shaderMain, pchar('enableTexture')), 0);
       
  1038 end;
       
  1039 {$ENDIF}
       
  1040 
       
  1041 procedure SetTexCoordPointer(p: Pointer; n: Integer);
       
  1042 begin
       
  1043 {$IFDEF GL2}
       
  1044     glBindBuffer(GL_ARRAY_BUFFER, tBuffer);
       
  1045     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * n * 2, p, GL_STATIC_DRAW);
       
  1046     glEnableVertexAttribArray(aTexCoord);
       
  1047     glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, pointer(0));
       
  1048 {$ELSE}
       
  1049     n:= n;
       
  1050     glTexCoordPointer(2, GL_FLOAT, 0, p);
       
  1051 {$ENDIF}
       
  1052 end;
       
  1053 
       
  1054 procedure SetVertexPointer(p: Pointer; n: Integer);
       
  1055 begin
       
  1056 {$IFDEF GL2}
       
  1057     glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
       
  1058     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * n * 2, p, GL_STATIC_DRAW);
       
  1059     glEnableVertexAttribArray(aVertex);
       
  1060     glVertexAttribPointer(aVertex, 2, GL_FLOAT, GL_FALSE, 0, pointer(0));
       
  1061 {$ELSE}
       
  1062     n:= n;
       
  1063     glVertexPointer(2, GL_FLOAT, 0, p);
       
  1064 {$ENDIF}
       
  1065 end;
       
  1066 
       
  1067 procedure SetColorPointer(p: Pointer; n: Integer);
       
  1068 begin
       
  1069 {$IFDEF GL2}
       
  1070     glBindBuffer(GL_ARRAY_BUFFER, cBuffer);
       
  1071     glBufferData(GL_ARRAY_BUFFER, n * 4, p, GL_STATIC_DRAW);
       
  1072     glEnableVertexAttribArray(aColor);
       
  1073     glVertexAttribPointer(aColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, pointer(0));
       
  1074 {$ELSE}
       
  1075     n:= n;
       
  1076     glColorPointer(4, GL_UNSIGNED_BYTE, 0, p);
       
  1077 {$ENDIF}
       
  1078 end;
       
  1079 
       
  1080 {$IFDEF GL2}
       
  1081 procedure UpdateModelviewProjection;
       
  1082 var
       
  1083     mvp: TMatrix4x4f;
       
  1084 begin
       
  1085     //MatrixMultiply(mvp, mProjection, mModelview);
       
  1086 {$HINTS OFF}
       
  1087     hglMVP(mvp);
       
  1088 {$HINTS ON}
       
  1089     glUniformMatrix4fv(uCurrentMVPLocation, 1, GL_FALSE, @mvp[0, 0]);
       
  1090 end;
       
  1091 {$ENDIF}
       
  1092 
       
  1093 (*
       
  1094 procedure UpdateProjection;
       
  1095 var
       
  1096     s: GLfloat;
       
  1097 begin
       
  1098     s:=cScaleFactor;
       
  1099     mProjection[0,0]:= s/cScreenWidth; mProjection[0,1]:=  0.0;             mProjection[0,2]:=0.0; mProjection[0,3]:=  0.0;
       
  1100     mProjection[1,0]:= 0.0;            mProjection[1,1]:= -s/cScreenHeight; mProjection[1,2]:=0.0; mProjection[1,3]:=  0.0;
       
  1101     mProjection[2,0]:= 0.0;            mProjection[2,1]:=  0.0;             mProjection[2,2]:=1.0; mProjection[2,3]:=  0.0;
       
  1102     mProjection[3,0]:= cStereoDepth;   mProjection[3,1]:=  s/2;             mProjection[3,2]:=0.0; mProjection[3,3]:=  1.0;
       
  1103 
       
  1104 {$IFDEF GL2}
       
  1105     UpdateModelviewProjection;
       
  1106 {$ELSE}
       
  1107     glMatrixMode(GL_PROJECTION);
       
  1108     glLoadMatrixf(@mProjection[0, 0]);
       
  1109     glMatrixMode(GL_MODELVIEW);
       
  1110 {$ENDIF}
       
  1111 end;
       
  1112 *)
   837 
  1113 
   838 procedure SetScale(f: GLfloat);
  1114 procedure SetScale(f: GLfloat);
   839 begin
  1115 begin
   840 // leave immediately if scale factor did not change
  1116 // leave immediately if scale factor did not change
   841     if f = cScaleFactor then
  1117     if f = cScaleFactor then
   842         exit;
  1118         exit;
   843 
  1119 
   844     if f = cDefaultZoomLevel then
  1120     if f = cDefaultZoomLevel then
   845         glPopMatrix         // return to default scaling
  1121 {$IFDEF GL2}
       
  1122         hglPopMatrix         // "return" to default scaling
       
  1123 {$ELSE}
       
  1124         glPopMatrix
       
  1125 {$ENDIF}
   846     else                    // other scaling
  1126     else                    // other scaling
   847         begin
  1127         begin
       
  1128 {$IFDEF GL2}
       
  1129         hglPushMatrix;       // save default scaling
       
  1130         hglLoadIdentity;
       
  1131         hglScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
       
  1132         hglTranslatef(0, -cScreenHeight / 2, 0);
       
  1133 {$ELSE}
   848         glPushMatrix;       // save default scaling
  1134         glPushMatrix;       // save default scaling
   849         glLoadIdentity;
  1135         glLoadIdentity;
   850         glScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
  1136         glScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
   851         glTranslatef(0, -cScreenHeight / 2, 0);
  1137         glTranslatef(0, -cScreenHeight / 2, 0);
       
  1138 {$ENDIF}
   852         end;
  1139         end;
   853 
  1140 
   854     cScaleFactor:= f;
  1141     cScaleFactor:= f;
   855 end;
  1142 
       
  1143 {$IFDEF GL2}
       
  1144     UpdateModelviewProjection;
       
  1145 {$ENDIF}
       
  1146 end;
       
  1147 
       
  1148 procedure BeginWater;
       
  1149 begin
       
  1150 {$IFDEF GL2}
       
  1151     glUseProgram(shaderWater);
       
  1152     uCurrentMVPLocation:=uWaterMVPLocation;
       
  1153     UpdateModelviewProjection;
       
  1154     glDisableVertexAttribArray(aTexCoord);
       
  1155     glEnableVertexAttribArray(aColor);
       
  1156 {$ELSE}
       
  1157     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
       
  1158     glEnableClientState(GL_COLOR_ARRAY);
       
  1159 {$ENDIF}
       
  1160 end;
       
  1161 
       
  1162 procedure EndWater;
       
  1163 begin
       
  1164 {$IFDEF GL2}
       
  1165     glUseProgram(shaderMain);
       
  1166     uCurrentMVPLocation:=uMainMVPLocation;
       
  1167     UpdateModelviewProjection;
       
  1168     glDisableVertexAttribArray(aColor);
       
  1169     glEnableVertexAttribArray(aTexCoord);
       
  1170 {$ELSE}
       
  1171     glDisableClientState(GL_COLOR_ARRAY);
       
  1172     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
       
  1173 {$ENDIF}
       
  1174 end;
       
  1175 
   856 
  1176 
   857 ////////////////////////////////////////////////////////////////////////////////
  1177 ////////////////////////////////////////////////////////////////////////////////
   858 procedure AddProgress;
  1178 procedure AddProgress;
   859 var r: TSDL_Rect;
  1179 var r: TSDL_Rect;
   860     texsurf: PSDL_Surface;
  1180     texsurf: PSDL_Surface;
   868         ProgrTex:= Surface2Tex(texsurf, false);
  1188         ProgrTex:= Surface2Tex(texsurf, false);
   869 
  1189 
   870         squaresize:= texsurf^.w shr 1;
  1190         squaresize:= texsurf^.w shr 1;
   871         numsquares:= texsurf^.h div squaresize;
  1191         numsquares:= texsurf^.h div squaresize;
   872         SDL_FreeSurface(texsurf);
  1192         SDL_FreeSurface(texsurf);
       
  1193         {$IFNDEF PAS2C}
   873         with mobileRecord do
  1194         with mobileRecord do
   874             if GameLoading <> nil then
  1195             if GameLoading <> nil then
   875                 GameLoading();
  1196                 GameLoading();
   876 
  1197         {$ENDIF}
   877         end;
  1198         end;
   878 
  1199 
   879     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
  1200     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
   880 
  1201 
   881     glClear(GL_COLOR_BUFFER_BIT);
  1202     glClear(GL_COLOR_BUFFER_BIT);
   889     r.h:= squaresize;
  1210     r.h:= squaresize;
   890 
  1211 
   891     DrawTextureFromRect( -squaresize div 2, (cScreenHeight - squaresize) shr 1, @r, ProgrTex);
  1212     DrawTextureFromRect( -squaresize div 2, (cScreenHeight - squaresize) shr 1, @r, ProgrTex);
   892 
  1213 
   893     SwapBuffers;
  1214     SwapBuffers;
       
  1215 
   894     inc(Step);
  1216     inc(Step);
   895 end;
  1217 end;
   896 
  1218 
   897 procedure FinishProgress;
  1219 procedure FinishProgress;
   898 begin
  1220 begin
       
  1221     {$IFNDEF PAS2C}
   899     with mobileRecord do
  1222     with mobileRecord do
   900         if GameLoaded <> nil then
  1223         if GameLoaded <> nil then
   901             GameLoaded();
  1224             GameLoaded();
       
  1225     {$ENDIF}
   902     WriteLnToConsole('Freeing progress surface... ');
  1226     WriteLnToConsole('Freeing progress surface... ');
   903     FreeTexture(ProgrTex);
  1227     FreeTexture(ProgrTex);
   904     ProgrTex:= nil;
  1228     ProgrTex:= nil;
   905     Step:= 0
  1229     Step:= 0
   906 end;
  1230 end;
  1242     {$ENDIF}
  1566     {$ENDIF}
  1243         end;
  1567         end;
  1244 {$ENDIF}
  1568 {$ENDIF}
  1245 
  1569 
  1246     SetupOpenGL();
  1570     SetupOpenGL();
       
  1571 
  1247     if reinit then
  1572     if reinit then
  1248         begin
  1573         begin
  1249         // clean the window from any previous content
  1574         // clean the window from any previous content
  1250         glClear(GL_COLOR_BUFFER_BIT);
  1575         glClear(GL_COLOR_BUFFER_BIT);
  1251         if SuddenDeathDmg then
  1576         if SuddenDeathDmg then
  1318 {$ENDIF}
  1643 {$ENDIF}
  1319 end;
  1644 end;
  1320 
  1645 
  1321 procedure freeModule;
  1646 procedure freeModule;
  1322 begin
  1647 begin
       
  1648 {$IFDEF GL2}
       
  1649     glDeleteProgram(shaderMain);
       
  1650     glDeleteProgram(shaderWater);
       
  1651     glDeleteBuffers(1, @vBuffer);
       
  1652     glDeleteBuffers(1, @tBuffer);
       
  1653     glDeleteBuffers(1, @cBuffer);
       
  1654 {$ENDIF}
  1323     StoreRelease(false);
  1655     StoreRelease(false);
  1324     TTF_Quit();
  1656     TTF_Quit();
  1325 {$IFDEF SDL2}
  1657 {$IFDEF SDL2}
  1326     SDL_GL_DeleteContext(SDLGLcontext);
  1658     SDL_GL_DeleteContext(SDLGLcontext);
  1327     SDL_DestroyWindow(SDLwindow);
  1659     SDL_DestroyWindow(SDLwindow);