Debian Codegraph @

2dlib/memsurf.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/2dlib/memsurf.cpp $
21 * $Revision: 6 $
22 * $Date: 4/23/98 6:38p $
23 * $Author: Jason $
24 *
25 * memory surface class
26 *
27 * $Log: /DescentIII/Main/2dlib/memsurf.cpp $
28 *
29 * 6 4/23/98 6:38p Jason
30 * made bitmaps use 1555 format
31 *
32 * 5 3/31/98 3:48p Jason
33 * added memory lib
34 *
35 * 4 11/16/97 2:19p Samir
36 * Added constructor takes a bitmap handle.
37 *
38 * 3 11/14/97 12:56p Samir
39 * Took out annoying mprintf.
40 *
41 * 2 10/01/97 4:22p Samir
42 * Fixed memory leak in memsurf library destruction
43 *
44 * 12 6/12/97 6:29p Samir
45 * DDGR v2.0 Changes in 2d system implemented.
46 *
47 * 11 5/15/97 4:24p Samir
48 * Fixed one-pixel overwrite of clear
49 *
50 * 10 4/10/97 6:45p Samir
51 * Fixed confusion of variables in memory clear function.
52 *
53 * 9 3/28/97 3:07p Samir
54 * Clear function now takes a color
55 *
56 * 8 3/27/97 11:11a Samir
57 * Moved code from ddgr library to 2dlib because it was ANSI compliant and
58 * should work on all systems
59 *
60 * 7 2/03/97 3:00p Samir
61 * Allow for first time initialization code to properly register surface
62 * in surface list.
63 *
64 * 6 1/30/97 6:01p Samir
65 * The partition of memory and os surfaces as well as the change in
66 * ddgr_surface structure and all related changes.
67 *
68 * $NoKeywords: $
69 */
70
71 #include <cstring>
72 #include <cstdlib>
73
74 #include "bitmap.h"cxx . . $ `<file>/lib/bitmap.h`/
75 #include "lib2d.h"cxx . . $ `<file>/2dlib/lib2d.h`/
76 #include "mem.h"cxx . . $ `<file>/mem/mem.h`/
77
78 // ----------------------------------------------------------------------------
79 // constructor and destructor
80 // ----------------------------------------------------------------------------
81
82 grMemorySurfacecxx . . $ grMemorySurface#::grMemorySurfacecxx . . $ grMemorySurface#grMemorySurface(49f6e7a06ebc5aa8).() : grSurfacecxx . . $ grSurface#grSurface(49f6e7a06ebc5aa8).() {
83 m_FirstTimeInitcxx . . $ grMemorySurface#m_FirstTimeInit. = true;
84 m_AllowInitcxx . . $ grMemorySurface#m_AllowInit. = true;
85 }
86
87 grMemorySurfacecxx . . $ grMemorySurface#::grMemorySurfacecxx . . $ grMemorySurface#grMemorySurface(f0b9f08e753626c2).(int w, int h, int bpp, unsigned flags, const char *name)
88 : grSurfacecxx . . $ grSurface#grSurface(589a245ae52835fb).(w, h, bpp, SURFTYPE_MEMORYcxx . . $ SURFTYPE_MEMORY., flags, name) {
89 m_FirstTimeInitcxx . . $ grMemorySurface#m_FirstTimeInit. = false;
90 m_AllowInitcxx . . $ grMemorySurface#m_AllowInit. = false;
91 }
92
93 grMemorySurfacecxx . . $ grMemorySurface#::grMemorySurfacecxx . . $ grMemorySurface#grMemorySurface(d4f767463ce0a6b3).(int bm) {
94 m_FirstTimeInitcxx . . $ grMemorySurface#m_FirstTimeInit. = true;
95 m_AllowInitcxx . . $ grMemorySurface#m_AllowInit. = true;
96
97 grMemorySurfacecxx . . $ grMemorySurface#::initcxx . . $ grMemorySurface#init(fe591fed253a3675).(bm_wcxx . . $ bm_w(9b79fb6aee4c0440).(bm, 0), bm_hcxx . . $ bm_h(9b79fb6aee4c0440).(bm, 0), bm_bppcxx . . $ bm_bpp(7864480464b09eea).(bm), (char *)bm_datacxx . . $ bm_data(501e1bc9f482c6bd).(bm, 0), bm_rowsizecxx . . $ bm_rowsize(9b79fb6aee4c0440).(bm, 0),
98 SURFFLAG_COLORKEYcxx . . $ SURFFLAG_COLORKEY.);
99 }
100
101 grMemorySurfacecxx . . $ grMemorySurface#::~cxx . . $ grMemorySurface#`~grMemorySurface`(ced63f7c635d850d).grMemorySurfacecxx . . $ grMemorySurface#() {
102 // this should invoke the grSurface destructor
103 // even if we just initialized the surface, the memory routines should know this and
104 // do appropriate deinitialization.
105 }
106
107 // ----------------------------------------------------------------------------
108 // initialize a memory surface with these values
109 // ----------------------------------------------------------------------------
110
111 bool grMemorySurfacecxx . . $ grMemorySurface#::initcxx . . $ grMemorySurface#init(fe591fed253a3675).(int w, int h, int bpp, char *data, int rowsize, unsigned flags, const char *name) {
112 ASSERTcxx . . $ `misc/pserror.h:246:9`!(m_AllowInit);
113
114 if (m_FirstTimeInitcxx . . $ grMemorySurface#m_FirstTimeInit.) {
115 // add to list ONLY ONCE!
116 add_to_listcxx . . $ grSurface#add_to_list(b6325dd6f6bb1424).(&ddsfObjcxx . . $ grSurface#ddsfObj.);
117 m_FirstTimeInitcxx . . $ grMemorySurface#m_FirstTimeInit. = false;
118 }
119
120 ddsfObjcxx . . $ grSurface#ddsfObj..wcxx . . $ ddgr_surface#w. = w;
121 ddsfObjcxx . . $ grSurface#ddsfObj..hcxx . . $ ddgr_surface#h. = h;
122 ddsfObjcxx . . $ grSurface#ddsfObj..bppcxx . . $ ddgr_surface#bpp. = bpp;
123 ddsfObjcxx . . $ grSurface#ddsfObj..typecxx . . $ ddgr_surface#type. = SURFTYPE_MEMORYcxx . . $ SURFTYPE_MEMORY.;
124 ddsfObjcxx . . $ grSurface#ddsfObj..flagscxx . . $ ddgr_surface#flags. = (uint16_tcxx . . $ uint16_t#)flags;
125 if (name)
126 strncpy(ddsfObjcxx . . $ grSurface#ddsfObj..namecxx . . $ ddgr_surface#name., name, 15);
127
128 if (!gr_mem_surf_Initcxx . . $ gr_mem_surf_Init(29b5e7245e149caf).(&ddsfObjcxx . . $ grSurface#ddsfObj., data, rowsize))
129 return false;
130
131 surf_initcxx . . $ grSurface#surf_init(d4f767463ce0a6b3).(1);
132
133 return true;
134 }
135
136 // ---------------------------------------------------------------------------
137 // gr_mem_xxx functions for memory surfaces
138 //
139 // internal functions
140 // ---------------------------------------------------------------------------
141
142 static mem_bitmapcxx . . $ mem_bitmap# grMemSurf_objectcxx . . $ grMemSurf_object.;
143
144 bool gr_bm_mem_Blt16cxx . . $ gr_bm_mem_Blt16(5ac5614b4169eec2).(ddgr_surfacecxx . . $ ddgr_surface# *dsf, int dx, int dy, ddgr_surfacecxx . . $ ddgr_surface# *ssf, int sx, int sy, int sw, int sh);
145 bool gr_bm_mem_Blt16ckcxx . . $ gr_bm_mem_Blt16ck(5ac5614b4169eec2).(ddgr_surfacecxx . . $ ddgr_surface# *dsf, int dx, int dy, ddgr_surfacecxx . . $ ddgr_surface# *ssf, int sx, int sy, int sw, int sh);
146
147 // ---------------------------------------------------------------------------
148 // initializes a memory surface by allocating memory based on bit depth
149
150 bool gr_mem_surf_Createcxx . . $ gr_mem_surf_Create(8a7e7c096868029c).(ddgr_surfacecxx . . $ ddgr_surface# *sf) {
151 mem_bitmapcxx . . $ mem_bitmap# *bm;
152
153 bm = new mem_bitmapcxx . . $ mem_bitmap#;
154 memset(bm, 0, sizeof(mem_bitmapcxx . . $ mem_bitmap#));
155
156 switch (sf->bppcxx . . $ ddgr_surface#bpp.) {
157 case BPP_16cxx . . $ `lib/grdefs.h:61:9`!:
158 bm->bppcxx . . $ mem_bitmap#bpp. = BPP_16cxx . . $ `lib/grdefs.h:61:9`!;
159 bm->rowsizecxx . . $ mem_bitmap#rowsize. = sf->wcxx . . $ ddgr_surface#w. * 2;
160 bm->allocedcxx . . $ mem_bitmap#alloced. = 1;
161 break;
162
163 case BPP_32cxx . . $ `lib/grdefs.h:63:9`!:
164 bm->bppcxx . . $ mem_bitmap#bpp. = BPP_32cxx . . $ `lib/grdefs.h:63:9`!;
165 bm->rowsizecxx . . $ mem_bitmap#rowsize. = sf->wcxx . . $ ddgr_surface#w. * 4;
166 bm->allocedcxx . . $ mem_bitmap#alloced. = 1;
167 break;
168
169 default:
170 Int3cxx . . $ `misc/pserror.h:247:9`!();
171 }
172
173 bm->datacxx . . $ mem_bitmap#data. = (char *)mem_malloccxx . . $ `mem/mem.h:79:9`!(bm->rowsize * sf->h);
174 if (!bm->datacxx . . $ mem_bitmap#data.) {
175 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "mem_Create malloc fail <%s>\n", sf->name);
176 delete bm;
177 return false;
178 }
179
180 sf->objcxx . . $ ddgr_surface#obj. = (void *)bm;
181
182 return true;
183 }
184
185 // ---------------------------------------------------------------------------
186 // deallocates memory created in a mem_Create
187
188 bool gr_mem_surf_Destroycxx . . $ gr_mem_surf_Destroy(8a7e7c096868029c).(ddgr_surfacecxx . . $ ddgr_surface# *sf) {
189 ASSERTcxx . . $ `misc/pserror.h:246:9`!(sf);
190
191 mem_bitmapcxx . . $ mem_bitmap# *bm = (mem_bitmapcxx . . $ mem_bitmap# *)sf->objcxx . . $ ddgr_surface#obj.;
192
193 // here, we check if we initialized, or allocated this memory surface.
194 if (bm->allocedcxx . . $ mem_bitmap#alloced. == 2) {
195 delete bm;
196 return false;
197 } else if (bm->allocedcxx . . $ mem_bitmap#alloced. == 0) {
198 return false;
199 }
200
201 // otherwise free all objects
202 mem_freecxx . . $ `mem/mem.h:80:9`!(bm->data);
203 delete bm;
204
205 return true;
206 }
207
208 // ---------------------------------------------------------------------------
209 // initializes a memory surface
210
211 bool gr_mem_surf_Initcxx . . $ gr_mem_surf_Init(29b5e7245e149caf).(ddgr_surfacecxx . . $ ddgr_surface# *sf, char *data, int rowsize) {
212 // initialize a memory surface, with preallocated memory
213 mem_bitmapcxx . . $ mem_bitmap# *mbm;
214
215 // create new memory bitmap object for initialized surface
216 if (!sf->objcxx . . $ ddgr_surface#obj.) {
217 mbm = new mem_bitmapcxx . . $ mem_bitmap#;
218 mbm->allocedcxx . . $ mem_bitmap#alloced. = 2;
219 } else {
220 mbm = (mem_bitmapcxx . . $ mem_bitmap# *)sf->objcxx . . $ ddgr_surface#obj.;
221 mbm->allocedcxx . . $ mem_bitmap#alloced. = 0;
222 }
223
224 mbm->datacxx . . $ mem_bitmap#data. = data;
225 mbm->rowsizecxx . . $ mem_bitmap#rowsize. = rowsize;
226 mbm->bppcxx . . $ mem_bitmap#bpp. = (int16_t)sf->bppcxx . . $ ddgr_surface#bpp.;
227 mbm->flagcxx . . $ mem_bitmap#flag. = 0;
228
229 sf->objcxx . . $ ddgr_surface#obj. = (void *)mbm;
230
231 return true;
232 }
233
234 // ---------------------------------------------------------------------------
235 // clears a memory surface
236
237 void gr_mem_surf_Clearcxx . . $ gr_mem_surf_Clear(18895a2c8f8b731a).(ddgr_surfacecxx . . $ ddgr_surface# *dsf, ddgr_colorcxx . . $ ddgr_color# col, int l, int t, int w, int h) {
238 mem_bitmapcxx . . $ mem_bitmap# *dbm = (mem_bitmapcxx . . $ mem_bitmap# *)dsf->objcxx . . $ ddgr_surface#obj.;
239
240 switch (dbm->bppcxx . . $ mem_bitmap#bpp.) {
241 case BPP_16cxx . . $ `lib/grdefs.h:61:9`!: {
242 uint16_tcxx . . $ uint16_t# *sptr;
243 uint16_tcxx . . $ uint16_t# scol;
244
245 scol = (uint16_tcxx . . $ uint16_t#)GR_COLOR_TO_16cxx . . $ GR_COLOR_TO_16(6557041c941ab807).(col);
246 sptr = (uint16_tcxx . . $ uint16_t# *)dbm->datacxx . . $ mem_bitmap#data. + (t * ((dbm->rowsizecxx . . $ mem_bitmap#rowsize.) >> 1));
247
248 for (int y = t; y < (t + h); y++) {
249 for (int x = l; x < (l + w); x++)
250 sptr[x] = scol;
251
252 sptr += ((dbm->rowsizecxx . . $ mem_bitmap#rowsize.) >> 1);
253 }
254 break;
255 }
256
257 case BPP_32cxx . . $ `lib/grdefs.h:63:9`!:
258 default:
259 Int3cxx . . $ `misc/pserror.h:247:9`!();
260 }
261 }
262
263 // ---------------------------------------------------------------------------
264 // blts a memory surface to another such surface
265
266 bool gr_mem_surf_Bltcxx . . $ gr_mem_surf_Blt(5ac5614b4169eec2).(ddgr_surfacecxx . . $ ddgr_surface# *dsf, int dx, int dy, ddgr_surfacecxx . . $ ddgr_surface# *ssf, int sx, int sy, int sw, int sh) {
267 // Maybe we should allow slow 16bpp to 24bpp and vice-versa blts.
268 // for now, we wont.
269 mem_bitmapcxx . . $ mem_bitmap# *sbm = (mem_bitmapcxx . . $ mem_bitmap# *)ssf->objcxx . . $ ddgr_surface#obj., *dbm = (mem_bitmapcxx . . $ mem_bitmap# *)dsf->objcxx . . $ ddgr_surface#obj.;
270
271 ASSERTcxx . . $ `misc/pserror.h:246:9`!(dbm->bpp == sbm->bpp);
272
273 switch (dbm->bppcxx . . $ mem_bitmap#bpp.) {
274 case BPP_16cxx . . $ `lib/grdefs.h:61:9`!:
275 if (ssf->flagscxx . . $ ddgr_surface#flags. & SURFFLAG_COLORKEYcxx . . $ SURFFLAG_COLORKEY.)
276 gr_bm_mem_Blt16ckcxx . . $ gr_bm_mem_Blt16ck(5ac5614b4169eec2).(dsf, dx, dy, ssf, sx, sy, sw, sh);
277 else
278 gr_bm_mem_Blt16cxx . . $ gr_bm_mem_Blt16(5ac5614b4169eec2).(dsf, dx, dy, ssf, sx, sy, sw, sh);
279 break;
280
281 case BPP_32cxx . . $ `lib/grdefs.h:63:9`!:
282 default:
283 Int3cxx . . $ `misc/pserror.h:247:9`!();
284 }
285
286 return true;
287 }
288
289 // ---------------------------------------------------------------------------
290 // just extracts info from mem_bitmap abstract object
291
292 bool gr_mem_surf_Lockcxx . . $ gr_mem_surf_Lock(52c8467f55fe71c1).(ddgr_surfacecxx . . $ ddgr_surface# *sf, void **ptr, int *rowsize) {
293 mem_bitmapcxx . . $ mem_bitmap# *bm = (mem_bitmapcxx . . $ mem_bitmap# *)sf->objcxx . . $ ddgr_surface#obj.;
294
295 *ptr = (char *)bm->datacxx . . $ mem_bitmap#data.;
296 *rowsize = bm->rowsizecxx . . $ mem_bitmap#rowsize.;
297
298 return true;
299 }
300
301 // ---------------------------------------------------------------------------
302 // invalidates sf members
303
304 bool gr_mem_surf_Unlockcxx . . $ gr_mem_surf_Unlock(274a3a14c0c4a006).(ddgr_surfacecxx . . $ ddgr_surface# *sf, void *ptr) { return true; }
305
306 // ---------------------------------------------------------------------------
307 // does shit.
308
309 bool gr_mem_surf_AttachHandlecxx . . $ gr_mem_surf_AttachHandle(7ab66f0ba4d59b70).(ddgr_surfacecxx . . $ ddgr_surface# *sf, unsigned handle) { return true; }
310
311 // ---------------------------------------------------------------------------
312 // performs 16-bit blts.
313
314 bool gr_bm_mem_Blt16cxx . . $ gr_bm_mem_Blt16(5ac5614b4169eec2).(ddgr_surfacecxx . . $ ddgr_surface# *dsf, int dx, int dy, ddgr_surfacecxx . . $ ddgr_surface# *ssf, int sx, int sy, int sw, int sh) {
315 mem_bitmapcxx . . $ mem_bitmap# *dbm = (mem_bitmapcxx . . $ mem_bitmap# *)dsf->objcxx . . $ ddgr_surface#obj., *sbm = (mem_bitmapcxx . . $ mem_bitmap# *)ssf->objcxx . . $ ddgr_surface#obj.;
316 uint16_tcxx . . $ uint16_t# *dbits;
317 uint16_tcxx . . $ uint16_t# *sbits;
318 int srowsize_w, drowsize_w, row, col;
319
320 // set up blt.
321 srowsize_w = sbm->rowsizecxx . . $ mem_bitmap#rowsize. >> 1; // rowsize in shorts
322 drowsize_w = dbm->rowsizecxx . . $ mem_bitmap#rowsize. >> 1;
323 dbits = (uint16_tcxx . . $ uint16_t# *)dbm->datacxx . . $ mem_bitmap#data. + (dy * drowsize_w) + dx;
324 sbits = (uint16_tcxx . . $ uint16_t# *)sbm->datacxx . . $ mem_bitmap#data. + (sy * srowsize_w) + sx;
325
326 for (row = 0; row < sh; row++) {
327 for (col = 0; col < sw; col++)
328 dbits[col] = sbits[col];
329 sbits += srowsize_w;
330 dbits += drowsize_w;
331 }
332
333 return true;
334 }
335
336 bool gr_bm_mem_Blt16ckcxx . . $ gr_bm_mem_Blt16ck(5ac5614b4169eec2).(ddgr_surfacecxx . . $ ddgr_surface# *dsf, int dx, int dy, ddgr_surfacecxx . . $ ddgr_surface# *ssf, int sx, int sy, int sw, int sh) {
337 mem_bitmapcxx . . $ mem_bitmap# *dbm = (mem_bitmapcxx . . $ mem_bitmap# *)dsf->objcxx . . $ ddgr_surface#obj., *sbm = (mem_bitmapcxx . . $ mem_bitmap# *)ssf->objcxx . . $ ddgr_surface#obj.;
338 uint16_tcxx . . $ uint16_t# *dbits;
339 uint16_tcxx . . $ uint16_t# *sbits;
340 int srowsize_w, drowsize_w, row, col;
341
342 // set up blt.
343 srowsize_w = sbm->rowsizecxx . . $ mem_bitmap#rowsize. >> 1; // rowsize in shorts
344 drowsize_w = dbm->rowsizecxx . . $ mem_bitmap#rowsize. >> 1;
345 dbits = (uint16_tcxx . . $ uint16_t# *)dbm->datacxx . . $ mem_bitmap#data. + (dy * drowsize_w) + dx;
346 sbits = (uint16_tcxx . . $ uint16_t# *)sbm->datacxx . . $ mem_bitmap#data. + (sy * srowsize_w) + sx;
347
348 for (row = 0; row < sh; row++) {
349 for (col = 0; col < sw; col++) {
350 if (sbits[col] & OPAQUE_FLAG16cxx . . $ `lib/grdefs.h:69:9`!)
351 dbits[col] = sbits[col];
352 }
353 sbits += srowsize_w;
354 dbits += drowsize_w;
355 }
356
357 return true;
358 }