Recent

Author Topic: Newbie With BGRALayeredBitmap Problem  (Read 3036 times)

Scamper_Neil

  • Newbie
  • Posts: 2
Newbie With BGRALayeredBitmap Problem
« on: May 26, 2013, 12:46:22 am »
Hi Folks,
Just a bit of background before the problem to give you an idea of where i'm coming from...
Programming experience:~ Bit of VBA (Excel & Access) & Bit of VB(2010). Mechanical design engineer in Solidworks,.
Proud owner of a Mini Scamp MK2 kit car, which my daughter has nicknamed 'Wall-e' (He's small, square & battered, like Wall-e.
Last year swapped the tired mini engine out for a Vauxhall astra 1.4 engine....thus the start of the dashboard instruments problems.

The plan.....Raspberry Pi, 12" touchscreen, some hardware interface, some graphical stuff & wahaay an electronic Dashboard....
Step 1...
Creating the graphical interface. Tool of choice, Solidworks....3d model the screen up & then render out to 8-bit png file.
Import the png file into a BGRALayeredBitmap to create the overlay bit. Then create layers for the static text & 'moving' bits.
Temporarily put a slider onto the form to set the position of the fuel gauge.
All working so far.....until...

I can move the position of the slider to any position & it updates & draws fine.
Move it a second time to any position & it's still fine.
Move it the third time & it's still fine.
Move the slider the fourth time & the display goes weird.
Move it a fifth time & i get a "raised exception class error.

See attached jpg for some idea of whats displaying.

Code: [Select]
unit MainDash001;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  BGRALayers, BGRABitmap, BGRABitmapTypes, RTTICtrls;
type

  { TForm1 }

  TForm1 = class(TForm)
    Track1: TTrackBar;
    TrackBar1: TTrackBar;

      procedure FormCreate(Sender: TObject);
      procedure FormPaint(Sender: TObject);
      procedure TrackBar1Click(Sender: TObject);

  private
      ImageS:   TBGRABitmap;  //stretched master image
      image: TBGRABitmap;
      layers: TBGRALayeredBitmap;     //original master image
      layersStretch: TBGRALayeredBitmap;
      StaticYel: TBGRABitmap;
      TempYel: TBGRABitmap;
      FuelYel: TBGRABitmap;
      RevYel: TBGRABitmap;
      SpeedYel: TBGRABitmap;
      TempBtm,TempTop, TempL, TempR,TempVal: integer;
      T1,T2,T3,T4,T5,T6,T7: integer;
      procedure PaintImage;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
                                               //Create Top Overlay Layer with main png file (8-bit)
  layers:= TBGRALayeredBitmap.Create(1280,800);
  layers.AddLayerFromFile('Dash-003-Background.png');
  layers.LayerOpacity[layers.AddLayerFromFile('Dash-003-Background.png')]:=255;
                                               //Create layer for the static yellow bits
  StaticYel:=  TBGRABitmap.Create(1280,800);
  StaticYel.FillRect(360,280,650,710,BGRA(245,255,120,255),dmset);
  StaticYel.FillRect(1010,250,1240,310,BGRA(245,255,120,255),dmset);
  StaticYel.FillRect(1010,250,1082,380,BGRA(245,255,120,255),dmset);
  StaticYel.FillRect(1010,536,1082,632,BGRA(245,255,120,255),dmXor);
  StaticYel.FillRect(1050,730,1245,764,BGRA(245,255,120,255),dmXor);
  layers.AddOwnedLayer(StaticYel);
  layers.MoveLayerDown(2);                     //move layer down so it displays under the main png layer
                                               //Create layer for temperature gauge
  TempYel:=  TBGRABitmap.Create(1280,800);
  layers.AddOwnedLayer(TempYel);
  layers.MoveLayerDown(3);                      //move layer down so it displays under the main png layer
  layers.Resample(1280,800, rmfineresample);    //resample the lot to display at 1280x800 res
  PaintImage;
end;

procedure TForm1.FormPaint(Sender: TObject);
  var i: integer ;
begin
  PaintImage;
    end;
                                                 //Use slider to replicate the sensing calculations & stuff
procedure TForm1.TrackBar1Click(Sender: TObject);
begin                                            //Assign variable for height of yellow rectangle
    TempL:=1080;
    TempR:=1250;
    TempBtm:=712;
    T1:=698;
    T2:=682;
    T3:=665;
    T4:=640;
    T5:=610;
    T6:=575;
    T7:=500;
if trackbar1.Position = 0 then                        //based on slider position assign a height for rectangle
begin
     TempTop := TempBtm+1
end;
if trackbar1.Position = 1 then
begin
     TempTop:=T1
end;
if Trackbar1.Position = 2 then
begin
     TempTop:=T2
end;
if Trackbar1.Position = 3 then
begin
     TempTop:=T3
end;
if Trackbar1.Position = 4 then
begin
     TempTop:=T4
end;
if Trackbar1.Position = 5 then
begin
     TempTop:=T5
end;
if Trackbar1.Position = 6 then
begin
     TempTop:=T6
end;
if Trackbar1.Position = 7 then
begin
     TempTop:=T7
end;                                           //draw a rectangle with background colour to cover old yellow rectangle
  TempYel.FillRect(TempL,400,TempR,TempBtm,BGRA(73,73,74,255),dmset);
                                               //draw the new yellow rectangle at the height chosen on the slider
  TempYel.FillRect(TempL,TempTop,TempR,TempBtm,BGRA(245,255,120,255),dmset);
  PaintImage;
end;

procedure TForm1.PaintImage;
begin
  layers.Resample(1280,800, rmfineresample);
  layers.Draw(Canvas,0,0);
end;
end.


If i've not posted in the right way or done something wrong, apols, :-[ it's my 1st post...please don't flame me.

Apologies for my programming in advance, any hints & tips would be appreciated. (Any criticism is constructive)

Thanx in advance,

Neil

circular

  • Hero Member
  • *****
  • Posts: 3440
    • Personal webpage
Re: Newbie With BGRALayeredBitmap Problem
« Reply #1 on: May 29, 2013, 12:52:19 am »
Ok, so first :
Code: [Select]
layers.AddLayerFromFile('Dash-003-Background.png');
  layers.LayerOpacity[layers.AddLayerFromFile('Dash-003-Background.png')]:=255;
This creates two layers. If you only want one layer and change its opacity remove first line. Alternatively you can do :
Code: [Select]
backgroundIndex:= layers.AddLayerFromFile...
layers.LayerOpacity[backgroundIndex] := ...

Second,
Code: [Select]
layers.Resample(1280,800, rmfineresample);This can change the references of bitmap, thus TempYel is not a valid reference anymore. If you want to draw with scaling, you should rather do something like
Code: [Select]
temp := layers.ComputeFlatImage;
BGRAReplace(temp, temp.Resample...)
temp.Draw...
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018