Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML.net How do I create a custom drawable shape?  (Read 2061 times)

0 Members and 2 Guests are viewing this topic.

Shadowblitz16

  • Newbie
  • *
  • Posts: 13
    • View Profile
SFML.net How do I create a custom drawable shape?
« on: September 16, 2018, 11:10:19 pm »
can anybody tell me how I create a custom drawable shape?
SetPoint does not exists and the GetPoint and GetPointCount are not implemented in base

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SFML.Graphics;
using SFML.System;

namespace SFMLTest.Library.GraphicsHelpers
{
    public class RectShape : Shape, Drawable
    {
        public Vector2f Offset { get; set; } = new Vector2f(0, 0);

        public RectShape(float x, float y, float w, float z) : base()
        {
            //This method does not exist?
            SetPoint(0, x);
            SetPoint(1, y);
            SetPoint(2, x + w);
            SetPoint(3, y + h);

        }

        //WTF do i do in these methods? can't call base because its abstract
        public override uint GetPointCount()
        {
        }

        public override Vector2f GetPoint(uint index)
        {
        }
    }
}


ok so I came up with
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SFML.Graphics;
using SFML.System;

namespace SFMLTest.GraphicsHelpers
{
    public class RectShape : Drawable
    {

        protected Vertex[] vertices;
        protected Transform transform;


        public Vector2f Pos    { get; set; } = new Vector2f(0,0);
        public Vector2f Size   { get; set; } = new Vector2f(0,0);
        public Vector2f Scale  { get; set; } = new Vector2f(1,1);
        public Vector2f Origin { get; set; } = new Vector2f(0,0);
        public float    Angle  { get; set; } = 0.0f;
        public Color    Color  { get; set; } = Color.White;


        public RectShape() : base()
        {
            vertices = new Vertex[4];
        }

        void Drawable.Draw(RenderTarget target, RenderStates states)
        {
            float x1 = Pos.X;
            float y1 = Pos.Y;
            float x2 = Pos.X + Size.X;
            float y2 = Pos.X + Size.Y;

            transform.Scale (Scale, Origin);
            transform.Rotate(Angle, Origin);

            vertices[0].Position = transform.TransformPoint(x1, y1);
            vertices[1].Position = transform.TransformPoint(x1, y2);
            vertices[2].Position = transform.TransformPoint(x2, y2);
            vertices[3].Position = transform.TransformPoint(x2, y1);
            vertices[0].Color    = Color;
            vertices[1].Color    = Color;
            vertices[2].Color    = Color;
            vertices[3].Color    = Color;
            target.Draw(vertices, PrimitiveType.Quads);
        }
       
        public uint     GetPointCount()
        {
            return (uint)vertices.Length;
        }
        public Vector2f GetPoint(uint index)
        {
            return vertices[index].Position;
        }
    }
}


however the vertices seems to always be 0,0 even though I apply the properties
« Last Edit: September 17, 2018, 01:33:16 am by Shadowblitz16 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML.net How do I create a custom drawable shape?
« Reply #1 on: September 17, 2018, 07:58:00 am »
Why don't you look at one of the built-in derived classes?

https://github.com/SFML/SFML.Net/blob/master/src/Graphics/RectangleShape.cs
Laurent Gomila - SFML developer

Shadowblitz16

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: SFML.net How do I create a custom drawable shape?
« Reply #2 on: September 18, 2018, 09:31:55 pm »
I was going to make my own like lines and arcs however I wanted to test it out on a custom rectangle first