PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Rotation with alpha surfaces

PsychoChris · 04 Aug 2006, 20:15 UTC
Hello,

the current PocketFrog rasterizer supports blitting with an alpha surface. Unfortunately all more advanced blit functions like BlitRotated(), BlitStretch() etc. don't consider the additional usage of alpha channels. So after checking the PocketHAL 0.8 ALPHA thread some question came up to me:

1. The most important one: Will development of PocketHAL still be continued? Last post in the PocketHAL 0.8 Alpha thread didn't sound promising.

2. If yes, can we expect that the mentioned functions will be implemented?

3. If answer to 3 is no: What would be a good way of implementing it (and considering performance) by my own?

Greetings and thanks in advance,
Chris
PsychoChris wrote:Hello,

the current PocketFrog rasterizer supports blitting with an alpha surface. Unfortunately all more advanced blit functions like BlitRotated(), BlitStretch() etc. don't consider the additional usage of alpha channels. So after checking the PocketHAL 0.8 ALPHA thread some question came up to me:

1. The most important one: Will development of PocketHAL still be continued? Last post in the PocketHAL 0.8 Alpha thread didn't sound promising.

2. If yes, can we expect that the mentioned functions will be implemented?



I really think this task shouldn't belong to PocketHAL: PHAL is abstracting all the hardware for you, but there is no hardware support for alpha, so you have to implement all the things by yourself.
PocketHAL gives you a Surface, that is, your hardware surface.
The thread pocketHal 0.8 alpha has nothing to do with alpha blending. I think it's meant to be taken as a 'pre-beta'. PocketFrog is the high level library with all the different blit functions and game framework.


So, which alpha 'mod' are you using? I think there are at least 2. The one in the bug-fix pack, or another one? If it's the one that takes 2 separate surfaces as inputs, it's the bug-fix pack one.

Anyway, since these were user submitted modifications, I wouldn't wait for them to be written for you. You could compare the code between the standard one and the alpha one to see what would need to be added. If I have time, I may take a look at it myself...
PsychoChris · 04 Aug 2006, 23:34 UTC
First of all thanks for your answers. It is very nice to see there are people like you who help others!

I really think this task shouldn't belong to PocketHAL: PHAL is abstracting all the hardware for you, but there is no hardware support for alpha, so you have to implement all the things by yourself.
Yes, i fully agree with that. The features were related to PocketFrog not PocketHAL but i must confess that i made myself not clear enough. My purpose on bringing PocketHAL into discussion was because from what i read in the PocketHAL 0.8 Alpha thread it doesn't seem to be developed anymore into future. Or did i get this wrong?


The thread pocketHal 0.8 alpha has nothing to do with alpha blending.
I might get confused or write some weird stuff because of my lacks in english language but i'm not that blind to get that... :)

So, which alpha 'mod' are you using?
I don't use any modification, only the PocketFrog 0.8.1. release that can be downloaded at the droneship page. There i found the following method which i use for alpha blit:
Code:
// Blit with Full Alpha Support - J Elmore
void Blit( int destX, int destY, const Surface* source, const Surface * AlphaSurf, const Rect* srcRect = 0 );


So now i tried to made my own rotated alpha blit method by trying to combine the Blit() (with alpha) and the BlitRotated() methods. This is what i came up with, but it doesn't work right now and i'm still trying to find out why.

Code:
void Rasterizer::BlitRotated( int destX, int destY, float angle, const Surface* source, const Surface * AlphaSurf )
{
// Calculate destination rectangle
//todo: find better fit
float tw = 0.5f * source->GetWidth();
float th = 0.5f * source->GetHeight();
int diag = int(sqrtf(tw*tw+th*th) + 1.0f);
Rect destRect( destX-diag, destY-diag, destX+diag, destY+diag );
int width = destRect.GetWidth();
int height = destRect.GetHeight();

// Clipping
if (!m_clipper.IsEmpty())
{
destRect.Intersect( m_clipper );
if (destRect.IsEmpty())
return;
}

int c = int(cosf( -angle ) * 1024.0f);
int s = int(sinf( -angle ) * 1024.0f);
int w = source->GetWidth() << 9;
int h = source->GetHeight() << 9;

// Use fixed point (22.10)
unsigned T[3][2];

T[0][0] = c;
T[0][1] = -s;
T[1][0] = s;
T[1][1] = c;
T[2][0] = -c * destX - s * destY + w;
T[2][1] = s * destX - c * destY + h;

// Optimization: compensate for fixed point arithmetic in rasterization loops
T[2][0] <<= 6;
T[2][1] <<= 6;

// Drawing
int colorMask = source->GetColorMask();
Surface* pRotatedPixels = new Surface(width, height);
Surface* pRotatedAlpha = new Surface(width, height);
Rect sizeRect(0, 0, width, destRect.GetHeight());

// Rotate both surfaces
m_raster->BlitTransform( *pRotatedPixels->m_buffer, sizeRect, T, *source->m_buffer );
m_raster->BlitTransform( *pRotatedAlpha->m_buffer, sizeRect, T, *AlphaSurf->m_buffer );

Blit( destX-diag, destY-diag, pRotatedPixels, pRotatedAlpha);

delete pRotatedPixels;
delete pRotatedAlpha;
}


I tried to create two surfaces into which i BlitRotate the image and the alpha surfaces. Seems like the BlitTransform() fails but i have no idea why. Perhaps one of you has an idea?

Greetings,
Chris
Code:
m_raster->BlitTransform( *pRotatedPixels->m_buffer, sizeRect, T, *source->m_buffer );


That code is doing the blitting, not just transforming.

You'll need to add another blitTransform that will take both surfaces. Look at the implentation of the Blit that takes 2 surfaces. It is calling m_raster->BlitAlpha

Of course, for the blitTransform, it is inside raster2d.cpp - and then gets into the algo2d.h. It's going to take modifications to all those I think...
PsychoChris · 05 Aug 2006, 00:14 UTC
Now it works. My main mistake was to miss out that the transformation matrix takes the destination coordinates into account.
If anyone else could use the code here it is:
Code:
void Rasterizer::BlitRotated( int destX, int destY, float angle, const Surface* source, const Surface * AlphaSurf )
{
// Calculate destination rectangle
//todo: find better fit
float tw = 0.5f * source->GetWidth();
float th = 0.5f * source->GetHeight();
int diag = int(sqrtf(tw*tw+th*th) + 1.0f);
Rect destRect( destX-diag, destY-diag, destX+diag, destY+diag );
int width = destRect.GetWidth();
int height = destRect.GetHeight();

// Clipping
if (!m_clipper.IsEmpty())
{
destRect.Intersect( m_clipper );
if (destRect.IsEmpty())
return;
}

int c = int(cosf( -angle ) * 1024.0f);
int s = int(sinf( -angle ) * 1024.0f);
int w = source->GetWidth() << 9;
int h = source->GetHeight() << 9;

// Use fixed point (22.10)
unsigned T[3][2];

T[0][0] = c;
T[0][1] = -s;
T[1][0] = s;
T[1][1] = c;
T[2][0] = -c * diag - s * diag + w;
T[2][1] = s * diag - c * diag + h;

// Optimization: compensate for fixed point arithmetic in rasterization loops
T[2][0] <<= 6;
T[2][1] <<= 6;

// Drawing
int colorMask = source->GetColorMask();
Surface* pRotatedPixels = new Surface(width, height);
Surface* pRotatedAlpha = new Surface(width, height);
Rect sizeRect(0, 0, width, height);

// Clear surfaces
Surface::LockInfo lockInfo;
pRotatedPixels->Lock(lockInfo);
memset(lockInfo.pixels, 0, sizeof(Pixel)*width*height);
pRotatedPixels->Unlock();
pRotatedAlpha->Lock(lockInfo);
memset(lockInfo.pixels, 0, sizeof(Pixel)*width*height);
pRotatedAlpha->Unlock();

// Rotate both surfaces
m_raster->BlitTransformMask( *pRotatedPixels->m_buffer, sizeRect, T, *source->m_buffer, 0 );
m_raster->BlitTransformMask( *pRotatedAlpha->m_buffer, sizeRect, T, *AlphaSurf->m_buffer, 0 );

// Blit rotated surfaces with alpha
m_raster->BlitAlpha( *m_surface->m_buffer, destRect, *pRotatedPixels->m_buffer, *pRotatedAlpha->m_buffer, sizeRect );

delete pRotatedPixels;
delete pRotatedAlpha;
}


I would also be glad to hear ideas of possible improvements.

Greetings and thanks,
Chris
I think you could use
m_raster->Clear( *pRotatedPixels->m_buffer, 0);
There is also no reason why you need to allocate surfaces on the heap:

Code:

Surface* pRotatedPixels = new Surface(width, height);
Surface* pRotatedAlpha = new Surface(width, height);


Instead, just allocate them on the stack:

Code:

Surface rotatedPixels(width, height);
Surface rotatedAlpha(width, height);


This code is wrong:

Code:

memset(lockInfo.pixels, 0, sizeof(Pixel)*width*height);


You need to take the pitch into account:

Code:

memset( lockInfo, pixels, 0, pitch * height );


Or even better, use the rasterizer Clear() function as suggested by fast_rx.
PsychoChris · 05 Aug 2006, 14:40 UTC
Thanks for your feedback! Here is how it looks now.

Code:
void Rasterizer::BlitRotated( int destX, int destY, float angle, const Surface* source, const Surface * AlphaSurf )
{
// Calculate destination rectangle
//todo: find better fit
float tw = 0.5f * source->GetWidth();
float th = 0.5f * source->GetHeight();
int diag = int(sqrtf(tw*tw+th*th) + 1.0f);
Rect destRect( destX-diag, destY-diag, destX+diag, destY+diag );
int width = destRect.GetWidth();
int height = destRect.GetHeight();

// Clipping
if (!m_clipper.IsEmpty())
{
destRect.Intersect( m_clipper );
if (destRect.IsEmpty())
return;
}

int c = int(cosf( -angle ) * 1024.0f);
int s = int(sinf( -angle ) * 1024.0f);
int w = source->GetWidth() << 9;
int h = source->GetHeight() << 9;

// Use fixed point (22.10)
unsigned T[3][2];

T[0][0] = c;
T[0][1] = -s;
T[1][0] = s;
T[1][1] = c;
T[2][0] = -c * diag - s * diag + w;
T[2][1] = s * diag - c * diag + h;

// Optimization: compensate for fixed point arithmetic in rasterization loops
T[2][0] <<= 6;
T[2][1] <<= 6;

// Drawing
Surface rotatedPixels(width, height);
Surface rotatedAlpha(width, height);
Rect sizeRect(0, 0, width, height);

// Clear surfaces
m_raster->Clear( *rotatedPixels.m_buffer, 0);
m_raster->Clear( *rotatedAlpha.m_buffer, 0);

// Rotate both surfaces
m_raster->BlitTransform( *rotatedPixels.m_buffer, sizeRect, T, *source->m_buffer );
m_raster->BlitTransform( *rotatedAlpha.m_buffer, sizeRect, T, *AlphaSurf->m_buffer );

// Blit rotated surfaces with alpha
m_raster->BlitAlpha( *m_surface->m_buffer, destRect, *rotatedPixels.m_buffer, *rotatedAlpha.m_buffer, sizeRect );
}


Greetings,
Chris
Hi. I found a trouble with your code. When I draw surface below display (surface y > 320 - surface height), my game crashes. Also when I draw surface above display (surface y < 0 - surface height), it appears in y=0 !!

A have no idea what kind of trouble is this. Maybe it's trouble with my code, but i'm not shure. Is there anybody who met this trouble? And how can i solve it?