Reaching OS3.9 (yes, OS3.9) -compatible offscreen Rastport with private LayerInfo+1Layer to have offscreen clipping
I've been coding raster routines under OS3.2 lately, and at some point in my life I figured out the "graphics.library draw call that asks for a struct RastPort *", would not clip , and so continue to draw anywhere in memory, a privately inited Rastport + bitmap inited with:
struct RastPort mytemprastport;
InitRastport(&mytemprastport);
mytemprastport.BitMap = thatbm;
somedrawcall(&mytemprastport); // <- that , will clip no border., mytemprastport.Layer is NULL
.... super usefull if you actually draw a rectangle with same size, and know that you don't need clipping anyway. Great. But clipping, need I. At some other poijnt of my tumulturous life I found this usefull piece of code (somewhere), that allows to have a complete Offscreen, unrelated to any screen or anything, and that does a legitimate clippable Layer+rastport to your bitmap:
void OffscreenBitMap_Init(OffscreenBitMap *ofsbm,
int pixelWidth, int pixelHeight ,
int depth,
int bmFlags,
struct BitMap *friendBitmapForMode)
{
if(!ofsbm) return;
ofsbm->_layer = NULL;
ofsbm->_layerinfo = NULL;
ofsbm->_rp = NULL;
ofsbm->_bm = NULL;
// the way to create RastPort that actually clips drawing.
if(friendBitmapForMode) depth = friendBitmapForMode->Depth;
ofsbm->_bm = AllocBitMap(pixelWidth,pixelHeight,depth,bmFlags, friendBitmapForMode);
if(!ofsbm->_bm) return;
ofsbm->_layerinfo = NewLayerInfo();
if(!ofsbm->_layerinfo) { OffscreenBitMap_Close(ofsbm); return; }
ofsbm->_layer = CreateUpfrontLayer(ofsbm->_layerinfo, ofsbm->_bm, 0, 0, pixelWidth - 1, pixelHeight - 1, 0, NULL);
if(!ofsbm->_layer) { OffscreenBitMap_Close(ofsbm); return; }
ofsbm->_rp = ofsbm->_layer->rp;
}
void OffscreenBitMap_Close(OffscreenBitMap *ofsbm)
{
if(ofsbm->_layer) DeleteLayer (0,ofsbm->_layer);
ofsbm->_layer = NULL;
if(ofsbm->_layerinfo) DisposeLayerInfo(ofsbm->_layerinfo);
ofsbm->_layerinfo = NULL;
ofsbm->_rp = NULL;
if(ofsbm->_bm) FreeBitMap(ofsbm->_bm);
ofsbm->_bm = NULL;
}
... works great no OS3.2, problem is, on OS3.9 , as soon you use NewLayerInfo() and CreateUpfrontLayer(), it ends up trashing the whole intuition at some point. No idea why, I 've heard layers.library has a long history, but well i'm "that close" to achieve OS3.9 compatibility. An idea to make that code "safe for OS3.9" ? Is there a more "soft way" to create a private clipable rastport ? Am I supposed to use LockLayerInfo()/Unlock() ? Any tips ?