Sunday, October 3, 2010

BB, part 2, creating a sprite

uses
  BB.Screen.D3D, BB.Screen.Types, BB.Input.Keys, BB.Colors, BB.Screen,
  BB.Screen.Surfaces, BB.Screen.Sprites, BB.Screen.Engines;


procedure TForm1.FormActivate(Sender: TObject);
var
  DD: TGraphicDriver;
  e: TSpriteEngine;
  demon, hero: TMaskedSprite;
  x, y: integer;

begin
  DD := TD3DDriver.Create;
  try
    DD.WindowHandle := WindowHandle;
    DD.Width := Width;
    DD.Height := Height;
    DD.BPP := 32;
    DD.ScreenFlags := [sfClear];
    DD.MaxFPS := 100;
    DD.Initialize;

    e := TSpriteEngine.Create;
    e.FileNames.Add('demon.bmp');
    e.FileNames.Add('fire.bmp');
    e.FileNames.Add('hero.bmp');
    e.LoadFile;

    demon := TMaskedSprite.Create;
    demon.Engine := e;
    demon.Masked := True;
    demon.AnimationName := 'demon';
    demon.GoCenter;
    demon.CollisionShape := csPrecise;

    hero := TMaskedSprite.Create;
    hero.Engine := e;
    hero.Masked := True;
    hero.AnimationName := 'hero';
    hero.GoCenterY;
    hero.CollisionShape := csPrecise;

    repeat
      DD.BackgroundColor := 0;

      if Inkey(VK_LEFT) then
        hero.x := hero.x - 1
      else if Inkey(VK_RIGHT) then
        hero.x := hero.x + 1;

      if Inkey(VK_UP) then
        hero.y := hero.y - 1
      else if Inkey(VK_DOWN) then
        hero.y := hero.y + 1;

      hero.Masked := True;
      demon.Masked := True;
      if hero.Collision(demon, x, y) then
        DD.BackgroundColor := TRGB.Blue.ToInt(32);

      hero.Masked := False;
      demon.Masked := False;
      if hero.Collision(demon, x, y) then
        DD.BackgroundColor := TRGB.Red.ToInt(32);

      DD.BeginRender;
      e.Render;
      DD.EndRender;

      Application.ProcessMessages;
    until Inkey(VK_ESCAPE);

  finally
    DD.Free;
  end;

  Close;
end;

end.



A TSpriteEngine is a container of sprites that also offers some loading methods, one of them is for every bitmap set create a .ini that configures the behaviour:

[cfg]
Method=Fix
Width=240
Height=144

[demon]
Frames=2,1,0
Type=PingPong
Cadency=10

[demon_mask]
Frames=5,4,3
Type=PingPong
Cadency=10




This way the engine knows what to do once you assign "demon" no the AnimationName property of the TSpriteAnimation class. The possible values for the [cfg] method are: 
  • Manual  (you set the values manually via Index option)
  • Circular (once the last frame is reached, next frame is the first one)
  • PingPong (once the last frame is reached, it goes back to the first one and again...)
  • Lineal (once the last frame is reached, nothing happens...)
  • LinealFree (once the last frame is reached, the sprite is freed)


No comments:

Post a Comment