Hello I have ported from NinePatch to C#. But It can't display
using SFML.Graphics;using SFML.System;namespace SFML_Test
{ public class NinePatch
: Transformable, Drawable
{ private PrimitiveType m_primitiveType
; private Vertex
[] m_vertices
; private Texture m_texture
; private Vector2f m_trimmedSize
; private Vector2f m_size
; private Vector2f m_scaleTopLeft
; private Vector2f m_scaleBottomRight
; private Vector2f m_contentTopLeft
; private Vector2f m_contentBottomRight
; private IntRect m_textureRectangle
; private Vector2f trimAmount
= new Vector2f
(1,
1); public NinePatch
() : base() { m_primitiveType
= PrimitiveType
.Quads; m_vertices
= new Vertex
[36]; m_texture
= null; m_trimmedSize
= new Vector2f
(0,
0); m_size
= new Vector2f
(0,
0); m_scaleTopLeft
= new Vector2f
(0,
0); m_scaleBottomRight
= new Vector2f
(0,
0); m_contentTopLeft
= new Vector2f
(0,
0); m_contentBottomRight
= new Vector2f
(0,
0); m_textureRectangle
= new IntRect
(0,
0,
3,
3); } private void ExtractScalePositionsAndContentAreaFromTexture
(Texture pTexture, IntRect textureRectangle,
ref Vector2f topLeft,
ref Vector2f bottomRight,
ref Vector2f contentTopLeft,
ref Vector2f contentBottomRight
) { Image image
= pTexture
.CopyToImage(); topLeft
= new Vector2f
(0,
0); bottomRight
= new Vector2f
(textureRectangle
.Width - 2f, textureRectangle
.Height - 2f
); bool foundStart
= false; for (uint x
= 1u
; x
< textureRectangle
.Width; x
++) { if (!foundStart
) { if (image
.GetPixel((uint)textureRectangle
.Left + x,
(uint)textureRectangle
.Top) == Color
.Black) { foundStart
= true; topLeft
.X = x
- 1f
; } else continue; } if (foundStart
) { if (image
.GetPixel((uint)textureRectangle
.Left + x,
(uint)textureRectangle
.Top) == Color
.Black) bottomRight
.X = x
- 1f
; else break; } } for (uint y
= 1u
; y
< textureRectangle
.Height; y
++) { if (!foundStart
) { if (image
.GetPixel((uint)textureRectangle
.Left,
(uint)textureRectangle
.Top + y
) == Color
.Black) { foundStart
= true; topLeft
.Y = y
- 1f
; } else continue; } if (foundStart
) { if (image
.GetPixel((uint)textureRectangle
.Left,
(uint)textureRectangle
.Top + y
) == Color
.Black) bottomRight
.Y = y
- 1f
; else break; } } contentTopLeft
= new Vector2f
(0,
0); contentBottomRight
= new Vector2f
(textureRectangle
.Width - 2f, textureRectangle
.Height - 2f
); Vector2u textureBottomRightPixel
= new Vector2u
((uint)textureRectangle
.Width - 1u,
(uint)textureRectangle
.Height - 1u
); for (uint x
= 1u
; x
< textureRectangle
.Width; x
++) { if (!foundStart
) { if (image
.GetPixel((uint)textureRectangle
.Left + x,
(uint)textureRectangle
.Top + textureBottomRightPixel
.Y) == Color
.Black) { foundStart
= true; contentTopLeft
.X = x
- 1f
; } else continue; } if (foundStart
) { if (image
.GetPixel((uint)textureRectangle
.Left + x,
(uint)textureRectangle
.Top + textureBottomRightPixel
.Y) == Color
.Black) contentBottomRight
.X = x
- 1f
; else break; } } for(uint y
= 1u
; y
< textureRectangle
.Height; y
++) { if (!foundStart
) { if (image
.GetPixel((uint)textureRectangle
.Left + textureBottomRightPixel
.X,
(uint)textureRectangle
.Top + y
) == Color
.Black) { foundStart
= true; contentTopLeft
.Y = y
- 1f
; } else continue; } if (foundStart
) { if (image
.GetPixel((uint)textureRectangle
.Left + textureBottomRightPixel
.X,
(uint)textureRectangle
.Top + y
) == Color
.Black) contentBottomRight
.Y = y
- 1f
; else break; } } } public void SetTexture
(Texture texture,
bool resetsize
= true,
bool resetrect
= true) { m_texture
= texture
; if (resetrect
) { m_textureRectangle
= new IntRect
(new Vector2i
(0,
0),
(Vector2i
)m_texture
.Size); } m_trimmedSize
= new Vector2f
(m_textureRectangle
.Width, m_textureRectangle
.Height) - trimAmount
* 2f
; if (resetsize
) m_size
= m_trimmedSize
; ExtractScalePositionsAndContentAreaFromTexture
(m_texture, m_textureRectangle,
ref m_scaleTopLeft,
ref m_scaleBottomRight,
ref m_contentTopLeft,
ref m_contentBottomRight
); UpdateVerticies
(); } public Vector2f Size
{ get { return m_size
; } set { m_size
= value; Vector2f minimumSize
= m_scaleTopLeft
+ (m_trimmedSize
- m_scaleBottomRight
); if (m_size
.X < minimumSize
.X) m_size
.X = minimumSize
.X; if (m_size
.Y < minimumSize
.Y) m_size
.Y = minimumSize
.Y; UpdateVerticiesPositions
(); } } public Color Color
{ get { return m_vertices
[0].Color; } set { foreach (Vertex vertex
in m_vertices
) { Vertex tempvex
= vertex
; tempvex
.Color = value; } } } public FloatRect LocalBounds
{ get { return new FloatRect
(new Vector2f
(0,
0), m_size
); } } public FloatRect GlocalBounds
{ get { return Transform
.TransformRect(LocalBounds
); } } public FloatRect LocalContentArea
{ get { Vector2f topleft
= GetResultingPositionOfTexCoord
(m_contentTopLeft
); return new FloatRect
(topleft, GetResultingPositionOfTexCoord
(m_contentBottomRight
) - topleft
+ new Vector2f
(1,
1)); } } public FloatRect GlocalContentArea
{ get { return Transform
.TransformRect(LocalContentArea
); } } public void SetTextureRect
(IntRect texrect,
bool resetsize
= true) { m_textureRectangle
= texrect
; m_trimmedSize
= new Vector2f
(m_textureRectangle
.Width, m_textureRectangle
.Height) - trimAmount
* 2f
; if (resetsize
) m_size
= m_trimmedSize
; if (m_texture
!= null) { ExtractScalePositionsAndContentAreaFromTexture
(m_texture, m_textureRectangle,
ref m_scaleTopLeft,
ref m_scaleBottomRight,
ref m_contentTopLeft,
ref m_contentBottomRight
); UpdateVerticies
(); } } public void ResetSize
() { Size
= m_trimmedSize
; } public bool IsPointInsideTransformedContentArea
(Vector2f point
) { return LocalContentArea
.Contains(InverseTransform
.TransformPoint(point
).X, InverseTransform
.TransformPoint(point
).Y); } public void Draw
(RenderTarget target, RenderStates states
) { states
.Texture = m_texture
; states
.Transform = Transform
; target
.Draw(m_vertices, m_primitiveType, states
); } private void UpdateVerticies
() { UpdateVerticiesPositions
(); UpdateVerticiesTextCoords
(); } private void UpdateVerticiesPositions
() { Vector2f newBottomRightScaled
= m_size
- (m_trimmedSize
- m_scaleBottomRight
); // top row m_vertices
[0].Position = new Vector2f
(0f, 0f
); m_vertices
[1].Position = new Vector2f
(m_scaleTopLeft
.X, 0f
); m_vertices
[2].Position = m_scaleTopLeft
; m_vertices
[3].Position = new Vector2f
(0f, m_scaleTopLeft
.Y); m_vertices
[4].Position = new Vector2f
(m_scaleTopLeft
.Y, 0f
); m_vertices
[5].Position = new Vector2f
(newBottomRightScaled
.X, 0f
); m_vertices
[6].Position = new Vector2f
(newBottomRightScaled
.X, m_scaleTopLeft
.Y); m_vertices
[7].Position = m_scaleTopLeft
; m_vertices
[8].Position = new Vector2f
(newBottomRightScaled
.X, 0f
); m_vertices
[9].Position = new Vector2f
(m_size
.X, 0f
); m_vertices
[10].Position = new Vector2f
( m_size
.X, m_scaleTopLeft
.Y ); m_vertices
[11].Position = new Vector2f
(newBottomRightScaled
.X, m_scaleTopLeft
.X ); // centre row m_vertices
[12].Position = new Vector2f
(0f, m_scaleTopLeft
.Y); m_vertices
[13].Position = new Vector2f
(m_scaleTopLeft
.X, m_scaleTopLeft
.Y); m_vertices
[14].Position = new Vector2f
(m_scaleTopLeft
.X, newBottomRightScaled
.Y); m_vertices
[15].Position = new Vector2f
(0f, newBottomRightScaled
.Y ); m_vertices
[16].Position = new Vector2f
( m_scaleTopLeft
.X, m_scaleTopLeft
.Y); m_vertices
[17].Position = new Vector2f
(newBottomRightScaled
.X, m_scaleTopLeft
.Y); m_vertices
[18].Position = new Vector2f
(newBottomRightScaled
.X, newBottomRightScaled
.Y ); m_vertices
[19].Position = new Vector2f
(m_scaleTopLeft
.X, newBottomRightScaled
.Y); m_vertices
[20].Position = new Vector2f
(newBottomRightScaled
.X, m_scaleTopLeft
.Y); m_vertices
[21].Position = new Vector2f
(m_size
.X, m_scaleTopLeft
.Y); m_vertices
[22].Position = new Vector2f
(m_size
.X, newBottomRightScaled
.Y); m_vertices
[23].Position = new Vector2f
(newBottomRightScaled
.X, newBottomRightScaled
.Y); // bottom row m_vertices
[24].Position = new Vector2f
(0f, newBottomRightScaled
.Y); m_vertices
[25].Position = new Vector2f
(m_scaleTopLeft
.X, newBottomRightScaled
.Y); m_vertices
[26].Position = new Vector2f
(m_scaleTopLeft
.X, m_size
.Y); m_vertices
[27].Position = new Vector2f
(0f, m_size
.Y); m_vertices
[28].Position = new Vector2f
(m_scaleTopLeft
.X, newBottomRightScaled
.Y); m_vertices
[29].Position = new Vector2f
(newBottomRightScaled
.X, newBottomRightScaled
.Y); m_vertices
[30].Position = new Vector2f
(newBottomRightScaled
.X, m_size
.Y ); m_vertices
[31].Position = new Vector2f
(m_scaleTopLeft
.X, m_size
.Y); m_vertices
[32].Position = new Vector2f
(newBottomRightScaled
.X, newBottomRightScaled
.Y); m_vertices
[33].Position = new Vector2f
(m_size
.X, newBottomRightScaled
.Y); m_vertices
[34].Position = new Vector2f
(m_size
.X, m_size
.Y); m_vertices
[35].Position = new Vector2f
(newBottomRightScaled
.X, m_size
.Y); } private void UpdateVerticiesTextCoords
() { Vector2f textureBottomRight
= m_trimmedSize
; // top row m_vertices
[0].TexCoords = new Vector2f
(0,
0); m_vertices
[1].TexCoords = new Vector2f
(m_scaleTopLeft
.X,
0); m_vertices
[2].TexCoords = m_scaleTopLeft
; m_vertices
[3].TexCoords = new Vector2f
(0f, m_scaleTopLeft
.Y); m_vertices
[4].TexCoords = new Vector2f
(m_scaleTopLeft
.X, 0f
); m_vertices
[5].TexCoords = new Vector2f
(m_scaleBottomRight
.X, 0f
); m_vertices
[6].TexCoords = new Vector2f
(m_scaleBottomRight
.X, m_scaleTopLeft
.Y ); m_vertices
[7].TexCoords = m_scaleTopLeft
; m_vertices
[8].TexCoords = new Vector2f
(m_scaleBottomRight
.X, 0f
); m_vertices
[9].TexCoords = new Vector2f
(textureBottomRight
.X, 0f
); m_vertices
[10].TexCoords = new Vector2f
(textureBottomRight
.X, m_scaleTopLeft
.Y ); m_vertices
[11].TexCoords = new Vector2f
(m_scaleBottomRight
.X, m_scaleTopLeft
.Y ); // centre row m_vertices
[12].TexCoords = new Vector2f
( 0f, m_scaleTopLeft
.Y ); m_vertices
[13].TexCoords = m_scaleTopLeft
; m_vertices
[14].TexCoords = new Vector2f
( m_scaleTopLeft
.X, m_scaleBottomRight
.Y ); m_vertices
[15].TexCoords = new Vector2f
(0f, m_scaleBottomRight
.Y); m_vertices
[16].TexCoords = m_scaleTopLeft
; m_vertices
[17].TexCoords = new Vector2f
(m_scaleBottomRight
.X, m_scaleTopLeft
.Y); m_vertices
[18].TexCoords = m_scaleBottomRight
; m_vertices
[19].TexCoords = new Vector2f
(m_scaleTopLeft
.X, m_scaleBottomRight
.Y ); m_vertices
[20].TexCoords = new Vector2f
(m_scaleBottomRight
.X, m_scaleTopLeft
.Y); m_vertices
[21].TexCoords = new Vector2f
(textureBottomRight
.X, m_scaleTopLeft
.Y ); m_vertices
[22].TexCoords = new Vector2f
(textureBottomRight
.X, m_scaleBottomRight
.Y); m_vertices
[23].TexCoords = m_scaleBottomRight
; // bottom row m_vertices
[24].TexCoords = new Vector2f
(0f, m_scaleBottomRight
.Y ); m_vertices
[25].TexCoords = new Vector2f
(m_scaleTopLeft
.X, m_scaleBottomRight
.Y ); m_vertices
[26].TexCoords = new Vector2f
(m_scaleTopLeft
.X, textureBottomRight
.Y ); m_vertices
[27].TexCoords = new Vector2f
(0f, textureBottomRight
.Y ); m_vertices
[28].TexCoords = new Vector2f
(m_scaleTopLeft
.X, m_scaleBottomRight
.Y); m_vertices
[29].TexCoords = m_scaleBottomRight
; m_vertices
[30].TexCoords = new Vector2f
(m_scaleBottomRight
.X, textureBottomRight
.Y); m_vertices
[31].TexCoords = new Vector2f
(m_scaleTopLeft
.X, textureBottomRight
.Y ); m_vertices
[32].TexCoords = m_scaleBottomRight
; m_vertices
[33].TexCoords = new Vector2f
(textureBottomRight
.X, m_scaleBottomRight
.Y ); m_vertices
[34].TexCoords = textureBottomRight
; m_vertices
[35].TexCoords = new Vector2f
(m_scaleBottomRight
.X, textureBottomRight
.Y ); Vector2f textureRectangleOffset
= new Vector2f
(m_textureRectangle
.Left, m_textureRectangle
.Top); foreach ( Vertex vertex
in m_vertices
) { Vector2f texCoords
= vertex
.TexCoords; texCoords
+= textureRectangleOffset
+ trimAmount
; } } private Vector2f GetResultingPositionOfTexCoord
(Vector2f textureCoords
) { Vector2f result
= new Vector2f
(); Vector2f newBottomRightScaled
= m_size
- (m_trimmedSize
- m_scaleBottomRight
); Vector2f scaleSize
= m_scaleBottomRight
- m_scaleTopLeft
; Vector2f newScaleSize
= newBottomRightScaled
- m_scaleTopLeft
; if (textureCoords
.X <= m_scaleTopLeft
.X) result
.X = textureCoords
.X; else if (textureCoords
.X >= m_scaleTopLeft
.X) result
.X = newBottomRightScaled
.X + (textureCoords
.X - m_scaleBottomRight
.X); else result
.X = (textureCoords
.X- m_scaleTopLeft
.X) / scaleSize
.X * newScaleSize
.X + m_scaleTopLeft
.X; if (textureCoords
.Y <= m_scaleTopLeft
.Y) result
.Y = textureCoords
.Y; else if (textureCoords
.Y >= m_scaleTopLeft
.Y) result
.Y = newBottomRightScaled
.Y + (textureCoords
.Y - m_scaleBottomRight
.Y); else result
.Y = (textureCoords
.Y - m_scaleTopLeft
.Y) / scaleSize
.Y * newScaleSize
.Y + m_scaleTopLeft
.Y; return result
; } }} But It can't display and I have downloaded picture into root directory of my compiled Release version of executable.
I have tried...
And I have same picture name by SalbeWard.
Result: picture doesn't show....
How do I resolve if image loads into SFML and doesn't shows me.