hedgewars/uVisualGears.pas
changeset 805 4d75759b38bd
parent 803 3f73901a350a
child 806 d397c502a5dd
equal deleted inserted replaced
804:30f687f380fa 805:4d75759b38bd
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17  *)
    18 
    18 
    19 unit uVisualGears;
    19 unit uVisualGears;
    20 interface
    20 interface
    21 uses SDLh, uConsts, uFloat;
    21 uses SDLh, uConsts, uFloat, GL;
    22 {$INCLUDE options.inc}
    22 {$INCLUDE options.inc}
    23 const AllInactive: boolean = false;
    23 const AllInactive: boolean = false;
    24 
    24 
    25 type PVisualGear = ^TVisualGear;
    25 type PVisualGear = ^TVisualGear;
    26      TVGearStepProcedure = procedure (Gear: PVisualGear; Steps: Longword);
    26      TVGearStepProcedure = procedure (Gear: PVisualGear; Steps: Longword);
    27      TVisualGear = record
    27      TVisualGear = record
    28              NextGear, PrevGear: PVisualGear;
    28              NextGear, PrevGear: PVisualGear;
    29              State : Longword;
    29              Frame,
       
    30              FrameTicks: Longword;
    30              X : hwFloat;
    31              X : hwFloat;
    31              Y : hwFloat;
    32              Y : hwFloat;
    32              dX: hwFloat;
    33              dX: hwFloat;
    33              dY: hwFloat;
    34              dY: hwFloat;
       
    35              Angle, dAngle: real;
    34              Kind: TVisualGearType;
    36              Kind: TVisualGearType;
    35              doStep: TVGearStepProcedure;
    37              doStep: TVGearStepProcedure;
    36              end;
    38              end;
    37 
    39 
    38 function  AddVisualGear(X, Y: LongInt; Kind: TVisualGearType; dX, dY: hwFloat): PVisualGear;
    40 function  AddVisualGear(X, Y: LongInt; Kind: TVisualGearType): PVisualGear;
    39 procedure ProcessVisualGears(Steps: Longword);
    41 procedure ProcessVisualGears(Steps: Longword);
    40 procedure DrawVisualGears();
    42 procedure DrawVisualGears();
    41 procedure AddClouds;
    43 procedure AddClouds;
    42 
    44 
    43 var VisualGearsList: PVisualGear = nil;
    45 var VisualGearsList: PVisualGear = nil;
       
    46     vobFrameTicks, vobFramesCount: Longword;
       
    47     vobVelocity, vobFallSpeed: LongInt;
    44 
    48 
    45 implementation
    49 implementation
    46 uses uWorld, uMisc, uStore, GL;
    50 uses uWorld, uMisc, uStore;
    47 
    51 
    48 // ==================================================================
    52 // ==================================================================
    49 procedure doStepFlake(Gear: PVisualGear; Steps: Longword);
    53 procedure doStepFlake(Gear: PVisualGear; Steps: Longword);
    50 begin
    54 begin
       
    55 with Gear^ do
       
    56   begin
       
    57   inc(FrameTicks, Steps);
       
    58   if FrameTicks > vobFrameTicks then
       
    59     begin
       
    60     dec(FrameTicks, vobFrameTicks);
       
    61     inc(Frame);
       
    62     if Frame = vobFramesCount then Frame:= 0
       
    63     end
       
    64   end;
       
    65 
       
    66 Gear^.X:= Gear^.X + (cWindSpeed * 200 + Gear^.dX) * Steps;
       
    67 Gear^.Y:= Gear^.Y + (Gear^.dY + cGravity * vobVelocity) * Steps;
       
    68 Gear^.Angle:= Gear^.Angle + Gear^.dAngle;
       
    69 
       
    70 if hwRound(Gear^.X) < -cScreenWidth - 64 then Gear^.X:= int2hwFloat(cScreenWidth + 2048) else
       
    71 if hwRound(Gear^.X) > cScreenWidth + 2048 then Gear^.X:= int2hwFloat(-cScreenWidth - 64);
       
    72 if hwRound(Gear^.Y) > 1024 then Gear^.Y:= - _128
    51 end;
    73 end;
    52 
    74 
    53 procedure doStepCloud(Gear: PVisualGear; Steps: Longword);
    75 procedure doStepCloud(Gear: PVisualGear; Steps: Longword);
    54 begin
    76 begin
    55 Gear^.X:= Gear^.X + (cWindSpeed * 200 + Gear^.dX) * Steps;
    77 Gear^.X:= Gear^.X + (cWindSpeed * 200 + Gear^.dX) * Steps;
    56 if hwRound(Gear^.Y) > -160 then Gear^.dY:= Gear^.dY - _1div50000
    78 if hwRound(Gear^.Y) > -160 then Gear^.dY:= Gear^.dY - _1div50000
    57                            else Gear^.dY:= Gear^.dY + _1div50000;
    79                            else Gear^.dY:= Gear^.dY + _1div50000;
       
    80 
    58 Gear^.Y:= Gear^.Y + Gear^.dY * Steps;
    81 Gear^.Y:= Gear^.Y + Gear^.dY * Steps;
       
    82 
    59 if hwRound(Gear^.X) < -cScreenWidth - 256 then Gear^.X:= int2hwFloat(cScreenWidth + 2048) else
    83 if hwRound(Gear^.X) < -cScreenWidth - 256 then Gear^.X:= int2hwFloat(cScreenWidth + 2048) else
    60 if hwRound(Gear^.X) > cScreenWidth + 2048 then Gear^.X:= int2hwFloat(-cScreenWidth - 256)
    84 if hwRound(Gear^.X) > cScreenWidth + 2048 then Gear^.X:= int2hwFloat(-cScreenWidth - 256)
    61 end;
    85 end;
    62 
    86 
    63 // ==================================================================
    87 // ==================================================================
    65                         (
    89                         (
    66                           @doStepFlake,
    90                           @doStepFlake,
    67                           @doStepCloud
    91                           @doStepCloud
    68                         );
    92                         );
    69 
    93 
    70 function  AddVisualGear(X, Y: LongInt; Kind: TVisualGearType; dX, dY: hwFloat): PVisualGear;
    94 function  AddVisualGear(X, Y: LongInt; Kind: TVisualGearType): PVisualGear;
    71 var Result: PVisualGear;
    95 var Result: PVisualGear;
    72 begin
    96 begin
    73 New(Result);
    97 New(Result);
    74 FillChar(Result^, sizeof(TVisualGearType), 0);
    98 FillChar(Result^, sizeof(TVisualGearType), 0);
    75 Result^.X:= int2hwFloat(X);
    99 Result^.X:= int2hwFloat(X);
    76 Result^.Y:= int2hwFloat(Y);
   100 Result^.Y:= int2hwFloat(Y);
    77 Result^.Kind := Kind;
   101 Result^.Kind := Kind;
    78 Result^.dX:= dX;
       
    79 Result^.dY:= dY;
       
    80 Result^.doStep:= doStepHandlers[Kind];
   102 Result^.doStep:= doStepHandlers[Kind];
    81 
   103 
    82 case Kind of
   104 case Kind of
    83    vgtCloud: Result^.State:= random(4);
   105    vgtFlake: with Result^ do
       
   106                begin
       
   107                FrameTicks:= random(vobFrameTicks);
       
   108                Frame:= random(vobFramesCount);
       
   109                Angle:= random * 360;
       
   110                dx.isNegative:= random(2) = 0;
       
   111                dx.QWordValue:= random(100000000);
       
   112                dy.isNegative:= false;
       
   113                dy.QWordValue:= random(20);
       
   114                dAngle:= (random(2) * 2 - 1) * (1 + random) * vobVelocity / 1000
       
   115                end;
       
   116    vgtCloud: with Result^ do
       
   117                begin
       
   118                Frame:= random(4);
       
   119                dx.isNegative:= random(2) = 0;
       
   120                dx.QWordValue:= random(214748364);
       
   121                dy.isNegative:= random(2) = 0;
       
   122                dy.QWordValue:= 21474836 + random(64424509)
       
   123                end;
    84      end;
   124      end;
    85 
   125 
    86 if VisualGearsList <> nil then
   126 if VisualGearsList <> nil then
    87    begin
   127    begin
    88    VisualGearsList^.PrevGear:= Result;
   128    VisualGearsList^.PrevGear:= Result;
   113 begin
   153 begin
   114 Gear:= VisualGearsList;
   154 Gear:= VisualGearsList;
   115 while Gear <> nil do
   155 while Gear <> nil do
   116       begin
   156       begin
   117       case Gear^.Kind of
   157       case Gear^.Kind of
   118            vgtFlake: ;
   158            vgtFlake: if vobVelocity = 0 then
   119            vgtCloud: DrawSprite(sprCloud, hwRound(Gear^.X) + WorldDx, hwRound(Gear^.Y) + WorldDy, Gear^.State, nil);
   159                         DrawSprite(sprFlake, hwRound(Gear^.X) + WorldDx, hwRound(Gear^.Y) + WorldDy, Gear^.Frame, nil)
       
   160                      else
       
   161                         DrawRotated(sprFlake, hwRound(Gear^.X) + WorldDx, hwRound(Gear^.Y) + WorldDy, Gear^.Angle);
       
   162 
       
   163            vgtCloud: DrawSprite(sprCloud, hwRound(Gear^.X) + WorldDx, hwRound(Gear^.Y) + WorldDy, Gear^.Frame, nil);
   120               end;
   164               end;
   121       Gear:= Gear^.NextGear
   165       Gear:= Gear^.NextGear
   122       end;
   166       end;
   123 end;
   167 end;
   124 
   168 
   125 procedure AddClouds;
   169 procedure AddClouds;
   126 var i: LongInt;
   170 var i: LongInt;
   127     dx, dy: hwFloat;
   171     dx, dy: hwFloat;
   128 begin
   172 begin
   129 for i:= 0 to cCloudsNumber do
   173 for i:= 0 to cCloudsNumber do
   130     begin
   174     AddVisualGear( - cScreenWidth + i * ((cScreenWidth * 2 + 2304) div cCloudsNumber), -140, vgtCloud)
   131     dx.isNegative:= random(2) = 1;
       
   132     dx.QWordValue:= random(214748364);
       
   133     dy.isNegative:= (i and 1) = 1;
       
   134     dy.QWordValue:= 21474836 + random(64424509);
       
   135     AddVisualGear( - cScreenWidth + i * ((cScreenWidth * 2 + 2304) div cCloudsNumber), -140,
       
   136              vgtCloud, dx, dy)
       
   137     end
       
   138 end;
   175 end;
   139 
   176 
   140 initialization
   177 initialization
   141 
   178 
   142 finalization
   179 finalization