hedgewars/uStore.pas
branchwebgl
changeset 8026 4a4f21070479
parent 7866 58d10edc5d21
child 8032 f8fcece14302
equal deleted inserted replaced
8023:7de85783b823 8026:4a4f21070479
    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 {$IFNDEF PAS2C} StrUtils, {$ENDIF}SysUtils, uConsts, SDLh, GLunit, uTypes, uLandTexture, uCaptions, uChat;
    24 uses SysUtils, uConsts, SDLh, GLunit, uTypes, uLandTexture, uCaptions, uChat
       
    25      {$IFDEF GL2}, uMatrix{$ENDIF}
       
    26      {$IFNDEF PAS2C}, StrUtils{$ENDIF}
       
    27      ;
    25 
    28 
    26 procedure initModule;
    29 procedure initModule;
    27 procedure freeModule;
    30 procedure freeModule;
    28 
    31 
    29 procedure StoreLoad(reload: boolean);
    32 procedure StoreLoad(reload: boolean);
    53 {$ENDIF}
    56 {$ENDIF}
    54 
    57 
    55 procedure WarpMouse(x, y: Word); inline;
    58 procedure WarpMouse(x, y: Word); inline;
    56 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
    59 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
    57 
    60 
       
    61 {$IFDEF GL2}
       
    62 procedure UpdateModelviewProjection;
       
    63 procedure EnableTexture(enable:Boolean);
       
    64 {$ENDIF}
       
    65 
       
    66 procedure SetTexCoordPointer(p: Pointer;n: Integer);
       
    67 procedure SetVertexPointer(p: Pointer;n: Integer);
       
    68 procedure SetColorPointer(p: Pointer;n: Integer);
       
    69 procedure BeginWater;
       
    70 procedure EndWater;
       
    71 
    58 implementation
    72 implementation
    59 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands,
    73 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands,
    60      uDebug{$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    74      uDebug{$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    61      {$IF NOT DEFINED(SDL13) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    75      {$IF NOT DEFINED(SDL13) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    62 
    76 
    66 {$IFDEF SDL13}
    80 {$IFDEF SDL13}
    67     SDLwindow: PSDL_Window;
    81     SDLwindow: PSDL_Window;
    68     SDLGLcontext: PSDL_GLContext;
    82     SDLGLcontext: PSDL_GLContext;
    69 {$ELSE}
    83 {$ELSE}
    70     SDLPrimSurface: PSDL_Surface;
    84     SDLPrimSurface: PSDL_Surface;
       
    85 {$ENDIF}
       
    86 
       
    87 {$IFDEF GL2}
       
    88     shaderMain: GLuint;
       
    89     shaderWater: GLuint;
       
    90 
       
    91     // attributes
       
    92 {$ENDIF}
       
    93 
       
    94 {$IFDEF WEBGL}
       
    95     OpenGLSetupedBefore : boolean;
    71 {$ENDIF}
    96 {$ENDIF}
    72 
    97 
    73 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    98 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    74 var w, h: LongInt;
    99 var w, h: LongInt;
    75     tmpsurf: PSDL_Surface;
   100     tmpsurf: PSDL_Surface;
   301     ai: TAmmoType;
   326     ai: TAmmoType;
   302     tmpsurf: PSDL_Surface;
   327     tmpsurf: PSDL_Surface;
   303     i, imflags: LongInt;
   328     i, imflags: LongInt;
   304 begin
   329 begin
   305 AddFileLog('StoreLoad()');
   330 AddFileLog('StoreLoad()');
   306 
   331 WriteLnToConsole('Entering StoreLoad');
   307 if not reload then
   332 if not reload then
   308     for fi:= Low(THWFont) to High(THWFont) do
   333     for fi:= Low(THWFont) to High(THWFont) do
   309         with Fontz[fi] do
   334         with Fontz[fi] do
   310             begin
   335             begin
   311             s:= UserPathz[ptFonts] + '/' + Name;
   336             s:= UserPathz[ptFonts] + '/' + Name;
   432     end;
   457     end;
   433 
   458 
   434 if not reload then
   459 if not reload then
   435     AddProgress;
   460     AddProgress;
   436 IMG_Quit();
   461 IMG_Quit();
       
   462 
       
   463 WriteLnToConsole('Leaving StoreLoad');
   437 end;
   464 end;
   438 
   465 
   439 {$IF NOT DEFINED(S3D_DISABLED) OR DEFINED(USE_VIDEO_RECORDING)}
   466 {$IF NOT DEFINED(S3D_DISABLED) OR DEFINED(USE_VIDEO_RECORDING)}
   440 procedure CreateFramebuffer(var frame, depth, tex: GLuint);
   467 procedure CreateFramebuffer(var frame, depth, tex: GLuint);
   441 begin
   468 begin
   660         end;
   687         end;
   661 end;
   688 end;
   662 
   689 
   663 function glLoadExtension(extension : shortstring) : boolean;
   690 function glLoadExtension(extension : shortstring) : boolean;
   664 begin
   691 begin
   665 {$IF GLunit = gles11}
   692 {$IF (GLunit = gles11) OR DEFINED(PAS2C)}
   666     // FreePascal doesnt come with OpenGL ES 1.1 Extension headers
   693     // FreePascal doesnt come with OpenGL ES 1.1 Extension headers
   667     extension:= extension; // avoid hint
   694     extension:= extension; // avoid hint
   668     glLoadExtension:= false;
   695     glLoadExtension:= false;
   669     AddFileLog('OpenGL - "' + extension + '" skipped')
   696     AddFileLog('OpenGL - "' + extension + '" skipped')
   670 {$ELSE}
   697 {$ELSE}
   694     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required
   721     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required
   695     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only
   722     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only
   696     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering
   723     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering
   697 end;
   724 end;
   698 
   725 
       
   726 {$IFDEF GL2}
       
   727 function CompileShader(shaderFile: string; shaderType: GLenum): GLuint;
       
   728 var
       
   729     shader: GLuint;
       
   730     f: Textfile;
       
   731     source, line: AnsiString;
       
   732     sourceA: Pchar;
       
   733     lengthA: GLint;
       
   734     compileResult: GLint;
       
   735     logLength: GLint;
       
   736     log: PChar;
       
   737 begin
       
   738     Assign(f, Pathz[ptShaders] + '/' + shaderFile);
       
   739     filemode:= 0; // readonly
       
   740     Reset(f);
       
   741     if IOResult <> 0 then
       
   742     begin
       
   743         AddFileLog('Unable to load ' + shaderFile);
       
   744         halt(-1);
       
   745     end;
       
   746 
       
   747     source:='';
       
   748     while not eof(f) do
       
   749     begin
       
   750         ReadLn(f, line);
       
   751         source:= source + line + #10;
       
   752     end;
       
   753     
       
   754     Close(f);
       
   755 
       
   756     WriteLnToConsole('Compiling shader: ' + Pathz[ptShaders] + '/' + shaderFile);
       
   757 
       
   758     sourceA:=PChar(source);
       
   759     lengthA:=Length(source);
       
   760 
       
   761     shader:=glCreateShader(shaderType);
       
   762     glShaderSource(shader, 1, @sourceA, @lengthA);
       
   763     glCompileShader(shader);
       
   764     glGetShaderiv(shader, GL_COMPILE_STATUS, @compileResult);
       
   765     glGetShaderiv(shader, GL_INFO_LOG_LENGTH, @logLength);
       
   766 
       
   767     if logLength > 1 then
       
   768     begin
       
   769         log := GetMem(logLength);
       
   770         glGetShaderInfoLog(shader, logLength, nil, log);
       
   771         WriteLnToConsole('========== Compiler log  ==========');
       
   772         WriteLnToConsole(shortstring(log));
       
   773         WriteLnToConsole('===================================');
       
   774         FreeMem(log, logLength);
       
   775     end;
       
   776 
       
   777     if compileResult <> GL_TRUE then
       
   778     begin
       
   779         WriteLnToConsole('Shader compilation failed, halting');
       
   780         halt(-1);
       
   781     end;
       
   782 
       
   783     CompileShader:= shader;
       
   784 end;
       
   785 
       
   786 function CompileProgram(shaderName: string): GLuint;
       
   787 var
       
   788     program_: GLuint;
       
   789     vs, fs: GLuint;
       
   790     linkResult: GLint;
       
   791     logLength: GLint;
       
   792     log: PChar;
       
   793 begin
       
   794     program_:= glCreateProgram();
       
   795     vs:= CompileShader(shaderName + '.vs', GL_VERTEX_SHADER);
       
   796     fs:= CompileShader(shaderName + '.fs', GL_FRAGMENT_SHADER);
       
   797     glAttachShader(program_, vs);
       
   798     glAttachShader(program_, fs);
       
   799 
       
   800     glBindAttribLocation(program_, aVertex, PChar('vertex'));
       
   801     glBindAttribLocation(program_, aTexCoord, PChar('texcoord'));
       
   802     glBindAttribLocation(program_, aColor, PChar('color'));
       
   803 
       
   804     glLinkProgram(program_);
       
   805     glDeleteShader(vs);
       
   806     glDeleteShader(fs);
       
   807 
       
   808     glGetProgramiv(program_, GL_LINK_STATUS, @linkResult);
       
   809     glGetProgramiv(program_, GL_INFO_LOG_LENGTH, @logLength);
       
   810 
       
   811     if logLength > 1 then
       
   812     begin
       
   813         log := GetMem(logLength);
       
   814         glGetProgramInfoLog(program_, logLength, nil, log);
       
   815         WriteLnToConsole('========== Compiler log  ==========');
       
   816         WriteLnToConsole(shortstring(log));
       
   817         WriteLnToConsole('===================================');
       
   818         FreeMem(log, logLength);
       
   819     end;
       
   820 
       
   821     if linkResult <> GL_TRUE then
       
   822     begin
       
   823         WriteLnToConsole('Linking program failed, halting');
       
   824         halt(-1);
       
   825     end;
       
   826 
       
   827     CompileProgram:= program_;
       
   828 end;
       
   829 
       
   830 {$ENDIF}
       
   831 
   699 procedure SetupOpenGL;
   832 procedure SetupOpenGL;
   700 //var vendor: shortstring = '';
   833 //var vendor: shortstring = '';
   701 var buf: array[byte] of char;
   834 var buf: array[byte] of char;
   702 {$IFDEF USE_VIDEO_RECORDING}
   835 {$IFDEF PAS2C}err: GLenum;{$ENDIF}
   703     AuxBufNum: LongInt;
   836 {$IFDEF USE_VIDEO_RECORDING}AuxBufNum: LongInt;{$ENDIF}
   704 {$ENDIF}
       
   705     tmpstr: AnsiString;
   837     tmpstr: AnsiString;
   706     tmpint: LongInt;
   838     tmpint: LongInt;
   707     tmpn: LongInt;
   839     tmpn: LongInt;
   708 begin
   840 begin
   709     buf[0]:= char(0); // avoid compiler hint
   841     buf[0]:= char(0); // avoid compiler hint
   710     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   842     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
       
   843 
       
   844 {$IFDEF WEBGL}
       
   845     if OpenGLSetupedBefore then
       
   846         begin
       
   847         glViewport(0, 0, cScreenWidth, cScreenHeight);
       
   848         hglMatrixMode(MATRIX_MODELVIEW);
       
   849         hglLoadIdentity();
       
   850         hglScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
       
   851         hglTranslatef(0, -cScreenHeight / 2, 0);
       
   852         exit;
       
   853         end
       
   854     OpenGLSetupedBefore := true;
       
   855 {$ENDIF}
   711 
   856 
   712 {$IFDEF SDL13}
   857 {$IFDEF SDL13}
   713     // this function creates an opengles1.1 context by default on mobile devices
   858     // this function creates an opengles1.1 context by default on mobile devices
   714     // unless you un-comment this two attributes
   859     // unless you un-comment this two attributes
   715     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
   860     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
   731         begin
   876         begin
   732         cReducedQuality := cReducedQuality or rqNoBackground;  
   877         cReducedQuality := cReducedQuality or rqNoBackground;  
   733         AddFileLog('Texture size too small for backgrounds, disabling.');
   878         AddFileLog('Texture size too small for backgrounds, disabling.');
   734         end;
   879         end;
   735 
   880 
   736 (*  // find out which gpu we are using (for extension compatibility maybe?)
   881 (*
       
   882     // find out which gpu we are using (for extension compatibility maybe?)
   737 {$IFDEF IPHONEOS}
   883 {$IFDEF IPHONEOS}
   738     vendor:= vendor; // avoid hint
   884     vendor:= vendor; // avoid hint
   739     cGPUVendor:= gvApple;
   885     cGPUVendor:= gvApple;
   740 {$ELSE}
   886 {$ELSE}
   741     vendor:= LowerCase(shortstring(pchar(glGetString(GL_VENDOR))));
   887     vendor:= LowerCase(shortstring(pchar(glGetString(GL_VENDOR))));
   758     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   904     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   759 {$IFDEF USE_VIDEO_RECORDING}
   905 {$IFDEF USE_VIDEO_RECORDING}
   760     glGetIntegerv(GL_AUX_BUFFERS, @AuxBufNum);
   906     glGetIntegerv(GL_AUX_BUFFERS, @AuxBufNum);
   761     AddFileLog('  |----- Number of auxiliary buffers: ' + inttostr(AuxBufNum));
   907     AddFileLog('  |----- Number of auxiliary buffers: ' + inttostr(AuxBufNum));
   762 {$ENDIF}
   908 {$ENDIF}
       
   909 {$IFDEF PAS2C}
       
   910 
       
   911     // doesn't seem to print >256 chars
       
   912     AddFileLogRaw(PChar(glGetString(GL_EXTENSIONS)));
       
   913 {$ELSE}
   763     AddFileLog('  \----- Extensions: ');
   914     AddFileLog('  \----- Extensions: ');
   764 {$IFNDEF PAS2C}
       
   765     // fetch extentions and store them in string
   915     // fetch extentions and store them in string
   766     tmpstr := StrPas(PChar(glGetString(GL_EXTENSIONS)));
   916     tmpstr := StrPas(PChar(glGetString(GL_EXTENSIONS)));
   767     tmpn := WordCount(tmpstr, [' ']);
   917     tmpn := WordCount(tmpstr, [' ']);
   768     tmpint := 1;
   918     tmpint := 1;
   769 
   919 
   777             ExtractWord(tmpint+2, tmpstr, [' '])
   927             ExtractWord(tmpint+2, tmpstr, [' '])
   778         ));
   928         ));
   779         tmpint := tmpint + 3;
   929         tmpint := tmpint + 3;
   780     end;
   930     end;
   781     until (tmpint > tmpn);
   931     until (tmpint > tmpn);
   782 {$ELSE}
       
   783     // doesn't seem to print >256 chars
       
   784     AddFileLogRaw(PChar(glGetString(GL_EXTENSIONS)));
       
   785 {$ENDIF}
   932 {$ENDIF}
   786     AddFileLog('');
   933     AddFileLog('');
   787 
   934 
   788     defaultFrame:= 0;
   935     defaultFrame:= 0;
   789 {$IFDEF USE_VIDEO_RECORDING}
   936 {$IFDEF USE_VIDEO_RECORDING}
   808             AddFileLog('Warning: off-screen rendering is not supported; using back buffer but it may not work.');
   955             AddFileLog('Warning: off-screen rendering is not supported; using back buffer but it may not work.');
   809         end;
   956         end;
   810     end;
   957     end;
   811 {$ENDIF}
   958 {$ENDIF}
   812 
   959 
       
   960 {$IFDEF GL2}
       
   961 
       
   962 {$IFDEF PAS2C}
       
   963     err := glewInit();
       
   964     if err <> GLEW_OK then
       
   965     begin
       
   966         WriteLnToConsole('Failed to initialize GLEW.');
       
   967         halt;
       
   968     end;
       
   969 {$ENDIF}
       
   970 
       
   971 {$IFNDEF PAS2C}
       
   972     if not Load_GL_VERSION_2_0 then
       
   973         halt;
       
   974 {$ENDIF}
       
   975 
       
   976     shaderWater:= CompileProgram('water');
       
   977     glUseProgram(shaderWater);
       
   978     glUniform1i(glGetUniformLocation(shaderWater, pchar('tex0')), 0);
       
   979     uWaterMVPLocation:= glGetUniformLocation(shaderWater, pchar('mvp'));
       
   980 
       
   981     shaderMain:= CompileProgram('default');
       
   982     glUseProgram(shaderMain);
       
   983     glUniform1i(glGetUniformLocation(shaderMain, pchar('tex0')), 0);
       
   984     uMainMVPLocation:= glGetUniformLocation(shaderMain, pchar('mvp'));
       
   985     uMainTintLocation:= glGetUniformLocation(shaderMain, pchar('tint'));
       
   986 
       
   987     uCurrentMVPLocation:= uMainMVPLocation;
       
   988 
       
   989     Tint(255, 255, 255, 255);
       
   990     UpdateModelviewProjection;
       
   991 {$ENDIF}
       
   992 
   813 {$IFNDEF S3D_DISABLED}
   993 {$IFNDEF S3D_DISABLED}
   814     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   994     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   815     begin
   995     begin
   816         // prepare left and right frame buffers and associated textures
   996         // prepare left and right frame buffers and associated textures
   817         if glLoadExtension('GL_EXT_framebuffer_object') then
   997         if glLoadExtension('GL_EXT_framebuffer_object') then
   825         else
  1005         else
   826             cStereoMode:= smNone;
  1006             cStereoMode:= smNone;
   827     end;
  1007     end;
   828 {$ENDIF}
  1008 {$ENDIF}
   829 
  1009 
   830     // set view port to whole window
  1010 // set view port to whole window
   831     glViewport(0, 0, cScreenWidth, cScreenHeight);
  1011 glViewport(0, 0, cScreenWidth, cScreenHeight);
   832 
  1012 
       
  1013 {$IFDEF GL2}
       
  1014     uMatrix.initModule;
       
  1015     hglMatrixMode(MATRIX_MODELVIEW);
       
  1016     // prepare default translation/scaling
       
  1017     hglLoadIdentity();
       
  1018     hglScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
       
  1019     hglTranslatef(0, -cScreenHeight / 2, 0);
       
  1020 
       
  1021     EnableTexture(True);
       
  1022     
       
  1023     glEnableVertexAttribArray(aVertex);
       
  1024     glEnableVertexAttribArray(aTexCoord);
       
  1025     glGenBuffers(1, @vBuffer);
       
  1026     glGenBuffers(1, @tBuffer);
       
  1027     glGenBuffers(1, @cBuffer);
       
  1028 {$ELSE}
   833     glMatrixMode(GL_MODELVIEW);
  1029     glMatrixMode(GL_MODELVIEW);
   834     // prepare default translation/scaling
  1030     // prepare default translation/scaling
   835     glLoadIdentity();
  1031     glLoadIdentity();
   836     glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
  1032     glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
   837     glTranslatef(0, -cScreenHeight / 2, 0);
  1033     glTranslatef(0, -cScreenHeight / 2, 0);
   838 
  1034 
   839     // enable alpha blending
       
   840     glEnable(GL_BLEND);
       
   841     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       
   842     // disable/lower perspective correction (will not need it anyway)
  1035     // disable/lower perspective correction (will not need it anyway)
   843     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  1036     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
   844     // disable dithering
  1037     // disable dithering
   845     glDisable(GL_DITHER);
  1038     glDisable(GL_DITHER);
   846     // enable common states by default as they save a lot
  1039     // enable common states by default as they save a lot
   847     glEnable(GL_TEXTURE_2D);
  1040     glEnable(GL_TEXTURE_2D);
   848     glEnableClientState(GL_VERTEX_ARRAY);
  1041     glEnableClientState(GL_VERTEX_ARRAY);
   849     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  1042     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   850 end;
  1043 {$ENDIF}
       
  1044 
       
  1045     // enable alpha blending
       
  1046     glEnable(GL_BLEND);
       
  1047     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       
  1048     // disable/lower perspective correction (will not need it anyway)
       
  1049 end;
       
  1050 
       
  1051 {$IFDEF GL2}
       
  1052 procedure EnableTexture(enable:Boolean);
       
  1053 begin
       
  1054     if enable then
       
  1055         glUniform1i(glGetUniformLocation(shaderMain, pchar('enableTexture')), 1)
       
  1056     else
       
  1057         glUniform1i(glGetUniformLocation(shaderMain, pchar('enableTexture')), 0);
       
  1058 end;
       
  1059 {$ENDIF}
       
  1060 
       
  1061 procedure SetTexCoordPointer(p: Pointer; n: Integer);
       
  1062 begin
       
  1063     {$IFDEF GL2}
       
  1064     glBindBuffer(GL_ARRAY_BUFFER, tBuffer);
       
  1065     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * n * 2, p, GL_STATIC_DRAW);
       
  1066     glEnableVertexAttribArray(aTexCoord);
       
  1067     glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, pointer(0));
       
  1068     {$ELSE}
       
  1069     glTexCoordPointer(2, GL_FLOAT, 0, p);
       
  1070     {$ENDIF}
       
  1071 end;
       
  1072 
       
  1073 procedure SetVertexPointer(p: Pointer; n: Integer);
       
  1074 begin
       
  1075     {$IFDEF GL2}
       
  1076     glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
       
  1077     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * n * 2, p, GL_STATIC_DRAW);
       
  1078     glEnableVertexAttribArray(aVertex);
       
  1079     glVertexAttribPointer(aVertex, 2, GL_FLOAT, GL_FALSE, 0, pointer(0));
       
  1080     {$ELSE}
       
  1081     glVertexPointer(2, GL_FLOAT, 0, p);
       
  1082     {$ENDIF}
       
  1083 end;
       
  1084 
       
  1085 procedure SetColorPointer(p: Pointer; n: Integer);
       
  1086 begin
       
  1087     {$IFDEF GL2}
       
  1088     glBindBuffer(GL_ARRAY_BUFFER, cBuffer);
       
  1089     glBufferData(GL_ARRAY_BUFFER, n * 4, p, GL_STATIC_DRAW);
       
  1090     glEnableVertexAttribArray(aColor);
       
  1091     glVertexAttribPointer(aColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, pointer(0));
       
  1092     {$ELSE}
       
  1093     glColorPointer(4, GL_UNSIGNED_BYTE, 0, p);
       
  1094     {$ENDIF}
       
  1095 end;
       
  1096 
       
  1097 {$IFDEF GL2}
       
  1098 procedure UpdateModelviewProjection;
       
  1099 var
       
  1100     mvp: TMatrix4x4f;
       
  1101 begin
       
  1102     //MatrixMultiply(mvp, mProjection, mModelview);
       
  1103     hglMVP(mvp);
       
  1104     glUniformMatrix4fv(uCurrentMVPLocation, 1, GL_FALSE, @mvp[0, 0]);
       
  1105 end;
       
  1106 {$ENDIF}
       
  1107 
       
  1108 (*
       
  1109 procedure UpdateProjection;
       
  1110 var
       
  1111     s: GLfloat;
       
  1112 begin
       
  1113     s:=cScaleFactor;
       
  1114     mProjection[0,0]:= s/cScreenWidth; mProjection[0,1]:=  0.0;             mProjection[0,2]:=0.0; mProjection[0,3]:=  0.0;
       
  1115     mProjection[1,0]:= 0.0;            mProjection[1,1]:= -s/cScreenHeight; mProjection[1,2]:=0.0; mProjection[1,3]:=  0.0;
       
  1116     mProjection[2,0]:= 0.0;            mProjection[2,1]:=  0.0;             mProjection[2,2]:=1.0; mProjection[2,3]:=  0.0;
       
  1117     mProjection[3,0]:= cStereoDepth;   mProjection[3,1]:=  s/2;             mProjection[3,2]:=0.0; mProjection[3,3]:=  1.0;
       
  1118 
       
  1119     {$IFDEF GL2}
       
  1120     UpdateModelviewProjection;
       
  1121     {$ELSE}
       
  1122     glMatrixMode(GL_PROJECTION);
       
  1123     glLoadMatrixf(@mProjection[0, 0]);
       
  1124     glMatrixMode(GL_MODELVIEW);
       
  1125     {$ENDIF}    
       
  1126 end;
       
  1127 *)
   851 
  1128 
   852 procedure SetScale(f: GLfloat);
  1129 procedure SetScale(f: GLfloat);
   853 begin
  1130 begin
   854 // leave immediately if scale factor did not change
  1131 // leave immediately if scale factor did not change
   855     if f = cScaleFactor then
  1132     if f = cScaleFactor then
   856         exit;
  1133         exit;
   857 
  1134 
   858     if f = cDefaultZoomLevel then
  1135     if f = cDefaultZoomLevel then
   859         glPopMatrix         // "return" to default scaling
  1136 {$IFDEF GL2}
       
  1137         hglPopMatrix         // "return" to default scaling
       
  1138 {$ELSE}
       
  1139         glPopMatrix
       
  1140 {$ENDIF}
   860     else                    // other scaling
  1141     else                    // other scaling
   861         begin
  1142         begin
       
  1143 {$IFDEF GL2}
       
  1144         hglPushMatrix;       // save default scaling
       
  1145         hglLoadIdentity;
       
  1146         hglScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
       
  1147         hglTranslatef(0, -cScreenHeight / 2, 0);
       
  1148 {$ELSE}
   862         glPushMatrix;       // save default scaling
  1149         glPushMatrix;       // save default scaling
   863         glLoadIdentity;
  1150         glLoadIdentity;
   864         glScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
  1151         glScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
   865         glTranslatef(0, -cScreenHeight / 2, 0);
  1152         glTranslatef(0, -cScreenHeight / 2, 0);
       
  1153 {$ENDIF}
   866         end;
  1154         end;
   867 
  1155 
   868     cScaleFactor:= f;
  1156     cScaleFactor:= f;
   869 end;
  1157 
       
  1158 {$IFDEF GL2}
       
  1159     UpdateModelviewProjection;
       
  1160 {$ENDIF}
       
  1161 end;
       
  1162 
       
  1163 procedure BeginWater;
       
  1164 begin
       
  1165     {$IFDEF GL2}
       
  1166     glUseProgram(shaderWater);
       
  1167     uCurrentMVPLocation:=uWaterMVPLocation;
       
  1168     UpdateModelviewProjection;
       
  1169     glDisableVertexAttribArray(aTexCoord);
       
  1170     glEnableVertexAttribArray(aColor);
       
  1171     {$ELSE}
       
  1172     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
       
  1173     glEnableClientState(GL_COLOR_ARRAY);
       
  1174     {$ENDIF}
       
  1175 end;
       
  1176 
       
  1177 procedure EndWater;
       
  1178 begin
       
  1179     {$IFDEF GL2}
       
  1180     glUseProgram(shaderMain);
       
  1181     uCurrentMVPLocation:=uMainMVPLocation;
       
  1182     UpdateModelviewProjection;
       
  1183     glDisableVertexAttribArray(aColor);
       
  1184     glEnableVertexAttribArray(aTexCoord);
       
  1185     {$ELSE}
       
  1186     glDisableClientState(GL_COLOR_ARRAY);
       
  1187     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
       
  1188     {$ENDIF}
       
  1189 end;
       
  1190 
   870 
  1191 
   871 ////////////////////////////////////////////////////////////////////////////////
  1192 ////////////////////////////////////////////////////////////////////////////////
   872 procedure AddProgress;
  1193 procedure AddProgress;
   873 var r: TSDL_Rect;
  1194 var r: TSDL_Rect;
   874     texsurf: PSDL_Surface;
  1195     texsurf: PSDL_Surface;
   900     r.h:= squaresize;
  1221     r.h:= squaresize;
   901 
  1222 
   902     DrawTextureFromRect( -squaresize div 2, (cScreenHeight - squaresize) shr 1, @r, ProgrTex);
  1223     DrawTextureFromRect( -squaresize div 2, (cScreenHeight - squaresize) shr 1, @r, ProgrTex);
   903 
  1224 
   904     SwapBuffers;
  1225     SwapBuffers;
       
  1226 
   905     inc(Step);
  1227     inc(Step);
   906 end;
  1228 end;
   907 
  1229 
   908 procedure FinishProgress;
  1230 procedure FinishProgress;
   909 begin
  1231 begin
  1176         ResetWorldTex;
  1498         ResetWorldTex;
  1177         //uTextures.freeModule; //DEBUG ONLY
  1499         //uTextures.freeModule; //DEBUG ONLY
  1178     {$ENDIF}
  1500     {$ENDIF}
  1179         AddFileLog('Freeing old primary surface...');
  1501         AddFileLog('Freeing old primary surface...');
  1180     {$IFNDEF SDL13}        
  1502     {$IFNDEF SDL13}        
       
  1503     {$IFNDEF WEBGL}
  1181         SDL_FreeSurface(SDLPrimSurface);
  1504         SDL_FreeSurface(SDLPrimSurface);
  1182         SDLPrimSurface:= nil;
  1505         SDLPrimSurface:= nil;
       
  1506     {$ENDIF}
  1183     {$ENDIF}
  1507     {$ENDIF}
  1184 {$ENDIF}
  1508 {$ENDIF}
  1185         end;
  1509         end;
  1186 
  1510 
  1187     // these attributes must be set up before creating the sdl window
  1511     // these attributes must be set up before creating the sdl window
  1228     {$IFDEF WIN32}SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));{$ENDIF}
  1552     {$IFDEF WIN32}SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));{$ENDIF}
  1229         end;
  1553         end;
  1230 {$ENDIF}
  1554 {$ENDIF}
  1231 
  1555 
  1232     SetupOpenGL();
  1556     SetupOpenGL();
       
  1557 
  1233     if reinit then
  1558     if reinit then
  1234         begin
  1559         begin
  1235         // clean the window from any previous content
  1560         // clean the window from any previous content
  1236         glClear(GL_COLOR_BUFFER_BIT);
  1561         glClear(GL_COLOR_BUFFER_BIT);
  1237         if SuddenDeathDmg then
  1562         if SuddenDeathDmg then
  1259     cScaleFactor:= 2.0;
  1584     cScaleFactor:= 2.0;
  1260     Step:= 0;
  1585     Step:= 0;
  1261     ProgrTex:= nil;
  1586     ProgrTex:= nil;
  1262     SupportNPOTT:= false;
  1587     SupportNPOTT:= false;
  1263 
  1588 
       
  1589 {$IFDEF WEBGL}
       
  1590     OpenGLSetupedBefore := false;
       
  1591 {$ENDIF}
       
  1592 
  1264     // init all ammo name texture pointers
  1593     // init all ammo name texture pointers
  1265     for ai:= Low(TAmmoType) to High(TAmmoType) do
  1594     for ai:= Low(TAmmoType) to High(TAmmoType) do
  1266     begin
  1595     begin
  1267         Ammoz[ai].NameTex := nil;
  1596         Ammoz[ai].NameTex := nil;
  1268     end;
  1597     end;
  1277 {$ENDIF}
  1606 {$ENDIF}
  1278 end;
  1607 end;
  1279 
  1608 
  1280 procedure freeModule;
  1609 procedure freeModule;
  1281 begin
  1610 begin
       
  1611 {$IFDEF GL2}
       
  1612     glDeleteProgram(shaderMain);
       
  1613     glDeleteProgram(shaderWater);
       
  1614     glDeleteBuffers(1, @vBuffer);
       
  1615     glDeleteBuffers(1, @tBuffer);
       
  1616     glDeleteBuffers(1, @cBuffer);
       
  1617 {$ENDIF}
  1282     StoreRelease(false);
  1618     StoreRelease(false);
  1283     TTF_Quit();
  1619     TTF_Quit();
  1284 {$IFDEF SDL13}
  1620 {$IFDEF SDL13}
  1285     SDL_GL_DeleteContext(SDLGLcontext);
  1621     SDL_GL_DeleteContext(SDLGLcontext);
  1286     SDL_DestroyWindow(SDLwindow);
  1622     SDL_DestroyWindow(SDLwindow);