File: surface.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (781 lines) | stat: -rw-r--r-- 20,443 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
* Descent 3 
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/Main/2dlib/surface.cpp $
 * $Revision: 7 $
 * $Date: 11/30/98 5:50p $
 * $Author: Jason $
 *
 * Master Surface class
 *
 * $Log: /DescentIII/Main/2dlib/surface.cpp $
 *
 * 7     11/30/98 5:50p Jason
 * added 4444 bitmap support
 *
 * 6     4/23/98 6:38p Jason
 * made bitmaps use 1555 format
 *
 * 5     12/19/97 2:26p Jason
 * more fixes for 2d/3d intergration
 *
 * 4     12/19/97 12:32p Samir
 * lock now calls renderer functions.
 *
 * 3     11/11/97 1:24p Samir
 * Fixed problem with clearing using the renderer.
 *
 * 2     10/15/97 5:20p Jason
 * did a HUGE overhaul of the bitmap system
 *
 * 24    6/12/97 6:29p Samir
 * DDGR v2.0 Changes in 2d system implemented.
 *
 * 23    6/06/97 12:18p Samir
 * OpenGL HACKS to call renderer for some drawing functions.  Must fix.
 *
 * 22    5/19/97 5:10p Jason
 * changes for our new abstracted renderer code
 *
 * 21    4/30/97 3:15p Jason
 * changes to support both 8bit and 16bit rendering in software
 *
 * 20    3/28/97 3:07p Samir
 * Clear function now takes a color
 *
 * 19    3/27/97 11:11a Samir
 * Moved code from ddgr library to 2dlib because it was ANSI compliant and
 * should work on all systems
 *
 * 18    2/28/97 2:51p Samir
 * took out __cdecl in atexit function.
 *
 * 17    2/17/97 2:19p Jason
 * made blit function scale instead of clip
 *
 * 16    2/11/97 3:19p Jason
 * added displaying of 8bit palettized graphics on 16bit surfaces
 *
 * 15    2/04/97 12:49p Samir
 * Added lock(x,y) for those special cases.
 *
 * 14    2/03/97 3:01p Samir
 * Used one memory surface global to grSurface for scratchpad blitting.
 *
 * 13    1/30/97 6:01p Samir
 * The partition of memory and os surfaces as well as the change in
 * ddgr_surface structure and all related changes.
 *
 * $NoKeywords: $
 */

#include <algorithm>
#include <cstdlib>
#include <cstring>

#include "bitmap.h"
#include "lib2d.h"
#include "pserror.h"
#include "renderer.h"

static inline unsigned XLAT_RGB_TO_16(ddgr_color c) {
  uint8_t r, g, b;
  r = (uint8_t)((c & 0x00ff0000) >> 16);
  g = (uint8_t)((c & 0x0000ff00) >> 8);
  b = (uint8_t)(c & 0x000000ff);

  return (((r >> 3) << 10) + ((g >> 3) << 5) + (b >> 3));
}

ddgr_surface_node *grSurface::surf_list = nullptr;     // the surface list
ddgr_surface_node *grSurface::surf_list_cur = nullptr; // current node on list.
ddgr_surface *grSurface::scratch_mem_surf = nullptr;

void grSurface::init_system() {
  //	allocate our scratch memory surface used in uniblt
  if (!grSurface::scratch_mem_surf) {
    grSurface::scratch_mem_surf = new ddgr_surface;
    memset(grSurface::scratch_mem_surf, 0, sizeof(ddgr_surface));
  }

  atexit(grSurface::close_system);
}

void grSurface::close_system() {
  //	cleanup our scratch memory surface for blt
  if (grSurface::scratch_mem_surf) {
    if (grSurface::scratch_mem_surf->obj != nullptr)
      gr_mem_surf_Destroy(grSurface::scratch_mem_surf);

    delete grSurface::scratch_mem_surf;
    grSurface::scratch_mem_surf = nullptr;
  }
}

//	---------------------------------------------------------------------------
//	grSurface constructor and destructor
//	---------------------------------------------------------------------------

grSurface::grSurface() {
  surf_Locked = 0;
  m_SurfInit = 0;
  memset(&ddsfObj, 0, sizeof(ddgr_surface));
}

grSurface::grSurface(int w, int h, int bpp, unsigned type, unsigned flags, const char *name) {
  surf_Locked = 0;
  m_SurfInit = 0;

  grSurface::create(w, h, bpp, type, flags, name);
}

grSurface::~grSurface() { grSurface::free(); }

void grSurface::create(int w, int h, int bpp, unsigned type, unsigned flags, const char *name) {
  bool grerr;

  if (m_SurfInit)
    grSurface::free();

  //	create new surface: set up surface structure
  memset(&ddsfObj, 0, sizeof(ddgr_surface));
  ddsfObj.w = w;
  ddsfObj.h = h;
  ddsfObj.bpp = bpp;
  ddsfObj.type = (uint16_t)type;
  ddsfObj.flags = (uint16_t)flags;

  if (name)
    strncpy(ddsfObj.name, name, 15);

  switch (type) {
  case SURFTYPE_VIDEOSCREEN:
    grerr = ddgr_surf_InitVideo(&ddsfObj);
    break;
  case SURFTYPE_GENERIC:
    grerr = ddgr_surf_Create(&ddsfObj);
    break;
  case SURFTYPE_MEMORY:
    grerr = (bool)gr_mem_surf_Create(&ddsfObj);
    break;
  default:
    Int3(); // Invalid!!
  }
  if (!grerr)
    Int3();

  add_to_list(&ddsfObj);

  m_SurfInit = 1;
}

void grSurface::free() {
  /*	Freeing surface is same regardless of whether it was loaded from a bitmap handle or
          created ourselves
  */
  if (!m_SurfInit)
    return;
  ASSERT(!surf_Locked);

  switch (ddsfObj.type) {
  case SURFTYPE_VIDEOSCREEN:
    ddgr_surf_CloseVideo(&ddsfObj);
    break;
  case SURFTYPE_GENERIC:
    ddgr_surf_Destroy(&ddsfObj);
    break;
  case SURFTYPE_MEMORY:
    gr_mem_surf_Destroy(&ddsfObj);
    break;
  default:
    Int3(); // This really shouldn't happen.  bad type!
  }

  remove_from_list(&ddsfObj);

  m_SurfInit = 0;
}

void grSurface::load(int handle) {
  /*	just load the bitmap from the handle info */
  ASSERT(m_SurfInit);

  grSurface::load((char *)bm_data(handle, 0), bm_w(handle, 0), bm_h(handle, 0), bm_bpp(handle), bm_format(handle));
}

void grSurface::load(char *data, int w, int h, int bpp, int format) {
  //	copy defined bitmap given as arguments to this surface, with correct bpp translation.
  int rowsize;
  ASSERT(m_SurfInit);

  /*	perform lock, then copy data to this surface, then unlock */
  grSurface::lock(&rowsize);

  switch (ddsfObj.bpp) {
  case BPP_16:
    if (bpp == BPP_16)
      xlat16_16(data, w, h, format);
    if (bpp == BPP_24)
      xlat24_16(data, w, h);
    if (bpp == BPP_32)
      xlat32_16(data, w, h);
    break;

  case BPP_24:
    if (bpp == BPP_16)
      xlat16_24(data, w, h);
    break;

  default:
    Int3(); // NOT SUPPORTED YET
  }

  grSurface::unlock();
}

//	loads 8bit palettized data onto this surface.
void grSurface::load(int handle, char *pal) {
  ASSERT(m_SurfInit);

  grSurface::load((char *)bm_data(handle, 0), bm_w(handle, 0), bm_h(handle, 0), pal);
}

//	loads 8bit palettized data onto this surface.
void grSurface::load(char *data, int w, int h, char *pal) {
  int rowsize;
  ASSERT(m_SurfInit);

  /*	perform lock, then copy data to this surface, then unlock */
  grSurface::lock(&rowsize);

  switch (ddsfObj.bpp) {
  case BPP_16:
    // TODO: This is probably software renderer only?
    xlat16_16(data, w, h);
  default:
    Int3(); // NOT SUPPORTED YET
  }

  grSurface::unlock();
}

//	---------------------------------------------------------------------------
//	blt functions:: clear
//	---------------------------------------------------------------------------

void grSurface::clear(ddgr_color col) { grSurface::clear(0, 0, ddsfObj.w, ddsfObj.h, col); }

void grSurface::clear(int l, int t, int w, int h, ddgr_color col) {
  ASSERT(m_SurfInit);
  ASSERT(!surf_Locked);

  switch (ddsfObj.type) {
  case SURFTYPE_VIDEOSCREEN:
  case SURFTYPE_GENERIC:
    //	HACK!!!!!!!  I MUST CHANGE THIS
    //	---------------------------------------------------------------------------
    if (ddsfObj.flags & SURFFLAG_RENDERER) {
      rend_FillRect(col, l, t, l + w, t + h);
      return;
    }
    //	---------------------------------------------------------------------------
    ddgr_surf_Clear(&ddsfObj, col, l, t, w, h);
    break;
  case SURFTYPE_MEMORY:
    gr_mem_surf_Clear(&ddsfObj, col, l, t, w, h);
    break;
  default:
    Int3();
  }
}

//	---------------------------------------------------------------------------
//	blt functions:
//		these functions must support memory->os, os->memory, os->os, mem->mem blts.
//		note that (os) blts and (video) blts use the os library.
//	---------------------------------------------------------------------------

void grSurface::uniblt(int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh) {
  int rowsize;

  ASSERT(m_SurfInit);

  //	mode 11b = dd_to_dd, 01b = mem_to_dd, 10b = dd_to_mem, 00b = mem_to_mem
  int mode = 0;
  ddgr_surface *dsfs = &ddsfObj, *ssfs = &ssf->ddsfObj;
  bool err;

  //	setup blitting mode
  if (ssfs->type == SURFTYPE_GENERIC || ssfs->type == SURFTYPE_VIDEOSCREEN)
    mode = mode | 0x1;
  if (dsfs->type == SURFTYPE_GENERIC || dsfs->type == SURFTYPE_VIDEOSCREEN)
    mode = mode | 0x2;

  /*	case 1: os->os
                  just invoke os blitter
  */
  if (mode == 0x3) {
    ASSERT(!surf_Locked);
    err = ddgr_surf_Blt(dsfs, dx, dy, ssfs, sx, sy, sw, sh);
    return;
  }

  /*	case 2: mem->mem
                  just lock both surfaces and do a memory blt.
  */
  if (mode == 0) {
    err = gr_mem_surf_Blt(dsfs, dx, dy, ssfs, sx, sy, sw, sh);
    return;
  }

  /*	case 3: os->mem
                  This requires locking both surfaces and performing a memory blt.
                  This also means tricking the memory blitter into thinking that the OS surface
                  is a memory surface
  */
  if (mode == 0x1) {
    char *data = ssf->lock(&rowsize);

    grSurface::scratch_mem_surf->w = ssfs->w;
    grSurface::scratch_mem_surf->h = ssfs->h;
    grSurface::scratch_mem_surf->bpp = ssfs->bpp;
    grSurface::scratch_mem_surf->type = SURFTYPE_MEMORY;
    grSurface::scratch_mem_surf->flags = ssfs->flags;

    gr_mem_surf_Init(grSurface::scratch_mem_surf, data, rowsize);
    err = gr_mem_surf_Blt(dsfs, dx, dy, grSurface::scratch_mem_surf, sx, sy, sw, sh);

    ssf->unlock();
    return;
  }

  /*	case 4: mem->os
                  This requires locking both surfaces and performing a memory blt.
                  This also means tricking the memory blitter into thinking that the OS surface
                  is a memory surface
  */
  if (mode == 0x2) {
    grSurface::lock(&rowsize);

    grSurface::scratch_mem_surf->w = dsfs->w;
    grSurface::scratch_mem_surf->h = dsfs->h;
    grSurface::scratch_mem_surf->bpp = dsfs->bpp;
    grSurface::scratch_mem_surf->type = SURFTYPE_MEMORY;
    grSurface::scratch_mem_surf->flags = dsfs->flags;

    gr_mem_surf_Init(grSurface::scratch_mem_surf, m_DataPtr, m_DataRowsize);
    err = gr_mem_surf_Blt(grSurface::scratch_mem_surf, dx, dy, ssfs, sx, sy, sw, sh);

    grSurface::unlock();
    return;
  }
}

void grSurface::blt(int dx, int dy, grSurface *ssf) {
  ASSERT(m_SurfInit);
  grSurface::uniblt(dx, dy, ssf, 0, 0, ssf->width(), ssf->height());
}

void grSurface::blt(int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh) {
  ASSERT(m_SurfInit);
  grSurface::uniblt(dx, dy, ssf, sx, sy, sw, sh);
}

/*	Helper functions
                replace_color, simply replaces one color in the surface with another
*/
void grSurface::replace_color(ddgr_color sc, ddgr_color dc) {
  int rowsize;
  ASSERT(m_SurfInit);

  grSurface::lock(&rowsize);

  switch (ddsfObj.bpp) {
  case BPP_16: {
    uint16_t *data = (uint16_t *)m_DataPtr;
    int rowsize_w = m_DataRowsize / 2;
    uint16_t scc = XLAT_RGB_TO_16(sc), dcc = XLAT_RGB_TO_16(dc);

    for (int y = 0; y < ddsfObj.h; y++) {
      for (int x = 0; x < ddsfObj.w; x++)
        if (data[x] == scc)
          data[x] = dcc;

      data += rowsize_w;
    }
    break;
  }

  case BPP_32:
  default:
    Int3();
  }

  grSurface::unlock();
}

//	---------------------------------------------------------------------------
//	lock and unlock functions
//		these handle memory and os locks
//	---------------------------------------------------------------------------

char *grSurface::lock(int *rowsize) {
  bool grerr;

  ASSERT(m_SurfInit);

  switch (ddsfObj.type) {
  case SURFTYPE_VIDEOSCREEN:
  case SURFTYPE_GENERIC:
    grerr = ddgr_surf_Lock(&ddsfObj, (void **)&m_OrigDataPtr, &m_DataRowsize);
    break;
  case SURFTYPE_MEMORY:
    grerr = gr_mem_surf_Lock(&ddsfObj, (void **)&m_OrigDataPtr, &m_DataRowsize);
    break;
  default:
    Int3(); // invalid type!!
  }

  m_DataPtr = m_OrigDataPtr;
  *rowsize = m_DataRowsize;

  if (!grerr)
    return nullptr;

  surf_Locked++; // increment lock counter for this surface

  rend_SetSoftwareParameters(ddgr_GetAspectRatio(), ddsfObj.w, ddsfObj.h, m_DataRowsize, (uint8_t *)m_OrigDataPtr);

  return m_DataPtr;
}

/* do lock based off of x,y offsets */
char *grSurface::lock(int x, int y, int *rowsize) {
  bool grerr = false;

  ASSERT(m_SurfInit);

  switch (ddsfObj.type) {
  case SURFTYPE_VIDEOSCREEN:
  case SURFTYPE_GENERIC:
    grerr = ddgr_surf_Lock(&ddsfObj, (void **)&m_OrigDataPtr, &m_DataRowsize);
    break;
  case SURFTYPE_MEMORY:
    grerr = gr_mem_surf_Lock(&ddsfObj, (void **)&m_OrigDataPtr, &m_DataRowsize);
    break;
  default:
    Int3(); // invalid type!!
  }

  *rowsize = m_DataRowsize;
  if (!grerr)
    return nullptr;

  /*	calculate the adjusted data pointer based off of x,y,bpp */
  char *data = m_OrigDataPtr;

  data += (y * m_DataRowsize);

  switch (ddsfObj.bpp) {
  case BPP_16:
    data += (x * 2);
    break;
  case BPP_32:
    data += (x * 4);
    break;
  default:
    Int3();
  }

  m_DataPtr = data;

  surf_Locked++; // increment lock counter for this surface

  rend_SetSoftwareParameters(ddgr_GetAspectRatio(), ddsfObj.w, ddsfObj.h, m_DataRowsize, (uint8_t *)m_OrigDataPtr);

  return m_DataPtr;
}

void grSurface::unlock() {
  bool grerr;

  ASSERT(m_SurfInit);
  ASSERT(surf_Locked);

  switch (ddsfObj.type) {
  case SURFTYPE_VIDEOSCREEN:
  case SURFTYPE_GENERIC:
    grerr = ddgr_surf_Unlock(&ddsfObj, m_OrigDataPtr);
    break;
  case SURFTYPE_MEMORY:
    grerr = gr_mem_surf_Unlock(&ddsfObj, m_OrigDataPtr);
    break;
  default:
    Int3(); // invalid type!!
  }

  surf_Locked--;
}

//	---------------------------------------------------------------------------
//	miscellaneous
//	---------------------------------------------------------------------------

void grSurface::attach_to_window(unsigned handle) {
  ASSERT(m_SurfInit);
  ddgr_surf_AttachHandle(&ddsfObj, handle);
}

//	Private functions
//		conversion_xlat
//	---------------------------------------------------------------------------

void grSurface::xlat8_16(char *data, int w, int h, char *pal) {
  /*	copy from 8bit source bitmap to destination surface just created and
          locked
  */
  uint16_t *dptr;
  char *sptr;
  int row, col;
  int rowsize_w;
  int height, width;
  uint8_t *upal = (uint8_t *)pal;

  dptr = (uint16_t *)m_DataPtr;
  sptr = (char *)data;
  rowsize_w = m_DataRowsize / 2;

  height = std::min(h, ddsfObj.h);
  width = std::min(w, ddsfObj.w);

  for (row = 0; row < height; row++) {
    for (col = 0; col < width; col++) {
      uint8_t spix = sptr[col];
      if (spix != 0) {
        int r = upal[spix * 3] >> 3;
        int g = upal[spix * 3 + 1] >> 2;
        int b = upal[spix * 3 + 2] >> 3;
        uint16_t destpix = (r << 11) | (g << 5) | b;

        dptr[col] = destpix;
      } else
        dptr[col] = 0;
    }
    sptr += 256;
    dptr += rowsize_w;
  }
}

void grSurface::xlat16_16(char *data, int w, int h, int format) {
  /*	16-bit copy from source bitmap to destination surface just created and
          locked
          This function performs scaling if the source width and height don't match
          that of the destinations - JL
  */
  uint16_t *sptr, *dptr;
  int row, col;
  int rowsize_w;
  int height, width;
  fix xstep = IntToFix(w) / ddsfObj.w;
  fix ystep = IntToFix(h) / ddsfObj.h;
  fix fu = 0, fv = 0;

  dptr = (uint16_t *)m_DataPtr;
  sptr = (uint16_t *)data;
  rowsize_w = m_DataRowsize / 2;

  height = ddsfObj.h;
  width = ddsfObj.w;

  // height = SET_MIN(h, ddsfObj.h);
  // width = SET_MIN(w, ddsfObj.w);

  for (row = 0; row < height; row++, fv += ystep, fu = 0) {
    for (col = 0; col < width; col++, fu += xstep) {
      int newpix;

      if (format == BITMAP_FORMAT_1555)
        newpix = sptr[(FixToInt(fv) * w) + FixToInt(fu)];
      else {
        int temp_pix = sptr[(FixToInt(fv) * w) + FixToInt(fu)];
        int r = (temp_pix >> 8) & 0xf;
        int g = (temp_pix >> 4) & 0xf;
        int b = (temp_pix) & 0xf;

        r <<= 4;
        g <<= 4;
        b <<= 4;

        newpix = OPAQUE_FLAG | GR_RGB16(r, g, b);
      }

      dptr[col] = newpix;
    }

    dptr += rowsize_w;
  }
}

void grSurface::xlat16_24(char *data, int w, int h) {
  uint16_t *sptr;
  char *dptr;
  int scol, dcol, row;
  int height, width;

  dptr = (char *)m_DataPtr;
  sptr = (uint16_t *)data;
  height = std::min(h, ddsfObj.h);
  width = std::min(w, ddsfObj.w);

  for (row = 0; row < height; row++) {
    dcol = 0;
    for (scol = 0; scol < width; scol++) {
      uint16_t pix;
      ddgr_color new_color;
      char r, g, b;
      pix = sptr[scol];

      new_color = GR_16_TO_COLOR(pix);

      r = GR_COLOR_RED(new_color);
      g = GR_COLOR_GREEN(new_color);
      b = GR_COLOR_BLUE(new_color);

      dptr[dcol++] = r;
      dptr[dcol++] = g;
      dptr[dcol++] = b;
    }
    sptr += w;
    dptr += m_DataRowsize;
  }
}

void grSurface::xlat24_16(char *data, int w, int h) {
  char *sptr;
  uint16_t *dptr;
  int scol, dcol, row;
  int rowsize_w, height, width;

  dptr = (uint16_t *)m_DataPtr;
  sptr = (char *)data;
  rowsize_w = m_DataRowsize / 2;
  height = std::min(h, ddsfObj.h);
  width = std::min(w, ddsfObj.w);

  for (row = 0; row < height; row++) {
    scol = 0;
    for (dcol = 0; dcol < width; dcol++) {
      uint16_t pix;
      char r, g, b;
      r = sptr[scol++];
      g = sptr[scol++];
      b = sptr[scol++];
      pix = ((uint16_t)(r >> 3) << 11) + ((uint16_t)(g >> 2) << 5) + ((uint16_t)(b >> 3));
      dptr[dcol] = pix;
    }
    sptr += (w * 3);
    dptr += m_DataRowsize;
  }
}

void grSurface::xlat32_16(char *data, int w, int h) {
  unsigned *sptr;
  uint16_t *dptr;
  int col, row;
  int rowsize_w, height, width;

  ASSERT((w % 4) == 0);

  dptr = (uint16_t *)m_DataPtr;
  sptr = (unsigned *)data;
  rowsize_w = m_DataRowsize / 2;
  height = std::min(h, ddsfObj.h);
  width = std::min(w, ddsfObj.w);

  for (row = 0; row < height; row++) {
    for (col = 0; col < width; col++) {
      unsigned pix;
      uint16_t spix;

      pix = sptr[col];
      spix = (uint16_t)(pix & 0x000000f8) >> 3;
      spix |= (uint16_t)(pix & 0x0000fc00) >> 5;
      spix |= (uint16_t)(pix & 0x00f80000) >> 8;
      dptr[col] = spix;
    }
    sptr += w;
    dptr += rowsize_w;
  }
}

//-----------------------------------------------------------------------------
//	surface list functions

void grSurface::add_to_list(ddgr_surface *sf) {
  ddgr_surface_node *node;

  ASSERT(sf != nullptr);

  if (!grSurface::surf_list_cur) {
    //	first surface on list
    node = new ddgr_surface_node;
    node->sf = sf;
    node->prev = nullptr;
    grSurface::surf_list = grSurface::surf_list_cur = node;
  } else {
    //	next surface on list.
    node = new ddgr_surface_node;
    node->sf = sf;
    node->prev = grSurface::surf_list_cur;
    grSurface::surf_list_cur = node;
  }

  //	mprintf(0, "Allocated surface %s [%d].\n", sf->name, DDData.surfs_alloc);
}

void grSurface::remove_from_list(ddgr_surface *sf) {
  ddgr_surface_node *node, *next_node;

  ASSERT(grSurface::surf_list_cur);
  ASSERT(sf != nullptr);

  //	node is current node.  next node is node after it on the list.  since we are
  //	going from end to beginning, this may confuse you.
  node = grSurface::surf_list_cur;
  next_node = nullptr; // grSurface::surf_list_cur should be at end of list, so no next node

  while (node) {
    if (node->sf == sf) {
      if (next_node) { // delete from within list.
        next_node->prev = node->prev;
      } else { // delete from end of list.  set list tail to deleted node prev.
        grSurface::surf_list_cur = node->prev;
      }
      delete node;

      node = nullptr; // break out of loop
    } else {
      //	go down one node.
      next_node = node;
      node = node->prev;
    }
  }

  //	mprintf(0, "Freed surface %s [%d].\n", sf->name, DDData.surfs_alloc);
}