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