2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
20 #define WIN32_LEAN_AND_MEAN
43 // ----------------------------------------------------------------------------
45 static LPDIRECT3D _lpD3D = NULL; // D3D Object
46 static LPDIRECT3DDEVICE _lpD3DDev = NULL; // D3D Device
47 static GUID _3DGUID; // Device GUID
49 static d3d_caps d3dCaps; // Direct 3d Device Caps
50 static BOOL d3d_initialized=0;// D3D flag
51 static FILE *LogFile=NULL; // Log File!
55 // Function prototypes
56 // ----------------------------------------------------------------------------
63 #define WRITELOG(t) if (LogFile) { fprintf t; fflush(LogFile); }
64 #define LOGINIT(n) LogFile = fopen(n, "wt");
65 #define LOGCLOSE if (LogFile) fclose(LogFile);
68 HRESULT FAR PASCAL d3d_enum_callback(LPGUID pguid, LPSTR lpDeviceDesc,
69 LPSTR lpDeviceName, LPD3DDEVICEDESC lpHWDevDesc,
70 LPD3DDEVICEDESC lpHELDevDesc, LPVOID lpUserArg);
73 int d3d_device_close();
74 int d3d_enum_devices(GUID *pguid);
75 int d3d_enum_texformats();
76 int d3d_init_device(GUID guid);
77 int d3d_handle_error(HRESULT err);
83 Direct 3D Initialization of Immediate Mode
85 We need to do a few things, first get the D3D interface from our
88 Then we need to find an active device.
90 Then we need to query the caps of such a device, and tell our GFX system
91 about the available video modes (15-bit color)
100 Assert(d3d_initialized == FALSE);
106 // initialize Direct3d interface
107 res = IDirectDraw_QueryInterface(_lpDD, &IID_IDirect3D, &_lpD3D);
109 return d3d_handle_error(res);
111 // enumerate all 3d devices useable by Direct3d
112 if (d3d_enum_devices(&guid)) return FALSE;
114 memcpy(&_3DGUID, &guid, sizeof(GUID));
116 d3d_initialized = TRUE;
124 if (d3d_initialized) {
125 IDirect3D_Release(_lpD3D);
127 d3d_initialized = FALSE;
133 int d3d_enum_devices(GUID *pguid)
138 res = IDirect3D_EnumDevices(_lpD3D, d3d_enum_callback, (LPVOID)&guid);
140 WRITELOG((LogFile, "Unable to find a proper 3D hardware device.\n"));
141 return d3d_handle_error(res);
144 WRITELOG((LogFile, "Found 3D Device (%s)\n", d3dCaps.devname));
146 memcpy(pguid, &guid, sizeof(GUID));
152 /* d3d_device functions
154 these functions are used once we really need to use the 3d system.
155 When done with it, we call device_close.
158 int d3d_init_device(GUID guid)
163 if (!d3d_initialized) return -1;
166 res = IDirectDrawSurface_QueryInterface(_lpDDSBack, &_3DGUID, &_lpD3DDev);
168 WRITELOG((LogFile, "Unable to retrieve device from back buffer. %x\n",res));
169 return d3d_handle_error(res);
172 // Enumerate texture formats
173 Assert(d3dCaps.tmap_acc == TRUE);
174 d3dCaps.tmap_formats = 0;
175 if (d3d_enum_texformats()) return -1;
181 int d3d_device_close()
184 IDirect3DDevice_Release(_lpD3DDev);
191 int d3d_enum_texformats()
199 /* Miscellaneous Utilities */
201 int d3d_handle_error(HRESULT err)
203 if (err != DD_OK) return err;
209 /* Direct 3D callbacks
211 enum_callback: enumerates all 3D devices attached to system.
216 HRESULT FAR PASCAL d3d_enum_callback(LPGUID pguid, LPSTR lpDeviceDesc,
218 LPD3DDEVICEDESC lpHWDevDesc,
219 LPD3DDEVICEDESC lpHELDevDesc,
223 /* Ignore emulated devices (we can use our own stuff then) */
224 if (lpHWDevDesc->dcmColorModel) {
225 d3dCaps.hw_acc = TRUE;
228 d3dCaps.hw_acc = FALSE;
229 return D3DENUMRET_OK;
232 /* Test hardware caps
235 if (lpHWDevDesc->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) {
236 d3dCaps.tmap_acc = TRUE;
239 d3dCaps.tmap_acc = FALSE;
240 return D3DENUMRET_OK;
243 memcpy(lpUserArg, pguid, sizeof(GUID));
244 lstrcpy(d3dCaps.devname, lpDeviceName);
246 return D3DENUMRET_CANCEL;