Ejemplo n.º 1
0
        public unsafe override void Loop(EngineLoopInfo loopInfo)
        {
            var style = ImGui.GetStyle();

            ImGui.StyleColorsClassic(style);

            if (ImGui.BeginMainMenuBar())
            {
                if (ImGui.BeginMenu("Options"))
                {
                    int min = 0;
                    int max = 1000000;
                    if (ImGui.SliderInt("Doges", ref doges, min, max, doges.ToString()))
                    {
                        Console.WriteLine($"You chose {doges} doges");

                        var toAdd = Math.Max(0, doges - sprites.Count);
                        Console.WriteLine($"Adding {toAdd}");
                        var texture = ResourceManager.GetTexture("Bunny");
                        var random  = new Random();

                        for (int i = 0; i < toAdd; i++)
                        {
                            // Sprites
                            var next   = random.NextDouble();
                            var sprite = new SpritePrimitive(texture)
                            {
                                Position = new Vector2((float)random.NextDouble() * 600, (float)random.NextDouble() * 600),
                                Origin   = Vector2.One / 2
                            };
                            //sprite.ZIndex = sprite.Position.X / 10000f;
                            sprites.Add(sprite);
                            Add(sprite);
                        }
                    }

                    ImGui.EndMenu();
                }
                ImGui.Separator();
                ImGui.Text($"{loopInfo.FramesPerSecond} fps / {loopInfo.MillisecondsPerFrame} ms");
                ImGui.EndMainMenuBar();
            }

            foreach (var spritePrimitive in sprites)
            {
                spritePrimitive.Rotation += 3.0f * loopInfo.SecondsPerFrame;
            }
        }
        /// <summary>
        /// Set the max <see cref="Sprite">Sprite</see>'s count can be attach to renderer.
        /// </summary>
        /// <param name="maxSprites">How many sprite the renderer can be attach.</param>
        public void Resize( int maxSprites )
        {
            int layer = gameObject.layer;
            int oldLen = primitives.Length;


            if (oldLen < maxSprites)
            {
                //System.Array.Resize<SpritePrimitive>(ref primitives, maxSprites * 2);
                //System.Array.Resize<SpriteTransform>(ref spriteTransforms, maxSprites * 2);

                SpritePrimitive[] oldPriv = primitives;
                primitives = new SpritePrimitive[maxSprites * 2];
                for (int i = 0; i < oldLen; i++)
                    primitives[i] = oldPriv[i];


                SpriteTransform[] oldSprTran = spriteTransforms;
                spriteTransforms = new SpriteTransform[maxSprites * 2];
                for (int i = 0; i < oldLen; i++)
                    spriteTransforms[i] = oldSprTran[i];


                int ownerID = instanceID;
                for (int i = oldLen; i < primitives.Length; i++)
                {
                    SpritePrimitive pri = new SpritePrimitive(1);
                    pri.ownerID = ownerID;
                    primitives[i] = pri;

                    spriteTransforms[i] = new SpriteTransform();
                    spriteTransforms[i].ResetTransform();
                }
            }

            primitiveCount = primitives.Length;

            primitiveGroup.primitives = primitives;
        }
        bool SetupSpritePrimitiveXZ(SpritePrimitive pri, SpriteTransform sTransform, Vector2 parentPosition, float parentRotation, Vector2 parentScale, int layer,  bool isFirst)
        {
            sTransform.primitive = pri;

            if ( !sTransform.visible || (!sTransform.isSpriteValid && !sTransform.isOverride) )
            {
                pri.visible = false;
                return isFirst;
            }

            Sprite spr = sTransform.isOverride ? sTransform._override : sTransform._sprite;


            layer = (layer - 0xf <= 0 ? 0 : layer) & 0xf;

            float z = (layer << 3) | sTransform.layer;
            pri.compareKey = calcSortKey((ulong)spr.texID, pri, gameObjectLayer, (uint)depth, (uint)sTransform.layer, renderMode, applyLayerToZ);

            //TRS
            {
                float ry = -pri.z;

                float r = Mathf.Deg2Rad * (sTransform.rotation);
                float sin = Mathf.Sin(r);
                float cos = Mathf.Cos(r);

                float pr = Mathf.Deg2Rad * (parentRotation);
                float psin = Mathf.Sin(pr);
                float pcos = Mathf.Cos(pr);


                float sx = 0;
                float sy = 0;
                float tx = 0;
                float ty = 0;
                float psx = 0;
                float psy = 0;
                float px = 0;
                float py = 0;


                for (int i = 0, e = spr.vertices.Length; i < e; i++)
                {
                    sx = (spr.vertices[i].x + (spr.vertices[i].y * -sTransform.shear.y)) * sTransform.scale.x;
                    sy = (spr.vertices[i].y + (spr.vertices[i].x * -sTransform.shear.x)) * sTransform.scale.y;

                    tx = (sx * cos - sy * sin);
                    ty = (sx * sin + sy * cos);

                    psx = (sTransform.position.x + tx) * parentScale.x;
                    psy = (sTransform.position.y + ty) * parentScale.y;

                    tx = parentPosition.x + (psx * pcos - psy * psin);
                    ty = parentPosition.y + (psx * psin + psy * pcos);

                    pri.position[i].x = tx;
                    pri.position[i].z = ty;
                    pri.position[i].y = ry;

                    //Vector2 
                    if (isFirst)
                    {
                        boundingAABB.position_minx = tx;
                        boundingAABB.position_miny = ty;
                        boundingAABB.position_maxx = tx;
                        boundingAABB.position_maxy = ty;
                        isFirst = false;
                    }
                    else
                    {
                        boundingAABB.position_minx = tx < boundingAABB.position_minx ? tx : boundingAABB.position_minx;
                        boundingAABB.position_miny = ty < boundingAABB.position_miny ? ty : boundingAABB.position_miny;
                        boundingAABB.position_maxx = tx > boundingAABB.position_maxx ? tx : boundingAABB.position_maxx;
                        boundingAABB.position_maxy = ty > boundingAABB.position_maxy ? ty : boundingAABB.position_maxy;
                    }
                }
            }

            {
                float tmpColorR = color.r * sTransform.color.r;
                float tmpColorG = color.g * sTransform.color.g;
                float tmpColorB = color.b * sTransform.color.b;
                float tmpColorA = color.a * sTransform.color.a;

                pri.color[3].a = tmpColorA * __colorLB.a;
                pri.color[3].r = tmpColorR * __colorLB.r;
                pri.color[3].g = tmpColorG * __colorLB.g;
                pri.color[3].b = tmpColorB * __colorLB.b;

                pri.color[2].a = tmpColorA * __colorRB.a;
                pri.color[2].r = tmpColorR * __colorRB.r;
                pri.color[2].g = tmpColorG * __colorRB.g;
                pri.color[2].b = tmpColorB * __colorRB.b;

                pri.color[1].a = tmpColorA * __colorRT.a;
                pri.color[1].r = tmpColorR * __colorRT.r;
                pri.color[1].g = tmpColorG * __colorRT.g;
                pri.color[1].b = tmpColorB * __colorRT.b;

                pri.color[0].a = tmpColorA * __colorLT.a;
                pri.color[0].r = tmpColorR * __colorLT.r;
                pri.color[0].g = tmpColorG * __colorLT.g;
                pri.color[0].b = tmpColorB * __colorLT.b;
            }

            pri.renderMode = _renderMode;

            //if (pri.texId != spr.texID)
            {
                pri.uv[0] = spr.uvs[0];
                pri.uv[1] = spr.uvs[1];
                pri.uv[2] = spr.uvs[2];
                pri.uv[3] = spr.uvs[3];

                pri.texture = spr.image;
                pri.texId = spr.texID;
            }

            pri.visible = _visible;

            return isFirst;
        }
        internal ulong calcSortKey(ulong texId, SpritePrimitive pri, uint gameObjectLayer, uint depthInLayer, uint subLayer, SpriteRenderModeSetting renderMode, bool applyLayerToZ)
        {
            ulong ret = 0;
            if (renderMode.sortByDepth)
            {
                pri.z = preCalcZ +
                    (float)subLayer * 0.00390625f;

                ret = preCalcKey |
                    ((0x1ful - subLayer) << 13) |                        // sublayer             14 - 17  4
                    (texId & 0x3ff);                                     // texture              0  -  9  10
            }
            else
            {
                pri.z = preCalcZ +
                    (float)subLayer * 0.00390625f;

                ret = preCalcKey |
                    (texId << 52);

            }
            return ret;
        }
        static internal ulong calcSortKey(ulong texId, SpritePrimitive pri, uint gameObjectLayer, uint depthInLayer, uint subLayer, SpriteRenderModeSetting renderMode, bool applyLayerToZ)
        {
            ulong ret = 0;
            ulong idx = (ulong)renderMode.renderMode & 0xf;

            uint layer = applyLayerToZ ? 
                ( (gameObjectLayer <= 0xf ? 0 : gameObjectLayer) & 0xf) : 0;

            if (renderMode.sortByDepth)
            {
                uint z = (uint)depthInLayer & 0x1ff;

                float _z = (layer * 64) +
                    ((float)z * 0.125f) +
                    (float)subLayer * 0.00390625f;

                pri.z = _z;

                uint oid = ((uint)pri.ownerID) & 0x7fffffff;
                                                                        // usage                 start    bits
                ret = (0x1ul << 63) |                                   // isSemiTransparent     63       1
                    ((0xful - layer) << 59) |                           // gameObjectLayer       58 - 62  4
                    ((0x1fful - z) << 49) |                             // transform z          48 - 57  9
                    ( oid << 15) |                     // group by owner       15 - 47  32
                    ((0x1ful - subLayer) << 10) |                        // sublayer             11 - 14  3
                    (idx << 6) |                                        // rendermode           7 -  10  4
                    (texId)                                             // texture              0  -  6  7     
                ;
            }
            else
            {
                uint z = (uint)depthInLayer & 0x1ff;

                float _z = (layer * 64) +
                    ((float)z * 0.125f) +
                    (float)subLayer * 0.0078125f;

                pri.z = _z;

                ret = (idx << 59) |
                    (texId << 52);

            }
            return ret;
        }
Ejemplo n.º 6
0
        public Tile(Vector2 position)
        {
            tile = new SpritePrimitive("tilesetComColunas", position, new Vector2(320, 320), 5, 5, 0);

            this.position = position;
        }