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
|
/* SCCS Id: @(#)tiletext.c 3.4 1999/10/24 */
/* NetHack may be freely redistributed. See license for details. */
#include "config.h"
#include "tile.h"
pixval ColorMap[3][MAXCOLORMAPSIZE];
int colorsinmap;
pixval MainColorMap[3][MAXCOLORMAPSIZE];
int colorsinmainmap;
static short color_index[MAXCOLORMAPSIZE];
static int num_colors;
static char charcolors[MAXCOLORMAPSIZE];
static int placeholder_init = 0;
static pixel placeholder[TILE_Y][TILE_X];
static FILE *tile_file;
static int tile_set, tile_set_indx;
#if (TILE_X==8)
static const char *text_sets[] = { "monthin.txt", "objthin.txt", "oththin.txt" };
#else
static const char *text_sets[] = { "monsters.txt", "objects.txt", "other.txt" };
#endif
extern const char *FDECL(tilename, (int, int));
static void FDECL(read_text_colormap, (FILE *));
static boolean FDECL(write_text_colormap, (FILE *));
static boolean FDECL(read_txttile, (FILE *, pixel(*)[TILE_X]));
static void FDECL(write_txttile, (FILE *, pixel(*)[TILE_X]));
/* Ugh. DICE doesn't like %[A-Z], so we have to spell it out... */
#define FORMAT_STRING \
"%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789] = (%d, %d, %d) "
static void
read_text_colormap(txtfile)
FILE *txtfile;
{
int i, r, g, b;
char c[2];
for (i = 0; i < MAXCOLORMAPSIZE; i++)
color_index[i] = -1;
num_colors = 0;
while (fscanf(txtfile, FORMAT_STRING, c, &r, &g, &b) == 4) {
color_index[(int) c[0]] = num_colors;
ColorMap[CM_RED][num_colors] = r;
ColorMap[CM_GREEN][num_colors] = g;
ColorMap[CM_BLUE][num_colors] = b;
num_colors++;
}
colorsinmap = num_colors;
}
#undef FORMAT_STRING
static boolean
write_text_colormap(txtfile)
FILE *txtfile;
{
int i;
char c;
num_colors = colorsinmainmap;
if (num_colors > 62) {
Fprintf(stderr, "too many colors (%d)\n", num_colors);
return FALSE;
}
for (i = 0; i < num_colors; i++) {
if (i < 26) c = 'A' + i;
else if (i < 52) c = 'a' + i - 26;
else c = '0' + i - 52;
charcolors[i] = c;
Fprintf(txtfile, "%c = (%d, %d, %d)\n", c,
(int)MainColorMap[CM_RED][i],
(int)MainColorMap[CM_GREEN][i],
(int)MainColorMap[CM_BLUE][i]);
}
return TRUE;
}
static boolean
read_txttile(txtfile, pixels)
FILE *txtfile;
pixel (*pixels)[TILE_X];
{
int ph, i, j, k;
char buf[BUFSZ], ttype[BUFSZ];
const char *p;
char c[2];
if (fscanf(txtfile, "# %s %d (%[^)])", ttype, &i, buf) <= 0)
return FALSE;
ph = strcmp(ttype, "placeholder") == 0;
if (!ph && strcmp(ttype,"tile") != 0)
Fprintf(stderr,
"Keyword \"%s\" unexpected for entry %d\n",
ttype, i);
if (tile_set != 0) {
/* check tile name, but not relative number, which will
* change when tiles are added
*/
p = tilename(tile_set, tile_set_indx);
if (p && strcmp(p, buf)) {
Fprintf(stderr, "warning: for tile %d (numbered %d) of %s,\n",
tile_set_indx, i, text_sets[tile_set-1]);
Fprintf(stderr, "\tfound '%s' while expecting '%s'\n",
buf, p);
}
}
tile_set_indx++;
/* look for non-whitespace at each stage */
if (fscanf(txtfile, "%1s", c) < 0) {
Fprintf(stderr, "unexpected EOF\n");
return FALSE;
}
if (c[0] != '{') {
Fprintf(stderr, "didn't find expected '{'\n");
return FALSE;
}
for (j = 0; j < TILE_Y; j++) {
for (i = 0; i < TILE_X; i++) {
if (fscanf(txtfile, "%1s", c) < 0) {
Fprintf(stderr, "unexpected EOF\n");
return FALSE;
}
k = color_index[(int) c[0]];
if (k == -1)
Fprintf(stderr,
"color %c not in colormap!\n", c[0]);
else {
pixels[j][i].r = ColorMap[CM_RED][k];
pixels[j][i].g = ColorMap[CM_GREEN][k];
pixels[j][i].b = ColorMap[CM_BLUE][k];
}
}
}
if ( ph ) {
/* remember it for later */
memcpy( placeholder, pixels, sizeof(placeholder) );
}
if (fscanf(txtfile, "%1s ", c) < 0) {
Fprintf(stderr, "unexpected EOF\n");
return FALSE;
}
if (c[0] != '}') {
Fprintf(stderr, "didn't find expected '}'\n");
return FALSE;
}
#ifdef _DCC
/* DICE again... it doesn't seem to eat whitespace after the } like
* it should, so we have to do so manually.
*/
while ((*c = fgetc(txtfile)) != EOF && isspace(*c))
;
ungetc(*c, txtfile);
#endif
return TRUE;
}
static void
write_txttile(txtfile, pixels)
FILE *txtfile;
pixel (*pixels)[TILE_X];
{
const char *p;
const char *type;
int i, j, k;
if ( memcmp(placeholder, pixels, sizeof(placeholder)) == 0 )
type = "placeholder";
else
type = "tile";
if (tile_set == 0)
Fprintf(txtfile, "# %s %d (unknown)\n", type, tile_set_indx);
else {
p = tilename(tile_set, tile_set_indx);
if (p)
Fprintf(txtfile, "# %s %d (%s)\n", type, tile_set_indx, p);
else
Fprintf(txtfile, "# %s %d (null)\n", type, tile_set_indx);
}
tile_set_indx++;
Fprintf(txtfile, "{\n");
for (j = 0; j < TILE_Y; j++) {
Fprintf(txtfile, " ");
for (i = 0; i < TILE_X; i++) {
for (k = 0; k < num_colors; k++) {
if (ColorMap[CM_RED][k] == pixels[j][i].r &&
ColorMap[CM_GREEN][k] == pixels[j][i].g &&
ColorMap[CM_BLUE][k] == pixels[j][i].b)
break;
}
if (k >= num_colors)
Fprintf(stderr, "color not in colormap!\n");
(void) fputc(charcolors[k], txtfile);
}
Fprintf(txtfile, "\n");
}
Fprintf(txtfile, "}\n");
}
/* initialize main colormap from globally accessed ColorMap */
void
init_colormap()
{
int i;
colorsinmainmap = colorsinmap;
for (i = 0; i < colorsinmap; i++) {
MainColorMap[CM_RED][i] = ColorMap[CM_RED][i];
MainColorMap[CM_GREEN][i] = ColorMap[CM_GREEN][i];
MainColorMap[CM_BLUE][i] = ColorMap[CM_BLUE][i];
}
}
/* merge new colors from ColorMap into MainColorMap */
void
merge_colormap()
{
int i, j;
for (i = 0; i < colorsinmap; i++) {
for (j = 0; j < colorsinmainmap; j++) {
if (MainColorMap[CM_RED][j] == ColorMap[CM_RED][i] &&
MainColorMap[CM_GREEN][j] == ColorMap[CM_GREEN][i] &&
MainColorMap[CM_BLUE][j] == ColorMap[CM_BLUE][i])
break;
}
if (j >= colorsinmainmap) { /* new color */
if (colorsinmainmap >= MAXCOLORMAPSIZE) {
Fprintf(stderr,
"Too many colors to merge -- excess ignored.\n");
}
j = colorsinmainmap;
MainColorMap[CM_RED][j] = ColorMap[CM_RED][i];
MainColorMap[CM_GREEN][j] = ColorMap[CM_GREEN][i];
MainColorMap[CM_BLUE][j] = ColorMap[CM_BLUE][i];
colorsinmainmap++;
}
}
}
boolean
fopen_text_file(filename, type)
const char *filename;
const char *type;
{
const char *p;
int i;
if (tile_file != (FILE *)0) {
Fprintf(stderr, "can only open one text file at at time\n");
return FALSE;
}
tile_file = fopen(filename, type);
if (tile_file == (FILE *)0) {
Fprintf(stderr, "cannot open text file %s\n", filename);
return FALSE;
}
p = rindex(filename, '/');
if (p) p++;
else p = filename;
tile_set = 0;
for (i = 0; i < SIZE(text_sets); i++) {
if (!strcmp(p, text_sets[i]))
tile_set = i+1;
}
tile_set_indx = 0;
if (!strcmp(type, RDTMODE)) {
/* Fill placeholder with noise */
if ( !placeholder_init ) {
placeholder_init++;
for ( i=0; i<sizeof(placeholder); i++ )
((char*)placeholder)[i]=i%256;
}
read_text_colormap(tile_file);
if (!colorsinmainmap)
init_colormap();
else
merge_colormap();
return TRUE;
} else if (!strcmp(type, WRTMODE)) {
if (!colorsinmainmap) {
Fprintf(stderr, "no colormap set yet\n");
return FALSE;
}
return(write_text_colormap(tile_file));
} else {
Fprintf(stderr, "bad mode (%s) for fopen_text_file\n", type);
return FALSE;
}
}
boolean
read_text_tile(pixels)
pixel (*pixels)[TILE_X];
{
return(read_txttile(tile_file, pixels));
}
boolean
write_text_tile(pixels)
pixel (*pixels)[TILE_X];
{
write_txttile(tile_file, pixels);
return TRUE;
}
int
fclose_text_file()
{
int ret;
ret = fclose(tile_file);
tile_file = (FILE *)0;
return ret;
}
|