//	Darkstar Engine  (c) UnderWorld Interactive Studio (all coding by Svarog    2007-2008)
//
//      Данный файл является частью Darkstar Engine. Любое использование кода файла
//      без согласия компании, является нарушением закона об авторском праве (в слу-
//      чае если не оговорено обратное).
//
//      Имя файла: loadableTexture.hpp
//      Краткое описание: текстура, загружаемая из файла
//

#ifndef DSE_RS_TEXTURES_LOADABLETEXTURE__HPP
#define DSE_RS_TEXTURES_LOADABLETEXTURE__HPP

#include <Darkstar/common.hpp>
#include <Darkstar/Resource System/Textures/baseTexture.hpp>
#include <Darkstar/Graphic System/Direct3D/direct3d_include.hpp>
#include <Darkstar/Kernel/Misc/misc_func.hpp>
#include <Darkstar/Kernel/FileSystem/dfile.hpp>
#include <Darkstar/Resource System/Textures/loadableTexture_common.hpp>

namespace rs_private
{
	class LoadableTexture : public rs::BaseTexture
	{
	private:
#ifdef GD_D3D10
		ID3D10Resource* texture;
		template <typename T, typename S>
		void _fillD3DStructures(T& tex, S& srv);
#endif

		_TextureType type;
		_Masks masks;
		unsigned int start_mip;
		unsigned int mip_count;

		bool _makeProxyTexture(const std::string& filename);
		unsigned int _getNextMipLevelSize(const unsigned int width, const unsigned int height,
			const unsigned int depth, const unsigned int bpp, const rs::DSEFORMAT& format) const;

		DWORD _getConvertedColor(const unsigned char red, const unsigned char green, const unsigned char blue, const unsigned char alpha) const;
		void _setCorrectConvertedFormat();

		// in DirectX9 specific format D3DFMT_A8R8G8B8 and in DirectX 10 specific format DXGI_FORMAT_R8G8B8A8_UNORM
		rs::DSEFORMAT _tryToSetSpecificPlatfromPixelFormat(const bool is_signed, const unsigned int bpp);
		rs::DSEFORMAT _tryToSetPixelFormat(const bool is_signed, const unsigned int bpp);
		rs::DSEFORMAT _tryToSetPixelFormat(const unsigned int fourcc);
		
		unsigned int _getRowPitch(const rs::DSEFORMAT& pixel_format, const unsigned int width, const unsigned int bpp);

		bool _createTexture2D(const shared_array<DSUBRES_DATA>& sub_data);
		bool _createTextureVolume(const shared_array<DSUBRES_DATA>& sub_data);
		bool _createTextureCube(const shared_array<DSUBRES_DATA>& sub_data);

		void _fillImageProperties(const unsigned int bpp, const unsigned int mip_count_in_file, const unsigned int cube_faces, unsigned int& num_of_faces_in_file, shared_array<unsigned int>& pointers, shared_array<unsigned int>& row_pitches, shared_array<unsigned int>& slice_pitches, unsigned int& data_size, unsigned int& data_to_skip);
		template <typename T>
		bool _getCorrectFileData(ker::DFile& file, unsigned int& data_size, const unsigned int data_to_skip, const unsigned int one_face_size, const unsigned int num_of_faces_in_file, const unsigned int cube_faces, shared_array<T>& file_data);

		template <typename T>
		bool _loadTextureData(ker::DFile& file, const unsigned int bpp, const unsigned char cube_faces);
		
		template <typename T>
		bool _convertData(const unsigned int bpp, const shared_array<T>& data, shared_array<DWORD>& converted_data);
	public:
		LoadableTexture();
		~LoadableTexture();

		bool load(const std::string& filename, const unsigned int quality);
	};
};

#include <Darkstar/Resource System/Textures/loadableTexture_common.inl>

#endif