Recent

Author Topic: Drawing a Buffer on a Canvas  (Read 2020 times)

Key-Real

  • New Member
  • *
  • Posts: 23
Drawing a Buffer on a Canvas
« on: April 30, 2021, 07:10:39 pm »
How to draw a Buffer on a Canvas?


  for y:=0 to height-1 do
      for x:=0 to width-1 do begin
        gfxCanvas.pixels[x,y]:=getPixel(bitmap,x,y);
      end; 

is to slow...

ia there a fast way like move( bitmap.data, canvas.data, width*height);  <- as Idea

Handoko

  • Hero Member
  • *****
  • Posts: 4384
  • My goal: build my own game engine using Lazarus
Re: Drawing a Buffer on a Canvas
« Reply #1 on: April 30, 2021, 08:50:40 pm »
Don't use TCanvas, it is slow.

Almost all VGAs even those 20 years old integrated VGAs have some basic support of hardware accelerated OpenGL. Performing graphics using hardware accelerated functions is not only twice or thrice faster than the non-hardware accelerated TCanvas but it can be hundreds times faster or more.
Read more:
https://forum.lazarus.freepascal.org/index.php/topic,51687.msg379944.html#msg379944

But if you don't have time to learn a new framework, use ScanLine. It will point you to the address of a horizontal line of pixels, this works much faster.

In the link below there is a "Get pixel color of an image" demo showing how to use ScanLine.
https://wiki.freepascal.org/Portal:HowTo_Demos#Graphics

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Drawing a Buffer on a Canvas
« Reply #2 on: April 30, 2021, 10:18:42 pm »
Note also that if your "bitmap" is some kind of TGraphic descendant, TCanvas has quite a lot of ways of drawing it or making it draw itself: CopyRect, Draw, StretchDraw, etc. most of which are usually "fast enough".

There's no need of copying it pixel by pixel. ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 9032
Re: Drawing a Buffer on a Canvas
« Reply #3 on: April 30, 2021, 10:54:03 pm »
Why do we always have to guess so many details?

Assuming that gfxCanvas is a TCanvas and bitmap is a TBitmap or another descendant of TGraphic then this works:

Code: Pascal  [Select][+][-]
  1.  gfxCanas.Draw(0, 0, bitmap);
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018