Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Sprite::DrawScaledAdditiveSubpixel( float x, float y, float w, float h, Surface *a_Target )
- {
- const unsigned int wI = (unsigned int)w >> 1;
- const unsigned int hI = (unsigned int)h >> 1;
- const float mW = m_Width / w;
- const float mH = m_Height / h;
- Pixel *src = GetBuffer() + m_CurrentFrame * m_Width;
- // calculate screen space bounding box of the scaled image
- const int ix = ( x - 615 ) * ( x - 615 );
- const int iy = ( y - 345 ) * ( y - 345 );
- const int x1 = ( x - wI );
- const int y1 = ( y - hI );
- const float y1f = ( y - hI );
- const int x2 = ( x + 1 + wI );
- const int y2 = ( y + 1 + hI );
- const float x2f = ( x - wI );
- Pixel *buffer = a_Target->GetBuffer();
- const int pitch = a_Target->GetPitch();
- //just make everything in the centre white
- if ( ix + ( iy << 1 ) < 5500 )
- {
- for ( int j = y1; j < y2; j++ )
- {
- for ( int i = x1; i < x2; i++ )
- {
- buffer[i + j * pitch] = 0xFFFFFFFF;
- }
- }
- return;
- }
- const int m_Width_min_1 = m_Width - 1;
- const int m_Height_min_1 = m_Height - 1;
- const float xw_div_2 = ( x - w / 2.0f );
- const int midX = 2 + ( x2 + x1 ) >> 1;
- const int midY = 2 + ( y2 + y1 ) >> 1;
- // loop over target box; do a filtered read from source for each pixel
- for ( int j = y1; j < y2; j++ )
- {
- const float sy = max( 0.0f, ( j - ( y - h / 2.0f ) ) * mH);
- const int isy = int( sy );
- const float y_in_pixel = sy - isy;
- const int y0 = min( m_Height_min_1, isy );
- const int y1 = min( m_Height_min_1, y0 + 1 );
- const int jpitch = j * pitch;
- const int dy = midY - j;
- const float one_min_y_in_pixel = 1.0f - y_in_pixel;
- const bool y_check = ( ( dy ^ ( dy >> 31 ) ) - ( dy >> 31 ) ) < 2;
- for ( int i = x1; i < x2; i++ )
- {
- int index = i + jpitch;
- if ( buffer[index] == 0xFFFFFFFF )
- {
- continue;
- }
- const int dx = midX - i;
- if ( ( ( dx ^ ( dx >> 31 ) ) - ( dx >> 31 ) ) < 2 && y_check)
- {
- buffer[index] = 0xFFFFFFFF;
- continue;
- }
- // determine floating point (sub-pixel) source coordinates
- const float sx = ( i - xw_div_2) * mW;
- // use bilinear interpolation to read from source image
- const int isx = (int)sx;
- const float x_in_pixel = sx - isx;
- const int x0 = min( m_Width_min_1, max(0, isx) );
- const int x1 = min( m_Width_min_1, x0 + 1 );
- const float one_min_x_in_pixel = 1.0f - x_in_pixel;
- const float w0 = one_min_x_in_pixel * one_min_y_in_pixel;
- const float w1 = x_in_pixel * one_min_y_in_pixel;
- const float w2 = one_min_x_in_pixel * y_in_pixel;
- const float w3 = 1.0f - ( w0 + w1 + w2 );
- Pixel color = 0;
- const Pixel p0 = src[x0 + ( y0 << 9 )];
- if ( p0 > 0xFF000000 )
- color += ScaleColor( p0, ( w0 * 256.0f ) );
- const Pixel p1 = src[x1 + ( y0 << 9 )];
- if ( p1 > 0xFF000000 )
- color += ScaleColor( p1, ( w1 * 256.0f ) );
- const Pixel p2 = src[x0 + ( y1 << 9 )];
- if ( p2 > 0xFF000000 )
- color += ScaleColor( p2, ( w2 * 256.0f ) );
- const Pixel p3 = src[x1 + ( y1 << 9 )];
- if ( p3 > 0xFF000000 )
- color += ScaleColor( p3, ( w3 * 256.0f ) );
- if ( color )
- {
- buffer[index] = AddBlend( buffer[index], color );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement