BarryK
BarryK

Reputation: 21

declaring TBitmap globally

I would like to declare a TBitmap Globally.

I tried as follows:

Locally within a method, this works fine

std::auto_ptr<Graphics::TBitmap> RenderGraphic(new Graphics::TBitmap());

OR

Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;

So to declare it globally I tried this in the header file

Graphics::TBitmap *RenderGraphic;

And this in the constructor

__fastcall TShipGraphic::TShipGraphic(TComponent* Owner)
: TForm(Owner)

{

  Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;

}

Which compiles fine but when running, throws an access violation exception at the first occurrence of

      RenderGraphic->Canvas->Pen->Color = clBlack;

Please advise, tks in advance.

The reference source I was using is C++ Builder Graphics Introduction

which suggested the declaration in the constructor

Upvotes: 0

Views: 362

Answers (1)

serge
serge

Reputation: 1022

You need to implement a singleton. Consider read-only case (bitmap is created once, no setter function).

In MyGraphics.h define accessor function

#include <vcl.h>
TBitmap* GetRenderGraphic();

Implementation in MyGraphics.cpp

static std::unique_ptr<TBitmap> renderGraphic(new TBitmap());

TBitmap* GetRenderGraphic()
{
    return renderGraphic.get();
}

Use it (required including of MyGraphics.h)

GetRenderGraphic()->Canvas->Pen->Color = clBlack;

Upvotes: 0

Related Questions