dsrc isn't necessary for this repo
[client-tools.git] / src / external / 3rd / application / UiBuilder / DDSCanvasGenerator.cpp
blobd3d5b0c0ef6dd673944c871e754e52664fd716c5
1 #include "FirstUiBuilder.h"
2 #include "DDSCanvasGenerator.h"
4 #include "UIDirect3DTextureCanvas.h"
5 #include "UIDirect3DPrimaryCanvas.h"
6 #include "UIOutputStream.h"
8 #include <cstdio>
10 #include "FileLocator.h"
12 DDSCanvasGenerator::DDSCanvasGenerator( const UINarrowString &Filename ) :
13 mFilename (Filename)
16 #if SWG_HACK
17 if (_strnicmp ("../texture/", mFilename.c_str (), 4) != 0)
18 mFilename = "../texture/" + mFilename;
19 #endif
21 if (_strnicmp (".dds", mFilename.substr (mFilename.length () - 4).c_str (), 4) != 0)
22 mFilename += ".dds";
24 UINarrowString result;
26 if (FileLocator::gFileLocator ().findFile (mFilename.c_str (), result))
28 mFilename = result;
32 DDSCanvasGenerator::~DDSCanvasGenerator()
36 bool DDSCanvasGenerator::GenerateOnto( UICanvas &DestinationCanvas ) const
38 FILE *fp;
39 unsigned char *p;
41 fp = fopen( mFilename.c_str(), "rb" );
43 if( !fp )
45 *GetUIOutputStream() << "Error opening '" << mFilename.c_str() << "'.\n";
46 return false;
49 fseek( fp, 0, SEEK_END );
50 long len = ftell(fp);
52 p = new unsigned char[len];
54 fseek( fp, 0, SEEK_SET );
56 if( !p )
58 fclose( fp );
59 return false;
62 fread( p, 1, len, fp );
63 fclose( fp );
65 bool rv = InitializeCanvasFromMemory( DestinationCanvas, p );
67 if (!rv)
68 *GetUIOutputStream() << "FAILED TO InitializeCanvasFromMemory '" << mFilename.c_str() << "'\n";
70 delete p;
71 return rv;
74 #if WIN32
76 #include <windows.h>
77 #include "UIDirectDrawCanvas.h"
79 bool DDSCanvasGenerator::GetSize( UISize &Out ) const
81 DWORD dwMagic;
82 DDSURFACEDESC2 ddsd;
83 FILE *fp;
85 fp = fopen( mFilename.c_str(), "rb" );
87 if( !fp )
89 *GetUIOutputStream() << "Error opening '" << mFilename.c_str() << "'\n";
90 return false;
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";
97 fclose( fp );
98 return false;
101 if( !fread( &ddsd, sizeof( ddsd ), 1, fp ) )
103 *GetUIOutputStream() << "Error reading '" << mFilename.c_str() << "'\n";
104 fclose( fp );
105 return false;
108 fclose( fp );
109 Out.x = ddsd.dwWidth;
110 Out.y = ddsd.dwHeight;
111 return true;
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";
127 return false;
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";
138 return false;
141 if( FAILED( InMemoryCanvas.GetSurface()->Lock( 0, &ddsd, DDLOCK_WAIT, 0 ) ) )
143 *GetUIOutputStream() << " ::: FAILED TO LOCK";
144 return false;
147 if( ddsd.dwFlags & DDSD_LINEARSIZE )
148 memcpy( ddsd.lpSurface, pdata, ddsd.dwLinearSize );
149 else
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";
166 return false;
169 DDSURFACEDESC2 ddsdHardwareSurface = ddsd;
171 if( !pDestCanvas->GetRenderCanvas()->MatchPixelFormat( &ddsd.ddpfPixelFormat, &ddsdHardwareSurface.ddpfPixelFormat ) )
173 *GetUIOutputStream() << " ::: FAILED TO MATCH PIXEL FORMAT\n";
174 return false;
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";
183 return false;
186 pDestCanvas->BltFrom( &InMemoryCanvas, UIPoint(0,0), UIPoint(0,0), UISize( ddsd.dwWidth, ddsd.dwHeight ) );
187 return true;
190 #endif