How to save (line) drawing correctly as Sprite

Hi there,

What I'm trying to accomplish is to save a line drawing to a sprite to later reuse the drawing.
In another thread I have seen the solution to this problem, but unfortunately I have some issues because the saved drawing is somehow cut off at some points. It seems like there is a problem with the dimensions. So far I have done following:

CCDrawNode line;

`public ModelNode(CCDrawNode line) : base()
{
CCSize cSize = line.ContentSize;
rtm = new CCRenderTexture(cSize, cSize, CCSurfaceFormat.Color, CCDepthFormat.Depth24Stencil8);

        rtm.BeginWithClear(CCColor4B.Transparent);
        line.Visit();
        rtm.End();

        rtm.Texture.IsAntialiased = true;

        modelSprite = new CCSprite(rtm.Texture);
        modelSprite.AnchorPoint = CCPoint.AnchorMiddle;
        this.AddChild(modelSprite);
    }`

`mNode = new ModelNode(line);
mNode.Position = this.VisibleBoundsWorldspace.Center;
this.AddChild(mNode);

            line.Clear();`

Here's a screenshot showing both the drawn line and the as a sprite saved, cut off node.

Someone an idea what is going wrong?

Posts

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    Hey @MuhamedAhmetovic

    We have a test example that does basically this already, unfortunately it has been change a little to work with our new release code base. Could you please create a very small project for us to take a look at. It could be something simple but would need to see exactly what you are doing in the code with the project and platform.

    Thanks

  • Hey @kjpou1

    Thanks for the answer!
    I have uploaded a zip file with my whole project, which es exactly (and only) doing as I described.
    Somehow the call "line.ContentSize" is showing a strange behavior to me... sometimes I get negative sizes when drawing small things.

    You can download the project HERE

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    @MuhamedAhmetovic

    Ok took a look at this.

    First, there is a problem with the ContentSize calculations which we have set an issue up for here. This should also fix your problem of negative values that you reported as well. Thanks for the example so we could fix this.

    Second, although there was a problem with the ContentSize calculations it does not have any bearing on your clipping problem. When the CCDrawNode is used via a Visit it is offset from the bottom left corner of the screen. At this time we do not support passing in a custom viewport for the render texture so you will need to offset the position by the amount from the bottom left. So when you gather your touches you will need to calculate the minimum origin of the lines that you are drawing to the line node. You can then pass that to the ModelNode creation.

        rtm.BeginWithClear(CCColor4B.Green);
       // Offset our drawing Visit
        line.Position = -minimumOriginOffset;
        line.Visit();
        rtm.End();
        // Reset position if need be or subsequent drawings may not be correct.
        line.Position = CCPoint.Zero;
    

    Third, to help with this we have created another issue to calculate this bounding rectangle. This will be available in the upcoming release. Should be this week.

    With this new property you will be able to do the following.

    rtm.BeginWithClear(CCColor4B.Green);
    line.Position = -line.BoundingRect.Origin;
    line.Visit();
    rtm.End();
    line.Position = CCPoint.Zero;
    

    Let us know if this works.

  • @kjpou1

    Wow. Many thanks for the fast answer and for the fixes! You're great!!

    So just that I understand this correctly: minimumOriginOffset is the Point (CCPoint) which basically in your screenshots is the bottom left point of the "green bounding rect" is. Is that correct? So that would mean calculating the minimum X and the minimum Y coordinate of all the touch points and creating the minimumOriginOffset with these two coordinates? Or did I understand it wrong?

    For the ContentSize method is the fix already available or is it also coming this week?

    Best regards
    Muhamed

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    @MuhamedAhmetovic

    Yes that is correct. Calculate the min X and min Y of the touch points.

    Here is the CCDrawNode with the fixes UpdateContentSize and new Property BoundingRect

    In the private void UpdateContextSize() is where we are calculating the min X and min Y of the vertices. Try something like what is done there.

    We are hoping to get the next release out sometime this week. Lots of bug fixes and internal changes which is why it has been so long.

  • MuhamedAhmetovicMuhamedAhmetovic CHMember
    edited June 2015

    @kjpou1

    So how can I replace the current CCDrawNode with the one you fixed (on a Mac)??

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai
    edited June 2015

    You will have to wait for the next release.

    (Edited) or of course build from source.

  • @kjpou1

    Ok I'm just a little bit confused. You said earlier that before the new release I can use following way to get it correct:

    rtm.BeginWithClear(CCColor4B.Green); // Offset our drawing Visit line.Position = -minimumOriginOffset; line.Visit(); rtm.End(); // Reset position if need be or subsequent drawings may not be correct. line.Position = CCPoint.Zero;

    I implemented it but I'm getting even worse results, but I guess that's because of the ContentSize bug isn't it?

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    @MuhamedAhmetovic

    Try the following from the files attached.

  • @kjpou1

    Many thanks again you are my lifesaver! (Need this for my bachelor thesis :smile: )

    Ah now I see I had to create the bounding rect by myself with the minX,minY,maxX and maxY and then pass that to the ModelNode.

    What exactly are you doing here?

    line.AdditionalTransform = CCAffineTransform.Translate(CCAffineTransform.Identity, -bounding.Origin.X + frameSize * 0.5f, -bounding.Origin.Y + frameSize * 0.5f);

    Is this the offset from the bottom left you were talking about? Why use an AffineTransform?

    One more question:
    When the release is out the whole calculating the max and min vertices and creating the rect can just be replaced by line.BoundingRect.Origin is this right?

    Sorry for the many questions. I really appreciate your help though! Many thanks!

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    Yes, the BoundingRect will be available.

    I just did it that way to show that there are multiple ways of accomplishing the same thing.

    Replace the Translate stuff with Position and you will be golden.

  • Many thanks!!

Sign In or Register to comment.