Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

AUtil.cpp

Go to the documentation of this file.
00001 /*
00002 CS Senior Project 2003
00003 Team : Leftfield
00004 Project : ModernWarfare
00005 Members :
00006 - Russ Christensen              <rchriste@cs.utah.edu>
00007 - Todd Smith                    <tcsmith@cs.utah.edu>
00008 - Usit Duongsaa                 <duongsaa@cs.utah.edu>
00009 
00010 Copyright 2003 Russ Christensen, Usit Duongsaa, and Todd Smith. All rights reserved.
00011 
00012 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00013 
00014 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00015 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00016 THIS SOFTWARE IS PROVIDED BY RUSS CHRISTENSEN, USIT DUONGSAA, AND TODD SMITH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RUSS, USIT, TODD OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017 */
00018 
00019 
00024 #include "AUtil.h"
00025 
00026 //------------------------------------------------------------------------------------
00027 TCHAR* util_stringToTChar( string s )
00028 {
00029         static TCHAR t[512];
00030         lstrcpy( t, s.c_str() );
00031         return t;
00032 }
00033 //------------------------------------------------------------------------------------
00034 DWORD util_loadVertexShader( string filename, DWORD *dwDecl )
00035 {
00036         TCHAR *strVSPath = util_stringToTChar(filename);
00037         DWORD resultShader;
00038 
00039         char szBuffer[128];                     // debug output
00040         DWORD*  pdwVS;                          // pointer to address space of the calling process
00041         HANDLE hFile, hMap;                     // handle file and handle mapped file
00042     TCHAR tchTempVSPath[512];   // temporary file path
00043         HRESULT hr;                                     // error 
00044 
00045     if( FAILED( hr = DXUtil_FindMediaFile( tchTempVSPath, strVSPath ) ) )
00046                 throw Error("Media file not found : "+filename);
00047         
00048         hFile = CreateFile(tchTempVSPath, GENERIC_READ,0,0,OPEN_EXISTING,
00049                 FILE_ATTRIBUTE_NORMAL,0);
00050 
00051         if(hFile != INVALID_HANDLE_VALUE) 
00052         {
00053                 if(GetFileSize(hFile,0) > 0) 
00054                         hMap = CreateFileMapping(hFile,0,PAGE_READONLY,0,0,0);
00055                 else
00056                 {
00057                         CloseHandle(hFile);
00058                         throw Error("Error file size is <= 0");
00059                 }
00060         }       
00061         else
00062                 throw Error("Unable to create file.");
00063         
00064         // maps a view of a file into the address space of the calling process
00065         pdwVS = (DWORD *)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
00066                 
00067         // Create the pixel shader
00068         hr = AS.pDevice->CreateVertexShader( dwDecl, pdwVS, &resultShader, 0 ); 
00069         if ( FAILED(hr) )
00070         {
00071                 OutputDebugString( "Failed to create Vertex Shader, errors:\n" );
00072                 D3DXGetErrorStringA(hr,szBuffer,sizeof(szBuffer));
00073                 OutputDebugString( szBuffer );
00074                 OutputDebugString( "\n" );
00075                 throw Error("Error creating Vertex shader : "+filename);
00076         }
00077         
00078         UnmapViewOfFile(pdwVS);
00079         CloseHandle(hMap);
00080         CloseHandle(hFile);     
00081         return resultShader;
00082 }
00083 //------------------------------------------------------------------------------------
00084 DWORD util_loadPixelShader( string filename )
00085 {
00086         TCHAR *strPSPath = util_stringToTChar(filename);
00087         DWORD resultShader;
00088 
00089         char szBuffer[128];                     // debug output
00090         DWORD*  pdwPS;                          // pointer to address space of the calling process
00091         HANDLE hFile, hMap;                     // handle file and handle mapped file
00092     TCHAR tchTempVSPath[512];   // temporary file path
00093         HRESULT hr;                                     // error 
00094 
00095     if( FAILED( hr = DXUtil_FindMediaFile( tchTempVSPath, strPSPath ) ) )
00096                 throw Error("Media file not found : " + filename);
00097         
00098         hFile = CreateFile(tchTempVSPath, GENERIC_READ,0,0,OPEN_EXISTING,
00099                 FILE_ATTRIBUTE_NORMAL,0);
00100 
00101         if(hFile != INVALID_HANDLE_VALUE) 
00102         {
00103                 if(GetFileSize(hFile,0) > 0) 
00104                         hMap = CreateFileMapping(hFile,0,PAGE_READONLY,0,0,0);
00105                 else
00106                 {
00107                         CloseHandle(hFile);
00108                         throw Error("Error file size is <= 0");
00109                 }
00110         }       
00111         else
00112                 throw Error("Unable to create file.");
00113         
00114         // maps a view of a file into the address space of the calling process
00115         pdwPS = (DWORD *)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
00116                 
00117         // Create the pixel shader
00118         hr = AS.pDevice->CreatePixelShader(pdwPS, &resultShader);
00119         if ( FAILED(hr) )
00120         {
00121                 OutputDebugString( "Failed to create Pixel Shader, errors:\n" );
00122                 D3DXGetErrorStringA(hr,szBuffer,sizeof(szBuffer));
00123                 OutputDebugString( szBuffer );
00124                 OutputDebugString( "\n" );
00125                 throw Error("Error creating pixel shader : " + filename );
00126         }
00127         
00128         UnmapViewOfFile(pdwPS);
00129         CloseHandle(hMap);
00130         CloseHandle(hFile);
00131         return resultShader;
00132 }
00133 //------------------------------------------------------------------------------------
00134 CD3DFont* util_loadFont( string fontName, string filename, int size )
00135 {
00136         if(filename!="")
00137                 if( 0==AddFontResource( filename.c_str() ) )
00138                         throw Error("Can't load Font File : " + filename );
00139         
00140         CD3DFont *font = new CD3DFont( util_stringToTChar(fontName), size );
00141         
00142         if( font==NULL )                  throw Error("Can't Create Font : " + fontName );      
00143         font->InitDeviceObjects(AS.pDevice);
00144         font->RestoreDeviceObjects();
00145         return font;
00146 }
00147 //------------------------------------------------------------------------------------
00148 FLOAT clipAngle( FLOAT degree )
00149 {
00150         if( degree < 0.0f )             return degree + 360.0f;
00151         if( degree >= 360.0f )  return degree - 360.0f;
00152         return degree;
00153 }
00154 //------------------------------------------------------------------------------------

Generated on Wed Apr 23 05:50:14 2003 for Modern Warfare by doxygen1.3-rc2