4 * Copyright (c) 1997 Ben Harrison
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies.
11 /* Purpose: a generic, efficient, terminal window package -BEN- */
18 /* Special flags in the attr data */
19 #define AF_BIGTILE2 0xf0
23 #define AF_KANJI1 0x10
24 #define AF_KANJI2 0x20
25 #define AF_KANJIC 0x0f
28 * °À¤ËÁ´³Ñʸ»ú¤Î£±¥Ð¥¤¥ÈÌÜ¡¢£²¥Ð¥¤¥ÈÌܤ⵲±¡£
33 * This file provides a generic, efficient, terminal window package,
34 * which can be used not only on standard terminal environments such
35 * as dumb terminals connected to a Unix box, but also in more modern
36 * "graphic" environments, such as the Macintosh or Unix/X11.
38 * Each "window" works like a standard "dumb terminal", that is, it
39 * can display a two dimensional array of grids containing colored
40 * textual symbols, plus an optional cursor, and it can be used to
41 * get keypress events from the user.
43 * In fact, this package can simply be used, if desired, to support
44 * programs which will look the same on a dumb terminal as they do
45 * on a graphic platform such as the Macintosh.
47 * This package was designed to help port the game "Angband" to a wide
48 * variety of different platforms. Angband, like many other games in
49 * the "rogue-like" heirarchy, requires, at the minimum, the ability
50 * to display "colored textual symbols" in a standard 80x24 "window",
51 * such as that provided by most dumb terminals, and many old personal
52 * computers, and to check for "keypresses" from the user. The major
53 * concerns were thus portability and efficiency, so Angband could be
54 * easily ported to many different systems, with minimal effort, and
55 * yet would run quickly on each of these systems, no matter what kind
56 * of underlying hardware/software support was being used.
58 * It is important to understand the differences between the older
59 * "dumb terminals" and the newer "graphic interface" machines, since
60 * this package was designed to work with both types of systems.
63 * waiting for a keypress is complex
64 * checking for a keypress is often cheap
65 * changing "colors" may be expensive
66 * the "color" of a "blank" is rarely important
67 * moving the "cursor" is relatively cheap
68 * use a "software" cursor (only moves when requested)
69 * drawing characters normally will not erase old ones
70 * drawing a character on the cursor often erases it
71 * may have fast routines for "clear a region"
72 * the bottom right corner is usually not special
75 * waiting for a keypress is simple
76 * checking for a keypress is often expensive
77 * changing "colors" is usually cheap
78 * the "color" of a "blank" may be important
79 * moving the "cursor" may be expensive
80 * use a "hardware" cursor (moves during screen updates)
81 * drawing new symbols automatically erases old ones
82 * characters may only be drawn at the cursor location
83 * drawing a character on the cursor will move the cursor
84 * may have fast routines for "clear entire window"
85 * may have fast routines for "clear to end of line"
86 * the bottom right corner is often dangerous
89 * This package provides support for multiple windows, each of an
90 * arbitrary size (up to 255x255), each with its own set of flags,
91 * and its own hooks to handle several low-level procedures which
92 * differ from platform to platform. Then the main program simply
93 * creates one or more "term" structures, setting the various flags
94 * and hooks in a manner appropriate for the current platform, and
95 * then it can use the various "term" structures without worrying
96 * about the underlying platform.
99 * This package allows each "grid" in each window to hold an attr/char
100 * pair, with each ranging from 0 to 255, and makes very few assumptions
101 * about the meaning of any attr/char values. Normally, we assume that
102 * "attr 0" is "black", with the semantics that "black" text should be
103 * sent to "Term_wipe()" instead of "Term_text()", but this sematics is
104 * modified if either the "always_pict" or the "always_text" flags are
105 * set. We assume that "char 0" is "dangerous", since placing such a
106 * "char" in the middle of a string "terminates" the string, and usually
107 * we prevent its use.
109 * Finally, we use a special attr/char pair, defaulting to "attr 0" and
110 * "char 32", also known as "black space", when we "erase" or "clear"
111 * any window, but this pair can be redefined to any pair, including
112 * the standard "white space", or the bizarre "emptiness" ("attr 0"
113 * and "char 0"), as long as various obscure restrictions are met.
116 * This package provides several functions which allow a program to
117 * interact with the "term" structures. Most of the functions allow
118 * the program to "request" certain changes to the current "term",
119 * such as moving the cursor, drawing an attr/char pair, erasing a
120 * region of grids, hiding the cursor, etc. Then there is a special
121 * function which causes all of the "pending" requests to be performed
122 * in an efficient manner. There is another set of functions which
123 * allow the program to query the "requested state" of the current
124 * "term", such as asking for the cursor location, or what attr/char
125 * is at a given location, etc. There is another set of functions
126 * dealing with "keypress" events, which allows the program to ask if
127 * the user has pressed any keys, or to forget any keys the user pressed.
128 * There is a pair of functions to allow this package to memorize the
129 * contents of the current "term", and to restore these contents at
130 * a later time. There is a special function which allows the program
131 * to specify which "term" structure should be the "current" one. At
132 * the lowest level, there is a set of functions which allow a new
133 * "term" to be initialized or destroyed, and which allow this package,
134 * or a program, to access the special "hooks" defined for the current
135 * "term", and a set of functions which those "hooks" can use to inform
136 * this package of the results of certain occurances, for example, one
137 * such function allows this package to learn about user keypresses,
138 * detected by one of the special "hooks".
140 * We provide, among other things, the functions "Term_keypress()"
141 * to "react" to keypress events, and "Term_redraw()" to redraw the
142 * entire window, plus "Term_resize()" to note a new size.
145 * Note that the current "term" contains two "window images". One of
146 * these images represents the "requested" contents of the "term", and
147 * the other represents the "actual" contents of the "term", at the time
148 * of the last performance of pending requests. This package uses these
149 * two images to determine the "minimal" amount of work needed to make
150 * the "actual" contents of the "term" match the "requested" contents of
151 * the "term". This method is not perfect, but it often reduces the
152 * amount of work needed to perform the pending requests, which thus
153 * increases the speed of the program itself. This package promises
154 * that the requested changes will appear to occur either "all at once"
155 * or in a "top to bottom" order. In addition, a "cursor" is maintained,
156 * and this cursor is updated along with the actual window contents.
158 * Currently, the "Term_fresh()" routine attempts to perform the "minimum"
159 * number of physical updates, in terms of total "work" done by the hooks
160 * Term_wipe(), Term_text(), and Term_pict(), making use of the fact that
161 * adjacent characters of the same color can both be drawn together using
162 * the "Term_text()" hook, and that "black" text can often be sent to the
163 * "Term_wipe()" hook instead of the "Term_text()" hook, and if something
164 * is already displayed in a window, then it is not necessary to display
165 * it again. Unfortunately, this may induce slightly non-optimal results
166 * in some cases, in particular, those in which, say, a string of ten
167 * characters needs to be written, but the fifth character has already
168 * been displayed. Currently, this will cause the "Term_text()" routine
169 * to be called once for each half of the string, instead of once for the
170 * whole string, which, on some machines, may be non-optimal behavior.
172 * The new formalism includes a "displayed" screen image (old) which
173 * is actually seen by the user, a "requested" screen image (scr)
174 * which is being prepared for display, a "memorized" screen image
175 * (mem) which is used to save and restore screen images, and a
176 * "temporary" screen image (tmp) which is currently unused.
179 * Several "flags" are available in each "term" to allow the underlying
180 * visual system (which initializes the "term" structure) to "optimize"
181 * the performance of this package for the given system, or to request
182 * certain behavior which is helpful/required for the given system.
184 * The "soft_cursor" flag indicates the use of a "soft" cursor, which
185 * only moves when explicitly requested,and which is "erased" when
186 * any characters are drawn on top of it. This flag is used for all
187 * "graphic" systems which handle the cursor by "drawing" it.
189 * The "icky_corner" flag indicates that the bottom right "corner"
190 * of the windows are "icky", and "printing" anything there may
191 * induce "messy" behavior, such as "scrolling". This flag is used
192 * for most old "dumb terminal" systems.
195 * The "term" structure contains the following function "hooks":
197 * Term->init_hook = Init the term
198 * Term->nuke_hook = Nuke the term
199 * Term->user_hook = Perform user actions
200 * Term->xtra_hook = Perform extra actions
201 * Term->curs_hook = Draw (or Move) the cursor
202 * Term->bigcurs_hook = Draw (or Move) the big cursor (bigtile mode)
203 * Term->wipe_hook = Draw some blank spaces
204 * Term->text_hook = Draw some text in the window
205 * Term->pict_hook = Draw some attr/chars in the window
207 * The "Term->user_hook" hook provides a simple hook to an implementation
208 * defined function, with application defined semantics. It is available
209 * to the program via the "Term_user()" function.
211 * The "Term->xtra_hook" hook provides a variety of different functions,
212 * based on the first parameter (which should be taken from the various
213 * TERM_XTRA_* defines) and the second parameter (which may make sense
214 * only for some first parameters). It is available to the program via
215 * the "Term_xtra()" function, though some first parameters are only
216 * "legal" when called from inside this package.
218 * The "Term->curs_hook" hook provides this package with a simple way
219 * to "move" or "draw" the cursor to the grid "x,y", depending on the
220 * setting of the "soft_cursor" flag. Note that the cursor is never
221 * redrawn if "nothing" has happened to the screen (even temporarily).
222 * This hook is required.
224 * The "Term->wipe_hook" hook provides this package with a simple way
225 * to "erase", starting at "x,y", the next "n" grids. This hook assumes
226 * that the input is valid. This hook is required, unless the setting
227 * of the "always_pict" or "always_text" flags makes it optional.
229 * The "Term->text_hook" hook provides this package with a simple way
230 * to "draw", starting at "x,y", the "n" chars contained in "cp", using
231 * the attr "a". This hook assumes that the input is valid, and that
232 * "n" is between 1 and 256 inclusive, but it should NOT assume that
233 * the contents of "cp" are null-terminated. This hook is required,
234 * unless the setting of the "always_pict" flag makes it optional.
236 * The "Term->pict_hook" hook provides this package with a simple way
237 * to "draw", starting at "x,y", the "n" attr/char pairs contained in
238 * the arrays "ap" and "cp". This hook assumes that the input is valid,
239 * and that "n" is between 1 and 256 inclusive, but it should NOT assume
240 * that the contents of "cp" are null-terminated. This hook is optional,
241 * unless the setting of the "always_pict" or "higher_pict" flags make
242 * it required. Note that recently, this hook was changed from taking
243 * a byte "a" and a char "c" to taking a length "n", an array of bytes
244 * "ap" and an array of chars "cp". Old implementations of this hook
245 * should now iterate over all "n" attr/char pairs.
248 * The game "Angband" uses a set of files called "main-xxx.c", for
249 * various "xxx" suffixes. Most of these contain a function called
250 * "init_xxx()", that will prepare the underlying visual system for
251 * use with Angband, and then create one or more "term" structures,
252 * using flags and hooks appropriate to the given platform, so that
253 * the "main()" function can call one (or more) of the "init_xxx()"
254 * functions, as appropriate, to prepare the required "term" structs
255 * (one for each desired sub-window), and these "init_xxx()" functions
256 * are called from a centralized "main()" function in "main.c". Other
257 * "main-xxx.c" systems contain their own "main()" function which, in
258 * addition to doing everything needed to initialize the actual program,
259 * also does everything that the normal "init_xxx()" functions would do.
261 * The game "Angband" defines, in addition to "attr 0", all of the
262 * attr codes from 1 to 15, using definitions in "defines.h", and
263 * thus the "main-xxx.c" files used by Angband must handle these
264 * attr values correctly. Also, they must handle all other attr
265 * values, though they may do so in any way they wish, for example,
266 * by always taking every attr code mod 16. Many of the "main-xxx.c"
267 * files use "white space" ("attr 1" / "char 32") to "erase" or "clear"
268 * any window, for efficiency.
270 * The game "Angband" uses the "Term_user" hook to allow any of the
271 * "main-xxx.c" files to interact with the user, by calling this hook
272 * whenever the user presses the "!" key when the game is waiting for
273 * a new command. This could be used, for example, to provide "unix
274 * shell commands" to the Unix versions of the game.
276 * See "main-xxx.c" for a simple skeleton file which can be used to
277 * create a "visual system" for a new platform when porting Angband.
293 /*** Local routines ***/
297 * Nuke a term_win (see below)
299 static errr term_win_nuke(term_win *s, int w, int h)
301 /* Free the window access arrays */
302 C_KILL(s->a, h, byte*);
303 C_KILL(s->c, h, char*);
305 /* Free the window content arrays */
306 C_KILL(s->va, h * w, byte);
307 C_KILL(s->vc, h * w, char);
309 /* Free the terrain access arrays */
310 C_KILL(s->ta, h, byte*);
311 C_KILL(s->tc, h, char*);
313 /* Free the terrain content arrays */
314 C_KILL(s->vta, h * w, byte);
315 C_KILL(s->vtc, h * w, char);
323 * Initialize a "term_win" (using the given window size)
325 static errr term_win_init(term_win *s, int w, int h)
329 /* Make the window access arrays */
330 C_MAKE(s->a, h, byte*);
331 C_MAKE(s->c, h, char*);
333 /* Make the window content arrays */
334 C_MAKE(s->va, h * w, byte);
335 C_MAKE(s->vc, h * w, char);
337 /* Make the terrain access arrays */
338 C_MAKE(s->ta, h, byte*);
339 C_MAKE(s->tc, h, char*);
341 /* Make the terrain content arrays */
342 C_MAKE(s->vta, h * w, byte);
343 C_MAKE(s->vtc, h * w, char);
346 /* Prepare the window access arrays */
347 for (y = 0; y < h; y++)
349 s->a[y] = s->va + w * y;
350 s->c[y] = s->vc + w * y;
352 s->ta[y] = s->vta + w * y;
353 s->tc[y] = s->vtc + w * y;
362 * Copy a "term_win" from another
364 static errr term_win_copy(term_win *s, term_win *f, int w, int h)
369 for (y = 0; y < h; y++)
371 byte *f_aa = f->a[y];
372 char *f_cc = f->c[y];
374 byte *s_aa = s->a[y];
375 char *s_cc = s->c[y];
377 byte *f_taa = f->ta[y];
378 char *f_tcc = f->tc[y];
380 byte *s_taa = s->ta[y];
381 char *s_tcc = s->tc[y];
383 for (x = 0; x < w; x++)
405 /*** External hooks ***/
409 * Execute the "Term->user_hook" hook, if available (see above).
411 errr Term_user(int n)
413 /* Verify the hook */
414 if (!Term->user_hook) return (-1);
417 return ((*Term->user_hook)(n));
421 * Execute the "Term->xtra_hook" hook, if available (see above).
423 errr Term_xtra(int n, int v)
425 /* Verify the hook */
426 if (!Term->xtra_hook) return (-1);
429 return ((*Term->xtra_hook)(n, v));
438 * Hack -- fake hook for "Term_curs()" (see above)
440 static errr Term_curs_hack(int x, int y)
451 * Hack -- fake hook for "Term_bigcurs()" (see above)
453 static errr Term_bigcurs_hack(int x, int y)
455 return (*Term->curs_hook)(x, y);
459 * Hack -- fake hook for "Term_wipe()" (see above)
461 static errr Term_wipe_hack(int x, int y, int n)
473 * Hack -- fake hook for "Term_text()" (see above)
475 static errr Term_text_hack(int x, int y, int n, byte a, cptr cp)
489 * Hack -- fake hook for "Term_pict()" (see above)
491 static errr Term_pict_hack(int x, int y, int n, const byte *ap, cptr cp, const byte *tap, cptr tcp)
508 /*** Efficient routines ***/
512 * Mentally draw an attr/char at a given location
514 * Assumes given location and values are valid.
516 void Term_queue_char(int x, int y, byte a, char c, byte ta, char tc)
518 term_win *scrn = Term->scr;
520 byte *scr_aa = &scrn->a[y][x];
521 char *scr_cc = &scrn->c[y][x];
523 byte *scr_taa = &scrn->ta[y][x];
524 char *scr_tcc = &scrn->tc[y][x];
526 /* Hack -- Ignore non-changes */
527 if ((*scr_aa == a) && (*scr_cc == c) &&
528 (*scr_taa == ta) && (*scr_tcc == tc)) return;
530 /* Save the "literal" information */
537 /* Check for new min/max row info */
538 if (y < Term->y1) Term->y1 = y;
539 if (y > Term->y2) Term->y2 = y;
541 /* Check for new min/max col info for this row */
542 if (x < Term->x1[y]) Term->x1[y] = x;
543 if (x > Term->x2[y]) Term->x2[y] = x;
546 if (((scrn->a[y][x] & AF_BIGTILE2) == AF_BIGTILE2) ||
547 (scrn->a[y][x] & AF_KANJI2))
549 if ((scrn->a[y][x] & AF_BIGTILE2) == AF_BIGTILE2)
551 if ((x - 1) < Term->x1[y]) Term->x1[y]--;
556 * Bigtile version of Term_queue_char().
558 * If use_bigtile is FALSE, simply call Term_queue_char().
560 * Otherwise, mentally draw a pair of attr/char at a given location.
562 * Assumes given location and values are valid.
564 void Term_queue_bigchar(int x, int y, byte a, char c, byte ta, char tc)
569 * A table which relates each ascii character to a multibyte
572 * ¡Ö¢£¡×¤ÏÆóÇÜÉýƦÉå¤ÎÆâÉô¥³¡¼¥É¤Ë»ÈÍÑ¡£
574 static char ascii_to_zenkaku[] =
575 "¡¡¡ª¡É¡ô¡ð¡ó¡õ¡Ç¡Ê¡Ë¡ö¡Ü¡¤¡Ý¡¥¡¿"
576 "£°£±£²£³£´£µ£¶£·£¸£¹¡§¡¨¡ã¡á¡ä¡©"
577 "¡÷£Á£Â£Ã£Ä£Å£Æ£Ç£È£É£Ê£Ë£Ì£Í£Î£Ï"
578 "£Ð£Ñ£Ò£Ó£Ô£Õ£Ö£×£Ø£Ù£Ú¡Î¡À¡Ï¡°¡²"
579 "¡Æ£á£â£ã£ä£å£æ£ç£è£é£ê£ë£ì£í£î£ï"
580 "£ð£ñ£ò£ó£ô£õ£ö£÷£ø£ù£ú¡Ð¡Ã¡Ñ¡Á¢£";
586 /* If non bigtile mode, call orginal function */
589 Term_queue_char(x, y, a, c, ta, tc);
593 /* A tile becomes a Bigtile */
594 if ((a & AF_TILE1) && (c & 0x80))
596 /* Mark it as a Bigtile */
601 /* Ignore non-tile background */
602 if (!((ta & AF_TILE1) && (tc & 0x80)))
611 * Use a multibyte character instead of a dirty pair of ASCII
614 else if (' ' <= c) /* isprint(c) */
616 c2 = ascii_to_zenkaku[2 * (c - ' ') + 1];
617 c = ascii_to_zenkaku[2 * (c - ' ')];
619 /* Mark it as a Kanji */
627 /* Dirty pair of ASCII characters */
632 /* Display pair of attr/char */
633 Term_queue_char(x, y, a, c, ta, tc);
634 Term_queue_char(x + 1, y, a2, c2, 0, 0);
639 * Mentally draw a string of attr/chars at a given location
641 * Assumes given location and values are valid.
643 * This function is designed to be fast, with no consistancy checking.
644 * It is used to update the map in the game.
646 void Term_queue_line(int x, int y, int n, byte *a, char *c, byte *ta, char *tc)
648 term_win *scrn = Term->scr;
653 byte *scr_aa = &scrn->a[y][x];
654 char *scr_cc = &scrn->c[y][x];
656 byte *scr_taa = &scrn->ta[y][x];
657 char *scr_tcc = &scrn->tc[y][x];
661 /* Hack -- Ignore non-changes */
662 if ((*scr_aa == *a) && (*scr_cc == *c) &&
663 (*scr_taa == *ta) && (*scr_tcc == *tc))
677 /* Save the "literal" information */
681 /* Save the "literal" information */
685 /* Track minimum changed column */
688 /* Track maximum changed column */
694 /* Expand the "change area" as needed */
697 /* Check for new min/max row info */
698 if (y < Term->y1) Term->y1 = y;
699 if (y > Term->y2) Term->y2 = y;
701 /* Check for new min/max col info in this row */
702 if (x1 < Term->x1[y]) Term->x1[y] = x1;
703 if (x2 > Term->x2[y]) Term->x2[y] = x2;
710 * Mentally draw some attr/chars at a given location
712 * Assumes that (x,y) is a valid location, that the first "n" characters
713 * of the string "s" are all valid (non-zero), and that (x+n-1,y) is also
714 * a valid location, so the first "n" characters of "s" can all be added
715 * starting at (x,y) without causing any illegal operations.
717 void Term_queue_chars(int x, int y, int n, byte a, cptr s)
719 int x1 = -1, x2 = -1;
721 byte *scr_aa = Term->scr->a[y];
723 char *scr_cc = Term->scr->c[y];
725 byte *scr_taa = Term->scr->ta[y];
726 char *scr_tcc = Term->scr->tc[y];
728 char *scr_cc = Term->scr->c[y];
730 byte *scr_taa = Term->scr->ta[y];
731 char *scr_tcc = Term->scr->tc[y];
737 if (n == 0 || *s == 0) return;
739 * Á´³Ñʸ»ú¤Î±¦È¾Ê¬¤«¤éʸ»ú¤òɽ¼¨¤¹¤ë¾ì¹ç¡¢
740 * ½Å¤Ê¤Ã¤¿Ê¸»ú¤Îº¸Éôʬ¤ò¾Ãµî¡£
741 * ɽ¼¨³«»Ï°ÌÃÖ¤¬º¸Ã¼¤Ç¤Ê¤¤¤È²¾Äê¡£
743 if ((scr_aa[x] & AF_KANJI2) && (scr_aa[x] & AF_BIGTILE2) != AF_BIGTILE2)
746 scr_aa[x - 1] &= AF_KANJIC;
750 /* Queue the attr/chars */
751 for ( ; n; x++, s++, n--)
754 /* Æüìʸ»ú¤È¤·¤ÆMSB¤¬Î©¤Ã¤Æ¤¤¤ë²ÄǽÀ¤¬¤¢¤ë */
755 /* ¤½¤Î¾ì¹çattr¤ÎMSB¤âΩ¤Ã¤Æ¤¤¤ë¤Î¤Ç¤³¤ì¤Ç¼±Ê̤¹¤ë */
757 if (!(a & AF_TILE1) && iskanji(*s))
762 byte na1 = (a | AF_KANJI1);
763 byte na2 = (a | AF_KANJI2);
765 if((--n == 0) || !nc2) break;
767 if(scr_aa[x++] == na1 && scr_aa[x] == na2 &&
768 scr_cc[x - 1] == nc1 && scr_cc[x] == nc2 &&
769 (scr_taa[x - 1] == 0) && (scr_taa[x]==0) &&
770 (scr_tcc[x - 1] == 0) && (scr_tcc[x]==0) )
778 if(x1 < 0) x1 = x - 1;
787 byte ota = scr_taa[x];
788 char otc = scr_tcc[x];
790 /* Hack -- Ignore non-changes */
791 if ((oa == a) && (oc == *s) && (ota == 0) && (otc == 0)) continue;
793 /* Save the "literal" information */
800 /* Note the "range" of window updates */
810 * Á´³Ñʸ»ú¤Îº¸È¾Ê¬¤Çɽ¼¨¤ò½ªÎ»¤¹¤ë¾ì¹ç¡¢
811 * ½Å¤Ê¤Ã¤¿Ê¸»ú¤Î±¦Éôʬ¤ò¾Ãµî¡£
812 * (¾ò·ïÄɲ᧥¿¥¤¥ë¤Î1ʸ»úÌܤǤʤ¤»ö¤ò³Î¤«¤á¤ë¤è¤¦¤Ë¡£)
817 Term_get_size(&w, &h);
818 if (x != w && !(scr_aa[x] & AF_TILE1) && (scr_aa[x] & AF_KANJI2))
821 scr_aa[x] &= AF_KANJIC;
827 /* Expand the "change area" as needed */
830 /* Check for new min/max row info */
831 if (y < Term->y1) Term->y1 = y;
832 if (y > Term->y2) Term->y2 = y;
834 /* Check for new min/max col info in this row */
835 if (x1 < Term->x1[y]) Term->x1[y] = x1;
836 if (x2 > Term->x2[y]) Term->x2[y] = x2;
842 /*** Refresh routines ***/
846 * Flush a row of the current window (see "Term_fresh")
848 * Display text using "Term_pict()"
850 static void Term_fresh_row_pict(int y, int x1, int x2)
854 byte *old_aa = Term->old->a[y];
855 char *old_cc = Term->old->c[y];
857 byte *scr_aa = Term->scr->a[y];
858 char *scr_cc = Term->scr->c[y];
860 byte *old_taa = Term->old->ta[y];
861 char *old_tcc = Term->old->tc[y];
863 byte *scr_taa = Term->scr->ta[y];
864 char *scr_tcc = Term->scr->tc[y];
886 /* Á´³Ñʸ»ú¤Î£²¥Ð¥¤¥ÈÌܤ«¤É¤¦¤« */
889 /* Scan "modified" columns */
890 for (x = x1; x <= x2; x++)
892 /* See what is currently here */
896 /* See what is desired there */
903 /* Á´³Ñʸ»ú£²¥Ð¥¤¥ÈÌÜ */
910 /* Æüìʸ»ú¤È¤·¤ÆMSB¤¬Î©¤Ã¤Æ¤¤¤ë²ÄǽÀ¤¬¤¢¤ë */
911 /* ¤½¤Î¾ì¹çattr¤ÎMSB¤âΩ¤Ã¤Æ¤¤¤ë¤Î¤Ç¤³¤ì¤Ç¼±Ê̤¹¤ë */
913 kanji = (iskanji(nc) && !(na & AF_TILE1));
922 /* Handle unchanged grids */
924 if ((na == oa) && (nc == oc) && (nta == ota) && (ntc == otc)
925 &&(!kanji || (scr_aa[x + 1] == old_aa[x + 1] &&
926 scr_cc[x + 1] == old_cc[x + 1] &&
927 scr_taa[x + 1] == old_taa[x + 1] &&
928 scr_tcc[x + 1] == old_tcc[x + 1])))
930 if ((na == oa) && (nc == oc) && (nta == ota) && (ntc == otc))
936 /* Draw pending attr/char pairs */
937 (void)((*Term->pict_hook)(fx, y, fn,
938 &scr_aa[fx], &scr_cc[fx],&scr_taa[fx], &scr_tcc[fx]));
945 /* Á´³Ñʸ»ú¤Î»þ¤ÏºÆ³«°ÌÃ֤ϡܣ± */
956 /* Save new contents */
963 /* Restart and Advance */
964 if (fn++ == 0) fx = x;
970 /* Draw pending attr/char pairs */
971 (void)((*Term->pict_hook)(fx, y, fn,
972 &scr_aa[fx], &scr_cc[fx], &scr_taa[fx], &scr_tcc[fx]));
979 * Flush a row of the current window (see "Term_fresh")
981 * Display text using "Term_text()" and "Term_wipe()",
982 * but use "Term_pict()" for high-bit attr/char pairs
984 static void Term_fresh_row_both(int y, int x1, int x2)
988 byte *old_aa = Term->old->a[y];
989 char *old_cc = Term->old->c[y];
991 byte *scr_aa = Term->scr->a[y];
992 char *scr_cc = Term->scr->c[y];
994 byte *old_taa = Term->old->ta[y];
995 char *old_tcc = Term->old->tc[y];
996 byte *scr_taa = Term->scr->ta[y];
997 char *scr_tcc = Term->scr->tc[y];
1004 /* The "always_text" flag */
1005 int always_text = Term->always_text;
1007 /* Pending length */
1014 byte fa = Term->attr_blank;
1023 /* Á´³Ñʸ»ú¤Î£²¥Ð¥¤¥ÈÌܤ«¤É¤¦¤« */
1026 /* Scan "modified" columns */
1027 for (x = x1; x <= x2; x++)
1029 /* See what is currently here */
1033 /* See what is desired there */
1040 /* Á´³Ñʸ»ú£²¥Ð¥¤¥ÈÌÜ */
1047 /* Æüìʸ»ú¤È¤·¤ÆMSB¤¬Î©¤Ã¤Æ¤¤¤ë²ÄǽÀ¤¬¤¢¤ë */
1048 /* ¤½¤Î¾ì¹çattr¤ÎMSB¤âΩ¤Ã¤Æ¤¤¤ë¤Î¤Ç¤³¤ì¤Ç¼±Ê̤¹¤ë */
1050 /* kanji = (iskanji(nc)); */
1051 kanji = (iskanji(nc) && !(na & AF_TILE1));
1060 /* Handle unchanged grids */
1062 if ((na == oa) && (nc == oc) && (nta == ota) && (ntc == otc)&&
1063 (!kanji || (scr_aa[x + 1] == old_aa[x + 1] &&
1064 scr_cc[x + 1] == old_cc[x + 1] &&
1065 scr_taa[x + 1] == old_taa[x + 1] &&
1066 scr_tcc[x + 1] == old_tcc[x + 1])))
1068 if ((na == oa) && (nc == oc) && (nta == ota) && (ntc == otc))
1074 /* Draw pending chars (normal) */
1075 if (fa || always_text)
1077 (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1080 /* Draw pending chars (black) */
1083 (void)((*Term->wipe_hook)(fx, y, fn));
1091 /* Á´³Ñʸ»ú¤Î»þ¤ÏºÆ³«°ÌÃ֤ϡܣ± */
1103 /* Save new contents */
1110 /* 2nd byte of bigtile */
1111 if ((na & AF_BIGTILE2) == AF_BIGTILE2) continue;
1113 /* Handle high-bit attr/chars */
1114 if ((na & AF_TILE1) && (nc & 0x80))
1119 /* Draw pending chars (normal) */
1120 if (fa || always_text)
1122 (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1125 /* Draw pending chars (black) */
1128 (void)((*Term->wipe_hook)(fx, y, fn));
1135 /* Hack -- Draw the special attr/char pair */
1136 (void)((*Term->pict_hook)(x, y, 1, &na, &nc, &nta, &ntc));
1142 /* Notice new color */
1144 if (fa != (na & AF_KANJIC))
1153 /* Draw the pending chars */
1154 if (fa || always_text)
1156 (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1159 /* Hack -- Erase "leading" spaces */
1162 (void)((*Term->wipe_hook)(fx, y, fn));
1169 /* Save the new color */
1171 fa = (na & AF_KANJIC);
1178 /* Restart and Advance */
1179 if (fn++ == 0) fx = x;
1185 /* Draw pending chars (normal) */
1186 if (fa || always_text)
1188 (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1191 /* Draw pending chars (black) */
1194 (void)((*Term->wipe_hook)(fx, y, fn));
1201 * Flush a row of the current window (see "Term_fresh")
1203 * Display text using "Term_text()" and "Term_wipe()"
1205 static void Term_fresh_row_text(int y, int x1, int x2)
1209 byte *old_aa = Term->old->a[y];
1210 char *old_cc = Term->old->c[y];
1212 byte *scr_aa = Term->scr->a[y];
1213 char *scr_cc = Term->scr->c[y];
1215 /* The "always_text" flag */
1216 int always_text = Term->always_text;
1218 /* Pending length */
1225 byte fa = Term->attr_blank;
1234 /* Á´³Ñʸ»ú¤Î£²¥Ð¥¤¥ÈÌܤ«¤É¤¦¤« */
1237 for (x = 0; x < x1; x++)
1238 if (!(old_aa[x] & AF_TILE1) && iskanji(old_cc[x]))
1249 /* Scan "modified" columns */
1250 for (x = x1; x <= x2; x++)
1252 /* See what is currently here */
1256 /* See what is desired there */
1263 /* Á´³Ñʸ»ú£²¥Ð¥¤¥ÈÌÜ */
1270 /* Æüìʸ»ú¤È¤·¤ÆMSB¤¬Î©¤Ã¤Æ¤¤¤ë²ÄǽÀ¤¬¤¢¤ë */
1271 /* ¤½¤Î¾ì¹çattr¤ÎMSB¤âΩ¤Ã¤Æ¤¤¤ë¤Î¤Ç¤³¤ì¤Ç¼±Ê̤¹¤ë */
1273 kanji = (iskanji(nc) && !(na & AF_TILE1));
1275 /* Handle unchanged grids */
1277 if ((na == oa) && (nc == oc) &&
1278 (!kanji || (scr_aa[x + 1] == old_aa[x + 1] &&
1279 scr_cc[x + 1] == old_cc[x + 1])))
1281 if ((na == oa) && (nc == oc))
1288 /* Draw pending chars (normal) */
1289 if (fa || always_text)
1291 (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1294 /* Draw pending chars (black) */
1297 (void)((*Term->wipe_hook)(fx, y, fn));
1305 /* Á´³Ñʸ»ú¤Î»þ¤ÏºÆ³«°ÌÃ֤ϡܣ± */
1317 /* Save new contents */
1321 /* Notice new color */
1323 if (fa != (na & AF_KANJIC))
1332 /* Draw the pending chars */
1333 if (fa || always_text)
1335 (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1338 /* Hack -- Erase "leading" spaces */
1341 (void)((*Term->wipe_hook)(fx, y, fn));
1348 /* Save the new color */
1350 fa = (na & AF_KANJIC);
1357 /* Restart and Advance */
1358 if (fn++ == 0) fx = x;
1364 /* Draw pending chars (normal) */
1365 if (fa || always_text)
1367 (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1370 /* Draw pending chars (black) */
1373 (void)((*Term->wipe_hook)(fx, y, fn));
1383 * Actually perform all requested changes to the window
1385 * If absolutely nothing has changed, not even temporarily, or if the
1386 * current "Term" is not mapped, then this function will return 1 and
1387 * do absolutely nothing.
1389 * Note that when "soft_cursor" is true, we erase the cursor (if needed)
1390 * whenever anything has changed, and redraw it (if needed) after all of
1391 * the screen updates are complete. This will induce a small amount of
1392 * "cursor flicker" but only when the screen has been updated. If the
1393 * screen is updated and then restored, you may still get this flicker.
1395 * When "soft_cursor" is not true, we make the cursor invisible before
1396 * doing anything else if it is supposed to be invisible by the time we
1397 * are done, and we make it visible after moving it to its final location
1398 * after all of the screen updates are complete.
1400 * Note that "Term_xtra(TERM_XTRA_CLEAR,0)" must erase the entire screen,
1401 * including the cursor, if needed, and may place the cursor anywhere.
1403 * Note that "Term_xtra(TERM_XTRA_FROSH,y)" will be always be called
1404 * after any row "y" has been "flushed", unless the "Term->never_frosh"
1405 * flag is set, and "Term_xtra(TERM_XTRA_FRESH,0)" will be called after
1406 * all of the rows have been "flushed".
1408 * Note the use of three different functions to handle the actual flush,
1409 * based on the settings of the "Term->always_pict" and "Term->higher_pict"
1410 * flags (see below).
1412 * The three helper functions (above) work by collecting similar adjacent
1413 * grids into stripes, and then sending each stripe to "Term->pict_hook",
1414 * "Term->text_hook", or "Term->wipe_hook", based on the settings of the
1415 * "Term->always_pict" and "Term->higher_pict" flags, which select which
1416 * of the helper functions to call to flush each row.
1418 * The helper functions currently "skip" any grids which already contain
1419 * the desired contents. This may or may not be the best method, especially
1420 * when the desired content fits nicely into the current stripe. For example,
1421 * it might be better to go ahead and queue them while allowed, but keep a
1422 * count of the "trailing skipables", then, when time to flush, or when a
1423 * "non skippable" is found, force a flush if there are too many skippables.
1425 * Perhaps an "initialization" stage, where the "text" (and "attr")
1426 * buffers are "filled" with information, converting "blanks" into
1427 * a convenient representation, and marking "skips" with "zero chars",
1428 * and then some "processing" is done to determine which chars to skip.
1430 * Currently, the helper functions are optimal for systems which prefer
1431 * to "print a char + move a char + print a char" to "print three chars",
1432 * and for applications that do a lot of "detailed" color printing.
1434 * In the two "queue" functions, total "non-changes" are "pre-skipped".
1435 * The helper functions must also handle situations in which the contents
1436 * of a grid are changed, but then changed back to the original value,
1437 * and situations in which two grids in the same row are changed, but
1438 * the grids between them are unchanged.
1440 * If the "Term->always_pict" flag is set, then "Term_fresh_row_pict()"
1441 * will be used instead of "Term_fresh_row_text()". This allows all the
1442 * modified grids to be collected into stripes of attr/char pairs, which
1443 * are then sent to the "Term->pict_hook" hook, which can draw these pairs
1444 * in whatever way it would like.
1446 * If the "Term->higher_pict" flag is set, then "Term_fresh_row_both()"
1447 * will be used instead of "Term_fresh_row_text()". This allows all the
1448 * "special" attr/char pairs (in which both the attr and char have the
1449 * high-bit set) to be sent (one pair at a time) to the "Term->pict_hook"
1450 * hook, which can draw these pairs in whatever way it would like.
1452 * Normally, the "Term_wipe()" function is used only to display "blanks"
1453 * that were induced by "Term_clear()" or "Term_erase()", and then only
1454 * if the "attr_blank" and "char_blank" fields have not been redefined
1455 * to use "white space" instead of the default "black space". Actually,
1456 * the "Term_wipe()" function is used to display all "black" text, such
1457 * as the default "spaces" created by "Term_clear()" and "Term_erase()".
1459 * Note that the "Term->always_text" flag will disable the use of the
1460 * "Term_wipe()" function hook entirely, and force all text, even text
1461 * drawn in the color "black", to be explicitly drawn. This is useful
1462 * for machines which implement "Term_wipe()" by just drawing spaces.
1464 * Note that the "Term->always_pict" flag will disable the use of the
1465 * "Term_wipe()" function entirely, and force everything, even text
1466 * drawn in the attr "black", to be explicitly drawn.
1468 * Note that if no "black" text is ever drawn, and if "attr_blank" is
1469 * not "zero", then the "Term_wipe" hook will never be used, even if
1470 * the "Term->always_text" flag is not set.
1472 * This function does nothing unless the "Term" is "mapped", which allows
1473 * certain systems to optimize the handling of "closed" windows.
1475 * On systems with a "soft" cursor, we must explicitly erase the cursor
1476 * before flushing the output, if needed, to prevent a "jumpy" refresh.
1477 * The actual method for this is horrible, but there is very little that
1478 * we can do to simplify it efficiently. XXX XXX XXX
1480 * On systems with a "hard" cursor, we will "hide" the cursor before
1481 * flushing the output, if needed, to avoid a "flickery" refresh. It
1482 * would be nice to *always* hide the cursor during the refresh, but
1483 * this might be expensive (and/or ugly) on some machines.
1485 * The "Term->icky_corner" flag is used to avoid calling "Term_wipe()"
1486 * or "Term_pict()" or "Term_text()" on the bottom right corner of the
1487 * window, which might induce "scrolling" or other nasty stuff on old
1488 * dumb terminals. This flag is handled very efficiently. We assume
1489 * that the "Term_curs()" call will prevent placing the cursor in the
1490 * corner, if needed, though I doubt such placement is ever a problem.
1491 * Currently, the use of "Term->icky_corner" and "Term->soft_cursor"
1492 * together may result in undefined behavior.
1494 errr Term_fresh(void)
1504 term_win *old = Term->old;
1505 term_win *scr = Term->scr;
1508 /* Do nothing unless "mapped" */
1509 if (!Term->mapped_flag) return (1);
1512 /* Trivial Refresh */
1514 (scr->cu == old->cu) &&
1515 (scr->cv == old->cv) &&
1516 (scr->cx == old->cx) &&
1517 (scr->cy == old->cy) &&
1518 !(Term->total_erase))
1525 /* Handle "total erase" */
1526 if (Term->total_erase)
1528 byte na = Term->attr_blank;
1529 char nc = Term->char_blank;
1531 /* Physically erase the entire window */
1532 Term_xtra(TERM_XTRA_CLEAR, 0);
1534 /* Hack -- clear all "cursor" data */
1535 old->cv = old->cu = old->cx = old->cy = 0;
1538 for (y = 0; y < h; y++)
1540 byte *aa = old->a[y];
1541 char *cc = old->c[y];
1543 byte *taa = old->ta[y];
1544 char *tcc = old->tc[y];
1547 /* Wipe each column */
1548 for (x = 0; x < w; x++)
1550 /* Wipe each grid */
1559 /* Redraw every row */
1561 Term->y2 = y2 = h - 1;
1563 /* Redraw every column */
1564 for (y = 0; y < h; y++)
1567 Term->x2[y] = w - 1;
1570 /* Forget "total erase" */
1571 Term->total_erase = FALSE;
1575 /* Cursor update -- Erase old Cursor */
1576 if (Term->soft_cursor)
1578 /* Cursor was visible */
1579 if (!old->cu && old->cv)
1585 byte *old_aa = old->a[ty];
1586 char *old_cc = old->c[ty];
1588 byte *old_taa = old->ta[ty];
1589 char *old_tcc = old->tc[ty];
1591 byte ota = old_taa[tx];
1592 char otc = old_tcc[tx];
1595 if (tx + 1 < Term->wid && !(old_aa[tx] & AF_TILE1)
1596 && iskanji(old_cc[tx]))
1599 /* Hack -- use "Term_pict()" always */
1600 if (Term->always_pict)
1602 (void)((*Term->pict_hook)(tx, ty, csize, &old_aa[tx], &old_cc[tx], &ota, &otc));
1605 /* Hack -- use "Term_pict()" sometimes */
1606 else if (Term->higher_pict && (old_aa[tx] & AF_TILE1) && (old_cc[tx] & 0x80))
1608 (void)((*Term->pict_hook)(tx, ty, 1, &old_aa[tx], &old_cc[tx], &ota, &otc));
1612 * Hack -- restore the actual character
1613 * ¸µ¤Îʸ»ú¤ÎÉÁ²èÈϰϤ¬¥«¡¼¥½¥ë¤è¤ê¾®¤µ¤¤¤È¡¢
1614 * ¾å½ñ¤¤µ¤ì¤Ê¤«¤Ã¤¿Éôʬ¤¬¥´¥ß¤È¤·¤Æ»Ä¤ë¡£
1615 * wipe_hook ¤Ç¥«¡¼¥½¥ë¤ò¾Ãµî¤·¤Æ text_hook ¤Ç½ñ¤Ä¾¤¹¡£
1617 else if (old_aa[tx] || Term->always_text)
1619 (void)((*Term->wipe_hook)(tx, ty, 1));
1620 (void)((*Term->text_hook)(tx, ty, csize, (unsigned char) (old_aa[tx] & 0xf), &old_cc[tx]));
1623 /* Hack -- erase the grid */
1626 (void)((*Term->wipe_hook)(tx, ty, 1));
1631 /* Cursor Update -- Erase old Cursor */
1634 /* Cursor will be invisible */
1635 if (scr->cu || !scr->cv)
1637 /* Make the cursor invisible */
1638 Term_xtra(TERM_XTRA_SHAPE, 0);
1643 /* Something to update */
1646 /* Handle "icky corner" */
1647 if (Term->icky_corner)
1649 /* Avoid the corner */
1652 /* Avoid the corner */
1653 if (Term->x2[h - 1] > w - 2)
1655 /* Avoid the corner */
1656 Term->x2[h - 1] = w - 2;
1662 /* Scan the "modified" rows */
1663 for (y = y1; y <= y2; ++y)
1665 int x1 = Term->x1[y];
1666 int x2 = Term->x2[y];
1668 /* Flush each "modified" row */
1671 /* Always use "Term_pict()" */
1672 if (Term->always_pict)
1675 Term_fresh_row_pict(y, x1, x2);
1678 /* Sometimes use "Term_pict()" */
1679 else if (Term->higher_pict)
1682 Term_fresh_row_both(y, x1, x2);
1685 /* Never use "Term_pict()" */
1689 Term_fresh_row_text(y, x1, x2);
1692 /* This row is all done */
1696 /* Hack -- Flush that row (if allowed) */
1697 if (!Term->never_frosh) Term_xtra(TERM_XTRA_FROSH, y);
1701 /* No rows are invalid */
1707 /* Cursor update -- Show new Cursor */
1708 if (Term->soft_cursor)
1710 /* Draw the cursor */
1711 if (!scr->cu && scr->cv)
1714 if ((scr->cx + 1 < w) &&
1715 ((old->a[scr->cy][scr->cx + 1] & AF_BIGTILE2) == AF_BIGTILE2 ||
1716 (!(old->a[scr->cy][scr->cx] & AF_TILE1) &&
1717 iskanji(old->c[scr->cy][scr->cx]))))
1719 if ((scr->cx + 1 < w) && (old->a[scr->cy][scr->cx + 1] & AF_BIGTILE2) == AF_BIGTILE2)
1722 /* Double width cursor for the Bigtile mode */
1723 (void)((*Term->bigcurs_hook)(scr->cx, scr->cy));
1727 /* Call the cursor display routine */
1728 (void)((*Term->curs_hook)(scr->cx, scr->cy));
1733 /* Cursor Update -- Show new Cursor */
1736 /* The cursor is useless, hide it */
1739 /* Paranoia -- Put the cursor NEAR where it belongs */
1740 (void)((*Term->curs_hook)(w - 1, scr->cy));
1742 /* Make the cursor invisible */
1743 /* Term_xtra(TERM_XTRA_SHAPE, 0); */
1746 /* The cursor is invisible, hide it */
1749 /* Paranoia -- Put the cursor where it belongs */
1750 (void)((*Term->curs_hook)(scr->cx, scr->cy));
1752 /* Make the cursor invisible */
1753 /* Term_xtra(TERM_XTRA_SHAPE, 0); */
1756 /* The cursor is visible, display it correctly */
1759 /* Put the cursor where it belongs */
1760 (void)((*Term->curs_hook)(scr->cx, scr->cy));
1762 /* Make the cursor visible */
1763 Term_xtra(TERM_XTRA_SHAPE, 1);
1768 /* Save the "cursor state" */
1775 /* Actually flush the output */
1776 Term_xtra(TERM_XTRA_FRESH, 0);
1785 /*** Output routines ***/
1789 * Set the cursor visibility
1791 errr Term_set_cursor(int v)
1794 if (Term->scr->cv == v) return (1);
1805 * Place the cursor at a given location
1807 * Note -- "illegal" requests do not move the cursor.
1809 errr Term_gotoxy(int x, int y)
1815 if ((x < 0) || (x >= w)) return (-1);
1816 if ((y < 0) || (y >= h)) return (-1);
1818 /* Remember the cursor */
1822 /* The cursor is not useless */
1831 * At a given location, place an attr/char
1832 * Do not change the cursor position
1833 * No visual changes until "Term_fresh()".
1835 errr Term_draw(int x, int y, byte a, char c)
1840 /* Verify location */
1841 if ((x < 0) || (x >= w)) return (-1);
1842 if ((y < 0) || (y >= h)) return (-1);
1844 /* Paranoia -- illegal char */
1845 if (!c) return (-2);
1847 /* Queue it for later */
1848 Term_queue_char(x, y, a, c, 0, 0);
1856 * Using the given attr, add the given char at the cursor.
1858 * We return "-2" if the character is "illegal". XXX XXX
1860 * We return "-1" if the cursor is currently unusable.
1862 * We queue the given attr/char for display at the current
1863 * cursor location, and advance the cursor to the right,
1864 * marking it as unuable and returning "1" if it leaves
1865 * the screen, and otherwise returning "0".
1867 * So when this function, or the following one, return a
1868 * positive value, future calls to either function will
1869 * return negative ones.
1871 errr Term_addch(byte a, char c)
1875 /* Handle "unusable" cursor */
1876 if (Term->scr->cu) return (-1);
1878 /* Paranoia -- no illegal chars */
1879 if (!c) return (-2);
1881 /* Queue the given character for display */
1882 Term_queue_char(Term->scr->cx, Term->scr->cy, a, c, 0, 0);
1884 /* Advance the cursor */
1888 if (Term->scr->cx < w) return (0);
1890 /* Note "Useless" cursor */
1893 /* Note "Useless" cursor */
1899 * Bigtile version of Term_addch().
1901 * If use_bigtile is FALSE, simply call Term_addch() .
1903 * Otherwise, queue a pair of attr/char for display at the current
1904 * cursor location, and advance the cursor to the right by two.
1906 errr Term_add_bigch(byte a, char c)
1908 if (!use_bigtile) return Term_addch(a, c);
1910 /* Handle "unusable" cursor */
1911 if (Term->scr->cu) return (-1);
1913 /* Paranoia -- no illegal chars */
1914 if (!c) return (-2);
1916 /* Queue the given character for display */
1917 Term_queue_bigchar(Term->scr->cx, Term->scr->cy, a, c, 0, 0);
1919 /* Advance the cursor */
1923 if (Term->scr->cx < Term->wid) return (0);
1925 /* Note "Useless" cursor */
1928 /* Note "Useless" cursor */
1934 * At the current location, using an attr, add a string
1936 * We also take a length "n", using negative values to imply
1937 * the largest possible value, and then we use the minimum of
1938 * this length and the "actual" length of the string as the
1939 * actual number of characters to attempt to display, never
1940 * displaying more characters than will actually fit, since
1941 * we do NOT attempt to "wrap" the cursor at the screen edge.
1943 * We return "-1" if the cursor is currently unusable.
1944 * We return "N" if we were "only" able to write "N" chars,
1945 * even if all of the given characters fit on the screen,
1946 * and mark the cursor as unusable for future attempts.
1948 * So when this function, or the preceding one, return a
1949 * positive value, future calls to either function will
1950 * return negative ones.
1952 errr Term_addstr(int n, byte a, cptr s)
1960 /* Handle "unusable" cursor */
1961 if (Term->scr->cu) return (-1);
1963 /* Obtain maximal length */
1964 k = (n < 0) ? (w + 1) : n;
1966 /* Obtain the usable string length */
1967 for (n = 0; (n < k) && s[n]; n++) /* loop */;
1969 /* React to reaching the edge of the screen */
1970 if (Term->scr->cx + n >= w) res = n = w - Term->scr->cx;
1972 /* Queue the first "n" characters for display */
1973 Term_queue_chars(Term->scr->cx, Term->scr->cy, n, a, s);
1975 /* Advance the cursor */
1978 /* Hack -- Notice "Useless" cursor */
1979 if (res) Term->scr->cu = 1;
1981 /* Success (usually) */
1987 * Move to a location and, using an attr, add a char
1989 errr Term_putch(int x, int y, byte a, char c)
1994 if ((res = Term_gotoxy(x, y)) != 0) return (res);
1996 /* Then add the char */
1997 if ((res = Term_addch(a, c)) != 0) return (res);
2005 * Move to a location and, using an attr, add a string
2007 errr Term_putstr(int x, int y, int n, byte a, cptr s)
2012 if ((res = Term_gotoxy(x, y)) != 0) return (res);
2014 /* Then add the string */
2015 if ((res = Term_addstr(n, a, s)) != 0) return (res);
2023 * Move to a location and, using an attr, add a string vertically
2025 errr Term_putstr_v(int x, int y, int n, byte a, cptr s)
2032 for (i = 0; i < n && s[i] != 0; i++)
2035 if ((res = Term_gotoxy(x, y0)) != 0) return (res);
2039 if ((res = Term_addstr(2, a, &s[i])) != 0) return (res);
2042 if (s[i] == 0) break;
2044 if ((res = Term_addstr(1, a, &s[i])) != 0) return (res);
2055 * Place cursor at (x,y), and clear the next "n" chars
2057 errr Term_erase(int x, int y, int n)
2062 /* int h = Term->hgt; */
2067 int na = Term->attr_blank;
2068 int nc = Term->char_blank;
2077 if (Term_gotoxy(x, y)) return (-1);
2079 /* Force legal size */
2080 if (x + n > w) n = w - x;
2083 scr_aa = Term->scr->a[y];
2084 scr_cc = Term->scr->c[y];
2086 scr_taa = Term->scr->ta[y];
2087 scr_tcc = Term->scr->tc[y];
2091 * Á´³Ñʸ»ú¤Î±¦È¾Ê¬¤«¤éʸ»ú¤òɽ¼¨¤¹¤ë¾ì¹ç¡¢
2092 * ½Å¤Ê¤Ã¤¿Ê¸»ú¤Îº¸Éôʬ¤ò¾Ãµî¡£
2094 if (n > 0 && (((scr_aa[x] & AF_KANJI2) && !(scr_aa[x] & AF_TILE1))
2095 || (scr_aa[x] & AF_BIGTILE2) == AF_BIGTILE2))
2097 if (n > 0 && (scr_aa[x] & AF_BIGTILE2) == AF_BIGTILE2)
2104 /* Scan every column */
2105 for (i = 0; i < n; i++, x++)
2110 /* Hack -- Ignore "non-changes" */
2111 if ((oa == na) && (oc == nc)) continue;
2115 * Á´³Ñʸ»ú¤Îº¸È¾Ê¬¤Çɽ¼¨¤ò½ªÎ»¤¹¤ë¾ì¹ç¡¢
2116 * ½Å¤Ê¤Ã¤¿Ê¸»ú¤Î±¦Éôʬ¤ò¾Ãµî¡£
2118 * 2001/04/29 -- Habu
2119 * ¹Ô¤Î±¦Ã¼¤Î¾ì¹ç¤Ï¤³¤Î½èÍý¤ò¤·¤Ê¤¤¤è¤¦¤Ë½¤Àµ¡£
2121 if ((oa & AF_KANJI1) && (i + 1) == n && x != w - 1)
2124 /* Save the "literal" information */
2131 /* Track minimum changed column */
2134 /* Track maximum changed column */
2138 /* Expand the "change area" as needed */
2141 /* Check for new min/max row info */
2142 if (y < Term->y1) Term->y1 = y;
2143 if (y > Term->y2) Term->y2 = y;
2145 /* Check for new min/max col info in this row */
2146 if (x1 < Term->x1[y]) Term->x1[y] = x1;
2147 if (x2 > Term->x2[y]) Term->x2[y] = x2;
2156 * Clear the entire window, and move to the top left corner
2158 * Note the use of the special "total_erase" code
2160 errr Term_clear(void)
2167 byte na = Term->attr_blank;
2168 char nc = Term->char_blank;
2173 /* Cursor to the top left */
2174 Term->scr->cx = Term->scr->cy = 0;
2177 for (y = 0; y < h; y++)
2179 byte *scr_aa = Term->scr->a[y];
2180 char *scr_cc = Term->scr->c[y];
2182 byte *scr_taa = Term->scr->ta[y];
2183 char *scr_tcc = Term->scr->tc[y];
2185 /* Wipe each column */
2186 for (x = 0; x < w; x++)
2195 /* This row has changed */
2197 Term->x2[y] = w - 1;
2200 /* Every row has changed */
2204 /* Force "total erase" */
2205 Term->total_erase = TRUE;
2216 * Redraw (and refresh) the whole window.
2218 errr Term_redraw(void)
2220 /* Force "total erase" */
2221 Term->total_erase = TRUE;
2223 /* Hack -- Refresh */
2232 * Redraw part of a widow.
2234 errr Term_redraw_section(int x1, int y1, int x2, int y2)
2240 /* Bounds checking */
2241 if (y2 >= Term->hgt) y2 = Term->hgt - 1;
2242 if (x2 >= Term->wid) x2 = Term->wid - 1;
2250 /* Set the x limits */
2251 for (i = Term->y1; i <= Term->y2; i++)
2259 if (Term->scr->a[i][x1j] & AF_KANJI2) x1j--;
2262 if (x2j < Term->wid - 1)
2264 if (Term->scr->a[i][x2j] & AF_KANJI1) x2j++;
2270 c_ptr = Term->old->c[i];
2272 /* Clear the section so it is redrawn */
2273 for (j = x1j; j <= x2j; j++)
2275 /* Hack - set the old character to "none" */
2282 c_ptr = Term->old->c[i];
2284 /* Clear the section so it is redrawn */
2285 for (j = x1; j <= x2; j++)
2287 /* Hack - set the old character to "none" */
2293 /* Hack -- Refresh */
2302 /*** Access routines ***/
2306 * Extract the cursor visibility
2308 errr Term_get_cursor(int *v)
2310 /* Extract visibility */
2311 (*v) = Term->scr->cv;
2319 * Extract the current window size
2321 errr Term_get_size(int *w, int *h)
2323 /* Access the cursor */
2333 * Extract the current cursor location
2335 errr Term_locate(int *x, int *y)
2337 /* Access the cursor */
2338 (*x) = Term->scr->cx;
2339 (*y) = Term->scr->cy;
2341 /* Warn about "useless" cursor */
2342 if (Term->scr->cu) return (1);
2350 * At a given location, determine the "current" attr and char
2351 * Note that this refers to what will be on the window after the
2352 * next call to "Term_fresh()". It may or may not already be there.
2354 errr Term_what(int x, int y, byte *a, char *c)
2359 /* Verify location */
2360 if ((x < 0) || (x >= w)) return (-1);
2361 if ((y < 0) || (y >= h)) return (-1);
2364 (*a) = Term->scr->a[y][x];
2365 (*c) = Term->scr->c[y][x];
2373 /*** Input routines ***/
2377 * Flush and forget the input
2379 errr Term_flush(void)
2381 /* Hack -- Flush all events */
2382 Term_xtra(TERM_XTRA_FLUSH, 0);
2384 /* Forget all keypresses */
2385 Term->key_head = Term->key_tail = 0;
2394 * Add a keypress to the "queue"
2396 errr Term_keypress(int k)
2398 /* Hack -- Refuse to enqueue non-keys */
2399 if (!k) return (-1);
2401 /* Store the char, advance the queue */
2402 Term->key_queue[Term->key_head++] = k;
2404 /* Circular queue, handle wrap */
2405 if (Term->key_head == Term->key_size) Term->key_head = 0;
2407 /* Success (unless overflow) */
2408 if (Term->key_head != Term->key_tail) return (0);
2411 /* Hack -- Forget the oldest key */
2412 if (++Term->key_tail == Term->key_size) Term->key_tail = 0;
2421 * Add a keypress to the FRONT of the "queue"
2423 errr Term_key_push(int k)
2425 /* Hack -- Refuse to enqueue non-keys */
2426 if (!k) return (-1);
2428 /* Hack -- Overflow may induce circular queue */
2429 if (Term->key_tail == 0) Term->key_tail = Term->key_size;
2431 /* Back up, Store the char */
2432 Term->key_queue[--Term->key_tail] = k;
2434 /* Success (unless overflow) */
2435 if (Term->key_head != Term->key_tail) return (0);
2438 /* Hack -- Forget the oldest key */
2439 if (++Term->key_tail == Term->key_size) Term->key_tail = 0;
2451 * Check for a pending keypress on the key queue.
2453 * Store the keypress, if any, in "ch", and return "0".
2454 * Otherwise store "zero" in "ch", and return "1".
2456 * Wait for a keypress if "wait" is true.
2458 * Remove the keypress if "take" is true.
2460 errr Term_inkey(char *ch, bool wait, bool take)
2469 /* Hack -- get bored */
2470 if (!Term->never_bored)
2472 /* Process random events */
2473 Term_xtra(TERM_XTRA_BORED, 0);
2479 /* Process pending events while necessary */
2480 while (Term->key_head == Term->key_tail)
2482 /* Process events (wait for one) */
2483 Term_xtra(TERM_XTRA_EVENT, TRUE);
2490 /* Process pending events if necessary */
2491 if (Term->key_head == Term->key_tail)
2493 /* Process events (do not wait) */
2494 Term_xtra(TERM_XTRA_EVENT, FALSE);
2498 /* No keys are ready */
2499 if (Term->key_head == Term->key_tail) return (1);
2501 /* Extract the next keypress */
2502 (*ch) = Term->key_queue[Term->key_tail];
2504 /* If requested, advance the queue, wrap around if necessary */
2505 if (take && (++Term->key_tail == Term->key_size)) Term->key_tail = 0;
2513 /*** Extra routines ***/
2517 * Save the "requested" screen into the "memorized" screen
2519 * Every "Term_save()" should match exactly one "Term_load()"
2521 errr Term_save(void)
2529 /* Allocate window */
2530 MAKE(Term->mem, term_win);
2532 /* Initialize window */
2533 term_win_init(Term->mem, w, h);
2537 term_win_copy(Term->mem, Term->scr, w, h);
2545 * Restore the "requested" contents (see above).
2547 * Every "Term_save()" should match exactly one "Term_load()"
2549 errr Term_load(void)
2559 /* Allocate window */
2560 MAKE(Term->mem, term_win);
2562 /* Initialize window */
2563 term_win_init(Term->mem, w, h);
2567 term_win_copy(Term->scr, Term->mem, w, h);
2570 for (y = 0; y < h; y++)
2574 Term->x2[y] = w - 1;
2587 * Exchange the "requested" screen with the "tmp" screen
2589 errr Term_exchange(void)
2596 term_win *exchanger;
2602 /* Allocate window */
2603 MAKE(Term->tmp, term_win);
2605 /* Initialize window */
2606 term_win_init(Term->tmp, w, h);
2610 exchanger = Term->scr;
2611 Term->scr = Term->tmp;
2612 Term->tmp = exchanger;
2615 for (y = 0; y < h; y++)
2619 Term->x2[y] = w - 1;
2633 * React to a new physical window size.
2635 errr Term_resize(int w, int h)
2649 /* Resizing is forbidden */
2650 if (Term->fixed_shape) return (-1);
2652 /* Ignore illegal changes */
2653 if ((w < 1) || (h < 1)) return (-1);
2656 /* Ignore non-changes */
2657 if ((Term->wid == w) && (Term->hgt == h) && (arg_bigtile == use_bigtile))
2660 use_bigtile = arg_bigtile;
2662 /* Minimum dimensions */
2663 wid = MIN(Term->wid, w);
2664 hgt = MIN(Term->hgt, h);
2670 /* Save old window */
2671 hold_old = Term->old;
2673 /* Save old window */
2674 hold_scr = Term->scr;
2676 /* Save old window */
2677 hold_mem = Term->mem;
2679 /* Save old window */
2680 hold_tmp = Term->tmp;
2682 /* Create new scanners */
2683 C_MAKE(Term->x1, h, byte);
2684 C_MAKE(Term->x2, h, byte);
2686 /* Create new window */
2687 MAKE(Term->old, term_win);
2689 /* Initialize new window */
2690 term_win_init(Term->old, w, h);
2692 /* Save the contents */
2693 term_win_copy(Term->old, hold_old, wid, hgt);
2695 /* Create new window */
2696 MAKE(Term->scr, term_win);
2698 /* Initialize new window */
2699 term_win_init(Term->scr, w, h);
2701 /* Save the contents */
2702 term_win_copy(Term->scr, hold_scr, wid, hgt);
2707 /* Create new window */
2708 MAKE(Term->mem, term_win);
2710 /* Initialize new window */
2711 term_win_init(Term->mem, w, h);
2713 /* Save the contents */
2714 term_win_copy(Term->mem, hold_mem, wid, hgt);
2720 /* Create new window */
2721 MAKE(Term->tmp, term_win);
2723 /* Initialize new window */
2724 term_win_init(Term->tmp, w, h);
2726 /* Save the contents */
2727 term_win_copy(Term->tmp, hold_tmp, wid, hgt);
2730 /* Free some arrays */
2731 C_KILL(hold_x1, Term->hgt, byte);
2732 C_KILL(hold_x2, Term->hgt, byte);
2735 term_win_nuke(hold_old, Term->wid, Term->hgt);
2738 KILL(hold_old, term_win);
2740 /* Illegal cursor */
2741 if (Term->old->cx >= w) Term->old->cu = 1;
2742 if (Term->old->cy >= h) Term->old->cu = 1;
2745 term_win_nuke(hold_scr, Term->wid, Term->hgt);
2748 KILL(hold_scr, term_win);
2750 /* Illegal cursor */
2751 if (Term->scr->cx >= w) Term->scr->cu = 1;
2752 if (Term->scr->cy >= h) Term->scr->cu = 1;
2758 term_win_nuke(hold_mem, Term->wid, Term->hgt);
2761 KILL(hold_mem, term_win);
2763 /* Illegal cursor */
2764 if (Term->mem->cx >= w) Term->mem->cu = 1;
2765 if (Term->mem->cy >= h) Term->mem->cu = 1;
2772 term_win_nuke(hold_tmp, Term->wid, Term->hgt);
2775 KILL(hold_tmp, term_win);
2777 /* Illegal cursor */
2778 if (Term->tmp->cx >= w) Term->tmp->cu = 1;
2779 if (Term->tmp->cy >= h) Term->tmp->cu = 1;
2786 /* Force "total erase" */
2787 Term->total_erase = TRUE;
2790 for (i = 0; i < h; i++)
2794 Term->x2[i] = w - 1;
2801 /* Execute the "resize_hook" hook, if available */
2802 if (Term->resize_hook)
2804 Term->resize_hook();
2814 * Activate a new Term (and deactivate the current Term)
2816 * This function is extremely important, and also somewhat bizarre.
2817 * It is the only function that should "modify" the value of "Term".
2819 * To "create" a valid "term", one should do "term_init(t)", then
2820 * set the various flags and hooks, and then do "Term_activate(t)".
2822 errr Term_activate(term *t)
2824 /* Hack -- already done */
2825 if (Term == t) return (1);
2827 /* Deactivate the old Term */
2828 if (Term) Term_xtra(TERM_XTRA_LEVEL, 0);
2830 /* Hack -- Call the special "init" hook */
2831 if (t && !t->active_flag)
2833 /* Call the "init" hook */
2834 if (t->init_hook) (*t->init_hook)(t);
2837 t->active_flag = TRUE;
2840 t->mapped_flag = TRUE;
2843 /* Remember the Term */
2846 /* Activate the new Term */
2847 if (Term) Term_xtra(TERM_XTRA_LEVEL, 1);
2858 errr term_nuke(term *t)
2864 /* Hack -- Call the special "nuke" hook */
2867 /* Call the "nuke" hook */
2868 if (t->nuke_hook) (*t->nuke_hook)(t);
2871 t->active_flag = FALSE;
2873 /* Assume not mapped */
2874 t->mapped_flag = FALSE;
2878 /* Nuke "displayed" */
2879 term_win_nuke(t->old, w, h);
2881 /* Kill "displayed" */
2882 KILL(t->old, term_win);
2884 /* Nuke "requested" */
2885 term_win_nuke(t->scr, w, h);
2887 /* Kill "requested" */
2888 KILL(t->scr, term_win);
2893 /* Nuke "memorized" */
2894 term_win_nuke(t->mem, w, h);
2896 /* Kill "memorized" */
2897 KILL(t->mem, term_win);
2903 /* Nuke "temporary" */
2904 term_win_nuke(t->tmp, w, h);
2906 /* Kill "temporary" */
2907 KILL(t->tmp, term_win);
2910 /* Free some arrays */
2911 C_KILL(t->x1, h, byte);
2912 C_KILL(t->x2, h, byte);
2914 /* Free the input queue */
2915 C_KILL(t->key_queue, t->key_size, char);
2923 * Initialize a term, using a window of the given size.
2924 * Also prepare the "input queue" for "k" keypresses
2925 * By default, the cursor starts out "invisible"
2926 * By default, we "erase" using "black spaces"
2928 errr term_init(term *t, int w, int h, int k)
2934 (void)WIPE(t, term);
2937 /* Prepare the input queue */
2938 t->key_head = t->key_tail = 0;
2940 /* Determine the input queue size */
2943 /* Allocate the input queue */
2944 C_MAKE(t->key_queue, t->key_size, char);
2951 /* Allocate change arrays */
2952 C_MAKE(t->x1, h, byte);
2953 C_MAKE(t->x2, h, byte);
2956 /* Allocate "displayed" */
2957 MAKE(t->old, term_win);
2959 /* Initialize "displayed" */
2960 term_win_init(t->old, w, h);
2963 /* Allocate "requested" */
2964 MAKE(t->scr, term_win);
2966 /* Initialize "requested" */
2967 term_win_init(t->scr, w, h);
2971 for (y = 0; y < h; y++)
2982 /* Force "total erase" */
2983 t->total_erase = TRUE;
2986 /* Default "blank" */
2988 t->char_blank = ' ';
2991 /* Prepare "fake" hooks to prevent core dumps */
2992 t->curs_hook = Term_curs_hack;
2993 t->bigcurs_hook = Term_bigcurs_hack;
2994 t->wipe_hook = Term_wipe_hack;
2995 t->text_hook = Term_text_hack;
2996 t->pict_hook = Term_pict_hack;