-
Notifications
You must be signed in to change notification settings - Fork 11
/
saveload.c
285 lines (226 loc) · 7.45 KB
/
saveload.c
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
/*
* ff7_opengl - Complete OpenGL replacement of the Direct3D renderer used in
* the original ports of Final Fantasy VII and Final Fantasy VIII for the PC.
*
* 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/>.
*/
/*
* saveload.c - load/save routines for modpath texture replacements
*/
#include <sys/stat.h>
#include <stdio.h>
#include <direct.h>
#include <gl/glew.h>
#include "types.h"
#include "globals.h"
#include "log.h"
#include "gl.h"
#include "cfg.h"
#include "compile_cfg.h"
#include "png.h"
#include "ctx.h"
#include "macro.h"
void make_path(char *name)
{
char *next = name;
while((next = strchr(next, '/')))
{
char tmp[128];
while(next[0] == '/') next++;
strncpy(tmp, name, next - name);
tmp[next - name] = 0;
mkdir(tmp);
}
}
bool save_texture(void *data, uint width, uint height, uint palette_index, char *name)
{
char filename[sizeof(basedir) + 1024];
struct stat dummy;
_snprintf(filename, sizeof(filename), "%s/mods/%s/%s_%02i.png", basedir, mod_path, name, palette_index);
make_path(filename);
if(stat(filename, &dummy)) return write_png(filename, width, height, data);
else return true;
}
struct ext_cache_data
{
uint texture;
uint size;
};
struct ext_cache_entry
{
char *name;
uint palette_index;
uint references;
time_t last_access;
struct ext_cache_data data;
};
#define EXT_CACHE_ENTRIES 512
// the texture cache can only hold a certain number of textures in memory
// there is also a configurable hard limit on how much memory can be used by the cache
// once it is full the texture with the oldest access time is evicted
struct ext_cache_entry *ext_cache[EXT_CACHE_ENTRIES];
// retrieve a single entry from the texture cache
struct ext_cache_data *ext_cache_get(char *name, uint palette_index, int refcount)
{
uint i;
for(i = 0; i < EXT_CACHE_ENTRIES; i++)
{
if(!ext_cache[i]) continue;
if(ext_cache[i]->palette_index != palette_index) continue;
if(trace_all) trace("Comparing: %s (%s)\n", ext_cache[i]->name, name);
if(!_stricmp(ext_cache[i]->name, name))
{
if(trace_all) trace("Matched: %s\n", ext_cache[i]->name);
if(refcount >= 0 || ext_cache[i]->references) ext_cache[i]->references += refcount;
if(refcount >= 0) qpc_get_time(&ext_cache[i]->last_access);
return &ext_cache[i]->data;
}
}
return 0;
}
// update the access time of a single entry in the texture cache
void ext_cache_access(struct texture_set *texture_set)
{
VOBJ(texture_set, texture_set, texture_set);
VOBJ(tex_header, tex_header, VREF(texture_set, tex_header));
if(!VREF(texture_set, ogl.external)) return;
if((uint)VREF(tex_header, file.pc_name) > 32) ext_cache_get(VREF(tex_header, file.pc_name), VREF(texture_set, palette_index), 0);
}
// remove a reference to an entry in the texture cache
void ext_cache_release(struct texture_set *texture_set)
{
uint i;
VOBJ(texture_set, texture_set, texture_set);
VOBJ(tex_header, tex_header, VREF(texture_set, tex_header));
if(!VREF(texture_set, ogl.external)) return;
if((uint)VREF(tex_header, file.pc_name) > 32)
{
for(i = 0; i < VREF(texture_set, ogl.gl_set->textures); i++)
{
if(VREF(texture_set, texturehandle[i]))
{
ext_cache_get(VREF(tex_header, file.pc_name), i, -1);
}
}
}
}
// add a new entry to the texture cache
struct ext_cache_data *ext_cache_put(char *name, uint palette_index)
{
uint i;
time_t oldest_access_time;
uint oldest_texture;
if(stats.ext_cache_size < texture_cache_size * 1024 * 1024)
{
for(i = 0; i < EXT_CACHE_ENTRIES; i++)
{
if(!ext_cache[i])
{
ext_cache[i] = driver_calloc(sizeof(*ext_cache[i]), 1);
ext_cache[i]->name = driver_malloc(strlen(name) + 1);
strcpy(ext_cache[i]->name, name);
ext_cache[i]->palette_index = palette_index;
ext_cache[i]->references = 1;
qpc_get_time(&ext_cache[i]->last_access);
return &ext_cache[i]->data;
}
}
}
// access time can't be higher than current time
qpc_get_time(&oldest_access_time);
oldest_texture = EXT_CACHE_ENTRIES;
for(i = 0; i < EXT_CACHE_ENTRIES; i++)
{
if(!ext_cache[i]) continue;
if(!ext_cache[i]->references && ext_cache[i]->last_access < oldest_access_time)
{
oldest_access_time = ext_cache[i]->last_access;
oldest_texture = i;
}
}
if(oldest_texture == EXT_CACHE_ENTRIES)
{
error("texture cache is full and nothing could be evicted!\n");
return 0;
}
glDeleteTextures(1, &ext_cache[oldest_texture]->data.texture);
stats.ext_cache_size -= ext_cache[oldest_texture]->data.size;
ext_cache[oldest_texture]->name = driver_realloc(ext_cache[oldest_texture]->name, strlen(name) + 1);
strcpy(ext_cache[oldest_texture]->name, name);
ext_cache[oldest_texture]->palette_index = palette_index;
memset(&ext_cache[oldest_texture]->data, 0, sizeof(ext_cache[oldest_texture]->data));
qpc_get_time(&ext_cache[oldest_texture]->last_access);
return &ext_cache[oldest_texture]->data;
}
uint load_texture_helper(char *png_name, char *ctx_name, uint *width, uint *height, bool use_compression)
{
uint ret;
uint *data;
if(!(use_compression && compress_textures) || !(ret = read_ctx(ctx_name, width, height)))
{
data = read_png(png_name, width, height);
if(!data) return 0;
gl_check_texture_dimensions(*width, *height, png_name);
if((use_compression && compress_textures))
{
ret = gl_compress_pixel_buffer(data, *width, *height, GL_BGRA);
if(!write_ctx(ctx_name, *width, *height, ret))
{
glDeleteTextures(1, &ret);
data = read_png(png_name, width, height);
ret = gl_commit_pixel_buffer(data, *width, *height, GL_BGRA, true);
stats.ext_cache_size += (*width) * (*height) * 4;
}
}
else
{
ret = gl_commit_pixel_buffer(data, *width, *height, GL_BGRA, true);
stats.ext_cache_size += (*width) * (*height) * 4;
}
}
return ret;
}
uint load_texture(char *name, uint palette_index, uint *width, uint *height, bool use_compression)
{
char png_name[sizeof(basedir) + 1024];
char ctx_name[sizeof(basedir) + 1024];
uint ret;
struct ext_cache_data *cache_data;
cache_data = ext_cache_get(name, palette_index, 1);
if(cache_data && cache_data->texture) return cache_data->texture;
_snprintf(png_name, sizeof(png_name), "%s/mods/%s/%s_%02i.png", basedir, mod_path, name, palette_index);
_snprintf(ctx_name, sizeof(ctx_name), "%s/mods/%s/cache/%s_%02i.ctx", basedir, mod_path, name, palette_index);
ret = load_texture_helper(png_name, ctx_name, width, height, use_compression);
if(!ret)
{
if(palette_index != 0)
{
if(show_missing_textures) info("tried to load %s, falling back to palette 0\n", png_name, palette_index);
return load_texture(name, 0, width, height, use_compression);
}
else
{
if(show_missing_textures) info("tried to load %s, failed\n", png_name);
return 0;
}
}
if(trace_all) trace("Created texture: %i\n", ret);
if(!cache_data) cache_data = ext_cache_put(name, palette_index);
if(cache_data)
{
cache_data->texture = ret;
cache_data->size += (*width) * (*height) * 4;
}
return ret;
}