Sprite moving over bitmap lazarus / 39-Graphics ---------------------------------------------------------------------- navyseabear · 2018-01-30 12:15 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 · 2018-01-30 13:45 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 · 2018-01-30 13:53 http://www.swissdelphicenter.com/de/showcode.php?id=1491 ---------------------------------------------------------------------- navyseabear · 2018-01-30 14:51 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 · 2018-01-31 17:40 Quote from: navyseabear on January 30, 2018, 03:51:01 pmI 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. ---------------------------------------------------------------------- balazsszekely · 2018-01-31 17:59 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 · 2018-01-31 22:05 QuoteBecause 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?!) ---------------------------------------------------------------------- navyseabear · 2018-02-01 05:28 Quote from: RAW on January 31, 2018, 11:05:15 pmThis 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 · 2018-02-01 12:01 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 · 2018-02-01 18:02 Without DoubleBuffering it's not looking good....  BTW: I would use OpenGL or any framework which uses GPU assistance... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i UNIT Unit1;{$MODE OBJFPC}{$H+}{$J-} Interface USES  Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, ExtCtrls;  TYPE  TForm1 = Class(TForm)    btnStart: TButton;   Timer   : TTimer;   imgLaz  : TImage;   imgBack : TImage;    Procedure FormCreate    (Sender: TObject);   Procedure FormDestroy   (Sender: TObject);   Procedure btnStartClick (Sender: TObject);   Procedure TimerTimer    (Sender: TObject);     PRIVATE     x, y, xv, yv, xo, yo: Integer;     picLaz, picBack: TPicture;  End;  VAR  Form1: TForm1; Implementation {$R *.LFM}  Procedure TForm1.FormCreate(Sender: TObject); Begin  picLaz:= TPicture.Create;  picLaz.Assign(imgLaz.Picture);   picBack:= TPicture.Create;  picBack.Assign(imgBack.Picture);   imgLaz.Free;   x := 0;  y := 0;  xv:= 2;  yv:= 2;  xo:= 0;  yo:= 0; End;  Procedure TForm1.btnStartClick(Sender: TObject); Begin  If Timer.Enabled  Then Timer.Enabled:= False  Else Timer.Enabled:= True; End;  Procedure TForm1.TimerTimer(Sender: TObject);  Var   rSrc: TRect; Begin  rSrc:= Rect(xo, yo, xo+picLaz.Width, yo+picLaz.Height);   imgBack.Canvas.CopyMode:= cmSrcCopy;  imgBack.Canvas.CopyRect(rSrc, picBack.Bitmap.Canvas, rSrc);  imgBack.Canvas.Draw(x, y, picLaz.Graphic);   xo:= x;  yo:= y;   Inc(x, xv);  Inc(y, yv);   If x  >= picBack.Width  Then x:= -picLaz.Width;   If y  >= picBack.Height  Then y:= -picLaz.Height; End;  Procedure TForm1.FormDestroy(Sender: TObject); Begin  If Assigned(picLaz)  Then picLaz.Free;   If Assigned(picBack)  Then picBack.Free; End; END. ---------------------------------------------------------------------- SymbolicFrank · 2018-02-01 18:25 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 · 2018-02-01 19:45 With DoubleBuffering it's a lot better ...  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i UNIT Unit1;{$MODE OBJFPC}{$H+}{$J-} Interface USES  Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, ExtCtrls;  TYPE  TForm1 = Class(TForm)    btnStart: TButton;   Timer   : TTimer;   imgLaz  : TImage;   imgBack : TImage;    Procedure FormCreate    (Sender: TObject);   Procedure FormDestroy   (Sender: TObject);   Procedure btnStartClick (Sender: TObject);   Procedure TimerTimer    (Sender: TObject);     PRIVATE     x, y, xv, yv, xo, yo: Integer;     picLaz, picBack: TPicture;     BMP: TBitmap;  End;  VAR  Form1: TForm1; Implementation {$R *.LFM}  Procedure TForm1.FormCreate(Sender: TObject); Begin  picLaz:= TPicture.Create;  picLaz.Assign(imgLaz.Picture);   picBack:= TPicture.Create;  picBack.Assign(imgBack.Picture);   imgLaz.Free;   x := 0;  y := 0;  xv:= 1;  yv:= 1;  xo:= 0;  yo:= 0;   BMP:= TBitmap.Create;  BMP.Height:= picLaz.Height+yv;  BMP.Width := picLaz.Width+xv; End;  Procedure TForm1.btnStartClick(Sender: TObject); Begin  If Timer.Enabled  Then Timer.Enabled:= False  Else Timer.Enabled:= True; End;  Procedure TForm1.TimerTimer(Sender: TObject);  Var   rSrc: TRect; Begin  rSrc:= Rect(xo, yo, xo+picLaz.Width+xv, yo+picLaz.Height+yv);   BMP.Canvas.CopyMode:= cmSrcCopy;  BMP.Canvas.CopyRect(BMP.Canvas.ClipRect, picBack.Bitmap.Canvas, rSrc);  BMP.Canvas.Draw(xv, yv, picLaz.Graphic);   imgBack.Canvas.CopyMode:= cmSrcCopy;  imgBack.Canvas.CopyRect(rSrc, BMP.Canvas, BMP.Canvas.ClipRect);   xo:= x;  yo:= y;   Inc(x, xv);  Inc(y, yv);   If x  >= picBack.Width  Then x:= -picLaz.Width;   If y  >= picBack.Height  Then y:= -picLaz.Height; End;  Procedure TForm1.FormDestroy(Sender: TObject); Begin  If Assigned(picLaz)  Then picLaz.Free;   If Assigned(picBack)  Then picBack.Free;   If Assigned(BMP)  Then BMP.Free; End; END.