Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. void Sprite::DrawScaledAdditiveSubpixel( float x, float y, float w, float h, Surface *a_Target )
  2. {
  3. const unsigned int wI = (unsigned int)w >> 1;
  4. const unsigned int hI = (unsigned int)h >> 1;
  5.  
  6. const float mW = m_Width / w;
  7. const float mH = m_Height / h;
  8. Pixel *src = GetBuffer() + m_CurrentFrame * m_Width;
  9. // calculate screen space bounding box of the scaled image
  10. const int ix = ( x - 615 ) * ( x - 615 );
  11. const int iy = ( y - 345 ) * ( y - 345 );
  12. const int x1 = ( x - wI );
  13. const int y1 = ( y - hI );
  14. const float y1f = ( y - hI );
  15. const int x2 = ( x + 1 + wI );
  16. const int y2 = ( y + 1 + hI );
  17. const float x2f = ( x - wI );
  18. Pixel *buffer = a_Target->GetBuffer();
  19. const int pitch = a_Target->GetPitch();
  20.  
  21. //just make everything in the centre white
  22. if ( ix + ( iy << 1 ) < 5500 )
  23. {
  24.  
  25. for ( int j = y1; j < y2; j++ )
  26. {
  27. for ( int i = x1; i < x2; i++ )
  28. {
  29. buffer[i + j * pitch] = 0xFFFFFFFF;
  30. }
  31. }
  32. return;
  33. }
  34.  
  35. const int m_Width_min_1 = m_Width - 1;
  36. const int m_Height_min_1 = m_Height - 1;
  37. const float xw_div_2 = ( x - w / 2.0f );
  38. const int midX = 2 + ( x2 + x1 ) >> 1;
  39. const int midY = 2 + ( y2 + y1 ) >> 1;
  40. // loop over target box; do a filtered read from source for each pixel
  41. for ( int j = y1; j < y2; j++ )
  42. {
  43. const float sy = max( 0.0f, ( j - ( y - h / 2.0f ) ) * mH);
  44. const int isy = int( sy );
  45. const float y_in_pixel = sy - isy;
  46. const int y0 = min( m_Height_min_1, isy );
  47. const int y1 = min( m_Height_min_1, y0 + 1 );
  48. const int jpitch = j * pitch;
  49. const int dy = midY - j;
  50.  
  51. const float one_min_y_in_pixel = 1.0f - y_in_pixel;
  52.  
  53. const bool y_check = ( ( dy ^ ( dy >> 31 ) ) - ( dy >> 31 ) ) < 2;
  54.  
  55. for ( int i = x1; i < x2; i++ )
  56. {
  57. int index = i + jpitch;
  58. if ( buffer[index] == 0xFFFFFFFF )
  59. {
  60. continue;
  61. }
  62.  
  63. const int dx = midX - i;
  64. if ( ( ( dx ^ ( dx >> 31 ) ) - ( dx >> 31 ) ) < 2 && y_check)
  65. {
  66. buffer[index] = 0xFFFFFFFF;
  67. continue;
  68. }
  69.  
  70. // determine floating point (sub-pixel) source coordinates
  71. const float sx = ( i - xw_div_2) * mW;
  72. // use bilinear interpolation to read from source image
  73. const int isx = (int)sx;
  74. const float x_in_pixel = sx - isx;
  75. const int x0 = min( m_Width_min_1, max(0, isx) );
  76. const int x1 = min( m_Width_min_1, x0 + 1 );
  77.  
  78. const float one_min_x_in_pixel = 1.0f - x_in_pixel;
  79.  
  80. const float w0 = one_min_x_in_pixel * one_min_y_in_pixel;
  81. const float w1 = x_in_pixel * one_min_y_in_pixel;
  82. const float w2 = one_min_x_in_pixel * y_in_pixel;
  83. const float w3 = 1.0f - ( w0 + w1 + w2 );
  84.  
  85. Pixel color = 0;
  86.  
  87. const Pixel p0 = src[x0 + ( y0 << 9 )];
  88. if ( p0 > 0xFF000000 )
  89. color += ScaleColor( p0, ( w0 * 256.0f ) );
  90.  
  91. const Pixel p1 = src[x1 + ( y0 << 9 )];
  92. if ( p1 > 0xFF000000 )
  93. color += ScaleColor( p1, ( w1 * 256.0f ) );
  94.  
  95. const Pixel p2 = src[x0 + ( y1 << 9 )];
  96. if ( p2 > 0xFF000000 )
  97. color += ScaleColor( p2, ( w2 * 256.0f ) );
  98.  
  99. const Pixel p3 = src[x1 + ( y1 << 9 )];
  100. if ( p3 > 0xFF000000 )
  101. color += ScaleColor( p3, ( w3 * 256.0f ) );
  102.  
  103. if ( color )
  104. {
  105. buffer[index] = AddBlend( buffer[index], color );
  106. }
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement