Debian Codegraph @

prboom2/src/gl_drawinfo.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 *
27 * DESCRIPTION:
28 *
29 * memory manager for GL data
30 *---------------------------------------------------------------------
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "doomtype.h"
38 #include "gl_intern.h"
39 #include "lprintf.h"
40
41 GLDrawInfo gld_drawinfo;
42
43 //
44 // gld_FreeDrawInfo
45 //
46 void gld_FreeDrawInfo(void)
47 {
48 int i;
49
50 for (i = 0; i < gld_drawinfo.maxsize; i++)
51 {
52 if (gld_drawinfo.data[i].data)
53 {
54 Z_Free(gld_drawinfo.data[i].data);
55 gld_drawinfo.data[i].data = 0;
56 }
57 }
58 Z_Free(gld_drawinfo.data);
59 gld_drawinfo.data = 0;
60
61 for (i = 0; i < GLDIT_TYPES; i++)
62 {
63 if (gld_drawinfo.items[i])
64 {
65 Z_Free(gld_drawinfo.items[i]);
66 gld_drawinfo.items[i] = 0;
67 }
68 }
69
70 memset(&gld_drawinfo, 0, sizeof(GLDrawInfo));
71 }
72
73 //
74 // gld_ResetDrawInfo
75 //
76 // Should be used between frames (in gld_StartDrawScene)
77 //
78 void gld_ResetDrawInfo(void)
79 {
80 int i;
81
82 for (i = 0; i < gld_drawinfo.maxsize; i++)
83 {
84 gld_drawinfo.data[i].size = 0;
85 }
86 gld_drawinfo.size = 0;
87
88 for (i = 0; i < GLDIT_TYPES; i++)
89 {
90 gld_drawinfo.num_items[i] = 0;
91 }
92 }
93
94 //
95 // gld_AddDrawRange
96 //
97 static void gld_AddDrawRange(int size)
98 {
99 gld_drawinfo.maxsize++;
100 gld_drawinfo.data = Z_Realloc(gld_drawinfo.data,
101 gld_drawinfo.maxsize * sizeof(gld_drawinfo.data[0]));
102
103 gld_drawinfo.data[gld_drawinfo.size].maxsize = size;
104 gld_drawinfo.data[gld_drawinfo.size].data = Z_Malloc(size);
105 gld_drawinfo.data[gld_drawinfo.size].size = 0;
106 }
107
108 //
109 // gld_AddDrawItem
110 //
111 #define NEWSIZE (MAX(64 * 1024, itemsize))
112 #define SIZEOF8(type) ((sizeof(type)+7)&~7)
113 void gld_AddDrawItem(GLDrawItemType itemtype, void *itemdata)
114 {
115 int itemsize = 0;
116 byte *item_p = NULL;
117
118 static int itemsizes[GLDIT_TYPES] = {
119 0,
120 SIZEOF8(GLWall), SIZEOF8(GLWall), SIZEOF8(GLWall), SIZEOF8(GLWall), SIZEOF8(GLWall),
121 SIZEOF8(GLWall), SIZEOF8(GLWall),
122 SIZEOF8(GLFlat), SIZEOF8(GLFlat),
123 SIZEOF8(GLFlat), SIZEOF8(GLFlat),
124 SIZEOF8(GLSprite), SIZEOF8(GLSprite), SIZEOF8(GLSprite),
125 SIZEOF8(GLShadow),
126 SIZEOF8(GLHealthBar)
127 };
128
129 itemsize = itemsizes[itemtype];
130 if (itemsize == 0)
131 {
132 I_Error("gld_AddDrawItem: unknown GLDrawItemType %d", itemtype);
133 }
134
135 if (gld_drawinfo.maxsize == 0)
136 {
137 gld_AddDrawRange(NEWSIZE);
138 }
139
140 if (gld_drawinfo.data[gld_drawinfo.size].size + itemsize >=
141 gld_drawinfo.data[gld_drawinfo.size].maxsize)
142 {
143 gld_drawinfo.size++;
144 if (gld_drawinfo.size >= gld_drawinfo.maxsize)
145 {
146 gld_AddDrawRange(NEWSIZE);
147 }
148 }
149
150 item_p = gld_drawinfo.data[gld_drawinfo.size].data +
151 gld_drawinfo.data[gld_drawinfo.size].size;
152
153 memcpy(item_p, itemdata, itemsize);
154
155 gld_drawinfo.data[gld_drawinfo.size].size += itemsize;
156
157 if (gld_drawinfo.num_items[itemtype] >= gld_drawinfo.max_items[itemtype])
158 {
159 gld_drawinfo.max_items[itemtype] += 64;
160 gld_drawinfo.items[itemtype] = Z_Realloc(
161 gld_drawinfo.items[itemtype],
162 gld_drawinfo.max_items[itemtype] * sizeof(gld_drawinfo.items[0][0]));
163 }
164
165 gld_drawinfo.items[itemtype][gld_drawinfo.num_items[itemtype]].item.item = item_p;
166 gld_drawinfo.num_items[itemtype]++;
167 }
168 #undef SIZEOF8
169 #undef NEWSIZE