2 * @file cmd-process-screen.c
9 #include "cmd/cmd-process-screen.h"
10 #include "cmd/cmd-draw.h"
14 // Encode the screen colors
15 static char hack[17] = "dwsorgbuDWvyRGBU";
17 static concptr tags[4] = { "HEADER_START:", "HEADER_END:", "FOOTER_START:", "FOOTER_END:", };
18 static concptr html_head[3] = { "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n", "<pre>", 0, };
19 static concptr html_foot[3] = { "</pre>\n", "</body>\n</html>\n", 0, };
22 * todo io/ 以下に移したいところだが、このファイルの行数も大したことがないので一旦保留
23 * @brief 一時ファイルを読み込み、ファイルに書き出す
24 * @param fff ファイルへの参照ポインタ
25 * @param tempfff 一時ファイルへの参照ポインタ
27 * @param buf_size バッファサイズ
30 static void read_temporary_file(FILE *fff, FILE *tmpfff, char buf[], size_t buf_size, int num_tag)
32 bool is_first_line = TRUE;
33 int next_tag = num_tag + 1;
34 while (!my_fgets(tmpfff, buf, buf_size))
38 if (strncmp(buf, tags[num_tag], strlen(tags[num_tag])) == 0)
39 is_first_line = FALSE;
44 if (strncmp(buf, tags[next_tag], strlen(tags[next_tag])) == 0)
47 fprintf(fff, "%s\n", buf);
56 * @param fff 記念撮影ファイルへの参照ポインタ
59 static void screen_dump_one_line(int wid, int y, FILE *fff)
61 TERM_COLOR a = 0, old_a = 0;
63 for (TERM_LEN x = 0; x < wid - 1; x++)
66 (void)(Term_what(x, y, &a, &c));
69 case '&': cc = "&"; break;
70 case '<': cc = "<"; break;
71 case '>': cc = ">"; break;
73 case 0x1f: c = '.'; break;
74 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
79 if ((y == 0 && x == 0) || a != old_a)
81 int rv = angband_color_table[a][1];
82 int gv = angband_color_table[a][2];
83 int bv = angband_color_table[a][3];
84 fprintf(fff, "%s<font color=\"#%02x%02x%02x\">",
85 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
90 fprintf(fff, "%s", cc);
92 fprintf(fff, "%c", c);
98 * @brief 記念撮影を行方向にスイープする
101 * @param fff 記念撮影ファイルへの参照ポインタ
104 static void screen_dump_lines(int wid, int hgt, FILE *fff)
106 for (TERM_LEN y = 0; y < hgt; y++)
111 screen_dump_one_line(wid, y, fff);
117 * @brief ファイルへ書き込めない場合にエラーを表示する
118 * @param fff ダンプファイルへの参照ポインタ
120 * @return ファイルへ書き込めるならTRUE、書き込めないならFALSE
122 static bool check_screen_html_can_open(FILE *fff, char *filename, int message)
124 if (fff) return TRUE;
125 if (message == 0) return FALSE;
127 msg_format(_("ファイル %s を開けませんでした。", "Failed to open file %s."), filename);
134 * @brief HTMLヘッダを書き込む
135 * @param tmpfff 一時ファイルへの参照ポインタ
136 * @param fff 記念撮影ファイルへの参照ポインタ
138 * @param buf_size バッファサイズ
140 static void write_html_header(FILE *tmpfff, FILE *fff, char buf[], size_t buf_size)
144 read_temporary_file(fff, tmpfff, buf, buf_size, 0);
148 for (int i = 0; html_head[i]; i++)
149 fputs(html_head[i], fff);
154 * @brief HTMLフッタを書き込む
155 * @param tmpfff 一時ファイルへの参照ポインタ
156 * @param fff 記念撮影ファイルへの参照ポインタ
158 * @param buf_size バッファサイズ
160 static void write_html_footer(FILE *tmpfff, FILE *fff, char buf[], size_t buf_size)
162 fprintf(fff, "</font>");
165 for (int i = 0; html_foot[i]; i++)
166 fputs(html_foot[i], fff);
171 read_temporary_file(fff, tmpfff, buf, buf_size, 2);
179 void do_cmd_save_screen_html_aux(char *filename, int message)
182 Term_get_size(&wid, &hgt);
183 FILE_TYPE(FILE_TYPE_TEXT);
185 fff = my_fopen(filename, "w");
186 if (!check_screen_html_can_open(fff, filename, message)) return;
188 if (message) screen_save();
191 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "htmldump.prf");
193 tmpfff = my_fopen(buf, "r");
194 write_html_header(tmpfff, fff, buf, sizeof(buf));
195 screen_dump_lines(wid, hgt, fff);
196 write_html_footer(tmpfff, fff, buf, sizeof(buf));
200 msg_print(_("画面(記念撮影)をファイルに書き出しました。", "Screen dump saved."));
210 * @brief HTML方式で記念撮影する / Save a screen dump to a file
214 static void do_cmd_save_screen_html(void)
216 char buf[1024], tmp[256] = "screen.html";
218 if (!get_string(_("ファイル名: ", "File name: "), tmp, 80))
220 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp);
224 do_cmd_save_screen_html_aux(buf, 1);
229 * @brief 記念撮影の方式を問い合わせる
230 * @param html_dump HTMLダンプするか否か
231 * @return ダンプするならTRUE、キャンセルならFALSE
233 static bool ask_html_dump(bool *html_dump)
238 if (c == 'Y' || c == 'y')
244 if (c == 'H' || c == 'h')
260 * @brief ファイルへ書き込めない場合にエラーを表示する
261 * @param fff ダンプファイルへの参照ポインタ
263 * @return ファイルへ書き込めるならTRUE、書き込めないならFALSE
265 static bool check_screen_text_can_open(FILE *fff, char buf[])
267 if (fff) return TRUE;
269 msg_format(_("ファイル %s を開けませんでした。", "Failed to open file %s."), buf);
276 * todo どこかバグっていて、(恐らく初期化されていない)変な文字列まで出力される
277 * @brief テキスト方式で記念撮影する
280 * @return 記念撮影に成功したらTRUE、ファイルが開けなかったらFALSE
282 static bool do_cmd_save_screen_text(int wid, int hgt)
288 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "dump.txt");
289 FILE_TYPE(FILE_TYPE_TEXT);
290 fff = my_fopen(buf, "w");
291 if (!check_screen_text_can_open(fff, buf)) return FALSE;
294 for (TERM_LEN y = 0; y < hgt; y++)
297 for (x = 0; x < wid - 1; x++)
299 (void)(Term_what(x, y, &a, &c));
304 fprintf(fff, "%s\n", buf);
308 for (TERM_LEN y = 0; y < hgt; y++)
311 for (x = 0; x < wid - 1; x++)
313 (void)(Term_what(x, y, &a, &c));
314 buf[x] = hack[a & 0x0F];
318 fprintf(fff, "%s\n", buf);
323 msg_print(_("画面(記念撮影)をファイルに書き出しました。", "Screen dump saved."));
331 * @brief 記念撮影のためにグラフィック使用をOFFにする
332 * @param creature_ptr プレーヤーへの参照ポインタ
333 * @param handle_stuff 画面更新用の関数ポインタ
334 * @return 記念撮影直前のグラフィックオプション
336 static bool update_use_graphics(player_type *creature_ptr, void(*handle_stuff)(player_type*), void(*process_autopick_file_command)(char*))
338 if (!use_graphics) return TRUE;
340 use_graphics = FALSE;
341 reset_visuals(creature_ptr, process_autopick_file_command);
342 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
343 (*handle_stuff)(creature_ptr);
349 * Save a screen dump to a file
350 * @param creature_ptr プレーヤーへの参照ポインタ
351 * @param handle_stuff 画面更新用の関数ポインタ
354 void do_cmd_save_screen(player_type *creature_ptr, void(*handle_stuff)(player_type*), void(*process_autopick_file_command)(char*))
356 prt(_("記念撮影しますか? [(y)es/(h)tml/(n)o] ", "Save screen dump? [(y)es/(h)tml/(n)o] "), 0, 0);
358 if (!ask_html_dump(&html_dump)) return;
361 Term_get_size(&wid, &hgt);
363 bool old_use_graphics = update_use_graphics(creature_ptr, handle_stuff, process_autopick_file_command);
367 do_cmd_save_screen_html();
368 do_cmd_redraw(creature_ptr);
370 else if (!do_cmd_save_screen_text(wid, hgt))
375 if (old_use_graphics) return;
378 reset_visuals(creature_ptr, process_autopick_file_command);
379 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
380 handle_stuff(creature_ptr);
385 * @brief 白文字だけ画面に描画する (todo 目的は不明瞭)
387 * @param fff 記念撮影ファイルへの参照ポインタ
390 * @return ファイルが読み込めなくなったらFALSEで抜ける
392 static bool draw_white_characters(char buf[], FILE *fff, int wid, int hgt)
395 for (TERM_LEN y = 0; okay; y++)
397 if (!fgets(buf, 1024, fff)) okay = FALSE;
399 if (buf[0] == '\n' || buf[0] == '\0') break;
400 if (y >= hgt) continue;
402 for (TERM_LEN x = 0; x < wid - 1; x++)
404 if (buf[x] == '\n' || buf[x] == '\0') break;
406 Term_draw(x, y, TERM_WHITE, buf[x]);
415 * @brief 白以外の文字を画面に描画する (todo 目的は不明瞭)
417 * @param fff 記念撮影ファイルへの参照ポインタ
420 * @param 白文字が途中で読み込めなくなっていたらTRUE
423 static void draw_colored_characters(char buf[], FILE *fff, int wid, int hgt, bool okay)
425 TERM_COLOR a = TERM_DARK;
427 for (TERM_LEN y = 0; okay; y++)
429 if (!fgets(buf, 1024, fff)) okay = FALSE;
431 if (buf[0] == '\n' || buf[0] == '\0') break;
432 if (y >= hgt) continue;
434 for (TERM_LEN x = 0; x < wid - 1; x++)
436 if (buf[x] == '\n' || buf[x] == '\0') break;
438 (void)(Term_what(x, y, &a, &c));
439 for (int i = 0; i < 16; i++)
441 if (hack[i] == buf[x]) a = (byte)i;
444 Term_draw(x, y, a, c);
451 * @brief Load a screen dump from a file
455 void do_cmd_load_screen(void)
460 Term_get_size(&wid, &hgt);
461 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "dump.txt");
462 fff = my_fopen(buf, "r");
465 msg_format(_("%s を開くことができませんでした。", "Failed to open %s."), buf);
472 bool okay = draw_white_characters(buf, fff, wid, hgt);
473 draw_colored_characters(buf, fff, wid, hgt, okay);
476 prt(_("ファイルに書き出された画面(記念撮影)をロードしました。", "Screen dump loaded."), 0, 0);