Debian Codegraph @

dd_grwin32/ddgrWin32DX.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ddgr_win32/ddgrWin32DX.cpp $
21 * $Revision: 2 $
22 * $Date: 9/12/97 4:13p $
23 * $Author: Samir $
24 *
25 * DirectX implementation of DDGR library.
26 *
27 * $Log: /DescentIII/Main/ddgr_win32/ddgrWin32DX.cpp $
28 *
29 * 2 9/12/97 4:13p Samir
30 * Added some private data access functions and more DirectX
31 * functionality.
32 *
33 * $NoKeywords: $
34 */
35
36 #include "ddgrWin32.h"
37 #include "ddgrWin32DX.h"
38 #include "Application.h"
39 #include "mono.h"
40 #include "pserror.h"
41
42 tDDGRDXInternalData DDGR_DX_lib_data;
43
44 // Initializes DX subsystem
45 bool ddgr_dx_Init(oeApplication *app) {
46 DX_DATA(hPrimaryWnd) = (HWND)((oeWin32Application *)app)->m_hWnd;
47
48 DX_DATA(lpDD) = NULL;
49
50 // Initialize DirectDraw system if we're going fullscreen, and GDIX system
51 HRESULT hres;
52
53 hres = DirectDrawCreate(NULL, &DX_DATA(lpDD), NULL);
54 if (hres != DD_OK) {
55 ddgr_PushError("Failure to initialize DirectDraw driver (%d)", LOWORD(hres));
56 return false;
57 }
58
59 hres =
60 DX_DATA(lpDD)->SetCooperativeLevel(DX_DATA(hPrimaryWnd), DDSCL_ALLOWREBOOT | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
61
62 if (hres != DD_OK) {
63 ddgr_PushError("Failed to set DirectDraw fullscreen access level (%d)", LOWORD(hres));
64 return false;
65 }
66
67 DX_DATA(vidrefs) = 0; // reset video reference count.
68 DX_DATA(init) = true;
69
70 mprintf(0, "DX system initialized.\n");
71
72 return true;
73 }
74
75 // Closes DX subsystem
76 void ddgr_dx_Close() {
77 ASSERT(DX_DATA(vidrefs) == 0);
78
79 if (DX_DATA(lpDD)) {
80 DX_DATA(lpDD)->RestoreDisplayMode();
81 DX_DATA(lpDD)->Release();
82 }
83 DX_DATA(lpDD) = NULL;
84 DX_DATA(hPrimaryWnd) = NULL;
85 DX_DATA(init) = false;
86
87 mprintf(0, "DX system closed.\n");
88 }
89
90 // Initializes the display for use with the DX subsystem.
91 bool ddgr_dx_surf_InitVideo(ddgr_surface *sf) {
92 tDXSurface *dxsf;
93 DDSURFACEDESC ddsd;
94
95 // we need to create a primary surface from the display mode we set, and possibly a backbuffer if
96 // requested.
97 if (DX_DATA(vidrefs) == 0) {
98 HRESULT hres;
99 DDSCAPS ddscaps;
100
101 hres = DX_DATA(lpDD)->SetDisplayMode(sf->w, sf->h, sf->bpp);
102 if (hres != DD_OK) {
103 ddgr_PushError("Unable to set DirectDraw display mode (%d)\n", LOWORD(hres));
104 return false;
105 }
106
107 memset(&ddsd, 0, sizeof(ddsd));
108 ddsd.dwSize = sizeof(ddsd);
109 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
110 ddsd.dwBackBufferCount = 1;
111 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
112
113 hres = DX_DATA(lpDD)->CreateSurface(&ddsd, &DX_DATA(vidsurf.lpdds), NULL);
114 if (hres != DD_OK) {
115 ddgr_PushError("Unable to capture DirectDraw display surface (%d)\n", LOWORD(hres));
116 return false;
117 }
118
119 ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
120
121 hres = DX_DATA(vidsurf.lpdds)->GetAttachedSurface(&ddscaps, &DX_DATA(vidsurf.lpddsback));
122 if (hres != DD_OK) {
123 ddgr_PushError("Unable to capture DirectDraw display back surface (%d)\n", LOWORD(hres));
124 return false;
125 }
126 }
127
128 DX_DATA(vidrefs)++; // increment vidref count
129
130 dxsf = new tDXSurface;
131 dxsf->lpdds = DX_DATA(vidsurf.lpdds);
132
133 // this means that we are capturing the screen, not creating a screen.
134 // get the screen's width and height and bitdepth
135 if (sf->flags == 0) {
136 memset(&ddsd, 0, sizeof(ddsd));
137 ddsd.dwSize = sizeof(ddsd);
138 dxsf->lpdds->GetSurfaceDesc(&ddsd);
139 sf->w = ddsd.dwWidth;
140 sf->h = ddsd.dwHeight;
141 sf->bpp = ddsd.ddpfPixelFormat.dwRGBBitCount;
142 }
143
144 // we create a dummy surface for the screen's back buffer, or our accessable buffer
145 // specifying SURFFLAG_BACKBUFFER ensures that we create a GDI bitmap of size requested
146 // in 'sf' and it will be our renderable bitmap, or OUR primary render buffer
147 if (sf->flags & SURFFLAG_BACKBUFFER) {
148 // set our front buffer to this newly created surface and delete the gdi bitmap object
149 dxsf->lpddsback = DX_DATA(vidsurf.lpddsback);
150 dxsf->backbuffer = true;
151 } else {
152 dxsf->lpddsback = NULL;
153 dxsf->backbuffer = false;
154 }
155
156 sf->obj = (void *)dxsf;
157
158 return true;
159 }
160
161 // Deinitializes the video surface
162 void ddgr_dx_surf_CloseVideo(ddgr_surface *sf) {
163 tDXSurface *bm = (tDXSurface *)sf->obj;
164
165 ASSERT(DX_DATA(vidrefs) > 0);
166
167 DX_DATA(vidrefs)--;
168 if (DX_DATA(vidrefs) == 0) {
169 DX_DATA(vidsurf.lpdds)->Release();
170 DX_DATA(vidsurf.lpdds) = NULL;
171 DX_DATA(vidsurf.lpddsback) = NULL;
172 }
173
174 delete bm;
175 }
176
177 // flips the buffers in a surface. really only useful for video screens
178 bool ddgr_dx_surf_FlipVideo(ddgr_surface *sf) {
179 tDXSurface *bm = (tDXSurface *)sf->obj;
180 HRESULT hres;
181
182 if (bm->backbuffer && (sf->flags & SURFFLAG_BACKBUFFER)) {
183 hres = bm->lpdds->Flip(NULL, 0);
184 if (hres == DDERR_WASSTILLDRAWING || hres == DD_OK)
185 return true;
186 else
187 return false;
188 }
189
190 return true;
191 }
192
193 /* input:
194 sf->name is optional.
195 sf->w, sf->h, sf->bpp are mandatory
196 sf->type and sf->flags are mandatory.
197 output:
198 sf->obj = surface object.
199 sf->locks = 0
200 */
201 bool ddgr_dx_surf_Create(ddgr_surface *sf) { return true; }
202
203 void ddgr_dx_surf_Destroy(ddgr_surface *sf) {}
204
205 /* retrieves a pointer to surface memory. allowed to lock one surface multiple times.
206 ptr is the returned pointer to surface memory. used to unlock surface also
207 rowsize is the size in bytes of one row of memory.
208 */
209 bool ddgr_dx_surf_Lock(ddgr_surface *sf, void **ptr, int *rowsize) {
210 HRESULT hres;
211 DDSURFACEDESC ddsd;
212 tDXSurface *bm = (tDXSurface *)sf->obj;
213
214 memset(&ddsd, 0, sizeof(ddsd));
215 ddsd.dwSize = sizeof(ddsd);
216
217 if (bm->backbuffer)
218 hres = bm->lpddsback->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
219 else
220 hres = bm->lpdds->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
221
222 if (hres != DD_OK) {
223 ddgr_PushError("Unable to set lock DirectDraw surface(%d)\n", LOWORD(hres));
224 return false;
225 }
226
227 *rowsize = (int)ddsd.lPitch;
228 *ptr = ddsd.lpSurface;
229
230 return true;
231 }
232
233 bool ddgr_dx_surf_Unlock(ddgr_surface *sf, void *ptr) {
234 HRESULT hres;
235 tDXSurface *bm = (tDXSurface *)sf->obj;
236
237 if (bm->backbuffer)
238 hres = bm->lpddsback->Unlock(ptr);
239 else
240 hres = bm->lpdds->Unlock(ptr);
241
242 if (hres != DD_OK) {
243 ddgr_PushError("Unable to unlock DirectDraw surface(%d)\n", LOWORD(hres));
244 return false;
245 }
246
247 return true;
248 }
249
250 // attaches an OS handle to a surface
251 void ddgr_dx_surf_AttachHandle(ddgr_surface *sf, unsigned handle) {}
252
253 // The only graphic primatives
254 // clear
255 // blt.
256 void ddgr_dx_surf_Clear(ddgr_surface *dsf, ddgr_color col, int l, int t, int w, int h) {
257 HRESULT hres;
258 DDBLTFX ddbltfx;
259 tDXSurface *dbm = (tDXSurface *)dsf->obj;
260 LPDIRECTDRAWSURFACE lpdds;
261 RECT rect;
262
263 lpdds = (dbm->backbuffer) ? dbm->lpddsback : dbm->lpdds;
264
265 memset(&ddbltfx, 0, sizeof(DDBLTFX));
266 ddbltfx.dwSize = sizeof(ddbltfx);
267 ddbltfx.dwFillColor = RGB(DDGR_COLOR_RED(col), DDGR_COLOR_GREEN(col), DDGR_COLOR_BLUE(col));
268 SetRect(&rect, l, t, l + w - 1, t + h - 1);
269
270 hres = lpdds->Blt(&rect, // dest rect
271 NULL, // src surface
272 NULL, // src rect
273 DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
274 }
275
276 bool ddgr_dx_surf_Blt(ddgr_surface *dsf, int dx, int dy, ddgr_surface *ssf, int sx, int sy, int sw, int sh) {
277 HRESULT hres;
278 tDXSurface *sbm = (tDXSurface *)ssf->obj;
279 tDXSurface *dbm = (tDXSurface *)dsf->obj;
280 LPDIRECTDRAWSURFACE lpddsd;
281 LPDIRECTDRAWSURFACE lpddss;
282 RECT srect, drect;
283
284 if (ssf->flags & SURFFLAG_BACKBUFFER)
285 lpddss = sbm->lpddsback;
286 else
287 lpddss = sbm->lpdds;
288
289 if (dsf->flags & SURFFLAG_BACKBUFFER)
290 lpddsd = dbm->lpddsback;
291 else
292 lpddsd = dbm->lpdds;
293
294 SetRect(&srect, sx, sy, sx + sw - 1, sy + sh - 1);
295 SetRect(&drect, dx, dy, dx + sw - 1, dy + sh + 1);
296
297 hres = lpddsd->Blt(&drect, lpddss, &srect, DDBLT_WAIT, NULL);
298 if (hres != DD_OK) {
299 ddgr_PushError("Unable to blt surface %s to DirectDraw surface %s(%d)\n", ssf->name, dsf->name, LOWORD(hres));
300 return false;
301 }
302
303 return true;
304 }