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

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

        int currentViewportIdIndex, currentLayerGroupIdIndex, currentGroupIdIndex;
        uint currentArrivalIndex;
        byte currentViewportGroupId, maxViewportGroupId;
        byte currentLayerGroupId, maxLayerGroupId;
        byte currentGroupId, maxGroupId;
        CCCommandType currentCommandType;
        CCRawList<CCV3F_C4B_T2F_Quad> currentBatchedQuads;
        CCRawList<CCQuadCommand> quadCommands;
        CCRawList<CCRenderCommand> renderQueue;
        CCDrawManager drawManager;

        const uint MaxStackDepth = 100;
        readonly Viewport[] viewportGroupStack;
        readonly Matrix[] layerGroupViewMatrixStack;
        readonly Matrix[] layerGroupProjMatrixStack;
        readonly byte[] viewportGroupIdStack, layerGroupIdStack, groupIdStack;


        #region Constructors

        internal CCRenderer(CCDrawManager drawManagerIn)
        {
            currentBatchedQuads = new CCRawList<CCV3F_C4B_T2F_Quad>(256, true);
            quadCommands = new CCRawList<CCQuadCommand>(256, true);
            renderQueue = new CCRawList<CCRenderCommand>();
            drawManager = drawManagerIn;

            viewportGroupStack = new Viewport[MaxStackDepth];
            layerGroupViewMatrixStack = new Matrix[MaxStackDepth];
            layerGroupProjMatrixStack = new Matrix[MaxStackDepth];
            viewportGroupIdStack = new byte[MaxStackDepth];
            layerGroupIdStack = new byte[MaxStackDepth];
            groupIdStack = new byte[MaxStackDepth];
        }

        #endregion Constructors

        public void AddCommand(CCRenderCommand command)
        {
            // Render command might be used multiple times per draw loop
            // e.g. within render texture
            if(currentGroupId != 0)
                command = command.Copy();

            command.Group = currentGroupId;
            command.ViewportGroup = currentViewportGroupId;
            command.LayerGroup = currentLayerGroupId;
            command.ArrivalIndex = ++currentArrivalIndex;

            renderQueue.Push(command);
        }

        internal void PushViewportGroup(ref Viewport viewport)
        {
            currentViewportGroupId = ++maxViewportGroupId;
            viewportGroupIdStack[++currentViewportIdIndex] = currentViewportGroupId;
            viewportGroupStack[currentViewportGroupId] = viewport;
        }

        internal void PopViewportGroup()
        {
            currentViewportGroupId = viewportGroupIdStack[--currentViewportIdIndex];
        }

        internal void PushLayerGroup(ref Matrix viewMatrix, ref Matrix projMatrix)
        {
            if(currentLayerGroupId == MaxStackDepth - 1)
            {
                Debug.Fail(String.Format("Maximum layer depth of {0} reached", MaxStackDepth));
                return;
            }<commit_msg>Renderer: Fix compile error for WindowsRT.  Debug.Fail does not exist<commit_after>﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

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

        int currentViewportIdIndex, currentLayerGroupIdIndex, currentGroupIdIndex;
        uint currentArrivalIndex;
        byte currentViewportGroupId, maxViewportGroupId;
        byte currentLayerGroupId, maxLayerGroupId;
        byte currentGroupId, maxGroupId;
        CCCommandType currentCommandType;
        CCRawList<CCV3F_C4B_T2F_Quad> currentBatchedQuads;
        CCRawList<CCQuadCommand> quadCommands;
        CCRawList<CCRenderCommand> renderQueue;
        CCDrawManager drawManager;

        const uint MaxStackDepth = 100;
        readonly Viewport[] viewportGroupStack;
        readonly Matrix[] layerGroupViewMatrixStack;
        readonly Matrix[] layerGroupProjMatrixStack;
        readonly byte[] viewportGroupIdStack, layerGroupIdStack, groupIdStack;


        #region Constructors

        internal CCRenderer(CCDrawManager drawManagerIn)
        {
            currentBatchedQuads = new CCRawList<CCV3F_C4B_T2F_Quad>(256, true);
            quadCommands = new CCRawList<CCQuadCommand>(256, true);
            renderQueue = new CCRawList<CCRenderCommand>();
            drawManager = drawManagerIn;

            viewportGroupStack = new Viewport[MaxStackDepth];
            layerGroupViewMatrixStack = new Matrix[MaxStackDepth];
            layerGroupProjMatrixStack = new Matrix[MaxStackDepth];
            viewportGroupIdStack = new byte[MaxStackDepth];
            layerGroupIdStack = new byte[MaxStackDepth];
            groupIdStack = new byte[MaxStackDepth];
        }

        #endregion Constructors

        public void AddCommand(CCRenderCommand command)
        {
            // Render command might be used multiple times per draw loop
            // e.g. within render texture
            if(currentGroupId != 0)
                command = command.Copy();

            command.Group = currentGroupId;
            command.ViewportGroup = currentViewportGroupId;
            command.LayerGroup = currentLayerGroupId;
            command.ArrivalIndex = ++currentArrivalIndex;

            renderQueue.Push(command);
        }

        internal void PushViewportGroup(ref Viewport viewport)
        {
            currentViewportGroupId = ++maxViewportGroupId;
            viewportGroupIdStack[++currentViewportIdIndex] = currentViewportGroupId;
            viewportGroupStack[currentViewportGroupId] = viewport;
        }

        internal void PopViewportGroup()
        {
            currentViewportGroupId = viewportGroupIdStack[--currentViewportIdIndex];
        }

        internal void PushLayerGroup(ref Matrix viewMatrix, ref Matrix projMatrix)
        {
            if(currentLayerGroupId == MaxStackDepth - 1)
            {
                Debug.Assert(false,String.Format("Maximum layer depth of {0} reached", MaxStackDepth));
                return;
            }
{'fraction_non_alphanumeric': 0.06629013079667063, 'fraction_numerical': 0.0089179548156956, 'mean_word_length': 2.4384261624936125, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5971887', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1730, 'n_words': 383}