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