hedgewars/uStore.pas
branchwebgl
changeset 8330 aaefa587e277
parent 8096 453917e94e55
parent 8309 edcfb5e43245
child 8336 4877d7333818
equal deleted inserted replaced
8116:d24257910f8d 8330:aaefa587e277
    34 procedure RenderHealth(var Hedgehog: THedgehog);
    34 procedure RenderHealth(var Hedgehog: THedgehog);
    35 procedure AddProgress;
    35 procedure AddProgress;
    36 procedure FinishProgress;
    36 procedure FinishProgress;
    37 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    37 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    38 
    38 
    39 // loads an image from the game's data files
    39 // loads an image from the games data files
    40 function  LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    40 function  LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    41 // like LoadDataImage but uses altPath as fallback-path if file not found/loadable in path
    41 // like LoadDataImage but uses altPath as fallback-path if file not found/loadable in path
    42 function  LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    42 function  LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    43 // like LoadDataImage but uses altFile as fallback-filename if file cannot be loaded
    43 // like LoadDataImage but uses altFile as fallback-filename if file cannot be loaded
    44 function  LoadDataImageAltFile(const path: TPathType; const filename, altFile: shortstring; imageFlags: LongInt): PSDL_Surface;
    44 function  LoadDataImageAltFile(const path: TPathType; const filename, altFile: shortstring; imageFlags: LongInt): PSDL_Surface;
    68 procedure SetColorPointer(p: Pointer;n: Integer);
    68 procedure SetColorPointer(p: Pointer;n: Integer);
    69 procedure BeginWater;
    69 procedure BeginWater;
    70 procedure EndWater;
    70 procedure EndWater;
    71 
    71 
    72 implementation
    72 implementation
    73 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    73 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    74     , uPhysFSLayer
    74     , uPhysFSLayer
    75     , uDebug
    75     , uDebug
    76     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    76     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    77     {$IF NOT DEFINED(SDL13) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    77     {$IF NOT DEFINED(SDL13) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
    78 
    78 
    83     SDLwindow: PSDL_Window;
    83     SDLwindow: PSDL_Window;
    84     SDLGLcontext: PSDL_GLContext;
    84     SDLGLcontext: PSDL_GLContext;
    85 {$ELSE}
    85 {$ELSE}
    86     SDLPrimSurface: PSDL_Surface;
    86     SDLPrimSurface: PSDL_Surface;
    87 {$ENDIF}
    87 {$ENDIF}
       
    88     squaresize : LongInt;
       
    89     numsquares : LongInt;
       
    90     ProgrTex: PTexture;
    88 
    91 
    89 {$IFDEF GL2}
    92 {$IFDEF GL2}
    90     shaderMain: GLuint;
    93     shaderMain: GLuint;
    91     shaderWater: GLuint;
    94     shaderWater: GLuint;
    92 
    95 
    94 {$ENDIF}
    97 {$ENDIF}
    95 
    98 
    96 {$IFDEF WEBGL}
    99 {$IFDEF WEBGL}
    97     OpenGLSetupedBefore : boolean;
   100     OpenGLSetupedBefore : boolean;
    98 {$ENDIF}
   101 {$ENDIF}
       
   102 
       
   103 const
       
   104     cHHFileName = 'Hedgehog';
       
   105     cCHFileName = 'Crosshair';
    99 
   106 
   100 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
   107 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
   101 var w, h: LongInt;
   108 var w, h: LongInt;
   102     tmpsurf: PSDL_Surface;
   109     tmpsurf: PSDL_Surface;
   103     clr: TSDL_Color;
   110     clr: TSDL_Color;
   217                 if (Gear <> nil) and (BotLevel > 0) then
   224                 if (Gear <> nil) and (BotLevel > 0) then
   218                     begin
   225                     begin
   219                     foundBot:= true;
   226                     foundBot:= true;
   220                     // initially was going to do the highest botlevel of the team, but for now, just apply if entire team has same bot level
   227                     // initially was going to do the highest botlevel of the team, but for now, just apply if entire team has same bot level
   221                     if maxLevel = -1 then maxLevel:= BotLevel
   228                     if maxLevel = -1 then maxLevel:= BotLevel
   222                     else if (maxLevel > 0) and (maxLevel <> BotLevel) then maxLevel:= 0; 
   229                     else if (maxLevel > 0) and (maxLevel <> BotLevel) then maxLevel:= 0;
   223                     //if (maxLevel > 0) and (BotLevel < maxLevel) then maxLevel:= BotLevel
   230                     //if (maxLevel > 0) and (BotLevel < maxLevel) then maxLevel:= BotLevel
   224                     end
   231                     end
   225                 else if Gear <> nil then  maxLevel:= 0;
   232                 else if Gear <> nil then  maxLevel:= 0;
   226 
   233 
   227         if foundBot then
   234         if foundBot then
   228             begin
   235             begin
   229             // disabled the plain flag - I think it looks ok even w/ full bars obscuring CPU
   236             // disabled the plain flag - I think it looks ok even w/ full bars obscuring CPU
   230             //if (maxLevel > 0) and (maxLevel < 3) then Flag:= 'cpu_plain' else 
   237             //if (maxLevel > 0) and (maxLevel < 3) then Flag:= 'cpu_plain' else
   231             Flag:= 'cpu'
   238             Flag:= 'cpu'
   232             end
   239             end
   233         else if (Flag = 'cpu') or (Flag = 'cpu_plain') then
   240         else if (Flag = 'cpu') or (Flag = 'cpu_plain') then
   234                 Flag:= 'hedgewars';
   241                 Flag:= 'hedgewars';
   235 
   242 
   236         flagsurf:= LoadDataImageAltFile(ptFlags, Flag, 'hedgewars', ifNone);
   243         flagsurf:= LoadDataImageAltFile(ptFlags, Flag, 'hedgewars', ifNone);
   237         TryDo(flagsurf <> nil, 'Failed to load flag "' + Flag + '" as well as the default flag', true);
   244         TryDo(flagsurf <> nil, 'Failed to load flag "' + Flag + '" as well as the default flag', true);
   238 
   245 
   239         case maxLevel of
   246         case maxLevel of
   240             1: copyToXY(SpritesData[sprBotlevels].Surface, flagsurf, 0, 0); 
   247             1: copyToXY(SpritesData[sprBotlevels].Surface, flagsurf, 0, 0);
   241             2: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 5, 2, 17, 13, 5, 2); 
   248             2: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 5, 2, 17, 13, 5, 2);
   242             3: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 9, 5, 13, 10, 9, 5); 
   249             3: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 9, 5, 13, 10, 9, 5);
   243             4: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 13, 9, 9, 6, 13, 9); 
   250             4: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 13, 9, 9, 6, 13, 9);
   244             5: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 17, 11, 5, 4, 17, 11)
   251             5: copyToXYFromRect(SpritesData[sprBotlevels].Surface, flagsurf, 17, 11, 5, 4, 17, 11)
   245             end;
   252             end;
   246 
   253 
   247         copyToXY(flagsurf, texsurf, 2, 2);
   254         copyToXY(flagsurf, texsurf, 2, 2);
   248         SDL_FreeSurface(flagsurf);
   255         SDL_FreeSurface(flagsurf);
   400                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, priority);
   407                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, priority);
   401 // This should maybe be flagged. It wastes quite a bit of memory.
   408 // This should maybe be flagged. It wastes quite a bit of memory.
   402                 if not reload then
   409                 if not reload then
   403                     begin
   410                     begin
   404 {$IFDEF USE_CONTEXT_RESTORE}
   411 {$IFDEF USE_CONTEXT_RESTORE}
   405                     Surface:= tmpsurf 
   412                     Surface:= tmpsurf
   406 {$ELSE}
   413 {$ELSE}
   407                     if saveSurf then
   414                     if saveSurf then
   408                         Surface:= tmpsurf
   415                         Surface:= tmpsurf
   409                     else
   416                     else
   410                         SDL_FreeSurface(tmpsurf)
   417                         SDL_FreeSurface(tmpsurf)
   462 IMG_Quit();
   469 IMG_Quit();
   463 
   470 
   464 WriteLnToConsole('Leaving StoreLoad');
   471 WriteLnToConsole('Leaving StoreLoad');
   465 end;
   472 end;
   466 
   473 
   467 {$IF NOT DEFINED(S3D_DISABLED) OR DEFINED(USE_VIDEO_RECORDING)}
   474 {$IF DEFINED(USE_S3D_RENDERING) OR DEFINED(USE_VIDEO_RECORDING)}
   468 procedure CreateFramebuffer(var frame, depth, tex: GLuint);
   475 procedure CreateFramebuffer(var frame, depth, tex: GLuint);
   469 begin
   476 begin
   470     glGenFramebuffersEXT(1, @frame);
   477     glGenFramebuffersEXT(1, @frame);
   471     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frame);
   478     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frame);
   472     glGenRenderbuffersEXT(1, @depth);
   479     glGenRenderbuffersEXT(1, @depth);
   564         end;
   571         end;
   565 {$IFDEF USE_VIDEO_RECORDING}
   572 {$IFDEF USE_VIDEO_RECORDING}
   566     if defaultFrame <> 0 then
   573     if defaultFrame <> 0 then
   567         DeleteFramebuffer(defaultFrame, depthv, texv);
   574         DeleteFramebuffer(defaultFrame, depthv, texv);
   568 {$ENDIF}
   575 {$ENDIF}
   569 {$IFNDEF S3D_DISABLED}
   576 {$IFDEF USE_S3D_RENDERING}
   570     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   577     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   571         begin
   578         begin
   572         DeleteFramebuffer(framel, depthl, texl);
   579         DeleteFramebuffer(framel, depthl, texl);
   573         DeleteFramebuffer(framer, depthr, texr);
   580         DeleteFramebuffer(framer, depthr, texr);
   574         end
   581         end
   746     while not eof(f) do
   753     while not eof(f) do
   747     begin
   754     begin
   748         ReadLn(f, line);
   755         ReadLn(f, line);
   749         source:= source + line + #10;
   756         source:= source + line + #10;
   750     end;
   757     end;
   751     
   758 
   752     Close(f);
   759     Close(f);
   753 
   760 
   754     WriteLnToConsole('Compiling shader: ' + Pathz[ptShaders] + '/' + shaderFile);
   761     WriteLnToConsole('Compiling shader: ' + Pathz[ptShaders] + '/' + shaderFile);
   755 
   762 
   756     sourceA:=PChar(source);
   763     sourceA:=PChar(source);
   870         MaxTextureSize:= 1024;
   877         MaxTextureSize:= 1024;
   871         AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
   878         AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
   872         end
   879         end
   873     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
   880     else if (MaxTextureSize < 1024) and (MaxTextureSize >= 512) then
   874         begin
   881         begin
   875         cReducedQuality := cReducedQuality or rqNoBackground;  
   882         cReducedQuality := cReducedQuality or rqNoBackground;
   876         AddFileLog('Texture size too small for backgrounds, disabling.');
   883         AddFileLog('Texture size too small for backgrounds, disabling.');
   877         end;
   884         end;
   878 
   885 
   879 (*
   886 (*
   880     // find out which gpu we are using (for extension compatibility maybe?)
   887     // find out which gpu we are using (for extension compatibility maybe?)
   986 
   993 
   987     Tint(255, 255, 255, 255);
   994     Tint(255, 255, 255, 255);
   988     UpdateModelviewProjection;
   995     UpdateModelviewProjection;
   989 {$ENDIF}
   996 {$ENDIF}
   990 
   997 
   991 {$IFNDEF S3D_DISABLED}
   998 {$IFNDEF USE_S3D_RENDERING}
   992     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   999     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   993     begin
  1000     begin
   994         // prepare left and right frame buffers and associated textures
  1001         // prepare left and right frame buffers and associated textures
   995         if glLoadExtension('GL_EXT_framebuffer_object') then
  1002         if glLoadExtension('GL_EXT_framebuffer_object') then
   996             begin
  1003             begin
  1015     hglLoadIdentity();
  1022     hglLoadIdentity();
  1016     hglScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
  1023     hglScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
  1017     hglTranslatef(0, -cScreenHeight / 2, 0);
  1024     hglTranslatef(0, -cScreenHeight / 2, 0);
  1018 
  1025 
  1019     EnableTexture(True);
  1026     EnableTexture(True);
  1020     
  1027 
  1021     glEnableVertexAttribArray(aVertex);
  1028     glEnableVertexAttribArray(aVertex);
  1022     glEnableVertexAttribArray(aTexCoord);
  1029     glEnableVertexAttribArray(aTexCoord);
  1023     glGenBuffers(1, @vBuffer);
  1030     glGenBuffers(1, @vBuffer);
  1024     glGenBuffers(1, @tBuffer);
  1031     glGenBuffers(1, @tBuffer);
  1025     glGenBuffers(1, @cBuffer);
  1032     glGenBuffers(1, @cBuffer);
  1118     UpdateModelviewProjection;
  1125     UpdateModelviewProjection;
  1119     {$ELSE}
  1126     {$ELSE}
  1120     glMatrixMode(GL_PROJECTION);
  1127     glMatrixMode(GL_PROJECTION);
  1121     glLoadMatrixf(@mProjection[0, 0]);
  1128     glLoadMatrixf(@mProjection[0, 0]);
  1122     glMatrixMode(GL_MODELVIEW);
  1129     glMatrixMode(GL_MODELVIEW);
  1123     {$ENDIF}    
  1130     {$ENDIF}
  1124 end;
  1131 end;
  1125 *)
  1132 *)
  1126 
  1133 
  1127 procedure SetScale(f: GLfloat);
  1134 procedure SetScale(f: GLfloat);
  1128 begin
  1135 begin
  1201         ProgrTex:= Surface2Tex(texsurf, false);
  1208         ProgrTex:= Surface2Tex(texsurf, false);
  1202 
  1209 
  1203         squaresize:= texsurf^.w shr 1;
  1210         squaresize:= texsurf^.w shr 1;
  1204         numsquares:= texsurf^.h div squaresize;
  1211         numsquares:= texsurf^.h div squaresize;
  1205         SDL_FreeSurface(texsurf);
  1212         SDL_FreeSurface(texsurf);
  1206 
  1213         with mobileRecord do
  1207         uMobile.GameLoading();
  1214             if GameLoading <> nil then
       
  1215                 GameLoading();
       
  1216 
  1208         end;
  1217         end;
  1209 
  1218 
  1210     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
  1219     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
  1211 
  1220 
  1212     glClear(GL_COLOR_BUFFER_BIT);
  1221     glClear(GL_COLOR_BUFFER_BIT);
  1226     inc(Step);
  1235     inc(Step);
  1227 end;
  1236 end;
  1228 
  1237 
  1229 procedure FinishProgress;
  1238 procedure FinishProgress;
  1230 begin
  1239 begin
  1231     uMobile.GameLoaded();
  1240     with mobileRecord do
       
  1241         if GameLoaded <> nil then
       
  1242             GameLoaded();
  1232     WriteLnToConsole('Freeing progress surface... ');
  1243     WriteLnToConsole('Freeing progress surface... ');
  1233     FreeTexture(ProgrTex);
  1244     FreeTexture(ProgrTex);
  1234     ProgrTex:= nil;
  1245     ProgrTex:= nil;
  1235     Step:= 0
  1246     Step:= 0
  1236 end;
  1247 end;
  1500         ResetLand;
  1511         ResetLand;
  1501         ResetWorldTex;
  1512         ResetWorldTex;
  1502         //uTextures.freeModule; //DEBUG ONLY
  1513         //uTextures.freeModule; //DEBUG ONLY
  1503     {$ENDIF}
  1514     {$ENDIF}
  1504         AddFileLog('Freeing old primary surface...');
  1515         AddFileLog('Freeing old primary surface...');
  1505     {$IFNDEF SDL13}        
  1516     {$IFNDEF SDL13}
  1506     {$IFNDEF WEBGL}
  1517     {$IFNDEF WEBGL}
  1507         SDL_FreeSurface(SDLPrimSurface);
  1518         SDL_FreeSurface(SDLPrimSurface);
  1508         SDLPrimSurface:= nil;
  1519         SDLPrimSurface:= nil;
  1509     {$ENDIF}
  1520     {$ENDIF}
  1510     {$ENDIF}
  1521     {$ENDIF}
  1548         begin
  1559         begin
  1549     {$IFDEF WIN32}
  1560     {$IFDEF WIN32}
  1550         s:= SDL_getenv('SDL_VIDEO_CENTERED');
  1561         s:= SDL_getenv('SDL_VIDEO_CENTERED');
  1551         SDL_putenv('SDL_VIDEO_CENTERED=1');
  1562         SDL_putenv('SDL_VIDEO_CENTERED=1');
  1552     {$ENDIF}
  1563     {$ENDIF}
  1553         SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, cBits, flags);
  1564         SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, 0, flags);
  1554         SDLTry(SDLPrimSurface <> nil, true);
  1565         SDLTry(SDLPrimSurface <> nil, true);
  1555     {$IFDEF WIN32}SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));{$ENDIF}
  1566     {$IFDEF WIN32}SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));{$ENDIF}
  1556         end;
  1567         end;
  1557 {$ENDIF}
  1568 {$ENDIF}
  1558 
  1569 
  1562         begin
  1573         begin
  1563         // clean the window from any previous content
  1574         // clean the window from any previous content
  1564         glClear(GL_COLOR_BUFFER_BIT);
  1575         glClear(GL_COLOR_BUFFER_BIT);
  1565         if SuddenDeathDmg then
  1576         if SuddenDeathDmg then
  1566             glClearColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255, 0.99)
  1577             glClearColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255, 0.99)
  1567         else if ((cReducedQuality and rqNoBackground) = 0) then 
  1578         else if ((cReducedQuality and rqNoBackground) = 0) then
  1568             glClearColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255, 0.99)
  1579             glClearColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255, 0.99)
  1569         else
  1580         else
  1570             glClearColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255, 0.99);
  1581             glClearColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255, 0.99);
  1571 
  1582 
  1572         // reload everything we had before
  1583         // reload everything we had before