]> icculus.org git repositories - btb/d2x.git/blob - unused/win95/direct3d.c
copied files from d1x
[btb/d2x.git] / unused / win95 / direct3d.c
1 /*
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.
12 */
13
14
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)
18
19
20
21
22 #define _WIN32
23 #define WIN95
24 #define WIN32_LEAN_AND_MEAN
25 #define INITGUID
26
27 #include <windows.h>
28 #include <windowsx.h>
29
30 #include <stdio.h>
31
32 #include "ddraw.h"
33 #include "d3d.h"
34
35 #include "pstypes.h"
36 #include "mono.h"
37 #include "args.h"
38 #include "error.h"
39 #include "winapp.h"
40
41 #include "dd.h"
42 #include "direct3d.h"
43
44
45
46 //      Globals
47 //      ----------------------------------------------------------------------------
48
49 static LPDIRECT3D                       _lpD3D = NULL;          // D3D Object
50 static LPDIRECT3DDEVICE         _lpD3DDev = NULL;       // D3D Device
51 static GUID                                             _3DGUID;                                // Device GUID
52
53 static d3d_caps                         d3dCaps;                                // Direct 3d Device Caps
54 static BOOL                                             d3d_initialized=0;// D3D flag
55 static FILE                                     *LogFile=NULL;          // Log File!
56
57
58
59 //      Function prototypes
60 //      ----------------------------------------------------------------------------
61
62 #ifdef NDEBUG
63 #define WRITELOG(t)
64 #define LOGINIT(n)
65 #define LOGCLOSE
66 #else
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);
70 #endif
71
72 HRESULT FAR PASCAL d3d_enum_callback(LPGUID pguid, LPSTR lpDeviceDesc, 
73                                                                     LPSTR lpDeviceName, LPD3DDEVICEDESC lpHWDevDesc, 
74                                                                     LPD3DDEVICEDESC lpHELDevDesc, LPVOID lpUserArg);
75
76 int d3d_close();
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);
82
83
84
85
86 /* d3d_init
87                 Direct 3D Initialization of Immediate Mode
88
89                 We need to do a few things, first get the D3D interface from our
90                 DirectDraw Object
91                 
92                 Then we need to find an active device.
93
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)
96 */
97
98
99 BOOL d3d_init(void)
100 {
101         HRESULT         res;
102         GUID            guid;
103
104         Assert(d3d_initialized == FALSE);
105
106         LOGINIT("d3d.log");
107
108         atexit(d3d_close);
109
110 //      initialize Direct3d interface
111         res = IDirectDraw_QueryInterface(_lpDD, &IID_IDirect3D, &_lpD3D);
112         if (res != DD_OK) 
113                 return d3d_handle_error(res);
114
115 //      enumerate all 3d devices useable by Direct3d
116         if (d3d_enum_devices(&guid)) return FALSE; 
117
118         memcpy(&_3DGUID, &guid, sizeof(GUID));
119
120         d3d_initialized = TRUE;
121
122         return TRUE;
123 }
124
125
126 int d3d_close(void)
127 {
128         if (d3d_initialized) {
129                 IDirect3D_Release(_lpD3D);
130                 _lpD3D = NULL;
131                 d3d_initialized = FALSE;
132         }
133         return 0;
134 }               
135
136
137 int d3d_enum_devices(GUID *pguid)
138 {
139         HRESULT res; 
140         GUID guid;
141
142         res = IDirect3D_EnumDevices(_lpD3D, d3d_enum_callback, (LPVOID)&guid);
143         if (res != DD_OK) {
144                 WRITELOG((LogFile, "Unable to find a proper 3D hardware device.\n"));
145                 return d3d_handle_error(res);
146         }
147
148         WRITELOG((LogFile, "Found 3D Device (%s)\n", d3dCaps.devname));
149
150         memcpy(pguid, &guid, sizeof(GUID));
151         return 0;
152 }
153
154
155
156 /* d3d_device functions
157
158                 these functions are used once we really need to use the 3d system.
159                 When done with it, we call device_close.
160 */
161
162 int d3d_init_device(GUID guid)
163 {
164         HRESULT res;
165         DDSURFACEDESC ddsd;
166
167         if (!d3d_initialized) return -1;
168
169 //      Grab back buffer.
170         res = IDirectDrawSurface_QueryInterface(_lpDDSBack, &_3DGUID, &_lpD3DDev);
171         if (res != DD_OK) {
172                 WRITELOG((LogFile, "Unable to retrieve device from back buffer. %x\n",res));
173                 return d3d_handle_error(res);
174         }
175                         
176 //      Enumerate texture formats
177         Assert(d3dCaps.tmap_acc == TRUE);
178         d3dCaps.tmap_formats = 0;
179         if (d3d_enum_texformats()) return -1;
180
181         return 0;
182 }
183
184
185 int d3d_device_close()
186 {
187         if (_lpD3DDev) {
188                 IDirect3DDevice_Release(_lpD3DDev);
189                 _lpD3DDev = NULL;
190         }
191         return 0;
192 }
193
194
195 int d3d_enum_texformats()
196 {
197
198         return 0;
199 }
200
201
202
203 /* Miscellaneous Utilities */
204         
205 int d3d_handle_error(HRESULT err)
206 {
207         if (err != DD_OK) return err;
208         else return 0;
209 }
210
211
212
213 /* Direct 3D callbacks 
214
215                 enum_callback:   enumerates all 3D devices attached to system.
216         
217
218 */
219
220 HRESULT FAR PASCAL d3d_enum_callback(LPGUID pguid, LPSTR lpDeviceDesc, 
221                                                                    LPSTR lpDeviceName, 
222                                                                         LPD3DDEVICEDESC lpHWDevDesc, 
223                                                                    LPD3DDEVICEDESC lpHELDevDesc, 
224                                                                         LPVOID lpUserArg)
225 {
226
227 /* Ignore emulated devices (we can use our own stuff then) */
228         if (lpHWDevDesc->dcmColorModel) {
229                 d3dCaps.hw_acc = TRUE;
230         }
231         else {
232                 d3dCaps.hw_acc = FALSE;
233                 return D3DENUMRET_OK;
234         }
235
236 /* Test hardware caps 
237                 texture = MUST HAVE
238 */
239         if (lpHWDevDesc->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) {
240                 d3dCaps.tmap_acc = TRUE;
241         }
242         else {
243                 d3dCaps.tmap_acc = FALSE;
244                 return D3DENUMRET_OK;
245         }
246
247         memcpy(lpUserArg, pguid, sizeof(GUID));
248         lstrcpy(d3dCaps.devname, lpDeviceName);
249
250         return D3DENUMRET_CANCEL;
251 }
252