How to apply multiple bitmaps in BView with using alpha?

I use an object from BView class to display a PNG file as a BBitmap. I use a second BView object to display a second PNG image on the first one. That works well.
After it I want to use alpha capability as the second image has irregular outline not a simple rectangle (let’s talk about a circle on the second image).

Here is the code as I draw second bitmap (stored in variable PointPic).

SetDrawingMode(B_OP_ALPHA);
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
SetDrawingMode(B_OP_ALPHA);
DrawBitmap(PointPic, B_ORIGIN);

Where it was transparent surface on the bitmap before now I can see white not pixels of first bitmap below the second bitmap.

So, how should I use these methods properly? Could you give me some helps?

Cozi

Make sure your second bitmap is in the B_RGBA32 colorspace (that is, alpha channel was correctly loaded).

Other than that I think your code should work.

1 Like

Well, I have made some efforts.

I created two BView object called BackView and FrontView. Each of them has different bitmap to display (BackPic, FrontPic).

BackPic:
minta1

FrontPic:
minta2

I put FrontPic on BackPic and FrontPic has a transparent background. These images were made by WoderBrush in Haiku.

As my program starts it uses alpha channel but main problem is the backgorund. I supposed that background picture would be BackPic for FrontPic but desktop got background! I cannot imagine how could be happened. I have some screenshots:
Screenshot_01_from_Haiku_20220411

Screenshot_02_from_Haiku_20220411

My code is here:

#include “MainWindow.h”

#include <Application.h>

#include <Bitmap.h>

#include <TranslationUtils.h>

#include <View.h>

#include <Entry.h>

#include <File.h>

#include <Path.h>

class FrontView : public BView

{

public:

FrontView();

void Draw(BRect);

BBitmap *FrontPic;

};

class BackView : public BView

{

public:

BackView();

void Draw(BRect);

BBitmap *BackPic;

};

FrontView::FrontView()

: BView(BRect(0, 0, 200, 100), “BackView”, B_FOLLOW_NONE, B_WILL_DRAW)

{

FrontPic = BTranslationUtils::GetBitmapFile(“/boot/home/Projects/X3/minta2.png”);

}

void FrontView::Draw(BRect)

{

DrawBitmap(FrontPic);

}

BackView::BackView()

: BView(BRect(0, 0, 200, 200), “BackView”, B_FOLLOW_NONE, B_WILL_DRAW)

{

BackPic = BTranslationUtils::GetBitmapFile(“/boot/home/Projects/X3/minta1.png”);

}

void BackView::Draw(BRect)

{

DrawBitmap(BackPic);

}

FrontView *fv;

BackView *bv;

MainWindow::MainWindow(void)

: BWindow(BRect(100, 100, 500, 400), “Main Window”, B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)

{

fv = new FrontView();

bv = new BackView();

fv->SetDrawingMode(B_OP_ALPHA);

fv->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);

fv->SetViewColor(B_TRANSPARENT_32_BIT);

bv->AddChild(fv);

AddChild(bv);

}

void MainWindow::MessageReceived(BMessage *msg)

{

switch (msg->what)

{

default:

{

BWindow::MessageReceived(msg);

break;

}

}

}

bool MainWindow::QuitRequested(void)

{

be_app->PostMessage(B_QUIT_REQUESTED);

return true;

}

How can I fix this problem?

I think you need to set the B_TRANSPARENT_VIEW flag on FrontView, otherwise it will draw its own background (even if you ask it to be transparent) and this results in strange things.

B_TRANSPARENT_VIEW makes sure the containing view is drawn first.

Here is my modified code:

FrontView::FrontView()
: BView(BRect(0, 0, 200, 100), “FrontView”, B_FOLLOW_NONE, B_WILL_DRAW | B_TRANSPARENT_VIEW)
{
FrontPic = BTranslationUtils::GetBitmapFile(“/boot/home/Projects/X3/minta2.png”);
}

Make with some errors:

After it I searched for keyword “B_TRANSPARENT_VIEW” with Google but I have not found any measurable results. Does it exist?

I’m starting to get lost. Could you help a bit, again?

No developer here, but maybe something useful here?

Thank you!

BeOS didn’t support transparent views.

Well, new strategy is needed :slight_smile:

Ok, next step: I will try manage bitmaps with transparency and the result (agregated) bitmap will be displyed in a single BView.

I am going to spend some time to make a second demo. Thanks, again!

But arent you using Haiku? It does support transparent views.

I think you only read that line? (not the full commit) :slight_smile:

Ok. I got a lot of experience.

  1. I should read all of the text, extensively :slight_smile:
  2. My working code is here (partly):

FrontView::FrontView()
: BView(BRect(0, 0, 200, 100), “FrontView”, B_FOLLOW_NONE, B_WILL_DRAW | B_TRANSPARENT_BACKGROUND)
{
FrontPic = BTranslationUtils::GetBitmapFile(“/boot/home/Projects/X3/minta2.png”);
}
Screenshot_Haiku_03_20220411

  1. The magical flag was B_TRANSPARENT_BACKGROUND
  2. This flag is not there in the documentation ( The Haiku Book: BView Class Reference , description of BView() constructor) or I live in an alternative galaxy, far far away …
  3. There must be a lot of luck during software development, e.g. questions sould be asked well and there sould be kind people answering them :slight_smile:

So, thank you, next chapter is coming!

3 Likes