This project is read-only.

when CCSprite resume game erro(windowphone )

Topics: Windows Phone 8
Sep 29, 2014 at 12:53 PM
  • I am using CCSprite draw circle, it is normal when game not resume.
  • if game is resume it fill bground while color and lost fill color circle
-code using :
internal class RoundRectPrimitive : CCNodeRGBA
{
    private const float kappa = 0.552228474f;
    protected int _radius;
    private int _cornerSegments;
    private CCPoint[] _vertices;
    private CCPoint[] _polyVertices;

    public RoundRectPrimitive(CCSize size, int radius)
    {
        _radius = radius;
        _cornerSegments = 4;
        ContentSize = size;
        InitVertices();
    }
    public RoundRectPrimitive(CCSize size, int radius, int cornerSegments)
    {
        _radius = radius;
        _cornerSegments = cornerSegments;
        ContentSize = size;
        InitVertices();
    }
    private void AppendCubicBezier(int startPoint, CCPoint[] vertices, CCPoint origin, CCPoint control1, CCPoint control2, CCPoint destination, int segments)
    {
        float t = 0;
        for (int i = 0; i < segments; i++)
        {
            float x = (float)Math.Pow(1 - t, 3) * origin.X + 3.0f * (float)Math.Pow(1 - t, 2) * t * control1.X + 3.0f * (1 - t) * t * t * control2.X + t * t * t * destination.X;
            float y = (float)Math.Pow(1 - t, 3) * origin.Y + 3.0f * (float)Math.Pow(1 - t, 2) * t * control1.Y + 3.0f * (1 - t) * t * t * control2.Y + t * t * t * destination.Y;
            vertices[startPoint + i] = new CCPoint(x * Scale, y * Scale);
            t += 1.0f / segments;
        }
    }
    public void InitVertices()
    {
        _vertices = new CCPoint[16];

        // Bottom left
        _vertices[0] = new CCPoint(1, 1 + _radius);
        _vertices[1] = new CCPoint(1, 1 + _radius * (1 - kappa));
        _vertices[2] = new CCPoint(1 + _radius * (1 - kappa), 1);
        _vertices[3] = new CCPoint(1 + _radius, 1);

        // Bottom right
        _vertices[4] = new CCPoint(ContentSize.Width - _radius - 1, 1);
        _vertices[5] = new CCPoint(ContentSize.Width - _radius * (1 - kappa) - 1, 1);
        _vertices[6] = new CCPoint(ContentSize.Width - 1, 1 + _radius * (1 - kappa));
        _vertices[7] = new CCPoint(ContentSize.Width - 1, 1 + _radius);

        // Top right
        _vertices[8] = new CCPoint(ContentSize.Width - 1, 1 + ContentSize.Height - _radius);
        _vertices[9] = new CCPoint(ContentSize.Width - 1, 1 + ContentSize.Height - _radius * (1 - kappa));
        _vertices[10] = new CCPoint(ContentSize.Width - _radius * (1 - kappa) - 1, ContentSize.Height - 1);
        _vertices[11] = new CCPoint(ContentSize.Width - _radius - 1, ContentSize.Height - 1);

        // Top left
        _vertices[12] = new CCPoint(1 + _radius, ContentSize.Height - 1);
        _vertices[13] = new CCPoint(1 + _radius * (1 - kappa), ContentSize.Height - 1);
        _vertices[14] = new CCPoint(1, ContentSize.Height - _radius * (1 - kappa) - 1);
        _vertices[15] = new CCPoint(1, ContentSize.Height - _radius - 1);

        _polyVertices = new CCPoint[4 * _cornerSegments + 1];
        AppendCubicBezier(0 * _cornerSegments, _polyVertices, _vertices[0], _vertices[1], _vertices[2], _vertices[3], _cornerSegments);
        AppendCubicBezier(1 * _cornerSegments, _polyVertices, _vertices[4], _vertices[5], _vertices[6], _vertices[7], _cornerSegments);
        AppendCubicBezier(2 * _cornerSegments, _polyVertices, _vertices[8], _vertices[9], _vertices[10], _vertices[11], _cornerSegments);
        AppendCubicBezier(3 * _cornerSegments, _polyVertices, _vertices[12], _vertices[13], _vertices[14], _vertices[15], _cornerSegments);
        _polyVertices[4 * _cornerSegments] = _vertices[0];
    }
    public override void Visit()
    {
        base.Visit();

        CCDrawingPrimitives.Begin();
        CCDrawingPrimitives.DrawSolidPoly(_polyVertices, 4 * _cornerSegments + 1, Color);
        CCDrawingPrimitives.DrawCubicBezier(_vertices[0], _vertices[1], _vertices[2], _vertices[3], _cornerSegments, Color);
        CCDrawingPrimitives.DrawCubicBezier(_vertices[4], _vertices[5], _vertices[6], _vertices[7], _cornerSegments, Color);
        CCDrawingPrimitives.DrawCubicBezier(_vertices[8], _vertices[9], _vertices[10], _vertices[11], _cornerSegments, Color);
        CCDrawingPrimitives.DrawCubicBezier(_vertices[12], _vertices[13], _vertices[14], _vertices[15], _cornerSegments, Color);

        CCDrawingPrimitives.End();

        CCDrawingPrimitives.Begin();
        CCDrawingPrimitives.DrawLine(_vertices[3], _vertices[4], Color);
        CCDrawingPrimitives.DrawLine(_vertices[7], _vertices[8], Color);
        CCDrawingPrimitives.DrawLine(_vertices[11], _vertices[12], Color);
        CCDrawingPrimitives.DrawLine(_vertices[15], _vertices[0], Color);

        CCDrawingPrimitives.End();
    }
}
internal class RoundRect : CCSprite
{
    protected RoundRectPrimitive _primitive;
    protected RoundRectRender _texture;

    public RoundRect() { }
    public RoundRect(CCSize size, int radius)
    {
        _primitive = new RoundRectPrimitive(size, radius)
        {
            Color = CCColor3B.Green
        };
        _texture = new RoundRectRender((int)size.Width, (int)size.Height);
        UpdateColor(CCColor3B.Green);
    }
    public RoundRect(CCSize size, int radius, CCColor3B color)
    {
        _primitive = new RoundRectPrimitive(size, radius)
        {
            Color = color
        };
        _texture = new RoundRectRender((int)size.Width, (int)size.Height);
        UpdateColor(color);
    }

    public void UpdateColor(CCColor3B color)
    {
        _primitive.Color = color;
        _texture.BeginWithClear(0, 0, 0, 0);
        _primitive.Visit();
        _texture.End();
        base.InitWithTexture(_texture.Sprite.Texture);

    }
    public override void Draw()
    {
        base.Draw();

    }

}
  • add screen :
    RoundCircle cc = new RoundCircle(62);
    cc.SetPosition(100, 100);
    AddChild(cc);
please help me , Thanks !
Sep 29, 2014 at 6:44 PM
•if game is resume it fill bground while color and lost fill color circle
This happens because the texture is being disposed during the background event.


public RoundRect(CCSize size, int radius, CCColor3B color)
{
    _primitive = new RoundRectPrimitive(size, radius)
    {
        Color = color
    };
    MakeNewTexture();
}

private void MakeNewTexture() {
    _texture = new RoundRectRender((int)size.Width, (int)size.Height);
    UpdateColor(color);
}

public void UpdateColor(CCColor3B color)
{
    _primitive.Color = color;
    _texture.BeginWithClear(0, 0, 0, 0);
    _primitive.Visit();
    _texture.End();
    base.InitWithTexture(_texture.Sprite.Texture);
}

public override void Draw()
{
    if(_texture == null || _texture.IsDisposed) {
        MakeNewTexture();
    }
    base.Draw();

}
Sep 30, 2014 at 9:44 AM
  • I am using code new MakeNewTexture() when resume it is error at line :
m_pRenderTarget2D = CCDrawManager.CreateRenderTarget(w, h, colorFormat, depthFormat, usage);
and error in class Texture2D.cs
return new SharpDX.Direct3D11.Texture2D(GraphicsDevice._d3dDevice, desc);
  • code using:
    public override void Draw()
    {
        if (_texture == null || _texture.GetDisPose())
        {
           MakeNewTexture();
        }  
        base.Draw();
    }
    
internal class RoundRectRender : CCRenderTexture
{
    private RenderTarget2D m_pRenderTarget2D;
    public RoundRectRender(int w, int h) : base(w, h)
    {

    }
    protected override bool InitWithWidthAndHeight(int w, int h, SurfaceFormat colorFormat, DepthFormat depthFormat, RenderTargetUsage usage)
    {
        w = (int)Math.Ceiling(w * CCMacros.CCContentScaleFactor());
        h = (int)Math.Ceiling(h * CCMacros.CCContentScaleFactor());

        m_pTexture = new CCTexture2D();
        m_pTexture.IsAntialiased = true;
        m_pTexture.HasPremultipliedAlpha = false;
        m_pRenderTarget2D = CCDrawManager.CreateRenderTarget(w, h, colorFormat, depthFormat, usage);
        m_pTexture.InitWithTexture(m_pRenderTarget2D);

        m_pSprite = new CCSprite(m_pTexture);

        AddChild(m_pSprite);

        return true;
    }
    public bool GetDisPose()
    {
        if (m_pTexture != null)
        {
            return m_pTexture.IsDisposed;
        }
        return false;
    }

}







Oct 2, 2014 at 12:56 AM
Edited Oct 2, 2014 at 12:57 AM
Here ya go:

https://github.com/Cocos2DXNA/cocos2d-xna/commit/0f2f59826081fa1508e79f4e16a2e2908dec275e

I added your rounded rect code as a test case and created a new test case for creating a sprite, on the fly, using the ccrendertexture and your rounded rect drawing code. I also fixed the restore on CCRenderTexture so that it recreates its texture on the fly.

Any time you are restoring from background, though, you need to test the IsDisposed property on your graphics assets. Almost always they will all be disposed and you will need to recreate them. The CCRenderTexture will do this automatically, as far as it can anyway.

The changes you are interested in are in this file:

tests/tests/classes/tests/DrawPrimitivesTest/DrawPrimitivesTest.cs