<commit_before>﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;

namespace CocosSharp
{
    internal class RenderQueuePriority : IComparer<long>
    {

        public int Compare(long first, long other)
        {
            // 2D - We could probably always return 0 here because our children should already be sorted.
            // but just to make sure if something changes later
            // When we implement 3D we will need to take other factores into account.
            var depth1 = first >> 24;
            var depth2 = other >> 24;

            if (depth1 < depth2)
                return 1;
            else if (depth1 > depth2)
                return -1;
            else return 0;
               
        }
    }

    // Implementation based off of discussion "Implementing a Render Queue for Games" http://ploobs.com.br/?p=2378 
    internal class CCRenderer
    {      
        [Flags]
        internal enum CCCommandType
        {
            None = 0x0,
            Quad = 0x1,
            Custom = 0x2,
            Primitive = 0x4,
        }

        CCCommandType currentCommandType;
        CCRawList<CCV3F_C4B_T2F_Quad> currentBatchedQuads;
        List<CCQuadCommand> quadCommands;
        CCRenderQueue<long, CCRenderCommand> renderQueue;
        CCDrawManager drawManager;


        #region Constructors

        internal CCRenderer(CCDrawManager drawManagerIn)
        {
            currentBatchedQuads = new CCRawList<CCV3F_C4B_T2F_Quad>();
            quadCommands = new List<CCQuadCommand>();
            renderQueue = new CCRenderQueue<long, CCRenderCommand>(new RenderQueuePriority());
            drawManager = drawManagerIn;
        }

        #endregion Constructors

        public void AddCommand(CCRenderCommand command)
        {
            renderQueue.Enqueue(command.RenderId, command);
        }

        internal void VisitRenderQueue()
        {
            currentCommandType = CCCommandType.None;

            while (renderQueue.HasItems)
            {
                var command = renderQueue.Dequeue();
                command.RequestRenderCommand(this);
            }

            // Flush any remaining render commands
            Flush();
        }

        #region Processing render commands

        internal void ProcessQuadRenderCommand(CCQuadCommand quadCommand)
        {
            var worldTransform = quadCommand.WorldTransform;
            var identity = worldTransform == CCAffineTransform.Identity;

            quadCommands.Add(quadCommand);

            var quads = quadCommand.Quads;
            for (int i = 0, N = quadCommand.QuadCount; i < N; ++i)
            {
                if (identity)
                    currentBatchedQuads.Add(quads[i]);
                else
                    currentBatchedQuads.Add(worldTransform.Transform(quads[i]));
            }

            // We've changing command types so render any pending sequence of commandss
            // e.g. Batched quad commands
            if((currentCommandType | CCCommandType.Quad) == CCCommandType.None)
                Flush();

            currentCommandType = CCCommandType.Quad;
        }

        internal void ProcessPrimitiveRenderCommand <T, T2> (CCPrimitiveCommand <T, T2> primitiveCommand)
            where T : struct, IVertexType
            where T2 : struct
           
        {
            var worldTransform = primitiveCommand.WorldTransform;

            // We've changing command types so render any pending sequence of commands
            // e.g. Batched quad commands
            if((currentCommandType | CCCommandType.Primitive) == CCCommandType.None)
                Flush();

            primitiveCommand.RenderPrimitive(drawManager);

            currentCommandType = CCCommandType.Primitive;

        }

        internal void ProcessCustomRenderCommand(CCCustomCommand customCommand)
        {
            // We've changing command types so render any pending sequence of commands
            // e.g. Batched quad commands
            if((currentCommandType | CCCommandType.Custom) == CCCommandType.None)
                Flush();

            currentCommandType = CCCommandType.Custom;
        }

        #endregion Processing render commands


        void Flush()
        {
            switch(currentCommandType)
            {
                case CCCommandType.Quad:
                    DrawBatchedQuads();
                    break;
                default:<commit_msg>Renderer: Make sure custom command Action is actually called<commit_after>﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;

namespace CocosSharp
{
    internal class RenderQueuePriority : IComparer<long>
    {

        public int Compare(long first, long other)
        {
            // 2D - We could probably always return 0 here because our children should already be sorted.
            // but just to make sure if something changes later
            // When we implement 3D we will need to take other factores into account.
            var depth1 = first >> 24;
            var depth2 = other >> 24;

            if (depth1 < depth2)
                return 1;
            else if (depth1 > depth2)
                return -1;
            else return 0;
               
        }
    }

    // Implementation based off of discussion "Implementing a Render Queue for Games" http://ploobs.com.br/?p=2378 
    internal class CCRenderer
    {      
        [Flags]
        internal enum CCCommandType
        {
            None = 0x0,
            Quad = 0x1,
            Custom = 0x2,
            Primitive = 0x4,
        }

        CCCommandType currentCommandType;
        CCRawList<CCV3F_C4B_T2F_Quad> currentBatchedQuads;
        List<CCQuadCommand> quadCommands;
        CCRenderQueue<long, CCRenderCommand> renderQueue;
        CCDrawManager drawManager;


        #region Constructors

        internal CCRenderer(CCDrawManager drawManagerIn)
        {
            currentBatchedQuads = new CCRawList<CCV3F_C4B_T2F_Quad>();
            quadCommands = new List<CCQuadCommand>();
            renderQueue = new CCRenderQueue<long, CCRenderCommand>(new RenderQueuePriority());
            drawManager = drawManagerIn;
        }

        #endregion Constructors

        public void AddCommand(CCRenderCommand command)
        {
            renderQueue.Enqueue(command.RenderId, command);
        }

        internal void VisitRenderQueue()
        {
            currentCommandType = CCCommandType.None;

            while (renderQueue.HasItems)
            {
                var command = renderQueue.Dequeue();
                command.RequestRenderCommand(this);
            }

            // Flush any remaining render commands
            Flush();
        }

        #region Processing render commands

        internal void ProcessQuadRenderCommand(CCQuadCommand quadCommand)
        {
            var worldTransform = quadCommand.WorldTransform;
            var identity = worldTransform == CCAffineTransform.Identity;

            quadCommands.Add(quadCommand);

            var quads = quadCommand.Quads;
            for (int i = 0, N = quadCommand.QuadCount; i < N; ++i)
            {
                if (identity)
                    currentBatchedQuads.Add(quads[i]);
                else
                    currentBatchedQuads.Add(worldTransform.Transform(quads[i]));
            }

            // We've changing command types so render any pending sequence of commandss
            // e.g. Batched quad commands
            if((currentCommandType | CCCommandType.Quad) == CCCommandType.None)
                Flush();

            currentCommandType = CCCommandType.Quad;
        }

        internal void ProcessPrimitiveRenderCommand <T, T2> (CCPrimitiveCommand <T, T2> primitiveCommand)
            where T : struct, IVertexType
            where T2 : struct
           
        {
            var worldTransform = primitiveCommand.WorldTransform;

            // We've changing command types so render any pending sequence of commands
            // e.g. Batched quad commands
            if((currentCommandType | CCCommandType.Primitive) == CCCommandType.None)
                Flush();

            primitiveCommand.RenderPrimitive(drawManager);

            currentCommandType = CCCommandType.Primitive;

        }

        internal void ProcessCustomRenderCommand(CCCustomCommand customCommand)
        {
            // We've changing command types so render any pending sequence of commands
            // e.g. Batched quad commands
            if((currentCommandType | CCCommandType.Custom) == CCCommandType.None)
                Flush();

            customCommand.RenderCustomCommand();

            currentCommandType = CCCommandType.Custom;
        }

        #endregion Processing render commands


        void Flush()
        {
            switch(currentCommandType)
            {
                case CCCommandType.Quad:
                    DrawBatchedQuads();
                    break;
                default:
{'fraction_non_alphanumeric': 0.06807900253779102, 'fraction_numerical': 0.008385744234800839, 'mean_word_length': 1.7450030284675955, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2943727', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2188, 'n_words': 588}