Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

D3ddrv7x.cpp

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  D3DDrv7x.cpp                                                                        */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description: D3D driver                                                             */
00006 /*                                                                                      */
00007 /*  07/16/2000 Wendell Buckner                                                          */
00008 /*   Convert to Directx7...                                                             */
00009 /*  The contents of this file are subject to the Genesis3D Public License               */
00010 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00011 /*  compliance with the License. You may obtain a copy of the License at                */
00012 /*  http://www.genesis3d.com                                                            */
00013 /*                                                                                      */
00014 /*  Software distributed under the License is distributed on an "AS IS"                 */
00015 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00016 /*  the License for the specific language governing rights and limitations              */
00017 /*  under the License.                                                                  */
00018 /*                                                                                      */
00019 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00020 /*  Genesis3D Version 1.1 released November 15, 1999                                    */
00021 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved                            */
00022 /*                                                                                      */
00023 /****************************************************************************************/
00024 #include <Windows.h>
00025 #include <stdio.h>
00026 
00027 /* 07/16/2000 Wendell Buckner
00028     Convert to Directx7...    
00029 #include "D3DDrv.h"           */
00030 #include "D3DDrv7x.h"           
00031 
00032 #include "DCommon.h"
00033 
00034 #include "Scene.h"
00035 #include "Render.h"
00036 #include "D3DCache.h"
00037 #include "D3D_Main.h"
00038 #include "PCache.h"
00039 #include "THandle.h"
00040 
00041 
00042 DRV_Window                      ClientWindow;
00043 BOOL                            ExitHandlerActive = FALSE;
00044 
00045 int32                           LastError;
00046 char                            LastErrorStr[200];
00047 
00048 geBoolean       DRIVERCC DrvShutdown(void);
00049 geBoolean       DRIVERCC ScreenShot(const char *Name);
00050 
00051 BOOL DRIVERCC DrvInit(DRV_DriverHook *Hook)
00052 {
00053         RECT    WRect;
00054 
00055         // Start up
00056         if (!D3DMain_InitD3D(Hook->hWnd, Hook->DriverName+5, Hook->Width, Hook->Height))
00057         {
00058                 //SetLastDrvError(DRV_ERROR_INIT_ERROR, "D3D_DrvInit: Could not init driver.\n");
00059                 return FALSE;
00060         }
00061         
00062         // If they are asking for a window mode, use there hWnd for the size
00063         if (Hook->Width ==-1 && Hook->Height == -1)
00064         {
00065                 GetClientRect(Hook->hWnd, &WRect);
00066                 
00067                 Hook->Width = (WRect.right - WRect.left);
00068                 Hook->Height = (WRect.bottom - WRect.top);
00069         }
00070 
00071         ClientWindow.Width = Hook->Width;
00072         ClientWindow.Height = Hook->Height;
00073         ClientWindow.hWnd = Hook->hWnd;
00074 
00075         return TRUE;
00076 }
00077 
00078 //============================================================================================
00079 //============================================================================================
00080 BOOL DRIVERCC DrvShutdown(void)
00081 {
00082         D3DMain_ShutdownD3D();
00083         return TRUE;
00084 }
00085 
00086 //============================================================================================
00087 //      DrvResetAll
00088 //============================================================================================
00089 geBoolean DRIVERCC DrvResetAll(void)
00090 {
00091         return D3DMain_Reset();
00092 }
00093 
00094 geRDriver_PixelFormat   PixelFormat[10]; 
00095 
00096 #define NUM_PIXEL_FORMATS (sizeof(PixelFormats)/sizeof(geRDriver_PixelFormat))
00097 
00098 geBoolean DRIVERCC EnumPixelFormats(DRV_ENUM_PFORMAT_CB *Cb, void *Context)
00099 {
00100         int32                   i;
00101         gePixelFormat   Format3d, Format2d;
00102         uint32                  CurrentBpp;
00103 
00104         CurrentBpp = AppInfo.ddsd.ddpfPixelFormat.dwRGBBitCount;
00105 
00106         // Setup the 2d surface format
00107         if (CurrentBpp == 32 && AppInfo.ddSurfFormat.ddpfPixelFormat.dwRGBAlphaBitMask == 0xff000000)
00108                 Format2d = GE_PIXELFORMAT_32BIT_ARGB;
00109         else if (CurrentBpp == 32 && AppInfo.ddSurfFormat.ddpfPixelFormat.dwBBitMask == 0xff)
00110                 Format2d = GE_PIXELFORMAT_32BIT_XRGB;
00111         else if (CurrentBpp == 24 && AppInfo.ddSurfFormat.ddpfPixelFormat.dwBBitMask == 0xff)
00112                 Format2d = GE_PIXELFORMAT_24BIT_RGB;
00113         else if (AppInfo.ddSurfFormat.ddpfPixelFormat.dwGBitMask == (31<<5))
00114                 Format2d = GE_PIXELFORMAT_16BIT_555_RGB;
00115         else
00116                 Format2d = GE_PIXELFORMAT_16BIT_565_RGB;
00117 
00118         // Setup the 3d (Texture) format
00119         if (AppInfo.ddTexFormat.ddpfPixelFormat.dwGBitMask == (31<<5))
00120                 Format3d = GE_PIXELFORMAT_16BIT_555_RGB;
00121         else
00122                 Format3d = GE_PIXELFORMAT_16BIT_565_RGB;
00123 
00124 
00125         // Create the surface formats now
00126         PixelFormat[0].PixelFormat = Format3d;                                                  // 3d 565/555 surface
00127         PixelFormat[0].Flags = RDRIVER_PF_3D | RDRIVER_PF_COMBINE_LIGHTMAP;
00128                 
00129         PixelFormat[1].PixelFormat = GE_PIXELFORMAT_16BIT_4444_ARGB;    // 3d 4444 surface
00130         PixelFormat[1].Flags = RDRIVER_PF_3D | RDRIVER_PF_COMBINE_LIGHTMAP;
00131 
00132         PixelFormat[2].PixelFormat = Format2d;                                                  // 2d 565/555 surface
00133         PixelFormat[2].Flags = RDRIVER_PF_2D | RDRIVER_PF_CAN_DO_COLORKEY;
00134 
00135         PixelFormat[3].PixelFormat = Format3d;                                                  // Lightmap 565/555 surface
00136         PixelFormat[3].Flags = RDRIVER_PF_LIGHTMAP;
00137 
00138         PixelFormat[4].PixelFormat = GE_PIXELFORMAT_16BIT_1555_ARGB;    
00139         PixelFormat[4].Flags = RDRIVER_PF_3D | RDRIVER_PF_COMBINE_LIGHTMAP;
00140 
00141         // Then hand them off to the caller
00142         for (i=0; i<5; i++)
00143         {
00144                 if (!Cb(&PixelFormat[i], Context))
00145                         return GE_TRUE;
00146         }
00147 
00148         return TRUE;
00149 }
00150 
00151 geBoolean DRIVERCC SetGamma(float Gamma)
00152 {
00153         return GE_TRUE;
00154 }
00155 
00156 geBoolean DRIVERCC GetGamma(float *Gamma)
00157 {
00158         *Gamma = 1.0f;
00159                 
00160         return GE_TRUE;
00161 }
00162 
00163 BOOL DRIVERCC EnumSubDrivers2(DRV_ENUM_DRV_CB *Cb, void *Context);
00164 BOOL DRIVERCC EnumModes2(int32 Driver, char *DriverName, DRV_ENUM_MODES_CB *Cb, void *Context);
00165 
00166 DRV_Driver D3DDRV = 
00167 {
00168     "D3D driver. v"DRV_VMAJS"."DRV_VMINS". Copyright 1999, WildTangent Inc.; All Rights Reserved.",
00169         DRV_VERSION_MAJOR,
00170         DRV_VERSION_MINOR,
00171 
00172         DRV_ERROR_NONE,
00173         NULL,
00174         
00175         EnumSubDrivers2,
00176         EnumModes2,
00177 
00178         EnumPixelFormats,
00179 
00180         DrvInit,
00181         DrvShutdown,
00182         DrvResetAll,
00183         D3DMain_UpdateWindow,
00184         D3DMain_SetActive,
00185 
00186         THandle_Create,
00187         THandle_Destroy,
00188 
00189         THandle_Lock,
00190         THandle_UnLock,
00191 
00192         NULL,                   // SetPal
00193         NULL,                   // GetPal
00194 
00195         NULL,                   // SetAlpha
00196         NULL,                   // GetAlpha
00197 
00198         THandle_GetInfo,
00199 
00200         BeginScene,
00201         EndScene,
00202         BeginWorld,
00203         EndWorld,
00204         BeginMeshes,
00205         EndMeshes,
00206         BeginModels,
00207         EndModels,
00208 
00209         RenderGouraudPoly,
00210         RenderWorldPoly,
00211         RenderMiscTexturePoly,
00212 
00213         DrawDecal,
00214 
00215         0,0,0,
00216         
00217         &CacheInfo,
00218 
00219         ScreenShot,
00220 
00221         SetGamma,
00222         GetGamma,
00223 
00224         D3DMain_SetFogEnable,
00225 
00226         NULL,
00227         NULL,                                                           // Init to NULL, engine SHOULD set this (SetupLightmap)
00228         NULL
00229 };
00230 
00231 DRV_EngineSettings EngineSettings;
00232 
00233 DllExport BOOL DriverHook(DRV_Driver **Driver)
00234 {
00235         EngineSettings.CanSupportFlags = (DRV_SUPPORT_ALPHA | DRV_SUPPORT_COLORKEY);
00236         EngineSettings.PreferenceFlags = 0;//DRV_PREFERENCE_NO_MIRRORS;
00237 
00238         D3DDRV.EngineSettings = &EngineSettings;
00239     
00240         *Driver = &D3DDRV;
00241 
00242         // Make sure the error string ptr is not null, or invalid!!!
00243     D3DDRV.LastErrorStr = LastErrorStr;
00244 
00245         SetLastDrvError(DRV_ERROR_NONE, "D3DDrv:  No error.");
00246 
00247         return TRUE;
00248 }
00249 
00250 void SetLastDrvError(int32 Error, char *ErrorStr)
00251 {
00252         LastError = Error;
00253         
00254         if (ErrorStr)
00255         {
00256                 strcpy(LastErrorStr, ErrorStr);
00257         }
00258         else
00259                 LastErrorStr[0] = NULL;
00260 
00261     D3DDRV.LastErrorStr = LastErrorStr;
00262     D3DDRV.LastError = LastError;
00263 }
00264 
00265 BOOL DRIVERCC ScreenShot(const char *Name)
00266 {
00267    DDSURFACEDESC2 ddsd;
00268    BITMAPFILEHEADER bfh;
00269    BITMAPINFOHEADER bih;
00270    HRESULT result;
00271    HDC surfDC = NULL;
00272    HDC memDC = NULL;
00273    HBITMAP bitmap = NULL;
00274    HGDIOBJ oldbit = NULL;
00275    FILE *file = NULL;
00276    void *data= NULL;
00277    int width, height, bpp;
00278    int datasize;
00279    BOOL success = FALSE;
00280 
00281    memset(&ddsd,0,sizeof(ddsd));
00282    ddsd.dwSize = sizeof(ddsd);
00283    result = AppInfo.lpBackBuffer->GetSurfaceDesc(&ddsd);
00284 
00285    if (FAILED(result))
00286       goto cleanup;
00287 
00288    width = ddsd.dwWidth;
00289    height= ddsd.dwHeight;
00290    bpp = ddsd.ddpfPixelFormat.dwRGBBitCount / 8;
00291 
00292    if (bpp < 2) 
00293       bpp = 2;
00294 
00295    if (bpp > 3)
00296       bpp = 3;
00297 
00298    datasize = width * bpp * height;
00299 
00300    if (width * bpp % 4)
00301       datasize += height * (4 - width * bpp % 4);
00302 
00303    memset((void*)&bfh, 0, sizeof(bfh));
00304 
00305    bfh.bfType = 'B'+('M'<<8);
00306    bfh.bfSize = sizeof(bfh) + sizeof(bih) + datasize;
00307    bfh.bfOffBits = sizeof(bfh) + sizeof(bih);
00308                             
00309    memset((void*)&bih, 0, sizeof(bih));
00310 
00311    bih.biSize = sizeof(bih);
00312    bih.biWidth = ddsd.dwWidth;
00313    bih.biHeight = ddsd.dwHeight;
00314    bih.biPlanes = 1;
00315    bih.biBitCount = (unsigned short)(bpp * 8);
00316    bih.biCompression = BI_RGB;
00317                             
00318    result = AppInfo.lpBackBuffer->GetDC(&surfDC);
00319 
00320    if (FAILED(result))
00321       goto cleanup;
00322 
00323    bitmap = CreateDIBSection(NULL, (BITMAPINFO *)&bih, DIB_RGB_COLORS, &data, NULL, 0);
00324 
00325    if (!bitmap)
00326       goto cleanup;
00327 
00328    if (!data)
00329       goto cleanup;
00330 
00331    memDC = CreateCompatibleDC(surfDC);
00332 
00333    if (!memDC)
00334       goto cleanup;
00335 
00336    oldbit = SelectObject(memDC, bitmap);
00337 
00338    if (!oldbit || FAILED(oldbit))
00339       goto cleanup;
00340 
00341    result = BitBlt(memDC, 0, 0, width, height, surfDC, 0, 0, SRCCOPY);
00342 
00343    if (!result)
00344       goto cleanup;
00345 
00346    AppInfo.lpBackBuffer->ReleaseDC(surfDC); 
00347    surfDC = NULL;                           
00348    file = fopen(Name, "wb");
00349 
00350    if (!file)
00351       goto cleanup;
00352                           
00353    fwrite((void*)&bfh, sizeof(bfh), 1, file);
00354    fwrite((void*)&bih, sizeof(bih), 1, file);
00355    fwrite((void*)data, 1, datasize, file);
00356 
00357    success = TRUE;
00358                            
00359 cleanup:
00360                           
00361    if (oldbit && !FAILED(oldbit))
00362       SelectObject(memDC, oldbit);
00363 
00364    if (memDC)
00365       DeleteDC(memDC);
00366  
00367    if (surfDC)
00368       AppInfo.lpBackBuffer->ReleaseDC(surfDC);
00369  
00370    if (bitmap)
00371       DeleteObject(bitmap);
00372  
00373    if (file)
00374       fclose(file);
00375 
00376    return success;
00377 }

Generated on Tue Sep 30 12:35:35 2003 for GTestAndEngine by doxygen 1.3.2