Main
using System;
using System.Collections;
using System.Collections.Generic;
using Sifteo;

namespace Libraries
{
    // Purpose:
    // Renerer for RenderObj.  Gets given render objects to render.
    // Renders the objects in layers and order in the layers.
    // Insert a object into the renderer and it will get stored.
    // After rendering, stored objects will get cleared.
    public class Renderer
    {
        // Purpose:
        // Constructor.
        // layers - How many layers there are in the renderer.
        public Renderer(int layers)
        {
            m_layers = new List<RenderObj>[layers];
            for(int layer = 0; layer < m_layers.Length; ++layer)
                m_layers[layer] = new List<RenderObj>();
        }

        // Purpose:
        // Default constructor.  Uses the default number of layers.
        public Renderer()
        {
            m_layers = new List<RenderObj>[c_defaultLayers];
            for(int layer = 0; layer < m_layers.Length; ++layer)
                m_layers[layer] = new List<RenderObj>();
        }

        // Purpose:
        // Insert the given object into the renderer.  Object is stored in the layer and order specified.
        // If the layer is larger than the max layer in the renderer, it gets stored at the max layer.
        // obj - Object to store.
        //     
        public void Insert(RenderObj obj)
        {
            if(null != obj)
            {
                int layer = obj.GetRenderLayer();
                if(layer >= m_layers.Length)
                    layer = m_layers.Length - 1;

                if(!m_layers[layer].Contains(obj))
                {
                    m_layers[layer].Add(obj);

                    // Sort the entries.
                    if(m_sortOnInsert)
                    {
                        RenderSort sorter = new RenderSort();
                        m_layers[layer].Sort(sorter);
                    }
                }
            }
        }

        // Purpose:
        // Sort all entries into the render order.
        public void Sort()
        {
            RenderSort sorter = new RenderSort();

            for(int layer = 0; layer < m_layers.Length; ++layer)
                m_layers[layer].Sort(sorter);
        }

        // Purpose:
        // Render everything to the given cube.
        // Entries are drawn in the order stored, so make sure to sort first.
        public void Render(Cube cube)
        {
            if(m_sortBeforeRender)
                Sort();

            foreach(List<RenderObj> list in m_layers)
            {
                foreach(RenderObj obj in list)
                {
                    obj.Render(cube);
                    obj.m_needsRender = false;
                }
            }

            if(m_clearAfterRender)
                ClearRenderObjs();
        }
       
        // Purpose:
        // Clear render lists, dropping everything stored.
        public void ClearRenderObjs()
        {
            for(int ind = 0; ind < m_layers.Length; ++ind)
                m_layers[ind] = new List<RenderObj>();     
        }      

        // Purpose:
        // Set whether render objects lists should be cleared after
        public void SetClearAfterRender(bool clear)
        {
            m_clearAfterRender = clear;
        }
       
        // Purpose:
        // Set whether render objects should be sorted when Render function is called.  Value is true if the sort should be done, false if not.
        public void SetSortBeforeRender(bool sort)
        {
            m_sortBeforeRender = sort;
        }

        // Purpose:
        // Check if there are any render objects stored.  Return true if there are, fals eif not.
        public bool ContainsRenderObj()
        {
            foreach(List<RenderObj> list in m_layers)
            {
                if(0 != list.Count)
                    return true;
            }

            return false;
        }
   
        // Store the layers of render objects.
        // Make sure to clear each
        private List<RenderObj> [] m_layers;

        // Whether render objects should be dropped after rendering.
        // Value is true to clear after render, false to not.
        private bool m_clearAfterRender = true;

        // Value is true if the entries should be sorted each time a new entry is inserted, false if not.
        private bool m_sortOnInsert = false;
       
        // True if the stored objects should be sorted when render is called, false if not.
        private bool m_sortBeforeRender = false;

        // Default number of layers.
        protected const int c_defaultLayers = 4;
    }
}

 
Website copyright (c) 2012 Benjamin Millek, all rights reserved.