OSDN Git Service

[Refactor] #40236 Removed dependency from cmd-process-screen to core
[hengband/hengband.git] / src / cmd / cmd-process-screen.c
1 /*!
2 * @file cmd-process-screen.c
3 * @brief 記念撮影のセーブとロード
4 * @date 2020/04/22
5 * @Author Hourier
6 */
7
8 #include "angband.h"
9 #include "cmd/cmd-process-screen.h"
10 #include "cmd/cmd-draw.h"
11 #include "files.h"
12 #include "gameterm.h"
13
14 // Encode the screen colors
15 static char hack[17] = "dwsorgbuDWvyRGBU";
16
17 void do_cmd_save_screen_html_aux(char *filename, int message)
18 {
19         concptr tags[4] = {
20                 "HEADER_START:",
21                 "HEADER_END:",
22                 "FOOTER_START:",
23                 "FOOTER_END:",
24         };
25         concptr html_head[] = {
26                 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
27                 "<pre>",
28                 0,
29         };
30         concptr html_foot[] = {
31                 "</pre>\n",
32                 "</body>\n</html>\n",
33                 0,
34         };
35
36         TERM_LEN wid, hgt;
37         Term_get_size(&wid, &hgt);
38         FILE_TYPE(FILE_TYPE_TEXT);
39         FILE *fff;
40         fff = my_fopen(filename, "w");
41         if (!fff)
42         {
43                 if (message)
44                 {
45                         msg_format(_("ファイル %s を開けませんでした。", "Failed to open file %s."), filename);
46                         msg_print(NULL);
47                 }
48
49                 return;
50         }
51
52         if (message) screen_save();
53
54         char buf[2048];
55         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "htmldump.prf");
56         FILE *tmpfff;
57         tmpfff = my_fopen(buf, "r");
58         if (!tmpfff)
59         {
60                 for (int i = 0; html_head[i]; i++)
61                         fputs(html_head[i], fff);
62         }
63         else
64         {
65                 bool is_first_line = TRUE;
66                 while (!my_fgets(tmpfff, buf, sizeof(buf)))
67                 {
68                         if (is_first_line)
69                         {
70                                 if (strncmp(buf, tags[0], strlen(tags[0])) == 0)
71                                         is_first_line = FALSE;
72                         }
73                         else
74                         {
75                                 if (strncmp(buf, tags[1], strlen(tags[1])) == 0)
76                                         break;
77                                 fprintf(fff, "%s\n", buf);
78                         }
79                 }
80         }
81
82         for (TERM_LEN y = 0; y < hgt; y++)
83         {
84                 if (y != 0) fprintf(fff, "\n");
85
86                 TERM_COLOR a = 0, old_a = 0;
87                 char c = ' ';
88                 for (TERM_LEN x = 0; x < wid - 1; x++)
89                 {
90                         concptr cc = NULL;
91                         (void)(Term_what(x, y, &a, &c));
92                         switch (c)
93                         {
94                         case '&': cc = "&amp;"; break;
95                         case '<': cc = "&lt;"; break;
96                         case '>': cc = "&gt;"; break;
97 #ifdef WINDOWS
98                         case 0x1f: c = '.'; break;
99                         case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
100 #endif
101                         }
102
103                         a = a & 0x0F;
104                         if ((y == 0 && x == 0) || a != old_a)
105                         {
106                                 int rv = angband_color_table[a][1];
107                                 int gv = angband_color_table[a][2];
108                                 int bv = angband_color_table[a][3];
109                                 fprintf(fff, "%s<font color=\"#%02x%02x%02x\">",
110                                         ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
111                                 old_a = a;
112                         }
113
114                         if (cc)
115                                 fprintf(fff, "%s", cc);
116                         else
117                                 fprintf(fff, "%c", c);
118                 }
119         }
120
121         fprintf(fff, "</font>");
122         if (!tmpfff)
123         {
124                 for (int i = 0; html_foot[i]; i++)
125                         fputs(html_foot[i], fff);
126         }
127         else
128         {
129                 rewind(tmpfff);
130                 bool is_first_line = TRUE;
131                 while (!my_fgets(tmpfff, buf, sizeof(buf)))
132                 {
133                         if (is_first_line)
134                         {
135                                 if (strncmp(buf, tags[2], strlen(tags[2])) == 0)
136                                         is_first_line = FALSE;
137                         }
138                         else
139                         {
140                                 if (strncmp(buf, tags[3], strlen(tags[3])) == 0)
141                                         break;
142                                 fprintf(fff, "%s\n", buf);
143                         }
144                 }
145
146                 my_fclose(tmpfff);
147         }
148
149         fprintf(fff, "\n");
150         my_fclose(fff);
151         if (message)
152         {
153                 msg_print(_("画面(記念撮影)をファイルに書き出しました。", "Screen dump saved."));
154                 msg_print(NULL);
155         }
156
157         if (message)
158                 screen_load();
159 }
160
161
162 /*
163  * Hack -- save a screen dump to a file
164  */
165 static void do_cmd_save_screen_html(void)
166 {
167         char buf[1024], tmp[256] = "screen.html";
168
169         if (!get_string(_("ファイル名: ", "File name: "), tmp, 80))
170                 return;
171         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp);
172
173         msg_print(NULL);
174
175         do_cmd_save_screen_html_aux(buf, 1);
176 }
177
178
179 /*
180  * Save a screen dump to a file
181  * @param creature_ptr プレーヤーへの参照ポインタ
182  * @return なし
183  */
184 void do_cmd_save_screen(player_type *creature_ptr, void(*handle_stuff)(player_type*))
185 {
186         prt(_("記念撮影しますか? [(y)es/(h)tml/(n)o] ", "Save screen dump? [(y)es/(h)tml/(n)o] "), 0, 0);
187         bool html_dump = FALSE;
188         while (TRUE)
189         {
190                 char c = inkey();
191                 if (c == 'Y' || c == 'y')
192                         break;
193                 else if (c == 'H' || c == 'h')
194                 {
195                         html_dump = TRUE;
196                         break;
197                 }
198                 else
199                 {
200                         prt("", 0, 0);
201                         return;
202                 }
203         }
204
205         int wid, hgt;
206         Term_get_size(&wid, &hgt);
207
208         bool old_use_graphics = use_graphics;
209         if (old_use_graphics)
210         {
211                 use_graphics = FALSE;
212                 reset_visuals(creature_ptr);
213                 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
214                 (*handle_stuff)(creature_ptr);
215         }
216
217         if (html_dump)
218         {
219                 do_cmd_save_screen_html();
220                 do_cmd_redraw(creature_ptr);
221         }
222         else
223         {
224                 TERM_LEN y, x;
225                 TERM_COLOR a = 0;
226                 SYMBOL_CODE c = ' ';
227                 FILE *fff;
228                 char buf[1024];
229                 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "dump.txt");
230                 FILE_TYPE(FILE_TYPE_TEXT);
231                 fff = my_fopen(buf, "w");
232                 if (!fff)
233                 {
234                         msg_format(_("ファイル %s を開けませんでした。", "Failed to open file %s."), buf);
235                         msg_print(NULL);
236                         return;
237                 }
238
239                 screen_save();
240                 for (y = 0; y < hgt; y++)
241                 {
242                         for (x = 0; x < wid - 1; x++)
243                         {
244                                 (void)(Term_what(x, y, &a, &c));
245                                 buf[x] = c;
246                         }
247
248                         buf[x] = '\0';
249                         fprintf(fff, "%s\n", buf);
250                 }
251
252                 fprintf(fff, "\n");
253                 for (y = 0; y < hgt; y++)
254                 {
255                         for (x = 0; x < wid - 1; x++)
256                         {
257                                 (void)(Term_what(x, y, &a, &c));
258                                 buf[x] = hack[a & 0x0F];
259                         }
260
261                         buf[x] = '\0';
262                         fprintf(fff, "%s\n", buf);
263                 }
264
265                 fprintf(fff, "\n");
266                 my_fclose(fff);
267                 msg_print(_("画面(記念撮影)をファイルに書き出しました。", "Screen dump saved."));
268                 msg_print(NULL);
269                 screen_load();
270         }
271
272         if (!old_use_graphics) return;
273
274         use_graphics = TRUE;
275         reset_visuals(creature_ptr);
276         creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
277         handle_stuff(creature_ptr);
278 }
279
280
281 /*
282  * @brief Load a screen dump from a file
283  */
284 void do_cmd_load_screen(void)
285 {
286         TERM_COLOR a = 0;
287         SYMBOL_CODE c = ' ';
288         bool okay = TRUE;
289         FILE *fff;
290         char buf[1024];
291         TERM_LEN wid, hgt;
292         Term_get_size(&wid, &hgt);
293         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "dump.txt");
294         fff = my_fopen(buf, "r");
295         if (!fff)
296         {
297                 msg_format(_("%s を開くことができませんでした。", "Failed to open %s."), buf);
298                 msg_print(NULL);
299                 return;
300         }
301
302         screen_save();
303         Term_clear();
304         for (TERM_LEN y = 0; okay; y++)
305         {
306                 if (!fgets(buf, 1024, fff)) okay = FALSE;
307
308                 if (buf[0] == '\n' || buf[0] == '\0') break;
309                 if (y >= hgt) continue;
310
311                 for (TERM_LEN x = 0; x < wid - 1; x++)
312                 {
313                         if (buf[x] == '\n' || buf[x] == '\0') break;
314
315                         Term_draw(x, y, TERM_WHITE, buf[x]);
316                 }
317         }
318
319         for (TERM_LEN y = 0; okay; y++)
320         {
321                 if (!fgets(buf, 1024, fff)) okay = FALSE;
322
323                 if (buf[0] == '\n' || buf[0] == '\0') break;
324                 if (y >= hgt) continue;
325
326                 for (TERM_LEN x = 0; x < wid - 1; x++)
327                 {
328                         if (buf[x] == '\n' || buf[x] == '\0') break;
329
330                         (void)(Term_what(x, y, &a, &c));
331                         for (int i = 0; i < 16; i++)
332                         {
333                                 if (hack[i] == buf[x]) a = (byte)i;
334                         }
335
336                         Term_draw(x, y, a, c);
337                 }
338         }
339
340         my_fclose(fff);
341         prt(_("ファイルに書き出された画面(記念撮影)をロードしました。", "Screen dump loaded."), 0, 0);
342         flush();
343         inkey();
344         screen_load();
345 }