Recent

Author Topic: Sprite moving over bitmap  (Read 5792 times)

navyseabear

  • Newbie
  • Posts: 3
Sprite moving over bitmap
« on: January 30, 2018, 01:15:36 pm »
Hello,
I would like to create a simple Sprite and this animate and move it over a Bitmap Background.
Right now I'm working with a TImage as Background and another TImage as Sprite which is loaded out of a TImageList (for animation).
My problem is, that it is very very slow.
Because it should be a program for children (in this case my daughter) there is no need for 3D and OpenGL. It should run on a old Linux machine.
Does anybody have a example how to do it? Just a background image and a moving animated sprite in a good speed?
Right now I'm really lost and need any help.

Tobias
 PS: Please excuse my bad english

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Sprite moving over bitmap
« Reply #1 on: January 30, 2018, 02:45:44 pm »
Does it have to be a GUI app or would a console program be ok?

If a console app, you could use one of the game libraries eg SDL/Allegro or a framework such as Castle Engine.

RAW

  • Hero Member
  • *****
  • Posts: 868
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

navyseabear

  • Newbie
  • Posts: 3
Re: Sprite moving over bitmap
« Reply #3 on: January 30, 2018, 03:51:01 pm »
I will use it under Linux GUI.
Because it is for my daughter, it should not too big or complicated.
I need just the option to move a sprite over a background bitmap.

Tobias

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Sprite moving over bitmap
« Reply #4 on: January 31, 2018, 06:40:56 pm »
I will use it under Linux GUI.
Because it is for my daughter, it should not too big or complicated.
I need just the option to move a sprite over a background bitmap.

Tobias
Then may be you should use a game library like Allegro or SDL.  They're much simpler and efficient for such tasks.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

balazsszekely

  • Guest
Re: Sprite moving over bitmap
« Reply #5 on: January 31, 2018, 06:59:04 pm »
Quote
@navyseabear
I will use it under Linux GUI.
Because it is for my daughter, it should not too big or complicated.
I need just the option to move a sprite over a background bitmap.
You should give BGRABitmap a try. Install the component then test attached project.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Sprite moving over bitmap
« Reply #6 on: January 31, 2018, 11:05:15 pm »
Quote
Because it is for my daughter, it should not too big or complicated.
I need just the option to move a sprite over a background bitmap.
This is the easiest way I can imagine...
Does it work? It totally depends on what speed you need and what PNG size you want to use...  :)
(Tearing Effect?!)
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

navyseabear

  • Newbie
  • Posts: 3
Re: Sprite moving over bitmap
« Reply #7 on: February 01, 2018, 06:28:31 am »
This is the easiest way I can imagine...
Does it work? It totally depends on what speed you need and what PNG size you want to use...  :)
(Tearing Effect?!)

Yes, that is the same way as I did it.
The problem is just, that it gets very slow when I resize the Form and I would like to make it fullscreen. :-(

Tobias

lainz

  • Hero Member
  • *****
  • Posts: 4463
    • https://lainz.github.io/
Re: Sprite moving over bitmap
« Reply #8 on: February 01, 2018, 01:01:07 pm »
You need to redraw only the background parts that changed when moving the object in the window.

First frame: Fill the background only once. Draw the object.

Then in the second frame: draw the part of the background you need to 'erase' (when the object previously was) with only a part of the original background image, scaled and fit perfectly in place. Then draw the object in the background again.

Repeat part 2 for each new frame.

Resize should redraw everything, because scaling changes.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Sprite moving over bitmap
« Reply #9 on: February 01, 2018, 07:02:24 pm »
Without DoubleBuffering it's not looking good....  :)

BTW: I would use OpenGL or any framework which uses GPU assistance...

Code: Pascal  [Select][+][-]
  1. UNIT Unit1;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, ExtCtrls;
  7.  
  8.  TYPE
  9.   TForm1 = Class(TForm)
  10.  
  11.    btnStart: TButton;
  12.    Timer   : TTimer;
  13.    imgLaz  : TImage;
  14.    imgBack : TImage;
  15.  
  16.    Procedure FormCreate    (Sender: TObject);
  17.    Procedure FormDestroy   (Sender: TObject);
  18.    Procedure btnStartClick (Sender: TObject);
  19.    Procedure TimerTimer    (Sender: TObject);
  20.  
  21.     PRIVATE
  22.      x, y, xv, yv, xo, yo: Integer;
  23.      picLaz, picBack: TPicture;
  24.   End;
  25.  
  26.  VAR
  27.   Form1: TForm1;
  28.  
  29. Implementation
  30.  {$R *.LFM}
  31.  
  32.  
  33. Procedure TForm1.FormCreate(Sender: TObject);
  34.  Begin
  35.   picLaz:= TPicture.Create;
  36.   picLaz.Assign(imgLaz.Picture);
  37.  
  38.   picBack:= TPicture.Create;
  39.   picBack.Assign(imgBack.Picture);
  40.  
  41.   imgLaz.Free;
  42.  
  43.   x := 0;
  44.   y := 0;
  45.   xv:= 2;
  46.   yv:= 2;
  47.   xo:= 0;
  48.   yo:= 0;
  49.  End;
  50.  
  51.  
  52. Procedure TForm1.btnStartClick(Sender: TObject);
  53.  Begin
  54.   If Timer.Enabled
  55.   Then Timer.Enabled:= False
  56.   Else Timer.Enabled:= True;
  57.  End;
  58.  
  59.  
  60. Procedure TForm1.TimerTimer(Sender: TObject);
  61.   Var
  62.    rSrc: TRect;
  63.  Begin
  64.   rSrc:= Rect(xo, yo, xo+picLaz.Width, yo+picLaz.Height);
  65.  
  66.   imgBack.Canvas.CopyMode:= cmSrcCopy;
  67.   imgBack.Canvas.CopyRect(rSrc, picBack.Bitmap.Canvas, rSrc);
  68.   imgBack.Canvas.Draw(x, y, picLaz.Graphic);
  69.  
  70.   xo:= x;
  71.   yo:= y;
  72.  
  73.   Inc(x, xv);
  74.   Inc(y, yv);
  75.  
  76.   If x  >= picBack.Width
  77.   Then x:= -picLaz.Width;
  78.  
  79.   If y  >= picBack.Height
  80.   Then y:= -picLaz.Height;
  81.  End;
  82.  
  83.  
  84. Procedure TForm1.FormDestroy(Sender: TObject);
  85.  Begin
  86.   If Assigned(picLaz)
  87.   Then picLaz.Free;
  88.  
  89.   If Assigned(picBack)
  90.   Then picBack.Free;
  91.  End;
  92.  
  93. END.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Sprite moving over bitmap
« Reply #10 on: February 01, 2018, 07:25:53 pm »
I made such a game engine long ago, when I had my first 80386SX, at 25 MHz. With a VGA card. It had many layers and parallax scrolling. I just copied everything to the video buffer, starting with the ones furthest back. On top of each other. That is by far the fastest way to do it.

Ok, I did make an efficient loop in assembler that did the actual checking for transparency and copying. Everything else was Borland Pascal objects (linked lists).

Even with many layers and animated sprites, it always ran at 60 fps (vsync).

And I blew up a monitor by experimenting with custom video modes.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Sprite moving over bitmap
« Reply #11 on: February 01, 2018, 08:45:29 pm »
With DoubleBuffering it's a lot better ...  :)

Code: Pascal  [Select][+][-]
  1. UNIT Unit1;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, ExtCtrls;
  7.  
  8.  TYPE
  9.   TForm1 = Class(TForm)
  10.  
  11.    btnStart: TButton;
  12.    Timer   : TTimer;
  13.    imgLaz  : TImage;
  14.    imgBack : TImage;
  15.  
  16.    Procedure FormCreate    (Sender: TObject);
  17.    Procedure FormDestroy   (Sender: TObject);
  18.    Procedure btnStartClick (Sender: TObject);
  19.    Procedure TimerTimer    (Sender: TObject);
  20.  
  21.     PRIVATE
  22.      x, y, xv, yv, xo, yo: Integer;
  23.      picLaz, picBack: TPicture;
  24.      BMP: TBitmap;
  25.   End;
  26.  
  27.  VAR
  28.   Form1: TForm1;
  29.  
  30. Implementation
  31.  {$R *.LFM}
  32.  
  33.  
  34. Procedure TForm1.FormCreate(Sender: TObject);
  35.  Begin
  36.   picLaz:= TPicture.Create;
  37.   picLaz.Assign(imgLaz.Picture);
  38.  
  39.   picBack:= TPicture.Create;
  40.   picBack.Assign(imgBack.Picture);
  41.  
  42.   imgLaz.Free;
  43.  
  44.   x := 0;
  45.   y := 0;
  46.   xv:= 1;
  47.   yv:= 1;
  48.   xo:= 0;
  49.   yo:= 0;
  50.  
  51.   BMP:= TBitmap.Create;
  52.   BMP.Height:= picLaz.Height+yv;
  53.   BMP.Width := picLaz.Width+xv;
  54.  End;
  55.  
  56.  
  57. Procedure TForm1.btnStartClick(Sender: TObject);
  58.  Begin
  59.   If Timer.Enabled
  60.   Then Timer.Enabled:= False
  61.   Else Timer.Enabled:= True;
  62.  End;
  63.  
  64.  
  65. Procedure TForm1.TimerTimer(Sender: TObject);
  66.   Var
  67.    rSrc: TRect;
  68.  Begin
  69.   rSrc:= Rect(xo, yo, xo+picLaz.Width+xv, yo+picLaz.Height+yv);
  70.  
  71.   BMP.Canvas.CopyMode:= cmSrcCopy;
  72.   BMP.Canvas.CopyRect(BMP.Canvas.ClipRect, picBack.Bitmap.Canvas, rSrc);
  73.   BMP.Canvas.Draw(xv, yv, picLaz.Graphic);
  74.  
  75.   imgBack.Canvas.CopyMode:= cmSrcCopy;
  76.   imgBack.Canvas.CopyRect(rSrc, BMP.Canvas, BMP.Canvas.ClipRect);
  77.  
  78.   xo:= x;
  79.   yo:= y;
  80.  
  81.   Inc(x, xv);
  82.   Inc(y, yv);
  83.  
  84.   If x  >= picBack.Width
  85.   Then x:= -picLaz.Width;
  86.  
  87.   If y  >= picBack.Height
  88.   Then y:= -picLaz.Height;
  89.  End;
  90.  
  91.  
  92. Procedure TForm1.FormDestroy(Sender: TObject);
  93.  Begin
  94.   If Assigned(picLaz)
  95.   Then picLaz.Free;
  96.  
  97.   If Assigned(picBack)
  98.   Then picBack.Free;
  99.  
  100.   If Assigned(BMP)
  101.   Then BMP.Free;
  102.  End;
  103.  
  104. END.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018