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.
15 #pragma off (unreferenced)
16 static char rcsid[] = "$Id: direct3d.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
17 #pragma on (unreferenced)
24 #define WIN32_LEAN_AND_MEAN
47 // ----------------------------------------------------------------------------
49 static LPDIRECT3D _lpD3D = NULL; // D3D Object
50 static LPDIRECT3DDEVICE _lpD3DDev = NULL; // D3D Device
51 static GUID _3DGUID; // Device GUID
53 static d3d_caps d3dCaps; // Direct 3d Device Caps
54 static BOOL d3d_initialized=0;// D3D flag
55 static FILE *LogFile=NULL; // Log File!
59 // Function prototypes
60 // ----------------------------------------------------------------------------
67 #define WRITELOG(t) if (LogFile) { fprintf t; fflush(LogFile); }
68 #define LOGINIT(n) LogFile = fopen(n, "wt");
69 #define LOGCLOSE if (LogFile) fclose(LogFile);
72 HRESULT FAR PASCAL d3d_enum_callback(LPGUID pguid, LPSTR lpDeviceDesc,
73 LPSTR lpDeviceName, LPD3DDEVICEDESC lpHWDevDesc,
74 LPD3DDEVICEDESC lpHELDevDesc, LPVOID lpUserArg);
77 int d3d_device_close();
78 int d3d_enum_devices(GUID *pguid);
79 int d3d_enum_texformats();
80 int d3d_init_device(GUID guid);
81 int d3d_handle_error(HRESULT err);
87 Direct 3D Initialization of Immediate Mode
89 We need to do a few things, first get the D3D interface from our
92 Then we need to find an active device.
94 Then we need to query the caps of such a device, and tell our GFX system
95 about the available video modes (15-bit color)
104 Assert(d3d_initialized == FALSE);
110 // initialize Direct3d interface
111 res = IDirectDraw_QueryInterface(_lpDD, &IID_IDirect3D, &_lpD3D);
113 return d3d_handle_error(res);
115 // enumerate all 3d devices useable by Direct3d
116 if (d3d_enum_devices(&guid)) return FALSE;
118 memcpy(&_3DGUID, &guid, sizeof(GUID));
120 d3d_initialized = TRUE;
128 if (d3d_initialized) {
129 IDirect3D_Release(_lpD3D);
131 d3d_initialized = FALSE;
137 int d3d_enum_devices(GUID *pguid)
142 res = IDirect3D_EnumDevices(_lpD3D, d3d_enum_callback, (LPVOID)&guid);
144 WRITELOG((LogFile, "Unable to find a proper 3D hardware device.\n"));
145 return d3d_handle_error(res);
148 WRITELOG((LogFile, "Found 3D Device (%s)\n", d3dCaps.devname));
150 memcpy(pguid, &guid, sizeof(GUID));
156 /* d3d_device functions
158 these functions are used once we really need to use the 3d system.
159 When done with it, we call device_close.
162 int d3d_init_device(GUID guid)
167 if (!d3d_initialized) return -1;
170 res = IDirectDrawSurface_QueryInterface(_lpDDSBack, &_3DGUID, &_lpD3DDev);
172 WRITELOG((LogFile, "Unable to retrieve device from back buffer. %x\n",res));
173 return d3d_handle_error(res);
176 // Enumerate texture formats
177 Assert(d3dCaps.tmap_acc == TRUE);
178 d3dCaps.tmap_formats = 0;
179 if (d3d_enum_texformats()) return -1;
185 int d3d_device_close()
188 IDirect3DDevice_Release(_lpD3DDev);
195 int d3d_enum_texformats()
203 /* Miscellaneous Utilities */
205 int d3d_handle_error(HRESULT err)
207 if (err != DD_OK) return err;
213 /* Direct 3D callbacks
215 enum_callback: enumerates all 3D devices attached to system.
220 HRESULT FAR PASCAL d3d_enum_callback(LPGUID pguid, LPSTR lpDeviceDesc,
222 LPD3DDEVICEDESC lpHWDevDesc,
223 LPD3DDEVICEDESC lpHELDevDesc,
227 /* Ignore emulated devices (we can use our own stuff then) */
228 if (lpHWDevDesc->dcmColorModel) {
229 d3dCaps.hw_acc = TRUE;
232 d3dCaps.hw_acc = FALSE;
233 return D3DENUMRET_OK;
236 /* Test hardware caps
239 if (lpHWDevDesc->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) {
240 d3dCaps.tmap_acc = TRUE;
243 d3dCaps.tmap_acc = FALSE;
244 return D3DENUMRET_OK;
247 memcpy(lpUserArg, pguid, sizeof(GUID));
248 lstrcpy(d3dCaps.devname, lpDeviceName);
250 return D3DENUMRET_CANCEL;