| 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/2dlib/surface.cpp $ |
| 21 | * $Revision: 7 $ |
| 22 | * $Date: 11/30/98 5:50p $ |
| 23 | * $Author: Jason $ |
| 24 | * |
| 25 | * Master Surface class |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/2dlib/surface.cpp $ |
| 28 | * |
| 29 | * 7 11/30/98 5:50p Jason |
| 30 | * added 4444 bitmap support |
| 31 | * |
| 32 | * 6 4/23/98 6:38p Jason |
| 33 | * made bitmaps use 1555 format |
| 34 | * |
| 35 | * 5 12/19/97 2:26p Jason |
| 36 | * more fixes for 2d/3d intergration |
| 37 | * |
| 38 | * 4 12/19/97 12:32p Samir |
| 39 | * lock now calls renderer functions. |
| 40 | * |
| 41 | * 3 11/11/97 1:24p Samir |
| 42 | * Fixed problem with clearing using the renderer. |
| 43 | * |
| 44 | * 2 10/15/97 5:20p Jason |
| 45 | * did a HUGE overhaul of the bitmap system |
| 46 | * |
| 47 | * 24 6/12/97 6:29p Samir |
| 48 | * DDGR v2.0 Changes in 2d system implemented. |
| 49 | * |
| 50 | * 23 6/06/97 12:18p Samir |
| 51 | * OpenGL HACKS to call renderer for some drawing functions. Must fix. |
| 52 | * |
| 53 | * 22 5/19/97 5:10p Jason |
| 54 | * changes for our new abstracted renderer code |
| 55 | * |
| 56 | * 21 4/30/97 3:15p Jason |
| 57 | * changes to support both 8bit and 16bit rendering in software |
| 58 | * |
| 59 | * 20 3/28/97 3:07p Samir |
| 60 | * Clear function now takes a color |
| 61 | * |
| 62 | * 19 3/27/97 11:11a Samir |
| 63 | * Moved code from ddgr library to 2dlib because it was ANSI compliant and |
| 64 | * should work on all systems |
| 65 | * |
| 66 | * 18 2/28/97 2:51p Samir |
| 67 | * took out __cdecl in atexit function. |
| 68 | * |
| 69 | * 17 2/17/97 2:19p Jason |
| 70 | * made blit function scale instead of clip |
| 71 | * |
| 72 | * 16 2/11/97 3:19p Jason |
| 73 | * added displaying of 8bit palettized graphics on 16bit surfaces |
| 74 | * |
| 75 | * 15 2/04/97 12:49p Samir |
| 76 | * Added lock(x,y) for those special cases. |
| 77 | * |
| 78 | * 14 2/03/97 3:01p Samir |
| 79 | * Used one memory surface global to grSurface for scratchpad blitting. |
| 80 | * |
| 81 | * 13 1/30/97 6:01p Samir |
| 82 | * The partition of memory and os surfaces as well as the change in |
| 83 | * ddgr_surface structure and all related changes. |
| 84 | * |
| 85 | * $NoKeywords: $ |
| 86 | */ |
| 87 | |
| 88 | #include <algorithm> |
| 89 | #include <cstdlib> |
| 90 | #include <cstring> |
| 91 | |
| 92 | #include "bitmap.h" |
| 93 | #include "lib2d.h" |
| 94 | #include "pserror.h" |
| 95 | #include "renderer.h" |
| 96 | |
| 97 | static inline unsigned XLAT_RGB_TO_16(ddgr_color c) { |
| 98 | uint8_t r, g, b; |
| 99 | r = (uint8_t)((c & 0x00ff0000) >> 16); |
| 100 | g = (uint8_t)((c & 0x0000ff00) >> 8); |
| 101 | b = (uint8_t)(c & 0x000000ff); |
| 102 | |
| 103 | return (((r >> 3) << 10) + ((g >> 3) << 5) + (b >> 3)); |
| 104 | } |
| 105 | |
| 106 | ddgr_surface_node *grSurface::surf_list = nullptr; // the surface list |
| 107 | ddgr_surface_node *grSurface::surf_list_cur = nullptr; // current node on list. |
| 108 | ddgr_surface *grSurface::scratch_mem_surf = nullptr; |
| 109 | |
| 110 | void grSurface::init_system() { |
| 111 | // allocate our scratch memory surface used in uniblt |
| 112 | if (!grSurface::scratch_mem_surf) { |
| 113 | grSurface::scratch_mem_surf = new ddgr_surface; |
| 114 | memset(grSurface::scratch_mem_surf, 0, sizeof(ddgr_surface)); |
| 115 | } |
| 116 | |
| 117 | atexit(grSurface::close_system); |
| 118 | } |
| 119 | |
| 120 | void grSurface::close_system() { |
| 121 | // cleanup our scratch memory surface for blt |
| 122 | if (grSurface::scratch_mem_surf) { |
| 123 | if (grSurface::scratch_mem_surf->obj != nullptr) |
| 124 | gr_mem_surf_Destroy(grSurface::scratch_mem_surf); |
| 125 | |
| 126 | delete grSurface::scratch_mem_surf; |
| 127 | grSurface::scratch_mem_surf = nullptr; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // --------------------------------------------------------------------------- |
| 132 | // grSurface constructor and destructor |
| 133 | // --------------------------------------------------------------------------- |
| 134 | |
| 135 | grSurface::grSurface() { |
| 136 | surf_Locked = 0; |
| 137 | m_SurfInit = 0; |
| 138 | memset(&ddsfObj, 0, sizeof(ddgr_surface)); |
| 139 | } |
| 140 | |
| 141 | grSurface::grSurface(int w, int h, int bpp, unsigned type, unsigned flags, const char *name) { |
| 142 | surf_Locked = 0; |
| 143 | m_SurfInit = 0; |
| 144 | |
| 145 | grSurface::create(w, h, bpp, type, flags, name); |
| 146 | } |
| 147 | |
| 148 | grSurface::~grSurface() { grSurface::free(); } |
| 149 | |
| 150 | void grSurface::create(int w, int h, int bpp, unsigned type, unsigned flags, const char *name) { |
| 151 | bool grerr; |
| 152 | |
| 153 | if (m_SurfInit) |
| 154 | grSurface::free(); |
| 155 | |
| 156 | // create new surface: set up surface structure |
| 157 | memset(&ddsfObj, 0, sizeof(ddgr_surface)); |
| 158 | ddsfObj.w = w; |
| 159 | ddsfObj.h = h; |
| 160 | ddsfObj.bpp = bpp; |
| 161 | ddsfObj.type = (uint16_t)type; |
| 162 | ddsfObj.flags = (uint16_t)flags; |
| 163 | |
| 164 | if (name) |
| 165 | strncpy(ddsfObj.name, name, 15); |
| 166 | |
| 167 | switch (type) { |
| 168 | case SURFTYPE_VIDEOSCREEN: |
| 169 | grerr = ddgr_surf_InitVideo(&ddsfObj); |
| 170 | break; |
| 171 | case SURFTYPE_GENERIC: |
| 172 | grerr = ddgr_surf_Create(&ddsfObj); |
| 173 | break; |
| 174 | case SURFTYPE_MEMORY: |
| 175 | grerr = (bool)gr_mem_surf_Create(&ddsfObj); |
| 176 | break; |
| 177 | default: |
| 178 | Int3(); // Invalid!! |
| 179 | } |
| 180 | if (!grerr) |
| 181 | Int3(); |
| 182 | |
| 183 | add_to_list(&ddsfObj); |
| 184 | |
| 185 | m_SurfInit = 1; |
| 186 | } |
| 187 | |
| 188 | void grSurface::free() { |
| 189 | /* Freeing surface is same regardless of whether it was loaded from a bitmap handle or |
| 190 | created ourselves |
| 191 | */ |
| 192 | if (!m_SurfInit) |
| 193 | return; |
| 194 | ASSERT(!surf_Locked); |
| 195 | |
| 196 | switch (ddsfObj.type) { |
| 197 | case SURFTYPE_VIDEOSCREEN: |
| 198 | ddgr_surf_CloseVideo(&ddsfObj); |
| 199 | break; |
| 200 | case SURFTYPE_GENERIC: |
| 201 | ddgr_surf_Destroy(&ddsfObj); |
| 202 | break; |
| 203 | case SURFTYPE_MEMORY: |
| 204 | gr_mem_surf_Destroy(&ddsfObj); |
| 205 | break; |
| 206 | default: |
| 207 | Int3(); // This really shouldn't happen. bad type! |
| 208 | } |
| 209 | |
| 210 | remove_from_list(&ddsfObj); |
| 211 | |
| 212 | m_SurfInit = 0; |
| 213 | } |
| 214 | |
| 215 | void grSurface::load(int handle) { |
| 216 | /* just load the bitmap from the handle info */ |
| 217 | ASSERT(m_SurfInit); |
| 218 | |
| 219 | grSurface::load((char *)bm_data(handle, 0), bm_w(handle, 0), bm_h(handle, 0), bm_bpp(handle), bm_format(handle)); |
| 220 | } |
| 221 | |
| 222 | void grSurface::load(char *data, int w, int h, int bpp, int format) { |
| 223 | // copy defined bitmap given as arguments to this surface, with correct bpp translation. |
| 224 | int rowsize; |
| 225 | ASSERT(m_SurfInit); |
| 226 | |
| 227 | /* perform lock, then copy data to this surface, then unlock */ |
| 228 | grSurface::lock(&rowsize); |
| 229 | |
| 230 | switch (ddsfObj.bpp) { |
| 231 | case BPP_16: |
| 232 | if (bpp == BPP_16) |
| 233 | xlat16_16(data, w, h, format); |
| 234 | if (bpp == BPP_24) |
| 235 | xlat24_16(data, w, h); |
| 236 | if (bpp == BPP_32) |
| 237 | xlat32_16(data, w, h); |
| 238 | break; |
| 239 | |
| 240 | case BPP_24: |
| 241 | if (bpp == BPP_16) |
| 242 | xlat16_24(data, w, h); |
| 243 | break; |
| 244 | |
| 245 | default: |
| 246 | Int3(); // NOT SUPPORTED YET |
| 247 | } |
| 248 | |
| 249 | grSurface::unlock(); |
| 250 | } |
| 251 | |
| 252 | // loads 8bit palettized data onto this surface. |
| 253 | void grSurface::load(int handle, char *pal) { |
| 254 | ASSERT(m_SurfInit); |
| 255 | |
| 256 | grSurface::load((char *)bm_data(handle, 0), bm_w(handle, 0), bm_h(handle, 0), pal); |
| 257 | } |
| 258 | |
| 259 | // loads 8bit palettized data onto this surface. |
| 260 | void grSurface::load(char *data, int w, int h, char *pal) { |
| 261 | int rowsize; |
| 262 | ASSERT(m_SurfInit); |
| 263 | |
| 264 | /* perform lock, then copy data to this surface, then unlock */ |
| 265 | grSurface::lock(&rowsize); |
| 266 | |
| 267 | switch (ddsfObj.bpp) { |
| 268 | case BPP_16: |
| 269 | // TODO: This is probably software renderer only? |
| 270 | xlat16_16(data, w, h); |
| 271 | default: |
| 272 | Int3(); // NOT SUPPORTED YET |
| 273 | } |
| 274 | |
| 275 | grSurface::unlock(); |
| 276 | } |
| 277 | |
| 278 | // --------------------------------------------------------------------------- |
| 279 | // blt functions:: clear |
| 280 | // --------------------------------------------------------------------------- |
| 281 | |
| 282 | void grSurface::clear(ddgr_color col) { grSurface::clear(0, 0, ddsfObj.w, ddsfObj.h, col); } |
| 283 | |
| 284 | void grSurface::clear(int l, int t, int w, int h, ddgr_color col) { |
| 285 | ASSERT(m_SurfInit); |
| 286 | ASSERT(!surf_Locked); |
| 287 | |
| 288 | switch (ddsfObj.type) { |
| 289 | case SURFTYPE_VIDEOSCREEN: |
| 290 | case SURFTYPE_GENERIC: |
| 291 | // HACK!!!!!!! I MUST CHANGE THIS |
| 292 | // --------------------------------------------------------------------------- |
| 293 | if (ddsfObj.flags & SURFFLAG_RENDERER) { |
| 294 | rend_FillRect(col, l, t, l + w, t + h); |
| 295 | return; |
| 296 | } |
| 297 | // --------------------------------------------------------------------------- |
| 298 | ddgr_surf_Clear(&ddsfObj, col, l, t, w, h); |
| 299 | break; |
| 300 | case SURFTYPE_MEMORY: |
| 301 | gr_mem_surf_Clear(&ddsfObj, col, l, t, w, h); |
| 302 | break; |
| 303 | default: |
| 304 | Int3(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // --------------------------------------------------------------------------- |
| 309 | // blt functions: |
| 310 | // these functions must support memory->os, os->memory, os->os, mem->mem blts. |
| 311 | // note that (os) blts and (video) blts use the os library. |
| 312 | // --------------------------------------------------------------------------- |
| 313 | |
| 314 | void grSurface::uniblt(int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh) { |
| 315 | int rowsize; |
| 316 | |
| 317 | ASSERT(m_SurfInit); |
| 318 | |
| 319 | // mode 11b = dd_to_dd, 01b = mem_to_dd, 10b = dd_to_mem, 00b = mem_to_mem |
| 320 | int mode = 0; |
| 321 | ddgr_surface *dsfs = &ddsfObj, *ssfs = &ssf->ddsfObj; |
| 322 | bool err; |
| 323 | |
| 324 | // setup blitting mode |
| 325 | if (ssfs->type == SURFTYPE_GENERIC || ssfs->type == SURFTYPE_VIDEOSCREEN) |
| 326 | mode = mode | 0x1; |
| 327 | if (dsfs->type == SURFTYPE_GENERIC || dsfs->type == SURFTYPE_VIDEOSCREEN) |
| 328 | mode = mode | 0x2; |
| 329 | |
| 330 | /* case 1: os->os |
| 331 | just invoke os blitter |
| 332 | */ |
| 333 | if (mode == 0x3) { |
| 334 | ASSERT(!surf_Locked); |
| 335 | err = ddgr_surf_Blt(dsfs, dx, dy, ssfs, sx, sy, sw, sh); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | /* case 2: mem->mem |
| 340 | just lock both surfaces and do a memory blt. |
| 341 | */ |
| 342 | if (mode == 0) { |
| 343 | err = gr_mem_surf_Blt(dsfs, dx, dy, ssfs, sx, sy, sw, sh); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | /* case 3: os->mem |
| 348 | This requires locking both surfaces and performing a memory blt. |
| 349 | This also means tricking the memory blitter into thinking that the OS surface |
| 350 | is a memory surface |
| 351 | */ |
| 352 | if (mode == 0x1) { |
| 353 | char *data = ssf->lock(&rowsize); |
| 354 | |
| 355 | grSurface::scratch_mem_surf->w = ssfs->w; |
| 356 | grSurface::scratch_mem_surf->h = ssfs->h; |
| 357 | grSurface::scratch_mem_surf->bpp = ssfs->bpp; |
| 358 | grSurface::scratch_mem_surf->type = SURFTYPE_MEMORY; |
| 359 | grSurface::scratch_mem_surf->flags = ssfs->flags; |
| 360 | |
| 361 | gr_mem_surf_Init(grSurface::scratch_mem_surf, data, rowsize); |
| 362 | err = gr_mem_surf_Blt(dsfs, dx, dy, grSurface::scratch_mem_surf, sx, sy, sw, sh); |
| 363 | |
| 364 | ssf->unlock(); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | /* case 4: mem->os |
| 369 | This requires locking both surfaces and performing a memory blt. |
| 370 | This also means tricking the memory blitter into thinking that the OS surface |
| 371 | is a memory surface |
| 372 | */ |
| 373 | if (mode == 0x2) { |
| 374 | grSurface::lock(&rowsize); |
| 375 | |
| 376 | grSurface::scratch_mem_surf->w = dsfs->w; |
| 377 | grSurface::scratch_mem_surf->h = dsfs->h; |
| 378 | grSurface::scratch_mem_surf->bpp = dsfs->bpp; |
| 379 | grSurface::scratch_mem_surf->type = SURFTYPE_MEMORY; |
| 380 | grSurface::scratch_mem_surf->flags = dsfs->flags; |
| 381 | |
| 382 | gr_mem_surf_Init(grSurface::scratch_mem_surf, m_DataPtr, m_DataRowsize); |
| 383 | err = gr_mem_surf_Blt(grSurface::scratch_mem_surf, dx, dy, ssfs, sx, sy, sw, sh); |
| 384 | |
| 385 | grSurface::unlock(); |
| 386 | return; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void grSurface::blt(int dx, int dy, grSurface *ssf) { |
| 391 | ASSERT(m_SurfInit); |
| 392 | grSurface::uniblt(dx, dy, ssf, 0, 0, ssf->width(), ssf->height()); |
| 393 | } |
| 394 | |
| 395 | void grSurface::blt(int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh) { |
| 396 | ASSERT(m_SurfInit); |
| 397 | grSurface::uniblt(dx, dy, ssf, sx, sy, sw, sh); |
| 398 | } |
| 399 | |
| 400 | /* Helper functions |
| 401 | replace_color, simply replaces one color in the surface with another |
| 402 | */ |
| 403 | void grSurface::replace_color(ddgr_color sc, ddgr_color dc) { |
| 404 | int rowsize; |
| 405 | ASSERT(m_SurfInit); |
| 406 | |
| 407 | grSurface::lock(&rowsize); |
| 408 | |
| 409 | switch (ddsfObj.bpp) { |
| 410 | case BPP_16: { |
| 411 | uint16_t *data = (uint16_t *)m_DataPtr; |
| 412 | int rowsize_w = m_DataRowsize / 2; |
| 413 | uint16_t scc = XLAT_RGB_TO_16(sc), dcc = XLAT_RGB_TO_16(dc); |
| 414 | |
| 415 | for (int y = 0; y < ddsfObj.h; y++) { |
| 416 | for (int x = 0; x < ddsfObj.w; x++) |
| 417 | if (data[x] == scc) |
| 418 | data[x] = dcc; |
| 419 | |
| 420 | data += rowsize_w; |
| 421 | } |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | case BPP_32: |
| 426 | default: |
| 427 | Int3(); |
| 428 | } |
| 429 | |
| 430 | grSurface::unlock(); |
| 431 | } |
| 432 | |
| 433 | // --------------------------------------------------------------------------- |
| 434 | // lock and unlock functions |
| 435 | // these handle memory and os locks |
| 436 | // --------------------------------------------------------------------------- |
| 437 | |
| 438 | char *grSurface::lock(int *rowsize) { |
| 439 | bool grerr; |
| 440 | |
| 441 | ASSERT(m_SurfInit); |
| 442 | |
| 443 | switch (ddsfObj.type) { |
| 444 | case SURFTYPE_VIDEOSCREEN: |
| 445 | case SURFTYPE_GENERIC: |
| 446 | grerr = ddgr_surf_Lock(&ddsfObj, (void **)&m_OrigDataPtr, &m_DataRowsize); |
| 447 | break; |
| 448 | case SURFTYPE_MEMORY: |
| 449 | grerr = gr_mem_surf_Lock(&ddsfObj, (void **)&m_OrigDataPtr, &m_DataRowsize); |
| 450 | break; |
| 451 | default: |
| 452 | Int3(); // invalid type!! |
| 453 | } |
| 454 | |
| 455 | m_DataPtr = m_OrigDataPtr; |
| 456 | *rowsize = m_DataRowsize; |
| 457 | |
| 458 | if (!grerr) |
| 459 | return nullptr; |
| 460 | |
| 461 | surf_Locked++; // increment lock counter for this surface |
| 462 | |
| 463 | rend_SetSoftwareParameters(ddgr_GetAspectRatio(), ddsfObj.w, ddsfObj.h, m_DataRowsize, (uint8_t *)m_OrigDataPtr); |
| 464 | |
| 465 | return m_DataPtr; |
| 466 | } |
| 467 | |
| 468 | /* do lock based off of x,y offsets */ |
| 469 | char *grSurface::lock(int x, int y, int *rowsize) { |
| 470 | bool grerr = false; |
| 471 | |
| 472 | ASSERT(m_SurfInit); |
| 473 | |
| 474 | switch (ddsfObj.type) { |
| 475 | case SURFTYPE_VIDEOSCREEN: |
| 476 | case SURFTYPE_GENERIC: |
| 477 | grerr = ddgr_surf_Lock(&ddsfObj, (void **)&m_OrigDataPtr, &m_DataRowsize); |
| 478 | break; |
| 479 | case SURFTYPE_MEMORY: |
| 480 | grerr = gr_mem_surf_Lock(&ddsfObj, (void **)&m_OrigDataPtr, &m_DataRowsize); |
| 481 | break; |
| 482 | default: |
| 483 | Int3(); // invalid type!! |
| 484 | } |
| 485 | |
| 486 | *rowsize = m_DataRowsize; |
| 487 | if (!grerr) |
| 488 | return nullptr; |
| 489 | |
| 490 | /* calculate the adjusted data pointer based off of x,y,bpp */ |
| 491 | char *data = m_OrigDataPtr; |
| 492 | |
| 493 | data += (y * m_DataRowsize); |
| 494 | |
| 495 | switch (ddsfObj.bpp) { |
| 496 | case BPP_16: |
| 497 | data += (x * 2); |
| 498 | break; |
| 499 | case BPP_32: |
| 500 | data += (x * 4); |
| 501 | break; |
| 502 | default: |
| 503 | Int3(); |
| 504 | } |
| 505 | |
| 506 | m_DataPtr = data; |
| 507 | |
| 508 | surf_Locked++; // increment lock counter for this surface |
| 509 | |
| 510 | rend_SetSoftwareParameters(ddgr_GetAspectRatio(), ddsfObj.w, ddsfObj.h, m_DataRowsize, (uint8_t *)m_OrigDataPtr); |
| 511 | |
| 512 | return m_DataPtr; |
| 513 | } |
| 514 | |
| 515 | void grSurface::unlock() { |
| 516 | bool grerr; |
| 517 | |
| 518 | ASSERT(m_SurfInit); |
| 519 | ASSERT(surf_Locked); |
| 520 | |
| 521 | switch (ddsfObj.type) { |
| 522 | case SURFTYPE_VIDEOSCREEN: |
| 523 | case SURFTYPE_GENERIC: |
| 524 | grerr = ddgr_surf_Unlock(&ddsfObj, m_OrigDataPtr); |
| 525 | break; |
| 526 | case SURFTYPE_MEMORY: |
| 527 | grerr = gr_mem_surf_Unlock(&ddsfObj, m_OrigDataPtr); |
| 528 | break; |
| 529 | default: |
| 530 | Int3(); // invalid type!! |
| 531 | } |
| 532 | |
| 533 | surf_Locked--; |
| 534 | } |
| 535 | |
| 536 | // --------------------------------------------------------------------------- |
| 537 | // miscellaneous |
| 538 | // --------------------------------------------------------------------------- |
| 539 | |
| 540 | void grSurface::attach_to_window(unsigned handle) { |
| 541 | ASSERT(m_SurfInit); |
| 542 | ddgr_surf_AttachHandle(&ddsfObj, handle); |
| 543 | } |
| 544 | |
| 545 | // Private functions |
| 546 | // conversion_xlat |
| 547 | // --------------------------------------------------------------------------- |
| 548 | |
| 549 | void grSurface::xlat8_16(char *data, int w, int h, char *pal) { |
| 550 | /* copy from 8bit source bitmap to destination surface just created and |
| 551 | locked |
| 552 | */ |
| 553 | uint16_t *dptr; |
| 554 | char *sptr; |
| 555 | int row, col; |
| 556 | int rowsize_w; |
| 557 | int height, width; |
| 558 | uint8_t *upal = (uint8_t *)pal; |
| 559 | |
| 560 | dptr = (uint16_t *)m_DataPtr; |
| 561 | sptr = (char *)data; |
| 562 | rowsize_w = m_DataRowsize / 2; |
| 563 | |
| 564 | height = std::min(h, ddsfObj.h); |
| 565 | width = std::min(w, ddsfObj.w); |
| 566 | |
| 567 | for (row = 0; row < height; row++) { |
| 568 | for (col = 0; col < width; col++) { |
| 569 | uint8_t spix = sptr[col]; |
| 570 | if (spix != 0) { |
| 571 | int r = upal[spix * 3] >> 3; |
| 572 | int g = upal[spix * 3 + 1] >> 2; |
| 573 | int b = upal[spix * 3 + 2] >> 3; |
| 574 | uint16_t destpix = (r << 11) | (g << 5) | b; |
| 575 | |
| 576 | dptr[col] = destpix; |
| 577 | } else |
| 578 | dptr[col] = 0; |
| 579 | } |
| 580 | sptr += 256; |
| 581 | dptr += rowsize_w; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | void grSurface::xlat16_16(char *data, int w, int h, int format) { |
| 586 | /* 16-bit copy from source bitmap to destination surface just created and |
| 587 | locked |
| 588 | This function performs scaling if the source width and height don't match |
| 589 | that of the destinations - JL |
| 590 | */ |
| 591 | uint16_t *sptr, *dptr; |
| 592 | int row, col; |
| 593 | int rowsize_w; |
| 594 | int height, width; |
| 595 | fix xstep = IntToFix(w) / ddsfObj.w; |
| 596 | fix ystep = IntToFix(h) / ddsfObj.h; |
| 597 | fix fu = 0, fv = 0; |
| 598 | |
| 599 | dptr = (uint16_t *)m_DataPtr; |
| 600 | sptr = (uint16_t *)data; |
| 601 | rowsize_w = m_DataRowsize / 2; |
| 602 | |
| 603 | height = ddsfObj.h; |
| 604 | width = ddsfObj.w; |
| 605 | |
| 606 | // height = SET_MIN(h, ddsfObj.h); |
| 607 | // width = SET_MIN(w, ddsfObj.w); |
| 608 | |
| 609 | for (row = 0; row < height; row++, fv += ystep, fu = 0) { |
| 610 | for (col = 0; col < width; col++, fu += xstep) { |
| 611 | int newpix; |
| 612 | |
| 613 | if (format == BITMAP_FORMAT_1555) |
| 614 | newpix = sptr[(FixToInt(fv) * w) + FixToInt(fu)]; |
| 615 | else { |
| 616 | int temp_pix = sptr[(FixToInt(fv) * w) + FixToInt(fu)]; |
| 617 | int r = (temp_pix >> 8) & 0xf; |
| 618 | int g = (temp_pix >> 4) & 0xf; |
| 619 | int b = (temp_pix) & 0xf; |
| 620 | |
| 621 | r <<= 4; |
| 622 | g <<= 4; |
| 623 | b <<= 4; |
| 624 | |
| 625 | newpix = OPAQUE_FLAG | GR_RGB16(r, g, b); |
| 626 | } |
| 627 | |
| 628 | dptr[col] = newpix; |
| 629 | } |
| 630 | |
| 631 | dptr += rowsize_w; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void grSurface::xlat16_24(char *data, int w, int h) { |
| 636 | uint16_t *sptr; |
| 637 | char *dptr; |
| 638 | int scol, dcol, row; |
| 639 | int height, width; |
| 640 | |
| 641 | dptr = (char *)m_DataPtr; |
| 642 | sptr = (uint16_t *)data; |
| 643 | height = std::min(h, ddsfObj.h); |
| 644 | width = std::min(w, ddsfObj.w); |
| 645 | |
| 646 | for (row = 0; row < height; row++) { |
| 647 | dcol = 0; |
| 648 | for (scol = 0; scol < width; scol++) { |
| 649 | uint16_t pix; |
| 650 | ddgr_color new_color; |
| 651 | char r, g, b; |
| 652 | pix = sptr[scol]; |
| 653 | |
| 654 | new_color = GR_16_TO_COLOR(pix); |
| 655 | |
| 656 | r = GR_COLOR_RED(new_color); |
| 657 | g = GR_COLOR_GREEN(new_color); |
| 658 | b = GR_COLOR_BLUE(new_color); |
| 659 | |
| 660 | dptr[dcol++] = r; |
| 661 | dptr[dcol++] = g; |
| 662 | dptr[dcol++] = b; |
| 663 | } |
| 664 | sptr += w; |
| 665 | dptr += m_DataRowsize; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | void grSurface::xlat24_16(char *data, int w, int h) { |
| 670 | char *sptr; |
| 671 | uint16_t *dptr; |
| 672 | int scol, dcol, row; |
| 673 | int rowsize_w, height, width; |
| 674 | |
| 675 | dptr = (uint16_t *)m_DataPtr; |
| 676 | sptr = (char *)data; |
| 677 | rowsize_w = m_DataRowsize / 2; |
| 678 | height = std::min(h, ddsfObj.h); |
| 679 | width = std::min(w, ddsfObj.w); |
| 680 | |
| 681 | for (row = 0; row < height; row++) { |
| 682 | scol = 0; |
| 683 | for (dcol = 0; dcol < width; dcol++) { |
| 684 | uint16_t pix; |
| 685 | char r, g, b; |
| 686 | r = sptr[scol++]; |
| 687 | g = sptr[scol++]; |
| 688 | b = sptr[scol++]; |
| 689 | pix = ((uint16_t)(r >> 3) << 11) + ((uint16_t)(g >> 2) << 5) + ((uint16_t)(b >> 3)); |
| 690 | dptr[dcol] = pix; |
| 691 | } |
| 692 | sptr += (w * 3); |
| 693 | dptr += m_DataRowsize; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | void grSurface::xlat32_16(char *data, int w, int h) { |
| 698 | unsigned *sptr; |
| 699 | uint16_t *dptr; |
| 700 | int col, row; |
| 701 | int rowsize_w, height, width; |
| 702 | |
| 703 | ASSERT((w % 4) == 0); |
| 704 | |
| 705 | dptr = (uint16_t *)m_DataPtr; |
| 706 | sptr = (unsigned *)data; |
| 707 | rowsize_w = m_DataRowsize / 2; |
| 708 | height = std::min(h, ddsfObj.h); |
| 709 | width = std::min(w, ddsfObj.w); |
| 710 | |
| 711 | for (row = 0; row < height; row++) { |
| 712 | for (col = 0; col < width; col++) { |
| 713 | unsigned pix; |
| 714 | uint16_t spix; |
| 715 | |
| 716 | pix = sptr[col]; |
| 717 | spix = (uint16_t)(pix & 0x000000f8) >> 3; |
| 718 | spix |= (uint16_t)(pix & 0x0000fc00) >> 5; |
| 719 | spix |= (uint16_t)(pix & 0x00f80000) >> 8; |
| 720 | dptr[col] = spix; |
| 721 | } |
| 722 | sptr += w; |
| 723 | dptr += rowsize_w; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | //----------------------------------------------------------------------------- |
| 728 | // surface list functions |
| 729 | |
| 730 | void grSurface::add_to_list(ddgr_surface *sf) { |
| 731 | ddgr_surface_node *node; |
| 732 | |
| 733 | ASSERT(sf != nullptr); |
| 734 | |
| 735 | if (!grSurface::surf_list_cur) { |
| 736 | // first surface on list |
| 737 | node = new ddgr_surface_node; |
| 738 | node->sf = sf; |
| 739 | node->prev = nullptr; |
| 740 | grSurface::surf_list = grSurface::surf_list_cur = node; |
| 741 | } else { |
| 742 | // next surface on list. |
| 743 | node = new ddgr_surface_node; |
| 744 | node->sf = sf; |
| 745 | node->prev = grSurface::surf_list_cur; |
| 746 | grSurface::surf_list_cur = node; |
| 747 | } |
| 748 | |
| 749 | // mprintf(0, "Allocated surface %s [%d].\n", sf->name, DDData.surfs_alloc); |
| 750 | } |
| 751 | |
| 752 | void grSurface::remove_from_list(ddgr_surface *sf) { |
| 753 | ddgr_surface_node *node, *next_node; |
| 754 | |
| 755 | ASSERT(grSurface::surf_list_cur); |
| 756 | ASSERT(sf != nullptr); |
| 757 | |
| 758 | // node is current node. next node is node after it on the list. since we are |
| 759 | // going from end to beginning, this may confuse you. |
| 760 | node = grSurface::surf_list_cur; |
| 761 | next_node = nullptr; // grSurface::surf_list_cur should be at end of list, so no next node |
| 762 | |
| 763 | while (node) { |
| 764 | if (node->sf == sf) { |
| 765 | if (next_node) { // delete from within list. |
| 766 | next_node->prev = node->prev; |
| 767 | } else { // delete from end of list. set list tail to deleted node prev. |
| 768 | grSurface::surf_list_cur = node->prev; |
| 769 | } |
| 770 | delete node; |
| 771 | |
| 772 | node = nullptr; // break out of loop |
| 773 | } else { |
| 774 | // go down one node. |
| 775 | next_node = node; |
| 776 | node = node->prev; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | // mprintf(0, "Freed surface %s [%d].\n", sf->name, DDData.surfs_alloc); |
| 781 | } |