1 #include "FirstUiBuilder.h"
2 #include "DDSCanvasGenerator.h"
4 #include "UIDirect3DTextureCanvas.h"
5 #include "UIDirect3DPrimaryCanvas.h"
6 #include "UIOutputStream.h"
10 #include "FileLocator.h"
12 DDSCanvasGenerator::DDSCanvasGenerator( const UINarrowString
&Filename
) :
17 if (_strnicmp ("../texture/", mFilename
.c_str (), 4) != 0)
18 mFilename
= "../texture/" + mFilename
;
21 if (_strnicmp (".dds", mFilename
.substr (mFilename
.length () - 4).c_str (), 4) != 0)
24 UINarrowString result
;
26 if (FileLocator::gFileLocator ().findFile (mFilename
.c_str (), result
))
32 DDSCanvasGenerator::~DDSCanvasGenerator()
36 bool DDSCanvasGenerator::GenerateOnto( UICanvas
&DestinationCanvas
) const
41 fp
= fopen( mFilename
.c_str(), "rb" );
45 *GetUIOutputStream() << "Error opening '" << mFilename
.c_str() << "'.\n";
49 fseek( fp
, 0, SEEK_END
);
52 p
= new unsigned char[len
];
54 fseek( fp
, 0, SEEK_SET
);
62 fread( p
, 1, len
, fp
);
65 bool rv
= InitializeCanvasFromMemory( DestinationCanvas
, p
);
68 *GetUIOutputStream() << "FAILED TO InitializeCanvasFromMemory '" << mFilename
.c_str() << "'\n";
77 #include "UIDirectDrawCanvas.h"
79 bool DDSCanvasGenerator::GetSize( UISize
&Out
) const
85 fp
= fopen( mFilename
.c_str(), "rb" );
89 *GetUIOutputStream() << "Error opening '" << mFilename
.c_str() << "'\n";
93 fread( &dwMagic
, sizeof(DWORD
), 1, fp
);
94 if( dwMagic
!= MAKEFOURCC('D','D','S',' ') )
96 *GetUIOutputStream() << "File '" << mFilename
.c_str() << "' is not an S3 compressed texture.\n";
101 if( !fread( &ddsd
, sizeof( ddsd
), 1, fp
) )
103 *GetUIOutputStream() << "Error reading '" << mFilename
.c_str() << "'\n";
109 Out
.x
= ddsd
.dwWidth
;
110 Out
.y
= ddsd
.dwHeight
;
114 bool DDSCanvasGenerator::InitializeCanvasFromMemory( UICanvas
&DestinationCanvas
, unsigned char *pbase
) const
116 assert( DestinationCanvas
.IsA( TUIDirect3DTextureCanvas
) );
118 DWORD dwMagic
= *(DWORD
*)pbase
;
119 DDSURFACEDESC2 ddsd
= *(LPDDSURFACEDESC2
)(pbase
+ sizeof( DWORD
));
120 void *pdata
= pbase
+ sizeof( DWORD
) + sizeof( DDSURFACEDESC2
);
121 UIDirect3DTextureCanvas
InMemoryCanvas( UISize( ddsd
.dwWidth
, ddsd
.dwHeight
), true );
122 UIDirect3DTextureCanvas
*pDestCanvas
= reinterpret_cast<UIDirect3DTextureCanvas
*>( &DestinationCanvas
);
124 if( dwMagic
!= MAKEFOURCC('D','D','S',' ') )
126 *GetUIOutputStream() << " ::: NOT A DDS\n";
130 ddsd
.ddsCaps
.dwCaps
|= DDSCAPS_SYSTEMMEMORY
;
131 ddsd
.dwMipMapCount
= 0;
132 ddsd
.ddsCaps
.dwCaps
&= ~( DDSCAPS_MIPMAP
| DDSCAPS_COMPLEX
);
133 ddsd
.dwFlags
&= ~(DDSD_PITCH
| DDSD_MIPMAPCOUNT
| DDSD_LINEARSIZE
);
135 if( !InMemoryCanvas
.CreateFromSurfaceDescription( &ddsd
) )
137 *GetUIOutputStream() << " ::: FAILED TO CREATE FROM SURFACE DESCRIPTION\n";
141 if( FAILED( InMemoryCanvas
.GetSurface()->Lock( 0, &ddsd
, DDLOCK_WAIT
, 0 ) ) )
143 *GetUIOutputStream() << " ::: FAILED TO LOCK";
147 if( ddsd
.dwFlags
& DDSD_LINEARSIZE
)
148 memcpy( ddsd
.lpSurface
, pdata
, ddsd
.dwLinearSize
);
151 BYTE
* pDest
= (BYTE
*)ddsd
.lpSurface
;
152 BYTE
* pSource
= (BYTE
*)pdata
;
153 DWORD dwBytesPerRow
= ddsd
.dwWidth
* ddsd
.ddpfPixelFormat
.dwRGBBitCount
/ 8;
155 for( DWORD yp
= 0; yp
< ddsd
.dwHeight
; yp
++ )
157 memcpy( pDest
, pSource
,dwBytesPerRow
);
158 pDest
+= ddsd
.lPitch
;
159 pSource
+= dwBytesPerRow
;
163 if( FAILED( InMemoryCanvas
.GetSurface()->Unlock( NULL
) ) )
165 *GetUIOutputStream() << " ::: FAILED TO UNLOCK SURFACE\n";
169 DDSURFACEDESC2 ddsdHardwareSurface
= ddsd
;
171 if( !pDestCanvas
->GetRenderCanvas()->MatchPixelFormat( &ddsd
.ddpfPixelFormat
, &ddsdHardwareSurface
.ddpfPixelFormat
) )
173 *GetUIOutputStream() << " ::: FAILED TO MATCH PIXEL FORMAT\n";
177 ddsdHardwareSurface
.ddsCaps
.dwCaps
&= ~DDSCAPS_SYSTEMMEMORY
;
178 ddsdHardwareSurface
.ddsCaps
.dwCaps
|= DDSCAPS_VIDEOMEMORY
;
180 if( !pDestCanvas
->CreateFromSurfaceDescription( &ddsdHardwareSurface
) )
182 *GetUIOutputStream() << " ::: FAILED TO CREATE FROM SURFACE DESCRIPTION\n";
186 pDestCanvas
->BltFrom( &InMemoryCanvas
, UIPoint(0,0), UIPoint(0,0), UISize( ddsd
.dwWidth
, ddsd
.dwHeight
) );