| 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/ddgrWin32API.cpp $ |
| 21 |
* $Revision: 3 $ |
| 22 |
* $Date: 9/12/97 4:13p $ |
| 23 |
* $Author: Samir $ |
| 24 |
* |
| 25 |
* DDGR v2.0 Initialization and housekeeping for Win32 |
| 26 |
* |
| 27 |
* $Log: /DescentIII/Main/ddgr_win32/ddgrWin32API.cpp $ |
| 28 |
* |
| 29 |
* 3 9/12/97 4:13p Samir |
| 30 |
* Added some private data access functions and more DirectX |
| 31 |
* functionality. |
| 32 |
* |
| 33 |
* 2 6/13/97 3:01p Samir |
| 34 |
* Fixed flip assertions and error handling. |
| 35 |
* |
| 36 |
* 1 6/12/97 6:33p Samir |
| 37 |
* Initial revision |
| 38 |
* |
| 39 |
* $NoKeywords: $ |
| 40 |
*/ |
| 41 |
|
| 42 |
#include "ddgrWin32.h" |
| 43 |
#include "ddgrWin32GDI.h" |
| 44 |
#include "ddgrWin32DX.h" |
| 45 |
#include "mono.h" |
| 46 |
#include "pserror.h" |
| 47 |
|
| 48 |
/* Surface Functions |
| 49 |
*/ |
| 50 |
|
| 51 |
/* input: |
| 52 |
sf->name is optional. |
| 53 |
sf->w, sf->h, sf->bpp are mandatory |
| 54 |
sf->type and sf->flags are mandatory. |
| 55 |
output: |
| 56 |
sf->obj = surface object. |
| 57 |
sf->locks = 0 |
| 58 |
*/ |
| 59 |
bool ddgr_surf_Create(ddgr_surface *sf) { |
| 60 |
bool success; |
| 61 |
|
| 62 |
switch (LIB_DATA(subsystem)) { |
| 63 |
case DDGR_GDIX_SUBSYSTEM: |
| 64 |
case DDGR_GDI_SUBSYSTEM: |
| 65 |
success = ddgr_gdi_surf_Create(sf); |
| 66 |
break; |
| 67 |
case DDGR_DX_SUBSYSTEM: |
| 68 |
success = ddgr_dx_surf_Create(sf); |
| 69 |
break; |
| 70 |
default: |
| 71 |
Int3(); |
| 72 |
} |
| 73 |
|
| 74 |
if (!success) { |
| 75 |
ddgr_FatalError("Failed to create surface <%s>", sf->name); |
| 76 |
} |
| 77 |
|
| 78 |
return success; |
| 79 |
} |
| 80 |
|
| 81 |
void ddgr_surf_Destroy(ddgr_surface *sf) { |
| 82 |
switch (LIB_DATA(subsystem)) { |
| 83 |
case DDGR_GDIX_SUBSYSTEM: |
| 84 |
case DDGR_GDI_SUBSYSTEM: |
| 85 |
ddgr_gdi_surf_Destroy(sf); |
| 86 |
break; |
| 87 |
case DDGR_DX_SUBSYSTEM: |
| 88 |
ddgr_dx_surf_Destroy(sf); |
| 89 |
break; |
| 90 |
default: |
| 91 |
Int3(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/* retrieves a pointer to surface memory. allowed to lock one surface multiple times. |
| 96 |
ptr is the returned pointer to surface memory. used to unlock surface also |
| 97 |
rowsize is the size in bytes of one row of memory. |
| 98 |
*/ |
| 99 |
bool ddgr_surf_Lock(ddgr_surface *sf, void **ptr, int *rowsize) { |
| 100 |
bool success; |
| 101 |
|
| 102 |
switch (LIB_DATA(subsystem)) { |
| 103 |
case DDGR_GDIX_SUBSYSTEM: |
| 104 |
case DDGR_GDI_SUBSYSTEM: |
| 105 |
success = ddgr_gdi_surf_Lock(sf, ptr, rowsize); |
| 106 |
break; |
| 107 |
case DDGR_DX_SUBSYSTEM: |
| 108 |
success = ddgr_dx_surf_Lock(sf, ptr, rowsize); |
| 109 |
break; |
| 110 |
default: |
| 111 |
Int3(); |
| 112 |
} |
| 113 |
|
| 114 |
if (!success) { |
| 115 |
ddgr_FatalError("Failed to lock surface <%s>", sf->name); |
| 116 |
} |
| 117 |
|
| 118 |
return success; |
| 119 |
} |
| 120 |
|
| 121 |
bool ddgr_surf_Unlock(ddgr_surface *sf, void *ptr) { |
| 122 |
bool success; |
| 123 |
|
| 124 |
switch (LIB_DATA(subsystem)) { |
| 125 |
case DDGR_GDIX_SUBSYSTEM: |
| 126 |
case DDGR_GDI_SUBSYSTEM: |
| 127 |
success = ddgr_gdi_surf_Unlock(sf, ptr); |
| 128 |
break; |
| 129 |
case DDGR_DX_SUBSYSTEM: |
| 130 |
success = ddgr_dx_surf_Unlock(sf, ptr); |
| 131 |
break; |
| 132 |
default: |
| 133 |
Int3(); |
| 134 |
} |
| 135 |
|
| 136 |
if (!success) { |
| 137 |
ddgr_FatalError("Failed to unlock surface <%s>", sf->name); |
| 138 |
} |
| 139 |
|
| 140 |
return success; |
| 141 |
} |
| 142 |
|
| 143 |
// attaches an OS handle to a surface |
| 144 |
void ddgr_surf_AttachHandle(ddgr_surface *sf, unsigned handle) { |
| 145 |
switch (LIB_DATA(subsystem)) { |
| 146 |
case DDGR_GDIX_SUBSYSTEM: |
| 147 |
case DDGR_GDI_SUBSYSTEM: |
| 148 |
ddgr_gdi_surf_AttachHandle(sf, handle); |
| 149 |
break; |
| 150 |
case DDGR_DX_SUBSYSTEM: |
| 151 |
ddgr_dx_surf_AttachHandle(sf, handle); |
| 152 |
break; |
| 153 |
default: |
| 154 |
Int3(); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/* initializes a video screen from the given input surface which specifies the dimensions |
| 159 |
of the screen. |
| 160 |
sf->name is optional |
| 161 |
sf->w, sf->h are mandatory |
| 162 |
sf->bpp is recommended. if set to BPP_DEFAULT, it uses the current display bit depth |
| 163 |
sf->type = SURFTYPE_VIDEOSCREEN |
| 164 |
sf->flags = 0 or SURFFLAG_BACKBUFFER. surfaces with backbuffers allow rendering to |
| 165 |
them and display via flip. monopage surfaces don't have this luxury. |
| 166 |
*/ |
| 167 |
bool ddgr_surf_InitVideo(ddgr_surface *sf) { |
| 168 |
bool success; |
| 169 |
|
| 170 |
ASSERT(sf->type == SURFTYPE_VIDEOSCREEN); |
| 171 |
|
| 172 |
switch (LIB_DATA(subsystem)) { |
| 173 |
case DDGR_GDIX_SUBSYSTEM: |
| 174 |
case DDGR_GDI_SUBSYSTEM: |
| 175 |
success = ddgr_gdi_surf_InitVideo(sf); |
| 176 |
break; |
| 177 |
case DDGR_DX_SUBSYSTEM: |
| 178 |
success = ddgr_dx_surf_InitVideo(sf); |
| 179 |
break; |
| 180 |
default: |
| 181 |
Int3(); |
| 182 |
} |
| 183 |
|
| 184 |
if (!success) { |
| 185 |
ddgr_FatalError("Failed to initialize video screen <%s>", sf->name); |
| 186 |
} |
| 187 |
|
| 188 |
return success; |
| 189 |
} |
| 190 |
|
| 191 |
/* close video reverses the operation of init video for that surface. the display should stay |
| 192 |
the same, but no more operations may occur to that surface through this library. |
| 193 |
*/ |
| 194 |
void ddgr_surf_CloseVideo(ddgr_surface *sf) { |
| 195 |
switch (LIB_DATA(subsystem)) { |
| 196 |
case DDGR_GDIX_SUBSYSTEM: |
| 197 |
case DDGR_GDI_SUBSYSTEM: |
| 198 |
ddgr_gdi_surf_CloseVideo(sf); |
| 199 |
break; |
| 200 |
case DDGR_DX_SUBSYSTEM: |
| 201 |
ddgr_dx_surf_CloseVideo(sf); |
| 202 |
break; |
| 203 |
default: |
| 204 |
Int3(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/* flips the buffers in a surface. really only useful for video screens |
| 209 |
*/ |
| 210 |
bool ddgr_surf_FlipVideo(ddgr_surface *sf) { |
| 211 |
bool success; |
| 212 |
|
| 213 |
// don't report error if there's no backbuffer. |
| 214 |
if (!(sf->flags & SURFFLAG_BACKBUFFER)) |
| 215 |
return true; |
| 216 |
|
| 217 |
switch (LIB_DATA(subsystem)) { |
| 218 |
case DDGR_GDIX_SUBSYSTEM: |
| 219 |
case DDGR_GDI_SUBSYSTEM: |
| 220 |
success = ddgr_gdi_surf_FlipVideo(sf); |
| 221 |
break; |
| 222 |
case DDGR_DX_SUBSYSTEM: |
| 223 |
success = ddgr_dx_surf_FlipVideo(sf); |
| 224 |
break; |
| 225 |
default: |
| 226 |
Int3(); |
| 227 |
} |
| 228 |
|
| 229 |
if (!success) { |
| 230 |
ddgr_FatalError("Failed to flip surface <%s>", sf->name); |
| 231 |
} |
| 232 |
|
| 233 |
return success; |
| 234 |
} |
| 235 |
|
| 236 |
/* graphic primatives |
| 237 |
*/ |
| 238 |
void ddgr_surf_Clear(ddgr_surface *dsf, ddgr_color col, int l, int t, int w, int h) { |
| 239 |
switch (LIB_DATA(subsystem)) { |
| 240 |
case DDGR_GDIX_SUBSYSTEM: |
| 241 |
case DDGR_GDI_SUBSYSTEM: |
| 242 |
ddgr_gdi_surf_Clear(dsf, col, l, t, w, h); |
| 243 |
break; |
| 244 |
case DDGR_DX_SUBSYSTEM: |
| 245 |
ddgr_dx_surf_Clear(dsf, col, l, t, w, h); |
| 246 |
break; |
| 247 |
default: |
| 248 |
Int3(); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
bool ddgr_surf_Blt(ddgr_surface *dsf, int dx, int dy, ddgr_surface *ssf, int sx, int sy, int sw, int sh) { |
| 253 |
bool success; |
| 254 |
|
| 255 |
switch (LIB_DATA(subsystem)) { |
| 256 |
case DDGR_GDIX_SUBSYSTEM: |
| 257 |
case DDGR_GDI_SUBSYSTEM: |
| 258 |
success = ddgr_gdi_surf_Blt(dsf, dx, dy, ssf, sx, sy, sw, sh); |
| 259 |
break; |
| 260 |
case DDGR_DX_SUBSYSTEM: |
| 261 |
success = ddgr_dx_surf_Blt(dsf, dx, dy, ssf, sx, sy, sw, sh); |
| 262 |
break; |
| 263 |
default: |
| 264 |
Int3(); |
| 265 |
} |
| 266 |
|
| 267 |
if (!success) { |
| 268 |
ddgr_FatalError("Failed to blt surface <%s> to <%s>", ssf->name, dsf->name); |
| 269 |
} |
| 270 |
|
| 271 |
return success; |
| 272 |
} |
| 273 |
|
| 274 |
// returns internal information about an ddgr_surface (only to low level libraries, on par with the ddgr |
| 275 |
// library). |
| 276 |
void ddgr_surf_GetPrivateData(ddgr_surface *sf, bool *ddraw_surf, uint32_t *object_ptr) { |
| 277 |
switch (LIB_DATA(subsystem)) { |
| 278 |
case DDGR_DX_SUBSYSTEM: { |
| 279 |
tDXSurface *bm = (tDXSurface *)sf->obj; |
| 280 |
*ddraw_surf = true; |
| 281 |
*object_ptr = (bm->backbuffer == true) ? (uint32_t)(bm->lpddsback) : (uint32_t)(bm->lpdds); |
| 282 |
break; |
| 283 |
} |
| 284 |
default: |
| 285 |
Int3(); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
// returns aspect ratio of current display. |
| 290 |
float ddgr_GetAspectRatio() { |
| 291 |
float aspect_ratio = (float)((3.0 * GetSystemMetrics(SM_CXSCREEN)) / (4.0 * GetSystemMetrics(SM_CYSCREEN))); |
| 292 |
return aspect_ratio; |
| 293 |
} |
| 294 |
|
| 295 |
unsigned ddgr_GetDDrawObject() { |
| 296 |
switch (LIB_DATA(subsystem)) { |
| 297 |
case DDGR_GDIX_SUBSYSTEM: |
| 298 |
case DDGR_GDI_SUBSYSTEM: |
| 299 |
return (unsigned)(GDI_DATA(lpDD)); |
| 300 |
case DDGR_DX_SUBSYSTEM: |
| 301 |
return (unsigned)(DX_DATA(lpDD)); |
| 302 |
default: |
| 303 |
Int3(); |
| 304 |
} |
| 305 |
|
| 306 |
return 0; |
| 307 |
} |