⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ddsreader.cpp

📁 DDS格式二次开发插件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DDSReader.cpp : Defines the entry point for the DLL application.
//
#include "DDSReader.h"
#include "d3Dx8.h"
#include "dds.h"
#include "ddraw.h"

IDirect3D8* g_D3D8 = NULL;
IDirect3DDevice8* g_Device = NULL;


#define READER_COUNT  1

#define DDS_READER_ID 0

/*********************************************************
Since we are using D3D8 for file io and format conversion
we need a IDirect3D8 interface created and destroyed with this DLL
**********************************************************/
BOOL APIENTRY DllMain( HANDLE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)
{
	switch (ul_reason_for_call) 
	{
		case DLL_PROCESS_ATTACH:	
			g_D3D8 = Direct3DCreate8(D3D_SDK_VERSION);
			if (!g_D3D8) return FALSE;
			break;

		case DLL_PROCESS_DETACH:
			if(g_Device){
				g_Device->Release();
			}

			if (g_D3D8) g_D3D8->Release();
			break;

		case DLL_THREAD_ATTACH:
			break;
		case DLL_THREAD_DETACH:
			break;

	}
    return TRUE;
}


/************************************************************************
PLUGIN DECLARATION
**************************************************************************/
CKPluginInfo g_DDSReader_PInfo[READER_COUNT]={
	CKPluginInfo(	DDS_READER_GUID,			// GUID
					"Dds",						// Extension supported
					"Direct Draw Surface",		// Reader Name
					"Virtools",					// Company
					"Direct Draw Surface",		// Summary String
					DDS_READER_VERSION,			// Reader Version	
					NULL,						// No Init Instance function needed
					NULL,						// No Exit Instance function needed
					CKPLUGIN_BITMAP_READER)};	// Plugin Type : Bitmap Reader



/**********************************************
 Called by the engine when a file with the DDS 
 extension is being loaded, a reader has to be 
 created.
***********************************************/
#ifdef CK_LIB
	CKDataReader *CKGet_DDSReader_Reader(int pos)
#else
	CKDataReader *CKGetReader(int pos)
#endif
{
	switch(pos) {
		case DDS_READER_ID: return new DDSReader(); break;
	}
	return NULL;
}


/******************************************
 Returns information about the reader which
 is the same as given to the engine at 
 initialisation.
*******************************************/
#ifndef CK_LIB
	CKPluginInfo* CKGetPluginInfo(int index)
#else	
	CKPluginInfo* CKGet_DDSReader_PluginInfo(int index)
#endif
{
	return &g_DDSReader_PInfo[index];
}


/************************************************************************
PLUGIN IMPLEMENTATION
**************************************************************************/

	
//-----------------------------------------
// Construction...

DDSReader::DDSReader()
{
	m_Properties.m_Ext = "dds";
	m_Properties.m_ReaderGuid = DDS_READER_GUID;
	m_Properties.m_Data = NULL;

	VxPixelFormat2ImageDesc(_32_ARGB8888,m_Properties.m_Format);

}

DDSReader::~DDSReader()
{
	delete[] m_Properties.m_Data;
	m_Properties.m_Data = NULL;
}


CKPluginInfo* DDSReader::GetReaderInfo()
{
	return &g_DDSReader_PInfo[DDS_READER_ID];
}

//-----------------------------------------
// Get Options Count (1 in our case which is the 
// pixel format when saving..)
int DDSReader::GetOptionsCount() {  return 1; }

/*--------------------------------------------------
Get Options Description: Returns a string describing the 
options this reader has when writing to file...

In this case we want to alias a DirectX enumeration (D3DFormat)

  typedef enum _D3DFORMAT {
    D3DFMT_R8G8B8               =  20,
    D3DFMT_A8R8G8B8             =  21,
    D3DFMT_R5G6B5               =  23,
    D3DFMT_A1R5G5B5             =  25,
    D3DFMT_A4R4G4B4             =  26,
    D3DFMT_R3G3B2               =  27,
    D3DFMT_DXT1                 =  MAKEFOURCC('D', 'X', 'T', '1'),
    D3DFMT_DXT2                 =  MAKEFOURCC('D', 'X', 'T', '2'),
    D3DFMT_DXT3                 =  MAKEFOURCC('D', 'X', 'T', '3'),
    D3DFMT_DXT4                 =  MAKEFOURCC('D', 'X', 'T', '4'),
    D3DFMT_DXT5                 =  MAKEFOURCC('D', 'X', 'T', '5'),
} D3DFORMAT;
*/
CKSTRING 
DDSReader::GetOptionDescription(int i)
{
	static XString OptionsString;

	OptionsString.Format("Enum:Pixel Format:"
		"D3DFMT_R8G8B8=%d,"
		"D3DFMT_A8R8G8B8=%d,"
		"D3DFMT_R5G6B5=%d,"
		"D3DFMT_A1R5G5B5=%d,"
		"D3DFMT_A4R4G4B4=%d,"
		"D3DFMT_R3G3B2=%d,"
		"D3DFMT_DXT1=%d,"
		"D3DFMT_DXT2=%d,"
		"D3DFMT_DXT3=%d,"
		"D3DFMT_DXT4=%d,"
		"D3DFMT_DXT5=%d"
		,D3DFMT_R8G8B8
		,D3DFMT_A8R8G8B8
		,D3DFMT_R5G6B5
		,D3DFMT_A1R5G5B5
		,D3DFMT_A4R4G4B4
		,D3DFMT_R3G3B2
		,D3DFMT_DXT1
		,D3DFMT_DXT2
		,D3DFMT_DXT3
		,D3DFMT_DXT4
		,D3DFMT_DXT5);


	switch(i) {
	case 0:
		return OptionsString.Str();
		break;
	}
	return "";
}

//-------------------------------------
// Alpha Support : This function must return
// whether the format stored in bp will handle
// alpha information..
CKBOOL DDSReader::IsAlphaSaved(CKBitmapProperties* bp)
{
	// TODO : optimize...
	DDSBitmapProperties dds_bp;
	if(bp->m_Size == dds_bp.m_Size) { // It's a Jpg Properties
		memcpy(&dds_bp,bp,bp->m_Size);
	} else {
		memcpy(&dds_bp,bp,sizeof(CKBitmapProperties));
	}

	switch ( dds_bp.m_PixelFormat) {
		case D3DFMT_R8G8B8:
		case D3DFMT_R5G6B5:
		case D3DFMT_R3G3B2:  return FALSE;
	}
	return TRUE;
}



BOOL CreateTemporaryDevice(LPDIRECT3DDEVICE8* Device)
{
    D3DDISPLAYMODE			dispMode;
    D3DPRESENT_PARAMETERS	presentParams;

//----- Create a D3D Device for the time to load the file...
    g_D3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dispMode);

    ZeroMemory(&presentParams, sizeof(presentParams));
    presentParams.Windowed = TRUE;
    presentParams.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
    presentParams.BackBufferWidth = 8;
    presentParams.BackBufferHeight = 8;
    presentParams.BackBufferFormat = dispMode.Format;

	HRESULT res = g_D3D8->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,GetDesktopWindow(),D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&presentParams,Device);

	return SUCCEEDED(res);

}


#define CLEANUPIFFAILED(res,error)\
	if (FAILED(res)) {\
		if (Surface) Surface->Release();\
		if (TextureSurface) TextureSurface->Release();\
		if (Texture) Texture->Release();\
		return error;\
	}\

/************************************************************
Load a .dds file (from memory or from file)
*************************************************************/
CKERROR DDS_Read(void* memptr,int size,CKBitmapProperties* bp)
{
	LPDIRECT3DTEXTURE8		Texture = NULL;
	LPDIRECT3DSURFACE8		Surface = NULL;
	LPDIRECT3DSURFACE8		TextureSurface = NULL;
	HRESULT					res;

	if(!g_Device){
		CreateTemporaryDevice(&g_Device);
	}

//-------- Create a texture from this file
	if (!size) {
		// Loads from a file
		//res = D3DXCreateTextureFromFile(Device,(char *)memptr,&Texture);
		res = D3DXCreateTextureFromFileEx(g_Device,(char *)memptr,D3DX_DEFAULT,D3DX_DEFAULT,1,0,
			D3DFMT_UNKNOWN,D3DPOOL_SCRATCH ,D3DX_DEFAULT,D3DX_DEFAULT,0x00000000,NULL,NULL,&Texture);

	} else {
		// Loads from a file in a memory buffer
		//res = D3DXCreateTextureFromFileInMemory(Device,memptr,size,&Texture);

		res = D3DXCreateTextureFromFileInMemoryEx(g_Device,(char *)memptr,size,D3DX_DEFAULT,D3DX_DEFAULT,1,0,
		D3DFMT_UNKNOWN,D3DPOOL_SCRATCH ,D3DX_DEFAULT,D3DX_DEFAULT,0x00000000,NULL,NULL,&Texture);


	}
	
	CLEANUPIFFAILED(res,CKBITMAPERROR_READERROR);

	D3DSURFACE_DESC TextureDesc;
	Texture->GetLevelDesc(0,&TextureDesc);

	VX_PIXELFORMAT vpf =  VxImageDesc2PixelFormat(((CKBitmapProperties*)bp)->m_Format);
	D3DFORMAT dpf = VxPixelFormatToD3DFormat(vpf);
	vpf = D3DFormatToVxPixelFormat(dpf);

	if(TextureDesc.Format != dpf){
				
	//--- Create a surface to perform conversion from the loaded image.. 
	
		VxPixelFormat2ImageDesc(vpf,((CKBitmapProperties*)bp)->m_Format);
	
		res = g_Device->CreateImageSurface(TextureDesc.Width,TextureDesc.Height,dpf,&Surface);
	
		CLEANUPIFFAILED(res,CKBITMAPERROR_READERROR);

		res= Texture->GetSurfaceLevel(0,&TextureSurface);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -