#pragma once
#define MLR_MLRSORTBYORDER_HPP

#include "MLR.hpp"
#include "MLRSorter.hpp"
#include "MLRState.hpp"

namespace MidLevelRenderer {

	struct SortAlpha {
		Stuff::Scalar distance;
 		DWORD render_state;
		int triangle;
		DWORD tex_2_3;
	};

	//##########################################################################
	//######################    MLRSortByOrder    ##############################
	//##########################################################################

	class MLRSortByOrder :
		public MLRSorter
	{
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Initialization
	//
	public:
		static void
			InitializeClass();
		static void
			TerminateClass();
		static ClassData
			*DefaultData;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Constructors/Destructors
	//
	public:
		MLRSortByOrder(MLRTexturePool*);
		~MLRSortByOrder();

		void AddPrimitive(MLRPrimitiveBase*, int=0);
		void AddEffect(MLREffect*, const MLRStateBase&);
		void AddEffect(MLREffect*, const MLRStateBase&, int&);
		void AddScreenQuads(GOSVertex*, const DrawScreenQuadsInformation*);

		SortAlpha* GetSortAlpha(int index);

		virtual void AddSortRawData(int);

	//	starts the action
		void RenderNow ();

	//	resets the sorting
		void Reset ();

		inline void
			BinSorterTBDP(int bin, ToBeDrawnPrimitiveIndex*);

		inline void
			BinSorterSD(int bin, int*);

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Testing
	//
	public:
		void
			TestInstance() const;

	protected:
		int
			lastUsedInBucket[MLRState::PriorityCount];

		Stuff::DynamicArrayOf<int>  //, Max_Number_Primitives_Per_Frame + Max_Number_ScreenQuads_Per_Frame
			priorityBuckets[MLRState::PriorityCount];

		Stuff::DynamicArrayOf<SortAlpha*>  //, Max_Number_Primitives_Per_Frame + Max_Number_ScreenQuads_Per_Frame
			alphaSort;
		Stuff::DynamicArrayOf<SortAlpha>  //, Max_Number_Primitives_Per_Frame + Max_Number_ScreenQuads_Per_Frame
			alphaSortArray;

		static int maxUsedAlphaPrimitives;

	};

}
