hedgewars/uWorld.pas
branchexperimental3D
changeset 3690 c247dee9e1c0
parent 3688 fd8c22a358de
child 3692 bbec1275e46f
equal deleted inserted replaced
3688:fd8c22a358de 3690:c247dee9e1c0
    20 
    20 
    21 unit uWorld;
    21 unit uWorld;
    22 interface
    22 interface
    23 uses SDLh, uGears, uConsts, uFloat, uRandom;
    23 uses SDLh, uGears, uConsts, uFloat, uRandom;
    24 
    24 
    25 type TRenderMode = (rmDefault, rmLeftEye, rmRightEye);
       
    26 
    25 
    27 var FollowGear: PGear;
    26 var FollowGear: PGear;
    28     WindBarWidth: LongInt;
    27     WindBarWidth: LongInt;
    29     bShowAmmoMenu: boolean;
    28     bShowAmmoMenu: boolean;
    30     bSelected: boolean;
    29     bSelected: boolean;
    43 procedure initModule;
    42 procedure initModule;
    44 procedure freeModule;
    43 procedure freeModule;
    45 
    44 
    46 procedure InitWorld;
    45 procedure InitWorld;
    47 procedure DrawWorld(Lag: LongInt);
    46 procedure DrawWorld(Lag: LongInt);
    48 procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode);
       
    49 procedure AddCaption(s: shortstring; Color: Longword; Group: TCapGroup);
    47 procedure AddCaption(s: shortstring; Color: Longword; Group: TCapGroup);
    50 procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt);
    48 procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt);
    51 procedure HideMission;
    49 procedure HideMission;
    52 procedure ShakeCamera(amount: LongWord);
    50 procedure ShakeCamera(amount: LongWord);
    53 procedure MoveCamera;
    51 procedure MoveCamera;
    71     SoundTimerTicks: Longword;
    69     SoundTimerTicks: Longword;
    72     prevPoint: TPoint;
    70     prevPoint: TPoint;
    73     amSel: TAmmoType = amNothing;
    71     amSel: TAmmoType = amNothing;
    74     missionTex: PTexture;
    72     missionTex: PTexture;
    75     missionTimer: LongInt;
    73     missionTimer: LongInt;
    76     stereoDepth: GLfloat = 0;
       
    77 
       
    78 const cStereo_Sky     = 0.0750;
       
    79       cStereo_Horizon = 0.0250;
       
    80       cStereo_Water   = 0.0125;
       
    81 
    74 
    82 procedure InitWorld;
    75 procedure InitWorld;
    83 var i, t: LongInt;
    76 var i, t: LongInt;
    84     cp: PClan;
    77     cp: PClan;
    85     g: ansistring;
    78     g: ansistring;
   533     end
   526     end
   534 end;
   527 end;
   535 
   528 
   536 
   529 
   537 procedure DrawWorld(Lag: LongInt);
   530 procedure DrawWorld(Lag: LongInt);
   538 var cc: array[0..3] of GLfloat;
   531 var i, t: LongInt;
       
   532     r: TSDL_Rect;
       
   533     tdx, tdy: Double;
       
   534     grp: TCapGroup;
       
   535     s: string[15];
       
   536     highlight: Boolean;
       
   537     offset, offsetX, offsetY, ScreenBottom: LongInt;
       
   538     VertexBuffer: array [0..3] of TVertex2f;
   539 begin
   539 begin
   540     if not isPaused then
   540     if not isPaused then
   541     begin
   541     begin
   542         if ZoomValue < zoom then
   542         if ZoomValue < zoom then
   543         begin
   543         begin
   562     //glScalef(1.0, 1.0, 1.0);
   562     //glScalef(1.0, 1.0, 1.0);
   563 
   563 
   564     if not isPaused then
   564     if not isPaused then
   565         MoveCamera;
   565         MoveCamera;
   566 
   566 
   567     if not isStereoEnabled then
       
   568     begin
       
   569         glClear(GL_COLOR_BUFFER_BIT);
       
   570         DrawWorldStereo(Lag, rmDefault)
       
   571     end
       
   572     else
       
   573     begin
       
   574         // create left fb
       
   575         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framel);
       
   576         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
       
   577         DrawWorldStereo(Lag, rmLeftEye);
       
   578 
       
   579         // create right fb
       
   580         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framer);
       
   581         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
       
   582         DrawWorldStereo(0, rmRightEye);
       
   583 
       
   584         // detatch drawing from fbs
       
   585         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
       
   586         glGetFloatv(GL_COLOR_CLEAR_VALUE, @cc);
       
   587         glClearColor(0, 0, 0, 0);
       
   588         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
       
   589         glClearColor(cc[0], cc[1], cc[2], cc[3]);
       
   590         SetScale(cDefaultZoomLevel);
       
   591 
       
   592         // enable gl stuff
       
   593         glEnable(GL_TEXTURE_2D);
       
   594         glEnable(GL_BLEND);
       
   595         glBlendFunc(GL_ONE, GL_ONE);
       
   596 
       
   597         // draw left frame
       
   598         glBindTexture(GL_TEXTURE_2D, texl);
       
   599         glColor3f(0.0, 1.0, 1.0);
       
   600         glBegin(GL_QUADS);
       
   601             glTexCoord2f(0.0, 0.0);
       
   602             glVertex2d(cScreenWidth / -2, cScreenHeight);
       
   603             glTexCoord2f(1.0, 0.0);
       
   604             glVertex2d(cScreenWidth / 2, cScreenHeight);
       
   605             glTexCoord2f(1.0, 1.0);
       
   606             glVertex2d(cScreenWidth / 2, 0);
       
   607             glTexCoord2f(0.0, 1.0);
       
   608             glVertex2d(cScreenWidth / -2, 0);
       
   609         glEnd();
       
   610 
       
   611         // draw right frame
       
   612         glBindTexture(GL_TEXTURE_2D, texr);
       
   613         glColor3f(1.0, 0.0, 0.0);
       
   614         glBegin(GL_QUADS);
       
   615             glTexCoord2f(0.0, 0.0);
       
   616             glVertex2d(cScreenWidth / -2, cScreenHeight);
       
   617             glTexCoord2f(1.0, 0.0);
       
   618             glVertex2d(cScreenWidth / 2, cScreenHeight);
       
   619             glTexCoord2f(1.0, 1.0);
       
   620             glVertex2d(cScreenWidth / 2, 0);
       
   621             glTexCoord2f(0.0, 1.0);
       
   622             glVertex2d(cScreenWidth / -2, 0);
       
   623         glEnd();
       
   624 
       
   625         // reset
       
   626         glColor3f(1.0, 1.0, 1.0);
       
   627         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       
   628         SetScale(zoom);
       
   629     end
       
   630 end;
       
   631 
       
   632 procedure ChangeDepth(rm: TRenderMode; d: GLfloat);
       
   633 begin
       
   634     if rm = rmDefault then exit
       
   635     else if rm = rmRightEye then d:= -d;
       
   636     stereoDepth:= stereoDepth + d;
       
   637     glMatrixMode(GL_PROJECTION);
       
   638     glTranslatef(d, 0, 0);
       
   639     glMatrixMode(GL_MODELVIEW)
       
   640 end;
       
   641  
       
   642 procedure ResetDepth(rm: TRenderMode);
       
   643 begin
       
   644     if rm = rmDefault then exit;
       
   645     glMatrixMode(GL_PROJECTION);
       
   646     glTranslatef(-stereoDepth, 0, 0);
       
   647     glMatrixMode(GL_MODELVIEW);
       
   648     stereoDepth:= 0;
       
   649 end;
       
   650  
       
   651 procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode);
       
   652 var i, t: LongInt;
       
   653     r: TSDL_Rect;
       
   654     tdx, tdy: Double;
       
   655     grp: TCapGroup;
       
   656     s: string[15];
       
   657     highlight: Boolean;
       
   658     offset, offsetX, offsetY, screenBottom: LongInt;
       
   659     scale: GLfloat;
       
   660     VertexBuffer: array [0..3] of TVertex2f;
       
   661 begin
       
   662     if (cReducedQuality and rqNoBackground) = 0 then
   567     if (cReducedQuality and rqNoBackground) = 0 then
   663     begin
   568     begin
   664         // Offsets relative to camera - spare them to wimpier cpus, no bg or flakes for them anyway
   569         // Offsets relative to camera - spare them to wimpier cpus, no bg or flakes for them anyway
   665         ScreenBottom:= (WorldDy - trunc(cScreenHeight/cScaleFactor) - (cScreenHeight div 2) + cWaterLine);
   570         ScreenBottom:= (WorldDy - trunc(cScreenHeight/cScaleFactor) - (cScreenHeight div 2) + cWaterLine);
   666         offsetY:= 10 * min(0, -145 - ScreenBottom);
   571         offsetY:= 10 * min(0, -145 - ScreenBottom);
   967 offsetX:= 8;
   872 offsetX:= 8;
   968 {$ELSE}
   873 {$ELSE}
   969 offsetX:= 10;
   874 offsetX:= 10;
   970 {$ENDIF}
   875 {$ENDIF}
   971 offsetY:= cOffsetY;
   876 offsetY:= cOffsetY;
   972 
   877 inc(Frames);
   973 // don't increment fps when drawing the right frame
   878 
   974 if (RM = rmDefault) or (RM = rmLeftEye) then
   879 if cShowFPS or (GameType = gmtDemo) then inc(CountTicks, Lag);
   975 begin
   880 if (GameType = gmtDemo) and (CountTicks >= 1000) then
   976     inc(Frames);
   881    begin
   977 
   882    i:=GameTicks div 1000;
   978     if cShowFPS or (GameType = gmtDemo) then
   883    t:= i mod 60;
   979         inc(CountTicks, Lag);
   884    s:= inttostr(t);
   980     if (GameType = gmtDemo) and (CountTicks >= 1000) then
   885    if t < 10 then s:= '0' + s;
   981     begin
   886    i:= i div 60;
   982         i:=GameTicks div 1000;
   887    t:= i mod 60;
   983         t:= i mod 60;
   888    s:= inttostr(t) + ':' + s;
   984         s:= inttostr(t);
   889    if t < 10 then s:= '0' + s;
   985         if t < 10 then s:= '0' + s;
   890    s:= inttostr(i div 60) + ':' + s;
   986         i:= i div 60;
       
   987         t:= i mod 60;
       
   988         s:= inttostr(t) + ':' + s;
       
   989         if t < 10 then s:= '0' + s;
       
   990         s:= inttostr(i div 60) + ':' + s;
       
   991    
   891    
   992         if timeTexture <> nil then
   892    if timeTexture <> nil then
   993             FreeTexture(timeTexture);
   893         FreeTexture(timeTexture);
   994         timeTexture:= nil;
   894     timeTexture:= nil;
   995     
   895     
   996         tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
   896    tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
   997         tmpSurface:= doSurfaceConversion(tmpSurface);
   897    tmpSurface:= doSurfaceConversion(tmpSurface);
   998         timeTexture:= Surface2Tex(tmpSurface, false);
   898    timeTexture:= Surface2Tex(tmpSurface, false);
   999         SDL_FreeSurface(tmpSurface)
   899    SDL_FreeSurface(tmpSurface)
  1000     end;
   900    end;
  1001 
   901 
  1002     if timeTexture <> nil then
   902 if timeTexture <> nil then
  1003         DrawTexture((cScreenWidth shr 1) - 20 - timeTexture^.w - offsetY, offsetX + timeTexture^.h+5, timeTexture);
   903    DrawTexture((cScreenWidth shr 1) - 20 - timeTexture^.w - offsetY, offsetX + timeTexture^.h+5, timeTexture);
  1004 
   904 
  1005     if cShowFPS then
   905 if cShowFPS then
  1006     begin
   906    begin
  1007         if CountTicks >= 1000 then
   907    if CountTicks >= 1000 then
  1008         begin
   908       begin
  1009             FPS:= Frames;
   909       FPS:= Frames;
  1010             Frames:= 0;
   910       Frames:= 0;
  1011             CountTicks:= 0;
   911       CountTicks:= 0;
  1012             s:= inttostr(FPS) + ' fps';
   912       s:= inttostr(FPS) + ' fps';
  1013             if fpsTexture <> nil then
   913       if fpsTexture <> nil then
  1014                 FreeTexture(fpsTexture);
   914         FreeTexture(fpsTexture);
  1015             fpsTexture:= nil;
   915     fpsTexture:= nil;
  1016             tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
   916       tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
  1017             tmpSurface:= doSurfaceConversion(tmpSurface);
   917       tmpSurface:= doSurfaceConversion(tmpSurface);
  1018             fpsTexture:= Surface2Tex(tmpSurface, false);
   918       fpsTexture:= Surface2Tex(tmpSurface, false);
  1019             SDL_FreeSurface(tmpSurface)
   919       SDL_FreeSurface(tmpSurface)
  1020         end;
   920       end;
  1021         if fpsTexture <> nil then
   921    if fpsTexture <> nil then
  1022             DrawTexture((cScreenWidth shr 1) - 60 - offsetY, offsetX, fpsTexture);
   922       DrawTexture((cScreenWidth shr 1) - 60 - offsetY, offsetX, fpsTexture);
  1023     end;
   923    end;
  1024 
   924 
  1025     if CountTicks >= 1000 then CountTicks:= 0;
   925 if CountTicks >= 1000 then CountTicks:= 0;
  1026 
   926 
  1027     // lag warning (?)
   927 // lag warning (?)
  1028     inc(SoundTimerTicks, Lag);
   928 inc(SoundTimerTicks, Lag);
  1029 end;
       
  1030 
       
  1031 if SoundTimerTicks >= 50 then
   929 if SoundTimerTicks >= 50 then
  1032    begin
   930    begin
  1033    SoundTimerTicks:= 0;
   931    SoundTimerTicks:= 0;
  1034    if cVolumeDelta <> 0 then
   932    if cVolumeDelta <> 0 then
  1035       begin
   933       begin