PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/nethack/win/share/tiletext.c

https://bitbucket.org/clivecrous/newt
C | 332 lines | 280 code | 38 blank | 14 comment | 73 complexity | bafadfe033214c966560a1af19fb1407 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /* SCCS Id: @(#)tiletext.c 3.4 1999/10/24 */
  2. /* NetHack may be freely redistributed. See license for details. */
  3. #include "config.h"
  4. #include "tile.h"
  5. pixval ColorMap[3][MAXCOLORMAPSIZE];
  6. int colorsinmap;
  7. pixval MainColorMap[3][MAXCOLORMAPSIZE];
  8. int colorsinmainmap;
  9. static short color_index[MAXCOLORMAPSIZE];
  10. static int num_colors;
  11. static char charcolors[MAXCOLORMAPSIZE];
  12. static int placeholder_init = 0;
  13. static pixel placeholder[TILE_Y][TILE_X];
  14. static FILE *tile_file;
  15. static int tile_set, tile_set_indx;
  16. #if (TILE_X==8)
  17. static const char *text_sets[] = { "monthin.txt", "objthin.txt", "oththin.txt" };
  18. #else
  19. static const char *text_sets[] = { "monsters.txt", "objects.txt", "other.txt" };
  20. #endif
  21. extern const char *FDECL(tilename, (int, int));
  22. static void FDECL(read_text_colormap, (FILE *));
  23. static boolean FDECL(write_text_colormap, (FILE *));
  24. static boolean FDECL(read_txttile, (FILE *, pixel(*)[TILE_X]));
  25. static void FDECL(write_txttile, (FILE *, pixel(*)[TILE_X]));
  26. /* Ugh. DICE doesn't like %[A-Z], so we have to spell it out... */
  27. #define FORMAT_STRING \
  28. "%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789] = (%d, %d, %d) "
  29. static void
  30. read_text_colormap(txtfile)
  31. FILE *txtfile;
  32. {
  33. int i, r, g, b;
  34. char c[2];
  35. for (i = 0; i < MAXCOLORMAPSIZE; i++)
  36. color_index[i] = -1;
  37. num_colors = 0;
  38. while (fscanf(txtfile, FORMAT_STRING, c, &r, &g, &b) == 4) {
  39. color_index[(int) c[0]] = num_colors;
  40. ColorMap[CM_RED][num_colors] = r;
  41. ColorMap[CM_GREEN][num_colors] = g;
  42. ColorMap[CM_BLUE][num_colors] = b;
  43. num_colors++;
  44. }
  45. colorsinmap = num_colors;
  46. }
  47. #undef FORMAT_STRING
  48. static boolean
  49. write_text_colormap(txtfile)
  50. FILE *txtfile;
  51. {
  52. int i;
  53. char c;
  54. num_colors = colorsinmainmap;
  55. if (num_colors > 62) {
  56. Fprintf(stderr, "too many colors (%d)\n", num_colors);
  57. return FALSE;
  58. }
  59. for (i = 0; i < num_colors; i++) {
  60. if (i < 26) c = 'A' + i;
  61. else if (i < 52) c = 'a' + i - 26;
  62. else c = '0' + i - 52;
  63. charcolors[i] = c;
  64. Fprintf(txtfile, "%c = (%d, %d, %d)\n", c,
  65. (int)MainColorMap[CM_RED][i],
  66. (int)MainColorMap[CM_GREEN][i],
  67. (int)MainColorMap[CM_BLUE][i]);
  68. }
  69. return TRUE;
  70. }
  71. static boolean
  72. read_txttile(txtfile, pixels)
  73. FILE *txtfile;
  74. pixel (*pixels)[TILE_X];
  75. {
  76. int ph, i, j, k;
  77. char buf[BUFSZ], ttype[BUFSZ];
  78. const char *p;
  79. char c[2];
  80. if (fscanf(txtfile, "# %s %d (%[^)])", ttype, &i, buf) <= 0)
  81. return FALSE;
  82. ph = strcmp(ttype, "placeholder") == 0;
  83. if (!ph && strcmp(ttype,"tile") != 0)
  84. Fprintf(stderr,
  85. "Keyword \"%s\" unexpected for entry %d\n",
  86. ttype, i);
  87. if (tile_set != 0) {
  88. /* check tile name, but not relative number, which will
  89. * change when tiles are added
  90. */
  91. p = tilename(tile_set, tile_set_indx);
  92. if (p && strcmp(p, buf)) {
  93. Fprintf(stderr, "warning: for tile %d (numbered %d) of %s,\n",
  94. tile_set_indx, i, text_sets[tile_set-1]);
  95. Fprintf(stderr, "\tfound '%s' while expecting '%s'\n",
  96. buf, p);
  97. }
  98. }
  99. tile_set_indx++;
  100. /* look for non-whitespace at each stage */
  101. if (fscanf(txtfile, "%1s", c) < 0) {
  102. Fprintf(stderr, "unexpected EOF\n");
  103. return FALSE;
  104. }
  105. if (c[0] != '{') {
  106. Fprintf(stderr, "didn't find expected '{'\n");
  107. return FALSE;
  108. }
  109. for (j = 0; j < TILE_Y; j++) {
  110. for (i = 0; i < TILE_X; i++) {
  111. if (fscanf(txtfile, "%1s", c) < 0) {
  112. Fprintf(stderr, "unexpected EOF\n");
  113. return FALSE;
  114. }
  115. k = color_index[(int) c[0]];
  116. if (k == -1)
  117. Fprintf(stderr,
  118. "color %c not in colormap!\n", c[0]);
  119. else {
  120. pixels[j][i].r = ColorMap[CM_RED][k];
  121. pixels[j][i].g = ColorMap[CM_GREEN][k];
  122. pixels[j][i].b = ColorMap[CM_BLUE][k];
  123. }
  124. }
  125. }
  126. if ( ph ) {
  127. /* remember it for later */
  128. memcpy( placeholder, pixels, sizeof(placeholder) );
  129. }
  130. if (fscanf(txtfile, "%1s ", c) < 0) {
  131. Fprintf(stderr, "unexpected EOF\n");
  132. return FALSE;
  133. }
  134. if (c[0] != '}') {
  135. Fprintf(stderr, "didn't find expected '}'\n");
  136. return FALSE;
  137. }
  138. #ifdef _DCC
  139. /* DICE again... it doesn't seem to eat whitespace after the } like
  140. * it should, so we have to do so manually.
  141. */
  142. while ((*c = fgetc(txtfile)) != EOF && isspace(*c))
  143. ;
  144. ungetc(*c, txtfile);
  145. #endif
  146. return TRUE;
  147. }
  148. static void
  149. write_txttile(txtfile, pixels)
  150. FILE *txtfile;
  151. pixel (*pixels)[TILE_X];
  152. {
  153. const char *p;
  154. const char *type;
  155. int i, j, k;
  156. if ( memcmp(placeholder, pixels, sizeof(placeholder)) == 0 )
  157. type = "placeholder";
  158. else
  159. type = "tile";
  160. if (tile_set == 0)
  161. Fprintf(txtfile, "# %s %d (unknown)\n", type, tile_set_indx);
  162. else {
  163. p = tilename(tile_set, tile_set_indx);
  164. if (p)
  165. Fprintf(txtfile, "# %s %d (%s)\n", type, tile_set_indx, p);
  166. else
  167. Fprintf(txtfile, "# %s %d (null)\n", type, tile_set_indx);
  168. }
  169. tile_set_indx++;
  170. Fprintf(txtfile, "{\n");
  171. for (j = 0; j < TILE_Y; j++) {
  172. Fprintf(txtfile, " ");
  173. for (i = 0; i < TILE_X; i++) {
  174. for (k = 0; k < num_colors; k++) {
  175. if (ColorMap[CM_RED][k] == pixels[j][i].r &&
  176. ColorMap[CM_GREEN][k] == pixels[j][i].g &&
  177. ColorMap[CM_BLUE][k] == pixels[j][i].b)
  178. break;
  179. }
  180. if (k >= num_colors)
  181. Fprintf(stderr, "color not in colormap!\n");
  182. (void) fputc(charcolors[k], txtfile);
  183. }
  184. Fprintf(txtfile, "\n");
  185. }
  186. Fprintf(txtfile, "}\n");
  187. }
  188. /* initialize main colormap from globally accessed ColorMap */
  189. void
  190. init_colormap()
  191. {
  192. int i;
  193. colorsinmainmap = colorsinmap;
  194. for (i = 0; i < colorsinmap; i++) {
  195. MainColorMap[CM_RED][i] = ColorMap[CM_RED][i];
  196. MainColorMap[CM_GREEN][i] = ColorMap[CM_GREEN][i];
  197. MainColorMap[CM_BLUE][i] = ColorMap[CM_BLUE][i];
  198. }
  199. }
  200. /* merge new colors from ColorMap into MainColorMap */
  201. void
  202. merge_colormap()
  203. {
  204. int i, j;
  205. for (i = 0; i < colorsinmap; i++) {
  206. for (j = 0; j < colorsinmainmap; j++) {
  207. if (MainColorMap[CM_RED][j] == ColorMap[CM_RED][i] &&
  208. MainColorMap[CM_GREEN][j] == ColorMap[CM_GREEN][i] &&
  209. MainColorMap[CM_BLUE][j] == ColorMap[CM_BLUE][i])
  210. break;
  211. }
  212. if (j >= colorsinmainmap) { /* new color */
  213. if (colorsinmainmap >= MAXCOLORMAPSIZE) {
  214. Fprintf(stderr,
  215. "Too many colors to merge -- excess ignored.\n");
  216. }
  217. j = colorsinmainmap;
  218. MainColorMap[CM_RED][j] = ColorMap[CM_RED][i];
  219. MainColorMap[CM_GREEN][j] = ColorMap[CM_GREEN][i];
  220. MainColorMap[CM_BLUE][j] = ColorMap[CM_BLUE][i];
  221. colorsinmainmap++;
  222. }
  223. }
  224. }
  225. boolean
  226. fopen_text_file(filename, type)
  227. const char *filename;
  228. const char *type;
  229. {
  230. const char *p;
  231. int i;
  232. if (tile_file != (FILE *)0) {
  233. Fprintf(stderr, "can only open one text file at at time\n");
  234. return FALSE;
  235. }
  236. tile_file = fopen(filename, type);
  237. if (tile_file == (FILE *)0) {
  238. Fprintf(stderr, "cannot open text file %s\n", filename);
  239. return FALSE;
  240. }
  241. p = rindex(filename, '/');
  242. if (p) p++;
  243. else p = filename;
  244. tile_set = 0;
  245. for (i = 0; i < SIZE(text_sets); i++) {
  246. if (!strcmp(p, text_sets[i]))
  247. tile_set = i+1;
  248. }
  249. tile_set_indx = 0;
  250. if (!strcmp(type, RDTMODE)) {
  251. /* Fill placeholder with noise */
  252. if ( !placeholder_init ) {
  253. placeholder_init++;
  254. for ( i=0; i<sizeof(placeholder); i++ )
  255. ((char*)placeholder)[i]=i%256;
  256. }
  257. read_text_colormap(tile_file);
  258. if (!colorsinmainmap)
  259. init_colormap();
  260. else
  261. merge_colormap();
  262. return TRUE;
  263. } else if (!strcmp(type, WRTMODE)) {
  264. if (!colorsinmainmap) {
  265. Fprintf(stderr, "no colormap set yet\n");
  266. return FALSE;
  267. }
  268. return(write_text_colormap(tile_file));
  269. } else {
  270. Fprintf(stderr, "bad mode (%s) for fopen_text_file\n", type);
  271. return FALSE;
  272. }
  273. }
  274. boolean
  275. read_text_tile(pixels)
  276. pixel (*pixels)[TILE_X];
  277. {
  278. return(read_txttile(tile_file, pixels));
  279. }
  280. boolean
  281. write_text_tile(pixels)
  282. pixel (*pixels)[TILE_X];
  283. {
  284. write_txttile(tile_file, pixels);
  285. return TRUE;
  286. }
  287. int
  288. fclose_text_file()
  289. {
  290. int ret;
  291. ret = fclose(tile_file);
  292. tile_file = (FILE *)0;
  293. return ret;
  294. }