diff --git a/win32/CDirect3D.cpp b/win32/CDirect3D.cpp index 56d16b9b..06f8f5a5 100644 --- a/win32/CDirect3D.cpp +++ b/win32/CDirect3D.cpp @@ -187,6 +187,7 @@ #include "wsnes9x.h" #include #include +#include "CXML.h" #include "../filter/hq2x.h" #include "../filter/2xsai.h" @@ -425,17 +426,9 @@ bool CDirect3D::SetShaderHLSL(const TCHAR *file) TCHAR folder[MAX_PATH]; TCHAR rubyLUTfileName[MAX_PATH]; TCHAR *slash; - char *shaderText = NULL; TCHAR errorMsg[MAX_PATH + 50]; - IXMLDOMDocument * pXMLDoc = NULL; - IXMLDOMElement * pXDE = NULL; - IXMLDOMNode * pXDN = NULL; - BSTR queryString, nodeContent; - - HRESULT hr; - shaderTimer = 1.0f; shaderTimeStart = 0; shaderTimeElapsed = 0; @@ -453,76 +446,20 @@ bool CDirect3D::SetShaderHLSL(const TCHAR *file) if (file == NULL || *file==TEXT('\0')) return true; - hr = CoCreateInstance(CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pXMLDoc)); + CXML xml; - if(FAILED(hr)) { - MessageBox(NULL, TEXT("Error creating XML Parser"), TEXT("Shader Loading Error"), - MB_OK|MB_ICONEXCLAMATION); - return false; - } + if(!xml.loadXmlFile(file)) + return false; - VARIANT fileName; - VARIANT_BOOL ret; - fileName.vt = VT_BSTR; -#ifdef UNICODE - fileName.bstrVal = SysAllocString(file); -#else - wchar_t tempfilename[MAX_PATH]; - MultiByteToWideChar(CP_UTF8,0,file,-1,tempfilename,MAX_PATH); - fileName.bstrVal = SysAllocString(tempfilename); -#endif - hr = pXMLDoc->load(fileName,&ret); - SysFreeString(fileName.bstrVal); + TCHAR *lang = xml.getAttribute(TEXT("/shader"),TEXT("language")); - if(FAILED(hr) || hr==S_FALSE) { - _stprintf(errorMsg,TEXT("Error loading HLSL shader file:\n%s"),file); + if(lstrcmpi(lang,TEXT("hlsl"))) { + _stprintf(errorMsg,TEXT("Shader language is <%s>, expected in file:\n%s"),lang,file); MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION); - pXMLDoc->Release(); return false; } - VARIANT attributeValue; - BSTR attributeName; - - hr = pXMLDoc->get_documentElement(&pXDE); - if(FAILED(hr) || hr==S_FALSE) { - _stprintf(errorMsg,TEXT("Error loading root element from file:\n%s"),file); - MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION); - pXMLDoc->Release(); - return false; - } - - attributeName=SysAllocString(L"language"); - pXDE->getAttribute(attributeName,&attributeValue); - SysFreeString(attributeName); - pXDE->Release(); - - if(attributeValue.vt!=VT_BSTR || lstrcmpiW(attributeValue.bstrVal,L"hlsl")) { - _stprintf(errorMsg,TEXT("Shader language is <%s>, expected in file:\n%s"),attributeValue.bstrVal,file); - MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION); - if(attributeValue.vt==VT_BSTR) SysFreeString(attributeValue.bstrVal); - pXMLDoc->Release(); - return false; - } - if(attributeValue.vt==VT_BSTR) SysFreeString(attributeValue.bstrVal); - - queryString=SysAllocString(L"/shader/source"); - hr = pXMLDoc->selectSingleNode(queryString,&pXDN); - SysFreeString(queryString); - - if(hr == S_OK) { - hr = pXDN->get_text(&nodeContent); - if(hr == S_OK) { - int requiredChars = WideCharToMultiByte(CP_ACP,0,nodeContent,-1,shaderText,0,NULL,NULL); - shaderText = new char[requiredChars]; - WideCharToMultiByte(CP_UTF8,0,nodeContent,-1,shaderText,requiredChars,NULL,NULL); - } - SysFreeString(nodeContent); - pXDN->Release(); - pXDN = NULL; - } - - pXMLDoc->Release(); + TCHAR *shaderText = xml.getNodeContent(TEXT("/shader/source")); if(!shaderText) { _stprintf(errorMsg,TEXT("No HLSL shader program in file:\n%s"),file); @@ -532,11 +469,17 @@ bool CDirect3D::SetShaderHLSL(const TCHAR *file) } LPD3DXBUFFER pBufferErrors = NULL; - hr = D3DXCreateEffect( pDevice,shaderText,strlen(shaderText),NULL, NULL, +#ifdef UNICODE + HRESULT hr = D3DXCreateEffect( pDevice,WideToCP(shaderText,CP_ACP),strlen(WideToCP(shaderText,CP_ACP)),NULL, NULL, D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &effect, &pBufferErrors ); - delete[] shaderText; - if( FAILED(hr) ) { +#else + HRESULT hr = D3DXCreateEffect( pDevice,shaderText,strlen(shaderText),NULL, NULL, + D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &effect, + &pBufferErrors ); +#endif + + if( FAILED(hr) ) { _stprintf(errorMsg,TEXT("Error parsing HLSL shader file:\n%s"),file); MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION); if(pBufferErrors) { diff --git a/win32/CXML.cpp b/win32/CXML.cpp new file mode 100644 index 00000000..ade7ba5c --- /dev/null +++ b/win32/CXML.cpp @@ -0,0 +1,162 @@ +#include "CXML.h" +#include +#include "_tfwopen.h" +#ifndef UNICODE +#define _tFromTCHAR(x) CPToWide(x,CP_ACP) +#define _tToTCHAR(x) WideToCP(x,CP_ACP) +#else +#define _tFromTCHAR +#define _tToTCHAR +#endif + +CXML::CXML(void) +{ + pXMLDoc = NULL; + xmlLoaded = false; + nodeContent = NULL; + attrValue = NULL; +} + +CXML::~CXML(void) +{ + unloadXml(); +} + +void CXML::unloadXml() +{ + if(pXMLDoc) { + pXMLDoc->Release(); + pXMLDoc = NULL; + } + if(nodeContent) { + delete [] nodeContent; + nodeContent = NULL; + } + if(attrValue) { + delete [] attrValue; + attrValue = NULL; + } + xmlLoaded = false; +} + +bool CXML::loadXmlFile(TCHAR const *file) +{ + unloadXml(); + + if(!file) + return false; + + TCHAR errorMsg[MAX_PATH + 50]; + + HRESULT hr = CoCreateInstance(CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pXMLDoc)); + + if(FAILED(hr)) { + MessageBox(NULL, TEXT("Error creating XML Parser"), TEXT("XML Error"), + MB_OK|MB_ICONEXCLAMATION); + pXMLDoc = NULL; + return false; + } + + + VARIANT fileName; + VARIANT_BOOL ret; + fileName.vt = VT_BSTR; + fileName.bstrVal = SysAllocString(_tFromTCHAR(file)); + hr = pXMLDoc->load(fileName,&ret); + SysFreeString(fileName.bstrVal); + + if(FAILED(hr) || hr==S_FALSE) { + _stprintf(errorMsg,TEXT("Error loading XML file:\n%s"),file); + MessageBox(NULL, errorMsg, TEXT("XML Error"), + MB_OK|MB_ICONEXCLAMATION); + unloadXml(); + return false; + } + + hr = pXMLDoc->get_documentElement(&pXrootElement); + if(FAILED(hr) || hr==S_FALSE) { + _stprintf(errorMsg,TEXT("Error loading root element from file:\n%s"),file); + MessageBox(NULL, errorMsg, TEXT("XML Error"), MB_OK|MB_ICONEXCLAMATION); + unloadXml(); + return false; + } + + xmlLoaded = true; + return true; +} + +IXMLDOMNode *CXML::getNode(TCHAR const *searchNode) +{ + IXMLDOMNode *pXDN; + BSTR queryString=SysAllocString(_tFromTCHAR(searchNode)); + HRESULT hr = pXMLDoc->selectSingleNode(queryString,&pXDN); + SysFreeString(queryString); + + return pXDN; +} + +TCHAR *CXML::getAttribute(TCHAR const *searchNode, TCHAR const *attrName) +{ + IXMLDOMNode *pXDN = getNode(searchNode); + + if(!pXDN) + return NULL; + + VARIANT attributeValue; + BSTR attributeName; + + IXMLDOMElement * pXDE = NULL; + HRESULT hr = pXDN->QueryInterface(IID_PPV_ARGS(&pXDE)); + if(FAILED(hr)) { + pXDN->Release(); + return NULL; + } + + attributeName=SysAllocString(_tFromTCHAR(attrName)); + pXDE->getAttribute(attributeName,&attributeValue); + SysFreeString(attributeName); + pXDE->Release(); + + if(attributeValue.vt!=VT_BSTR) + return NULL; + + if(attrValue) { + delete [] attrValue; + attrValue = NULL; + } + + attrValue = new TCHAR[lstrlen(_tToTCHAR(attributeValue.bstrVal)) + 1]; + lstrcpy(attrValue,_tToTCHAR(attributeValue.bstrVal)); + + SysFreeString(attributeValue.bstrVal); + + return attrValue; +} + +TCHAR *CXML::getNodeContent(TCHAR const *searchNode) +{ + IXMLDOMNode *pXDN = getNode(searchNode); + + if(!pXDN) + return NULL; + + BSTR nodeText; + + HRESULT hr = pXDN->get_text(&nodeText); + pXDN->Release(); + + if(hr != S_OK) + return NULL; + + if(nodeContent) { + delete [] nodeContent; + nodeContent = NULL; + } + + nodeContent = new TCHAR[lstrlen(_tToTCHAR(nodeText)) + 1]; + lstrcpy(nodeContent,_tToTCHAR(nodeText)); + + SysFreeString(nodeText); + + return nodeContent; +} diff --git a/win32/CXML.h b/win32/CXML.h new file mode 100644 index 00000000..95cf2e27 --- /dev/null +++ b/win32/CXML.h @@ -0,0 +1,29 @@ +#ifndef CXML_H +#define CXML_H + +#include "msxml2.h" +#include + +class CXML +{ +private: + IXMLDOMDocument * pXMLDoc; + IXMLDOMElement * pXrootElement; + bool xmlLoaded; + TCHAR *nodeContent; + TCHAR *attrValue; + + IXMLDOMNode *getNode(TCHAR const *searchNode); + +public: + CXML(void); + ~CXML(void); + + bool loadXmlFile(TCHAR const *xmlFile); + void unloadXml(); + + TCHAR *getAttribute(TCHAR const *searchNode, TCHAR const *attrName); + TCHAR *getNodeContent(TCHAR const *searchNode); +}; + +#endif \ No newline at end of file diff --git a/win32/_tfwopen.cpp b/win32/_tfwopen.cpp index 3ec11451..3b5e8d7b 100644 --- a/win32/_tfwopen.cpp +++ b/win32/_tfwopen.cpp @@ -192,10 +192,16 @@ WideToUtf8::WideToUtf8(const wchar_t *wideChars) { WideCharToMultiByte(CP_UTF8,0,wideChars,-1,utf8Chars,requiredChars,NULL,NULL); } -MS932ToWide::MS932ToWide(const char *ms932Chars) { - int requiredChars = MultiByteToWideChar(932,0,ms932Chars,-1,wideChars,0); +CPToWide::CPToWide(const char *chars, unsigned int cp) { + int requiredChars = MultiByteToWideChar(cp,0,chars,-1,wideChars,0); wideChars = new wchar_t[requiredChars]; - MultiByteToWideChar(932,0,ms932Chars,-1,wideChars,requiredChars); + MultiByteToWideChar(cp,0,chars,-1,wideChars,requiredChars); +} + +WideToCP::WideToCP(const wchar_t *wideChars, unsigned int cp) { + int requiredChars = WideCharToMultiByte(cp,0,wideChars,-1,cpchars,0,NULL,NULL); + cpchars = new char[requiredChars]; + WideCharToMultiByte(cp,0,wideChars,-1,cpchars,requiredChars,NULL,NULL); } extern "C" FILE *_tfwopen(const char *filename, const char *mode ) { diff --git a/win32/_tfwopen.h b/win32/_tfwopen.h index a354b073..e77de029 100644 --- a/win32/_tfwopen.h +++ b/win32/_tfwopen.h @@ -215,15 +215,24 @@ public: operator char *() { return utf8Chars; } }; -class MS932ToWide { +class CPToWide { private: wchar_t *wideChars; public: - MS932ToWide(const char *ms932Chars); - ~MS932ToWide() { delete [] wideChars; } + CPToWide(const char *chars, unsigned int cp); + ~CPToWide() { delete [] wideChars; } operator wchar_t *() { return wideChars; } }; +class WideToCP { +private: + char *cpchars; +public: + WideToCP(const wchar_t *wideChars, unsigned int cp); + ~WideToCP() { delete [] cpchars; } + operator char *() { return cpchars; } +}; + namespace std { class u8nifstream: public std::ifstream { diff --git a/win32/snes9xw.vcproj b/win32/snes9xw.vcproj index 146294f5..7ddb4586 100644 --- a/win32/snes9xw.vcproj +++ b/win32/snes9xw.vcproj @@ -3014,6 +3014,14 @@ RelativePath=".\AVIOutput.h" > + + + + diff --git a/win32/wsnes9x.h b/win32/wsnes9x.h index 7f6252b5..3c8b071d 100644 --- a/win32/wsnes9x.h +++ b/win32/wsnes9x.h @@ -212,7 +212,7 @@ #ifdef UNICODE #define _tToChar WideToUtf8 #define _tFromChar Utf8ToWide -#define _tFromMS932 MS932ToWide +#define _tFromMS932(x) CPToWide(x,932) #else #define _tToChar #define _tFromChar