OSDN Git Service

Since the contents of Term aren't used, don't bother to call Term_activate() from...
[hengbandforosx/hengbandosx.git] / src / main-cocoa.m
index 620fe3d..3cc5dff 100644 (file)
@@ -33,7 +33,7 @@
 #define kVK_ANSI_KeypadEnter 0x4C
 
 static NSString * const AngbandDirectoryNameLib = @"lib";
-static NSString * const AngbandDirectoryNameBase = @"Hengband";
+static NSString * const AngbandDirectoryNameBase = @VERSION_NAME;
 
 static NSString * const AngbandMessageCatalog = @"Localizable";
 static NSString * const AngbandTerminalsDefaultsKey = @"Terminals";
@@ -47,13 +47,6 @@ static NSString * const AngbandSoundDefaultsKey = @"AllowSound";
 static NSInteger const AngbandWindowMenuItemTagBase = 1000;
 static NSInteger const AngbandCommandMenuItemTagBase = 2000;
 
-/* We can blit to a large layer or image and then scale it down during live
- * resize, which makes resizing much faster, at the cost of some image quality
- * during resizing */
-#ifndef USE_LIVE_RESIZE_CACHE
-# define USE_LIVE_RESIZE_CACHE 1
-#endif
-
 /* Global defines etc from Angband 3.5-dev - NRM */
 #define ANGBAND_TERM_MAX 8
 
@@ -82,6 +75,10 @@ static bool new_game = FALSE;
 
 @class AngbandView;
 
+#ifdef JP
+static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp);
+#endif
+
 /**
  * Load sound effects based on sound.cfg within the xtra/sound directory;
  * bridge to Cocoa to use NSSound for simple loading and playback, avoiding
@@ -345,51 +342,126 @@ static __strong AngbandSoundCatalog* gSharedSounds = nil;
 
 @end
 
-/*
- * To handle fonts where an individual glyph's bounding box can extend into
- * neighboring columns, Term_curs_cocoa(), Term_pict_cocoa(),
- * Term_text_cocoa(), and Term_wipe_cocoa() merely record what needs to be
- * done with the actual drawing happening in response to the notification to
- * flush all rows, the TERM_XTRA_FRESH case in Term_xtra_cocoa().  Can not use
- * the TERM_XTRA_FROSH notification (the per-row flush), since with a software
- * cursor, there are calls to Term_pict_cocoa(), Term_text_cocoa(), or
- * Term_wipe_cocoa() to take care of the old cursor position which are not
- * followed by a row flush.
+/**
+ * Each location in the terminal either stores a character, a tile,
+ * padding for a big tile, or padding for a big character (for example a
+ * kanji that takes two columns).  These structures represent that.  Note
+ * that tiles do not overlap with each other (excepting the double-height
+ * tiles, i.e. from the Shockbolt set; that's handled as a special case).
+ * Characters can overlap horizontally:  that is for handling fonts that
+ * aren't fixed width.
  */
-enum PendingCellChangeType {
-    CELL_CHANGE_NONE = 0,
-    CELL_CHANGE_WIPE,
-    CELL_CHANGE_TEXT,
-    CELL_CHANGE_TILE
-};
-struct PendingTextChange {
+struct TerminalCellChar {
     wchar_t glyph;
-    int color;
+    int attr;
+};
+struct TerminalCellTile {
     /*
-     * Is YES if glyph is a character that takes up two columns (i.e.
-     * Japanese kanji); otherwise it is NO.
+     * These are the coordinates, within the tile set, for the foreground
+     * tile and background tile.
      */
-    BOOL doubleWidth;
-};
-struct PendingTileChange {
     char fgdCol, fgdRow, bckCol, bckRow;
 };
-struct PendingCellChange {
-    union { struct PendingTextChange txc; struct PendingTileChange tic; } v;
-    enum PendingCellChangeType changeType;
+struct TerminalCellPadding {
+       /*
+       * If the cell at (x, y) is padding, the cell at (x - hoff, y - voff)
+       * has the attributes affecting the padded region.
+       */
+    unsigned char hoff, voff;
+};
+struct TerminalCell {
+    union {
+       struct TerminalCellChar ch;
+       struct TerminalCellTile ti;
+       struct TerminalCellPadding pd;
+    } v;
+    /*
+     * Used for big characters or tiles which are hscl x vscl cells.
+     * The upper left corner of the big tile or character is marked as
+     * TERM_CELL_TILE or TERM_CELL_CHAR.  The remainder are marked as
+     * TERM_CELL_TILE_PADDING or TERM_CELL_CHAR_PADDING and have hscl and
+     * vscl set to matcn what's in the upper left corner.  Big tiles are
+     * tiles scaled up to occupy more space.  Big characters, on the other
+     * hand, are characters that naturally take up more space than standard
+     * for the font with the assumption that vscl will be one for any big
+     * character and hscl will hold the number of columns it occupies (likely
+     * just 2, i.e. for Japanese kanji).
+     */
+    unsigned char hscl;
+    unsigned char vscl;
+    /*
+     * Hold the offsets, as fractions of the tile size expressed as the
+     * rational numbers hoff_n / hoff_d and voff_n / voff_d, within the tile
+     * or character.  For something that is not a big tile or character, these
+     * will be 0, 0, 1, and 1.  For a big tile or character, these will be
+     * set when the tile or character is changed to be 0, 0, hscl, and vscl
+     * for the upper left corner and i, j, hscl, vscl for the padding element
+     * at (i, j) relative to the upper left corner.  For a big tile or
+     * character that is partially overwritten, these are not modified in the
+     * parts that are not overwritten while hscl, vscl, and, for padding,
+     * v.pd.hoff and v.pd.voff are.
+     */
+    unsigned char hoff_n;
+    unsigned char voff_n;
+    unsigned char hoff_d;
+    unsigned char voff_d;
+    /*
+     * Is either TERM_CELL_CHAR, TERM_CELL_CHAR_PADDING, TERM_CELL_TILE, or
+     * TERM_CELL_TILE_PADDING.
+     */
+    unsigned char form;
+};
+#define TERM_CELL_CHAR (0x1)
+#define TERM_CELL_CHAR_PADDING (0x2)
+#define TERM_CELL_TILE (0x4)
+#define TERM_CELL_TILE_PADDING (0x8)
+
+struct TerminalCellBlock {
+    int ulcol, ulrow, w, h;
+};
+
+struct TerminalCellLocation {
+    int col, row;
 };
 
-@interface PendingTermChanges : NSObject {
-@private
-    int *colBounds;
-    struct PendingCellChange **changesByRow;
+typedef int (*TerminalCellPredicate)(const struct TerminalCell*);
+
+static int isTileTop(const struct TerminalCell *c)
+{
+    return (c->form == TERM_CELL_TILE ||
+           (c->form == TERM_CELL_TILE_PADDING && c->v.pd.voff == 0)) ? 1 : 0;
+}
+
+static int isPartiallyOverwrittenBigChar(const struct TerminalCell *c)
+{
+    if ((c->form & (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
+       /*
+        * When the tile is set in Term_pict_cocoa, hoff_d is the same as hscl
+        * and voff_d is the same as vscl.  hoff_d and voff_d aren't modified
+        * after that, but hscl and vscl are in response to partial overwrites.
+        * If they're diffent, an overwrite has occurred.
+        */
+       return ((c->hoff_d > 1 || c->voff_d > 1) &&
+               (c->hoff_d != c->hscl || c->voff_d != c->vscl)) ? 1 : 0;
+    }
+    return 0;
+}
+
+static int isCharNoPartial(const struct TerminalCell *c)
+{
+    return ((c->form & (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0 &&
+           ! isPartiallyOverwrittenBigChar(c)) ? 1 : 0;
 }
 
 /**
- * Returns YES if nCol and nRow are a feasible size for the pending changes.
- * Otherwise, returns NO.
+ * Since the drawing is decoupled from Angband's calls to the text_hook,
+ * pict_hook, wipe_hook, curs_hook, and bigcurs_hook callbacks of a terminal,
+ * maintain a version of the Terminal contents.
  */
-+ (BOOL)isValidSize:(int)nCol rows:(int)nRow;
+@interface TerminalContents : NSObject {
+@private
+    struct TerminalCell *cells;
+}
 
 /**
  * Initialize with zero columns and zero rows.
@@ -397,137 +469,150 @@ struct PendingCellChange {
 - (id)init;
 
 /**
- * Initialize with nCol columns and nRow rows.  No changes will be marked.
- */
-- (id)initWithColumnsRows:(int)nCol rows:(int)nRow NS_DESIGNATED_INITIALIZER;
-
-/**
- * Clears all marked changes.
+ * Initialize with nCol columns and nRow rows.  All elements will be set to
+ * blanks.
  */
-- (void)clear;
+- (id)initWithColumns:(int)nCol rows:(int)nRow NS_DESIGNATED_INITIALIZER;
 
 /**
- * Changes the bounds over which changes are recorded.  Has the side effect
- * of clearing any marked changes.  Will throw an exception if nCol or nRow
- * is negative.
+ * Resize to be nCol by nRow.  Current contents still within the new bounds
+ * are preserved.  Added areas are filled with blanks.
  */
-- (void)resize:(int)nCol rows:(int)nRow;
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow;
 
 /**
- * Mark the cell, (iCol, iRow), as having changed text.
+ * Get the contents of a given cell.
  */
-- (void)markTextChange:(int)iCol row:(int)iRow glyph:(wchar_t)g color:(int)c
-        isDoubleWidth:(BOOL)dw;
+- (const struct TerminalCell*)getCellAtColumn:(int)icol row:(int)irow;
 
 /**
- * Mark the cell, (iCol, iRow), as having a changed tile.
+ * Scans the row, irow, starting at the column, icol0, and stopping before the
+ * column, icol1.  Returns the column index for the first cell that's within
+ * the given type mask, tm.  If all of the cells in that range are not within
+ * the given type mask, returns icol1.
  */
-- (void)markTileChange:(int)iCol row:(int)iRow
-        foregroundCol:(char)fc foregroundRow:(char)fr
-        backgroundCol:(char)bc backgroundRow:(char)br;
+- (int)scanForTypeMaskInRow:(int)irow mask:(unsigned int)tm col0:(int)icol0
+                      col1:(int)icol1;
 
 /**
- * Mark the cells from (iCol, iRow) to (iCol + nCol - 1, iRow) as wiped.
+ * Scans the w x h block whose upper left corner is at (icol, irow).  The
+ * scan starts at (icol + pcurs->col, irow + pcurs->row) and proceeds from
+ * left to right and top to bottom.  At exit, pcurs will have the location
+ * (relative to icol, irow) of the first cell encountered that's within the
+ * given type mask, tm.  If no such cell was found, pcurs->col will be w
+ * and pcurs->row will be h.
  */
-- (void)markWipeRange:(int)iCol row:(int)iRow n:(int)nCol;
+- (void)scanForTypeMaskInBlockAtColumn:(int)icol row:(int)irow width:(int)w
+                               height:(int)h mask:(unsigned int)tm
+                               cursor:(struct TerminalCellLocation*)pcurs;
 
 /**
- * Mark the location of the cursor.  The cursor will be the standard size:
- * one cell.
+ * Scans the row, irow, starting at the column, icol0, and stopping before the
+ * column, icol1.  Returns the column index for the first cell that
+ * func(cell_address) != rval.  If all of the cells in the range satisfy the
+ * predicate, returns icol1.
  */
-- (void)markCursor:(int)iCol row:(int)iRow;
+- (int)scanForPredicateInRow:(int)irow
+                  predicate:(TerminalCellPredicate)func
+                    desired:(int)rval
+                       col0:(int)icol0
+                       col1:(int)icol1;
 
 /**
- * Mark the location of the cursor.  The cursor will be w cells wide and
- * h cells tall and the given location is the position of the upper left
- * corner.
+ * Change the contents to have the given string of n characters appear with
+ * the leftmost character at (icol, irow).
  */
-- (void)markBigCursor:(int)iCol row:(int)iRow
-           cellsWide:(int)w cellsHigh:(int)h;
+- (void)setUniformAttributeTextRunAtColumn:(int)icol
+                                      row:(int)irow
+                                        n:(int)n
+                                   glyphs:(const char*)g
+                                attribute:(int)a;
 
 /**
- * Return the zero-based index of the first column changed for the given
- * zero-based row index.  If there are no changes in the row, the returned
- * value will be the number of columns.
+ * Change the contents to have a tile scaled to w x h appear with its upper
+ * left corner at (icol, irow).
  */
-- (int)getFirstChangedColumnInRow:(int)iRow;
+- (void)setTileAtColumn:(int)icol
+                   row:(int)irow
+       foregroundColumn:(char)fgdCol
+         foregroundRow:(char)fgdRow
+       backgroundColumn:(char)bckCol
+         backgroundRow:(char)bckRow
+             tileWidth:(int)w
+            tileHeight:(int)h;
 
 /**
- * Return the zero-based index of the last column changed for the given
- * zero-based row index.  If there are no changes in the row, the returned
- * value will be -1.
+ * Wipe the w x h block whose upper left corner is at (icol, irow).
  */
-- (int)getLastChangedColumnInRow:(int)iRow;
+- (void)wipeBlockAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h;
 
 /**
- * Return the type of change at the given cell, (iCol, iRow).
+ * Wipe all the contents.
  */
-- (enum PendingCellChangeType)getCellChangeType:(int)iCol row:(int)iRow;
+- (void)wipe;
 
 /**
- * Return the nature of a text change at the given cell, (iCol, iRow).
- * Will throw an exception if [obj getCellChangeType:iCol row:iRow] is
- * neither CELL_CHANGE_TEXT nor CELL_CHANGE_WIPE.
+ * Wipe any tiles.
  */
-- (struct PendingTextChange)getCellTextChange:(int)iCol row:(int)iRow;
+- (void)wipeTiles;
 
 /**
- * Return the nature of a tile change at the given cell, (iCol, iRow).
- * Will throw an exception if [obj getCellChangeType:iCol row:iRow] is
- * different than CELL_CHANGE_TILE.
+ * Thie is a helper function for wipeBlockAtColumn.
  */
-- (struct PendingTileChange)getCellTileChange:(int)iCol row:(int)iRow;
+- (void)wipeBlockAuxAtColumn:(int)icol row:(int)irow width:(int)w
+                     height:(int)h;
 
 /**
- * Is the number of columns for recording changes.
+ * This is a helper function for checkForBigStuffOverwriteAtColumn.
  */
-@property (readonly) int columnCount;
+- (void) splitBlockAtColumn:(int)icol row:(int)irow n:(int)nsub
+                    blocks:(const struct TerminalCellBlock*)b;
 
 /**
- * Is the number of rows for recording changes.
+ * This is a helper function for setUniformAttributeTextRunAtColumn,
+ * setTileAtColumn, and wipeBlockAtColumn.  If a modification could partially
+ * overwrite a big character or tile, make adjustments so what's left can
+ * be handled appropriately in rendering.
  */
-@property (readonly) int rowCount;
+- (void)checkForBigStuffOverwriteAtColumn:(int)icol row:(int)irow
+                                   width:(int)w height:(int)h;
 
 /**
- * Will be YES if there are any pending changes to locations rendered as text.
- * Otherwise, it will be NO.
+ * Position the upper left corner of the cursor at (icol, irow) and have it
+ * encompass w x h cells.
  */
-@property (readonly) BOOL hasTextChanges;
+- (void)setCursorAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h;
 
 /**
- * Will be YES if there are any pending changes to locations rendered as tiles.
- * Otherwise, it will be NO.
+ * Remove the cursor.  cursorColumn and cursorRow will be -1 until
+ * setCursorAtColumn is called.
  */
-@property (readonly) BOOL hasTileChanges;
+- (void)removeCursor;
 
 /**
- * Will be YES if there are any pending wipes.  Otherwise, it will be NO.
+ * Verify that everying is consistent.
  */
-@property (readonly) BOOL hasWipeChanges;
+- (void)assertInvariants;
 
 /**
- * Is the zero-based index of the first row with changes.  Will be equal to
- * the number of rows if there are no changes.
+ * Is the number of columns.
  */
-@property (readonly) int firstChangedRow;
+@property (readonly) int columnCount;
 
 /**
- * Is the zero-based index of the last row with changes.  Will be equal to
- * -1 if there are no changes.
+ * Is the number of rows.
  */
-@property (readonly) int lastChangedRow;
+@property (readonly) int rowCount;
 
 /**
- * Is the zero-based index for the column with the upper left corner of the
- * cursor.  It will be -1 if the cursor position has not been set since the
- * changes were cleared.
+ * Is the column index for the upper left corner of the cursor.  It will be -1
+ * if the cursor is disabled.
  */
 @property (readonly) int cursorColumn;
 
 /**
- * Is the zero-based index for the row with the upper left corner of the
- * cursor.  It will be -1 if the cursor position has not been set since the
- * changes were cleared.
+ * Is the row index for the upper left corner of the cursor.  It will be -1
+ * if the cursor is disabled.
  */
 @property (readonly) int cursorRow;
 
@@ -542,439 +627,1209 @@ struct PendingCellChange {
 @property (readonly) int cursorHeight;
 
 /**
- * This is a helper for the mark* messages.
- */
-- (void)setupForChange:(int)iCol row:(int)iRow n:(int)nCol;
-
-/**
- * Throw an exception if the given range of column indices is invalid
- * (including non-positive values for nCol).
+ * Return the character to be used for blanks.
  */
-- (void)checkColumnIndices:(int)iCol n:(int)nCol;
++ (wchar_t)getBlankChar;
 
 /**
- * Throw an exception if the given row index is invalid.
+ * Return the attribute to be used for blanks.
  */
-- (void)checkRowIndex:(int)iRow;
++ (int)getBlankAttribute;
 
 @end
 
-@implementation PendingTermChanges
-
-+ (BOOL)isValidSize:(int)nCol rows:(int)nRow
-{
-    if (nCol < 0 ||
-       (size_t) nCol > SIZE_MAX / sizeof(struct PendingCellChange) ||
-       nRow < 0 ||
-       (size_t) nRow > SIZE_MAX / sizeof(struct PendingCellChange*) ||
-       (size_t) nRow > SIZE_MAX / (2 * sizeof(int))) {
-       return NO;
-    }
-    return YES;
-}
+@implementation TerminalContents
 
 - (id)init
 {
-    return [self initWithColumnsRows:0 rows:0];
+    return [self initWithColumns:0 rows:0];
 }
 
-- (id)initWithColumnsRows:(int)nCol rows:(int)nRow
+- (id)initWithColumns:(int)nCol rows:(int)nRow
 {
     if (self = [super init]) {
-       if (! [PendingTermChanges isValidSize:nCol rows:nRow]) {
-           return nil;
-       }
-       self->colBounds = malloc((size_t) 2 * sizeof(int) * nRow);
-       if (self->colBounds == 0 && nRow > 0) {
-           return nil;
-       }
-       self->changesByRow = calloc(nRow, sizeof(struct PendingCellChange*));
-       if (self->changesByRow == 0 && nRow > 0) {
-           free(self->colBounds);
-           return nil;
-       }
-       for (int i = 0; i < nRow + nRow; i += 2) {
-           self->colBounds[i] = nCol;
-           self->colBounds[i + 1] = -1;
-       }
+       self->cells = malloc(nCol * nRow * sizeof(struct TerminalCell));
        self->_columnCount = nCol;
        self->_rowCount = nRow;
-       self->_hasTextChanges = NO;
-       self->_hasTileChanges = NO;
-       self->_hasWipeChanges = NO;
-       self->_firstChangedRow = nRow;
-       self->_lastChangedRow = -1;
        self->_cursorColumn = -1;
        self->_cursorRow = -1;
        self->_cursorWidth = 1;
        self->_cursorHeight = 1;
+       [self wipe];
     }
     return self;
 }
 
 - (void)dealloc
 {
-    if (self->changesByRow != 0) {
-       for (int i = 0; i < self.rowCount; ++i) {
-           if (self->changesByRow[i] != 0) {
-               free(self->changesByRow[i]);
-               self->changesByRow[i] = 0;
-           }
-       }
-       free(self->changesByRow);
-       self->changesByRow = 0;
-    }
-    if (self->colBounds != 0) {
-       free(self->colBounds);
-       self->colBounds = 0;
+    if (self->cells != 0) {
+       free(self->cells);
+       self->cells = 0;
     }
 }
 
-- (void)clear
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow
 {
-    for (int i = 0; i < self.rowCount; ++i) {
-       self->colBounds[i + i] = self.columnCount;
-       self->colBounds[i + i + 1] = -1;
-       if (self->changesByRow[i] != 0) {
-           free(self->changesByRow[i]);
-           self->changesByRow[i] = 0;
+    /*
+     * Potential issue: big tiles or characters can become clipped by the
+     * resize.  That will only matter if drawing occurs before the contents
+     * are updated by Angband.  Even then, unless the drawing mode is used
+     * where AppKit doesn't clip to the window bounds, the only artifact will
+     * be clipping when drawn which is acceptable and doesn't require
+     * additional logic to either filter out the clipped big stuff here or
+     * to just clear it when drawing.
+     */
+    struct TerminalCell *newCells =
+       malloc(nCol * nRow * sizeof(struct TerminalCell));
+    struct TerminalCell *cellsOutCursor = newCells;
+    const struct TerminalCell *cellsInCursor = self->cells;
+    int nColCommon = (nCol < self.columnCount) ? nCol : self.columnCount;
+    int nRowCommon = (nRow < self.rowCount) ? nRow : self.rowCount;
+    wchar_t blank = [TerminalContents getBlankChar];
+    int blank_attr = [TerminalContents getBlankAttribute];
+    int i;
+
+    for (i = 0; i < nRowCommon; ++i) {
+       (void) memcpy(
+           cellsOutCursor,
+           cellsInCursor,
+           nColCommon * sizeof(struct TerminalCell));
+       cellsInCursor += self.columnCount;
+       for (int j = nColCommon; j < nCol; ++j) {
+           cellsOutCursor[j].v.ch.glyph = blank;
+           cellsOutCursor[j].v.ch.attr = blank_attr;
+           cellsOutCursor[j].hscl = 1;
+           cellsOutCursor[j].vscl = 1;
+           cellsOutCursor[j].hoff_n = 0;
+           cellsOutCursor[j].voff_n = 0;
+           cellsOutCursor[j].hoff_d = 1;
+           cellsOutCursor[j].voff_d = 1;
+           cellsOutCursor[j].form = TERM_CELL_CHAR;
+       }
+       cellsOutCursor += nCol;
+    }
+    while (cellsOutCursor != newCells + nCol * nRow) {
+       cellsOutCursor->v.ch.glyph = blank;
+       cellsOutCursor->v.ch.attr = blank_attr;
+       cellsOutCursor->hscl = 1;
+       cellsOutCursor->vscl = 1;
+       cellsOutCursor->hoff_n = 0;
+       cellsOutCursor->voff_n = 0;
+       cellsOutCursor->hoff_d = 1;
+       cellsOutCursor->voff_d = 1;
+       cellsOutCursor->form = TERM_CELL_CHAR;
+       ++cellsOutCursor;
+    }
+
+    free(self->cells);
+    self->cells = newCells;
+    self->_columnCount = nCol;
+    self->_rowCount = nRow;
+    if (self->_cursorColumn >= nCol || self->_cursorRow >= nRow) {
+       self->_cursorColumn = -1;
+       self->_cursorRow = -1;
+    } else {
+       if (self->_cursorColumn + self->_cursorWidth > nCol) {
+           self->_cursorWidth = nCol - self->_cursorColumn;
+       }
+       if (self->_cursorRow + self->_cursorHeight > nRow) {
+           self->_cursorHeight = nRow - self->_cursorRow;
        }
     }
-    self->_hasTextChanges = NO;
-    self->_hasTileChanges = NO;
-    self->_hasWipeChanges = NO;
-    self->_firstChangedRow = self.rowCount;
-    self->_lastChangedRow = -1;
-    self->_cursorColumn = -1;
-    self->_cursorRow = -1;
-    self->_cursorWidth = 1;
-    self->_cursorHeight = 1;
 }
 
-- (void)resize:(int)nCol rows:(int)nRow
+- (const struct TerminalCell*)getCellAtColumn:(int)icol row:(int)irow
 {
-    if (! [PendingTermChanges isValidSize:nCol rows:nRow]) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"PendingTermChangesRowsColumns"
-                              reason:@"resize called with number of columns or rows that is negative or too large"
-                              userInfo:nil];
-       @throw exc;
-    }
+    return self->cells + icol + irow * self.columnCount;
+}
 
-    int *cb = malloc((size_t) 2 * sizeof(int) * nRow);
-    struct PendingCellChange** cbr =
-       calloc(nRow, sizeof(struct PendingCellChange*));
-    int i;
+- (int)scanForTypeMaskInRow:(int)irow mask:(unsigned int)tm col0:(int)icol0
+                      col1:(int)icol1
+{
+    int i = icol0;
+    const struct TerminalCell *cellsRow =
+       self->cells + irow * self.columnCount;
 
-    if ((cb == 0 || cbr == 0) && nRow > 0) {
-       if (cbr != 0) {
-           free(cbr);
+    while (1) {
+       if (i >= icol1) {
+           return icol1;
        }
-       if (cb != 0) {
-           free(cb);
+       if ((cellsRow[i].form & tm) != 0) {
+           return i;
        }
-
-       NSException *exc = [NSException
-                              exceptionWithName:@"OutOfMemory"
-                              reason:@"resize called for PendingTermChanges"
-                              userInfo:nil];
-       @throw exc;
+       ++i;
     }
+}
 
-    for (i = 0; i < nRow; ++i) {
-       cb[i + i] = nCol;
-       cb[i + i + 1] = -1;
-    }
-    if (self->changesByRow != 0) {
-       for (i = 0; i < self.rowCount; ++i) {
-           if (self->changesByRow[i] != 0) {
-               free(self->changesByRow[i]);
-               self->changesByRow[i] = 0;
+- (void)scanForTypeMaskInBlockAtColumn:(int)icol row:(int)irow width:(int)w
+                               height:(int)h mask:(unsigned int)tm
+                               cursor:(struct TerminalCellLocation*)pcurs
+{
+    const struct TerminalCell *cellsRow =
+       self->cells + (irow + pcurs->row) * self.columnCount;
+    while (1) {
+       if (pcurs->col == w) {
+           if (pcurs->row >= h - 1) {
+               pcurs->row = h;
+               return;
            }
+           ++pcurs->row;
+           pcurs->col = 0;
+           cellsRow += self.columnCount;
        }
-       free(self->changesByRow);
-    }
-    if (self->colBounds != 0) {
-       free(self->colBounds);
-    }
 
-    self->colBounds = cb;
-    self->changesByRow = cbr;
-    self->_columnCount = nCol;
-    self->_rowCount = nRow;
-    self->_hasTextChanges = NO;
-    self->_hasTileChanges = NO;
-    self->_hasWipeChanges = NO;
-    self->_firstChangedRow = self.rowCount;
-    self->_lastChangedRow = -1;
-    self->_cursorColumn = -1;
-    self->_cursorRow = -1;
-    self->_cursorWidth = 1;
-    self->_cursorHeight = 1;
-}
+       if ((cellsRow[icol + pcurs->col].form & tm) != 0) {
+           return;
+       }
 
-- (void)markTextChange:(int)iCol row:(int)iRow glyph:(wchar_t)g color:(int)c
-        isDoubleWidth:(BOOL)dw
-{
-    [self setupForChange:iCol row:iRow n:((dw) ? 2 : 1)];
-    struct PendingCellChange *pcc = self->changesByRow[iRow] + iCol;
-    pcc->v.txc.glyph = g;
-    pcc->v.txc.color = c;
-    pcc->v.txc.doubleWidth = dw;
-    pcc->changeType = CELL_CHANGE_TEXT;
-    /*
-     * Fill in a dummy since the previous character will take up two columns.
-     */
-    if (dw) {
-       pcc[1].v.txc.glyph = 0;
-       pcc[1].v.txc.color = c;
-       pcc[1].v.txc.doubleWidth = NO;
-       pcc[1].changeType = CELL_CHANGE_TEXT;
+       ++pcurs->col;
     }
-    self->_hasTextChanges = YES;
 }
 
-- (void)markTileChange:(int)iCol row:(int)iRow
-        foregroundCol:(char)fc foregroundRow:(char)fr
-        backgroundCol:(char)bc backgroundRow:(char)br
+- (int)scanForPredicateInRow:(int)irow
+                  predicate:(TerminalCellPredicate)func
+                    desired:(int)rval
+                       col0:(int)icol0
+                       col1:(int)icol1
 {
-    [self setupForChange:iCol row:iRow n:1];
-    struct PendingCellChange *pcc = self->changesByRow[iRow] + iCol;
-    pcc->v.tic.fgdCol = fc;
-    pcc->v.tic.fgdRow = fr;
-    pcc->v.tic.bckCol = bc;
-    pcc->v.tic.bckRow = br;
-    pcc->changeType = CELL_CHANGE_TILE;
-    self->_hasTileChanges = YES;
-}
+    int i = icol0;
+    const struct TerminalCell *cellsRow =
+       self->cells + irow * self.columnCount;
 
-- (void)markWipeRange:(int)iCol row:(int)iRow n:(int)nCol
-{
-    [self setupForChange:iCol row:iRow n:nCol];
-    struct PendingCellChange *pcc = self->changesByRow[iRow] + iCol;
-    for (int i = 0; i < nCol; ++i) {
-       pcc[i].v.txc.glyph = 0;
-       pcc[i].v.txc.color = 0;
-       pcc[i].changeType = CELL_CHANGE_WIPE;
+    while (1) {
+       if (i >= icol1) {
+           return icol1;
+       }
+       if (func(cellsRow + i) != rval) {
+           return i;
+       }
+       ++i;
     }
-    self->_hasWipeChanges = YES;
 }
 
-- (void)markCursor:(int)iCol row:(int)iRow
+- (void)setUniformAttributeTextRunAtColumn:(int)icol
+                                      row:(int)irow
+                                        n:(int)n
+                                   glyphs:(const char*)g
+                                attribute:(int)a
 {
-    /* Allow negative indices to indicate an invalid cursor. */
-    [self checkColumnIndices:((iCol >= 0) ? iCol : 0) n:1];
-    [self checkRowIndex:((iRow >= 0) ? iRow : 0)];
-    self->_cursorColumn = iCol;
-    self->_cursorRow = iRow;
-    self->_cursorWidth = 1;
-    self->_cursorHeight = 1;
-}
+    [self checkForBigStuffOverwriteAtColumn:icol row:irow width:n height:1];
 
-- (void)markBigCursor:(int)iCol row:(int)iRow
-           cellsWide:(int)w cellsHigh:(int)h
-{
-    /* Allow negative indices to indicate an invalid cursor. */
-    [self checkColumnIndices:((iCol >= 0) ? iCol : 0) n:1];
-    [self checkRowIndex:((iRow >= 0) ? iRow : 0)];
-    if (w < 1 || h < 1) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"InvalidCursorDimensions"
-                              reason:@"markBigCursor called for PendingTermChanges"
-                              userInfo:nil];
-       @throw exc;
+    struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
+    int i = icol;
+
+    while (i < icol + n) {
+#ifdef JP
+       if (iskanji(*g)) {
+           if (i == n - 1) {
+               /*
+                * The second byte of the character is past the end.  Ignore
+                * the character.
+                */
+               break;
+           }
+           cellsRow[i].v.ch.glyph = convert_two_byte_eucjp_to_utf16_native(g);
+           cellsRow[i].v.ch.attr = a;
+           cellsRow[i].hscl = 2;
+           cellsRow[i].vscl = 1;
+           cellsRow[i].hoff_n = 0;
+           cellsRow[i].voff_n = 0;
+           cellsRow[i].hoff_d = 2;
+           cellsRow[i].voff_d = 1;
+           cellsRow[i].form = TERM_CELL_CHAR;
+           ++i;
+           cellsRow[i].v.pd.hoff = 1;
+           cellsRow[i].v.pd.voff = 0;
+           cellsRow[i].hscl = 2;
+           cellsRow[i].vscl = 1;
+           cellsRow[i].hoff_n = 1;
+           cellsRow[i].voff_n = 0;
+           cellsRow[i].hoff_d = 2;
+           cellsRow[i].voff_d = 1;
+           cellsRow[i].form = TERM_CELL_CHAR_PADDING;
+           ++i;
+           g += 2;
+       } else {
+           cellsRow[i].v.ch.glyph = *g++;
+           cellsRow[i].v.ch.attr = a;
+           cellsRow[i].hscl = 1;
+           cellsRow[i].vscl = 1;
+           cellsRow[i].hoff_n = 0;
+           cellsRow[i].voff_n = 0;
+           cellsRow[i].hoff_d = 1;
+           cellsRow[i].voff_d = 1;
+           cellsRow[i].form = TERM_CELL_CHAR;
+           ++i;
+       }
+#else
+       cellsRow[i].v.ch.glyph = *g++;
+       cellsRow[i].v.ch.attr = a;
+       cellsRow[i].hscl = 1;
+       cellsRow[i].vscl = 1;
+       cellsRow[i].hoff_n = 0;
+       cellsRow[i].voff_n = 0;
+       cellsRow[i].hoff_d = 1;
+       cellsRow[i].voff_d = 1;
+       cellsRow[i].form = TERM_CELL_CHAR;
+       ++i;
+#endif /* JP */
     }
-    self->_cursorColumn = iCol;
-    self->_cursorRow = iRow;
-    self->_cursorWidth = w;
-    self->_cursorHeight = h;
 }
 
-- (void)setupForChange:(int)iCol row:(int)iRow n:(int)nCol
-{
-    [self checkColumnIndices:iCol n:nCol];
-    [self checkRowIndex:iRow];
-    if (self->changesByRow[iRow] == 0) {
-       self->changesByRow[iRow] =
-           malloc(self.columnCount * sizeof(struct PendingCellChange));
-       if (self->changesByRow[iRow] == 0 && self.columnCount > 0) {
-           NSException *exc = [NSException
-                                  exceptionWithName:@"OutOfMemory"
-                                  reason:@"setupForChange called for PendingTermChanges"
-                                  userInfo:nil];
-           @throw exc;
-       }
-       struct PendingCellChange* pcc = self->changesByRow[iRow];
-       for (int i = 0; i < self.columnCount; ++i) {
-           pcc[i].changeType = CELL_CHANGE_NONE;
+- (void)setTileAtColumn:(int)icol
+                   row:(int)irow
+       foregroundColumn:(char)fgdCol
+         foregroundRow:(char)fgdRow
+       backgroundColumn:(char)bckCol
+         backgroundRow:(char)bckRow
+             tileWidth:(int)w
+            tileHeight:(int)h
+{
+    [self checkForBigStuffOverwriteAtColumn:icol row:irow width:w height:h];
+
+    struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
+
+    cellsRow[icol].v.ti.fgdCol = fgdCol;
+    cellsRow[icol].v.ti.fgdRow = fgdRow;
+    cellsRow[icol].v.ti.bckCol = bckCol;
+    cellsRow[icol].v.ti.bckRow = bckRow;
+    cellsRow[icol].hscl = w;
+    cellsRow[icol].vscl = h;
+    cellsRow[icol].hoff_n = 0;
+    cellsRow[icol].voff_n = 0;
+    cellsRow[icol].hoff_d = w;
+    cellsRow[icol].voff_d = h;
+    cellsRow[icol].form = TERM_CELL_TILE;
+
+    int ic;
+    for (ic = icol + 1; ic < icol + w; ++ic) {
+       cellsRow[ic].v.pd.hoff = ic - icol;
+       cellsRow[ic].v.pd.voff = 0;
+       cellsRow[ic].hscl = w;
+       cellsRow[ic].vscl = h;
+       cellsRow[ic].hoff_n = ic - icol;
+       cellsRow[ic].voff_n = 0;
+       cellsRow[ic].hoff_d = w;
+       cellsRow[ic].voff_d = h;
+       cellsRow[ic].form = TERM_CELL_TILE_PADDING;
+    }
+    cellsRow += self.columnCount;
+    for (int ir = irow + 1; ir < irow + h; ++ir) {
+       for (ic = icol; ic < icol + w; ++ic) {
+           cellsRow[ic].v.pd.hoff = ic - icol;
+           cellsRow[ic].v.pd.voff = ir - irow;
+           cellsRow[ic].hscl = w;
+           cellsRow[ic].vscl = h;
+           cellsRow[ic].hoff_n = ic - icol;
+           cellsRow[ic].voff_n = ir - irow;
+           cellsRow[ic].hoff_d = w;
+           cellsRow[ic].voff_d = h;
+           cellsRow[ic].form = TERM_CELL_TILE_PADDING;
        }
-    }
-    if (self.firstChangedRow > iRow) {
-       self->_firstChangedRow = iRow;
-    }
-    if (self.lastChangedRow < iRow) {
-       self->_lastChangedRow = iRow;
-    }
-    if ([self getFirstChangedColumnInRow:iRow] > iCol) {
-       self->colBounds[iRow + iRow] = iCol;
-    }
-    if ([self getLastChangedColumnInRow:iRow] < iCol + nCol - 1) {
-       self->colBounds[iRow + iRow + 1] = iCol + nCol - 1;
+       cellsRow += self.columnCount;
     }
 }
 
-- (int)getFirstChangedColumnInRow:(int)iRow
+- (void)wipeBlockAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h
 {
-    [self checkRowIndex:iRow];
-    return self->colBounds[iRow + iRow];
+    [self checkForBigStuffOverwriteAtColumn:icol row:irow width:w height:h];
+    [self wipeBlockAuxAtColumn:icol row:irow width:w height:h];
 }
 
-- (int)getLastChangedColumnInRow:(int)iRow
+- (void)wipe
 {
-    [self checkRowIndex:iRow];
-    return self->colBounds[iRow + iRow + 1];
-}
+    wchar_t blank = [TerminalContents getBlankChar];
+    int blank_attr = [TerminalContents getBlankAttribute];
+    struct TerminalCell *cellCursor = self->cells +
+       self.columnCount * self.rowCount;
 
-- (enum PendingCellChangeType)getCellChangeType:(int)iCol row:(int)iRow
-{
-    [self checkColumnIndices:iCol n:1];
-    [self checkRowIndex:iRow];
-    if (iRow < self.firstChangedRow || iRow > self.lastChangedRow) {
-       return CELL_CHANGE_NONE;
+    while (cellCursor != self->cells) {
+       --cellCursor;
+       cellCursor->v.ch.glyph = blank;
+       cellCursor->v.ch.attr = blank_attr;
+       cellCursor->hscl = 1;
+       cellCursor->vscl = 1;
+       cellCursor->hoff_n = 0;
+       cellCursor->voff_n = 0;
+       cellCursor->hoff_d = 1;
+       cellCursor->voff_d = 1;
+       cellCursor->form = TERM_CELL_CHAR;
     }
-    if (iCol < [self getFirstChangedColumnInRow:iRow] ||
-       iCol > [self getLastChangedColumnInRow:iRow]) {
-       return CELL_CHANGE_NONE;
-    }
-    return self->changesByRow[iRow][iCol].changeType;
 }
 
-- (struct PendingTextChange)getCellTextChange:(int)iCol row:(int)iRow
+- (void)wipeTiles
 {
-    [self checkColumnIndices:iCol n:1];
-    [self checkRowIndex:iRow];
-    if (iRow < self.firstChangedRow || iRow > self.lastChangedRow ||
-       iCol < [self getFirstChangedColumnInRow:iRow] ||
-       iCol > [self getLastChangedColumnInRow:iRow] ||
-       (self->changesByRow[iRow][iCol].changeType != CELL_CHANGE_TEXT &&
-        self->changesByRow[iRow][iCol].changeType != CELL_CHANGE_WIPE)) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"NotTextChange"
-                              reason:@"getCellTextChange called for PendingTermChanges"
-                              userInfo:nil];
-       @throw exc;
-    }
-    return self->changesByRow[iRow][iCol].v.txc;
-}
+    wchar_t blank = [TerminalContents getBlankChar];
+    int blank_attr = [TerminalContents getBlankAttribute];
+    struct TerminalCell *cellCursor = self->cells +
+       self.columnCount * self.rowCount;
 
-- (struct PendingTileChange)getCellTileChange:(int)iCol row:(int)iRow
-{
-    [self checkColumnIndices:iCol n:1];
-    [self checkRowIndex:iRow];
-    if (iRow < self.firstChangedRow || iRow > self.lastChangedRow ||
-       iCol < [self getFirstChangedColumnInRow:iRow] ||
-       iCol > [self getLastChangedColumnInRow:iRow] ||
-       self->changesByRow[iRow][iCol].changeType != CELL_CHANGE_TILE) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"NotTileChange"
-                              reason:@"getCellTileChange called for PendingTermChanges"
-                              userInfo:nil];
-       @throw exc;
+    while (cellCursor != self->cells) {
+       --cellCursor;
+       if ((cellCursor->form &
+            (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+           cellCursor->v.ch.glyph = blank;
+           cellCursor->v.ch.attr = blank_attr;
+           cellCursor->hscl = 1;
+           cellCursor->vscl = 1;
+           cellCursor->hoff_n = 0;
+           cellCursor->voff_n = 0;
+           cellCursor->hoff_d = 1;
+           cellCursor->voff_d = 1;
+           cellCursor->form = TERM_CELL_CHAR;
+       }
     }
-    return self->changesByRow[iRow][iCol].v.tic;
 }
 
-- (void)checkColumnIndices:(int)iCol n:(int)nCol
+- (void)wipeBlockAuxAtColumn:(int)icol row:(int)irow width:(int)w
+                     height:(int)h
 {
-    if (iCol < 0) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"InvalidColumnIndex"
-                              reason:@"negative column index"
-                              userInfo:nil];
-       @throw exc;
-    }
-    if (iCol >= self.columnCount || iCol + nCol > self.columnCount) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"InvalidColumnIndex"
-                              reason:@"column index exceeds number of columns"
-                              userInfo:nil];
-       @throw exc;
-    }
-    if (nCol <= 0) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"InvalidColumnIndex"
-                              reason:@"empty column range"
-                              userInfo:nil];
-       @throw exc;
+    struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
+    wchar_t blank = [TerminalContents getBlankChar];
+    int blank_attr = [TerminalContents getBlankAttribute];
+
+    for (int ir = irow; ir < irow + h; ++ir) {
+       for (int ic = icol; ic < icol + w; ++ic) {
+           cellsRow[ic].v.ch.glyph = blank;
+           cellsRow[ic].v.ch.attr = blank_attr;
+           cellsRow[ic].hscl = 1;
+           cellsRow[ic].vscl = 1;
+           cellsRow[ic].hoff_n = 0;
+           cellsRow[ic].voff_n = 0;
+           cellsRow[ic].hoff_d = 1;
+           cellsRow[ic].voff_d = 1;
+           cellsRow[ic].form = TERM_CELL_CHAR;
+       }
+       cellsRow += self.columnCount;
     }
 }
 
-- (void)checkRowIndex:(int)iRow
+- (void) splitBlockAtColumn:(int)icol row:(int)irow n:(int)nsub
+                    blocks:(const struct TerminalCellBlock*)b
 {
-    if (iRow < 0) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"InvalidRowIndex"
-                              reason:@"negative row index"
-                              userInfo:nil];
-       @throw exc;
-    }
-    if (iRow >= self.rowCount) {
-       NSException *exc = [NSException
-                              exceptionWithName:@"InvalidRowIndex"
-                              reason:@"row index exceeds number of rows"
-                              userInfo:nil];
-       @throw exc;
-    }
-}
+    const struct TerminalCell *pulold = [self getCellAtColumn:icol row:irow];
 
-@end
+    for (int isub = 0; isub < nsub; ++isub) {
+       struct TerminalCell* cellsRow =
+           self->cells + b[isub].ulrow * self.columnCount;
 
+       /*
+        * Copy the data from the upper left corner of the big block to
+        * the upper left corner of the piece.
+        */
+       if (b[isub].ulcol != icol || b[isub].ulrow != irow) {
+           if (pulold->form == TERM_CELL_CHAR) {
+               cellsRow[b[isub].ulcol].v.ch = pulold->v.ch;
+               cellsRow[b[isub].ulcol].form = TERM_CELL_CHAR;
+           } else {
+               cellsRow[b[isub].ulcol].v.ti = pulold->v.ti;
+               cellsRow[b[isub].ulcol].form = TERM_CELL_TILE;
+           }
+       }
+       cellsRow[b[isub].ulcol].hscl = b[isub].w;
+       cellsRow[b[isub].ulcol].vscl = b[isub].h;
 
-/* The max number of glyphs we support.  Currently this only affects
- * updateGlyphInfo() for the calculation of the tile size, fontAscender,
- * fontDescender, nColPre, and nColPost.  The rendering in drawWChar() will
- * work for a glyph not in updateGlyphInfo()'s set, and that is used for
- * rendering Japanese characters, though there may be clipping or clearing
- * artifacts because it wasn't included in updateGlyphInfo()'s calculations.
- */
-#define GLYPH_COUNT 256
+       /*
+        * Point the padding elements in the piece to the new upper left
+        * corner.
+        */
+       int ic;
+       for (ic = b[isub].ulcol + 1; ic < b[isub].ulcol + b[isub].w; ++ic) {
+           cellsRow[ic].v.pd.hoff = ic - b[isub].ulcol;
+           cellsRow[ic].v.pd.voff = 0;
+           cellsRow[ic].hscl = b[isub].w;
+           cellsRow[ic].vscl = b[isub].h;
+       }
+       cellsRow += self.columnCount;
+       for (int ir = b[isub].ulrow + 1;
+            ir < b[isub].ulrow + b[isub].h;
+            ++ir) {
+           for (ic = b[isub].ulcol; ic < b[isub].ulcol + b[isub].w; ++ic) {
+               cellsRow[ic].v.pd.hoff = ic - b[isub].ulcol;
+               cellsRow[ic].v.pd.voff = ir - b[isub].ulrow;
+               cellsRow[ic].hscl = b[isub].w;
+               cellsRow[ic].vscl = b[isub].h;
+           }
+           cellsRow += self.columnCount;
+       }
+    }
+}
 
-/* An AngbandContext represents a logical Term (i.e. what Angband thinks is
- * a window). This typically maps to one NSView, but may map to more than one
- * NSView (e.g. the Test and real screen saver view). */
-@interface AngbandContext : NSObject <NSWindowDelegate>
+- (void)checkForBigStuffOverwriteAtColumn:(int)icol row:(int)irow
+                                   width:(int)w height:(int)h
 {
-@public
+    int ire = irow + h, ice = icol + w;
 
-    /* The Angband term */
-    term *terminal;
+    for (int ir = irow; ir < ire; ++ir) {
+       for (int ic = icol; ic < ice; ++ic) {
+           const struct TerminalCell *pcell =
+               [self getCellAtColumn:ic row:ir];
 
-@private
-    /* Is the last time we drew, so we can throttle drawing. */
-    CFAbsoluteTime lastRefreshTime;
+           if ((pcell->form & (TERM_CELL_CHAR | TERM_CELL_TILE)) != 0 &&
+               (pcell->hscl > 1 || pcell->vscl > 1)) {
+               /*
+                * Lost chunk including upper left corner.  Split into at most
+                * two new blocks.
+                */
+               /*
+                * Tolerate blocks that were clipped by a resize at some point.
+                */
+               int wb = (ic + pcell->hscl <= self.columnCount) ?
+                   pcell->hscl : self.columnCount - ic;
+               int hb = (ir + pcell->vscl <= self.rowCount) ?
+                   pcell->vscl : self.rowCount - ir;
+               struct TerminalCellBlock blocks[2];
+               int nsub = 0, ww, hw;
+
+               if (ice < ic + wb) {
+                   /* Have something to the right not overwritten. */
+                   blocks[nsub].ulcol = ice;
+                   blocks[nsub].ulrow = ir;
+                   blocks[nsub].w = ic + wb - ice;
+                   blocks[nsub].h = (ire < ir + hb) ? ire - ir : hb;
+                   ++nsub;
+                   ww = ice - ic;
+               } else {
+                   ww = wb;
+               }
+               if (ire < ir + hb) {
+                   /* Have something below not overwritten. */
+                   blocks[nsub].ulcol = ic;
+                   blocks[nsub].ulrow = ire;
+                   blocks[nsub].w = wb;
+                   blocks[nsub].h = ir + hb - ire;
+                   ++nsub;
+                   hw = ire - ir;
+               } else {
+                   hw = hb;
+               }
+               if (nsub > 0) {
+                   [self splitBlockAtColumn:ic row:ir n:nsub blocks:blocks];
+               }
+               /*
+                * Wipe the part of the block that's destined to be overwritten
+                * so it doesn't receive further consideration in this loop.
+                * For efficiency, would like to have the loop skip over it or
+                * fill it with the desired content, but this is easier to
+                * implement.
+                */
+               [self wipeBlockAuxAtColumn:ic row:ir width:ww height:hw];
+           } else if ((pcell->form & (TERM_CELL_CHAR_PADDING |
+                                      TERM_CELL_TILE_PADDING)) != 0) {
+               /*
+                * Lost a chunk that doesn't cover the upper left corner.  In
+                * general will split into up to four new blocks (one above,
+                * one to the left, one to the right, and one below).
+                */
+               int pcol = ic - pcell->v.pd.hoff;
+               int prow = ir - pcell->v.pd.voff;
+               const struct TerminalCell *pcell2 =
+                   [self getCellAtColumn:pcol row:prow];
 
-    /*
-     * Whether we are currently in live resize, which affects how big we
-     * render our image.
-     */
-    int inLiveResize;
+               /*
+                * Tolerate blocks that were clipped by a resize at some point.
+                */
+               int wb = (pcol + pcell2->hscl <= self.columnCount) ?
+                   pcell2->hscl : self.columnCount - pcol;
+               int hb = (prow + pcell2->vscl <= self.rowCount) ?
+                   pcell2->vscl : self.rowCount - prow;
+               struct TerminalCellBlock blocks[4];
+               int nsub = 0, ww, hw;
+
+               if (prow < ir) {
+                   /* Have something above not overwritten. */
+                   blocks[nsub].ulcol = pcol;
+                   blocks[nsub].ulrow = prow;
+                   blocks[nsub].w = wb;
+                   blocks[nsub].h = ir - prow;
+                   ++nsub;
+               }
+               if (pcol < ic) {
+                   /* Have something to the left not overwritten. */
+                   blocks[nsub].ulcol = pcol;
+                   blocks[nsub].ulrow = ir;
+                   blocks[nsub].w = ic - pcol;
+                   blocks[nsub].h =
+                       (ire < prow + hb) ? ire - ir : prow + hb - ir;
+                   ++nsub;
+               }
+               if (ice < pcol + wb) {
+                   /* Have something to the right not overwritten. */
+                   blocks[nsub].ulcol = ice;
+                   blocks[nsub].ulrow = ir;
+                   blocks[nsub].w = pcol + wb - ice;
+                   blocks[nsub].h =
+                       (ire < prow + hb) ? ire - ir : prow + hb - ir;
+                   ++nsub;
+                   ww = ice - ic;
+               } else {
+                   ww = pcol + wb - ic;
+               }
+               if (ire < prow + hb) {
+                   /* Have something below not overwritten. */
+                   blocks[nsub].ulcol = pcol;
+                   blocks[nsub].ulrow = ire;
+                   blocks[nsub].w = wb;
+                   blocks[nsub].h = prow + hb - ire;
+                   ++nsub;
+                   hw = ire - ir;
+               } else {
+                   hw = prow + hb - ir;
+               }
 
-    /* Flags whether or not a fullscreen transition is in progress. */
-    BOOL inFullscreenTransition;
+               [self splitBlockAtColumn:pcol row:prow n:nsub blocks:blocks];
+               /* Same rationale for wiping as above. */
+               [self wipeBlockAuxAtColumn:ic row:ir width:ww height:hw];
+           }
+       }
+    }
 }
 
-/* Column and row counts, by default 80 x 24 */
-@property int cols;
-@property int rows;
+- (void)setCursorAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h
+{
+    self->_cursorColumn = icol;
+    self->_cursorRow = irow;
+    self->_cursorWidth = w;
+    self->_cursorHeight = h;
+}
 
-/* The size of the border between the window edge and the contents */
-@property (readonly) NSSize borderSize;
+- (void)removeCursor
+{
+    self->_cursorColumn = -1;
+    self->_cursorHeight = -1;
+    self->_cursorWidth = 1;
+    self->_cursorHeight = 1;
+}
 
-/* Our array of views */
-@property NSMutableArray *angbandViews;
+- (void)assertInvariants
+{
+    const struct TerminalCell *cellsRow = self->cells;
 
-/* The buffered image */
-@property CGLayerRef angbandLayer;
+    /*
+     * The comments with the definition for TerminalCell define the
+     * relationships of hoff_n, voff_n, hoff_d, voff_d, hscl, and vscl
+     * asserted here.
+     */
+    for (int ir = 0; ir < self.rowCount; ++ir) {
+       for (int ic = 0; ic < self.columnCount; ++ic) {
+           switch (cellsRow[ic].form) {
+           case TERM_CELL_CHAR:
+               assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
+               assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
+                      cellsRow[ic].voff_n < cellsRow[ic].voff_d);
+               if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
+                   assert(cellsRow[ic].hoff_n == 0);
+               }
+               if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
+                   assert(cellsRow[ic].voff_n == 0);
+               }
+               /*
+                * Verify that the padding elements have the correct tag
+                * and point back to this cell.
+                */
+               if (cellsRow[ic].hscl > 1 || cellsRow[ic].vscl > 1) {
+                   const struct TerminalCell *cellsRow2 = cellsRow;
+
+                   for (int ir2 = ir; ir2 < ir + cellsRow[ic].vscl; ++ir2) {
+                       for (int ic2 = ic;
+                            ic2 < ic + cellsRow[ic].hscl;
+                            ++ic2) {
+                           if (ir2 == ir && ic2 == ic) {
+                               continue;
+                           }
+                           assert(cellsRow2[ic2].form ==
+                                  TERM_CELL_CHAR_PADDING);
+                           assert(ic2 - cellsRow2[ic2].v.pd.hoff == ic &&
+                                  ir2 - cellsRow2[ic2].v.pd.voff == ir);
+                       }
+                       cellsRow2 += self.columnCount;
+                   }
+               }
+               break;
+
+           case TERM_CELL_TILE:
+               assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
+               assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
+                      cellsRow[ic].voff_n < cellsRow[ic].voff_d);
+               if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
+                   assert(cellsRow[ic].hoff_n == 0);
+               }
+               if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
+                   assert(cellsRow[ic].voff_n == 0);
+               }
+               /*
+                * Verify that the padding elements have the correct tag
+                * and point back to this cell.
+                */
+               if (cellsRow[ic].hscl > 1 || cellsRow[ic].vscl > 1) {
+                   const struct TerminalCell *cellsRow2 = cellsRow;
+
+                   for (int ir2 = ir; ir2 < ir + cellsRow[ic].vscl; ++ir2) {
+                       for (int ic2 = ic;
+                            ic2 < ic + cellsRow[ic].hscl;
+                            ++ic2) {
+                           if (ir2 == ir && ic2 == ic) {
+                               continue;
+                           }
+                           assert(cellsRow2[ic2].form ==
+                                  TERM_CELL_TILE_PADDING);
+                           assert(ic2 - cellsRow2[ic2].v.pd.hoff == ic &&
+                                  ir2 - cellsRow2[ic2].v.pd.voff == ir);
+                       }
+                       cellsRow2 += self.columnCount;
+                   }
+               }
+               break;
+
+           case TERM_CELL_CHAR_PADDING:
+               assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
+               assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
+                      cellsRow[ic].voff_n < cellsRow[ic].voff_d);
+               assert(cellsRow[ic].hoff_n > 0 || cellsRow[ic].voff_n > 0);
+               if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
+                   assert(cellsRow[ic].hoff_n == cellsRow[ic].v.pd.hoff);
+               }
+               if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
+                   assert(cellsRow[ic].voff_n == cellsRow[ic].v.pd.voff);
+               }
+               assert(ic >= cellsRow[ic].v.pd.hoff &&
+                      ir >= cellsRow[ic].v.pd.voff);
+               /*
+                * Verify that it's padding for something that can point
+                * back to it.
+                */
+               {
+                   const struct TerminalCell *parent =
+                       [self getCellAtColumn:(ic - cellsRow[ic].v.pd.hoff)
+                             row:(ir - cellsRow[ic].v.pd.voff)];
+
+                   assert(parent->form == TERM_CELL_CHAR);
+                   assert(parent->hscl > cellsRow[ic].v.pd.hoff &&
+                          parent->vscl > cellsRow[ic].v.pd.voff);
+                   assert(parent->hscl == cellsRow[ic].hscl &&
+                          parent->vscl == cellsRow[ic].vscl);
+                   assert(parent->hoff_d == cellsRow[ic].hoff_d &&
+                          parent->voff_d == cellsRow[ic].voff_d);
+               }
+               break;
+
+           case TERM_CELL_TILE_PADDING:
+               assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
+               assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
+                      cellsRow[ic].voff_n < cellsRow[ic].voff_d);
+               assert(cellsRow[ic].hoff_n > 0 || cellsRow[ic].voff_n > 0);
+               if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
+                   assert(cellsRow[ic].hoff_n == cellsRow[ic].v.pd.hoff);
+               }
+               if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
+                   assert(cellsRow[ic].voff_n == cellsRow[ic].v.pd.voff);
+               }
+               assert(ic >= cellsRow[ic].v.pd.hoff &&
+                      ir >= cellsRow[ic].v.pd.voff);
+               /*
+                * Verify that it's padding for something that can point
+                * back to it.
+                */
+               {
+                   const struct TerminalCell *parent =
+                       [self getCellAtColumn:(ic - cellsRow[ic].v.pd.hoff)
+                             row:(ir - cellsRow[ic].v.pd.voff)];
+
+                   assert(parent->form == TERM_CELL_TILE);
+                   assert(parent->hscl > cellsRow[ic].v.pd.hoff &&
+                          parent->vscl > cellsRow[ic].v.pd.voff);
+                   assert(parent->hscl == cellsRow[ic].hscl &&
+                          parent->vscl == cellsRow[ic].vscl);
+                   assert(parent->hoff_d == cellsRow[ic].hoff_d &&
+                          parent->voff_d == cellsRow[ic].voff_d);
+               }
+               break;
+
+           default:
+               assert(0);
+           }
+       }
+       cellsRow += self.columnCount;
+    }
+}
+
++ (wchar_t)getBlankChar
+{
+    return L' ';
+}
+
++ (int)getBlankAttribute
+{
+    return 0;
+}
+
+@end
+
+/**
+ * TerminalChanges is used to track changes made via the text_hook, pict_hook,
+ * wipe_hook, curs_hook, and bigcurs_hook callbacks on the terminal since the
+ * last call to xtra_hook for TERM_XTRA_FRESH.  The locations marked as changed
+ * can then be used to make bounding rectangles for the regions that need to
+ * be redisplayed.
+ */
+@interface TerminalChanges : NSObject {
+    int* colBounds;
+    /*
+     * Outside of firstChangedRow, lastChangedRow and what's in colBounds, the
+     * contents of this are handled lazily.
+     */
+    BOOL* marks;
+}
+
+/**
+ * Initialize with zero columns and zero rows.
+ */
+- (id)init;
+
+/**
+ * Initialize with nCol columns and nRow rows.  No changes will be marked.
+ */
+- (id)initWithColumns:(int)nCol rows:(int)nRow NS_DESIGNATED_INITIALIZER;
+
+/**
+ * Resize to be nCol by nRow.  Current contents still within the new bounds
+ * are preserved.  Added areas are marked as unchanged.
+ */
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow;
+
+/**
+ * Clears all marked changes.
+ */
+- (void)clear;
+
+- (BOOL)isChangedAtColumn:(int)icol row:(int)irow;
+
+/**
+ * Scans the row, irow, starting at the column, icol0, and stopping before the
+ * column, icol1.  Returns the column index for the first cell that is
+ * changed.  The returned index will be equal to icol1 if all of the cells in
+ * the range are unchanged.
+ */
+- (int)scanForChangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1;
+
+/**
+ * Scans the row, irow, starting at the column, icol0, and stopping before the
+ * column, icol1.  returns the column index for the first cell that has not
+ * changed.  The returned index will be equal to icol1 if all of the cells in
+ * the range have changed.
+ */
+- (int)scanForUnchangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1;
+
+- (void)markChangedAtColumn:(int)icol row:(int)irow;
+
+- (void)markChangedRangeAtColumn:(int)icol row:(int)irow width:(int)w;
+
+/**
+ * Marks the block as changed who's upper left hand corner is at (icol, irow).
+ */
+- (void)markChangedBlockAtColumn:(int)icol
+                            row:(int)irow
+                          width:(int)w
+                         height:(int)h;
+
+/**
+ * Returns the index of the first changed column in the given row.  That index
+ * will be equal to the number of columns if there are no changes in the row.
+ */
+- (int)getFirstChangedColumnInRow:(int)irow;
+
+/**
+ * Returns the index of the last changed column in the given row.  That index
+ * will be equal to -1 if there are no changes in the row.
+ */
+- (int)getLastChangedColumnInRow:(int)irow;
+
+/**
+ * Is the number of columns.
+ */
+@property (readonly) int columnCount;
+
+/**
+ * Is the number of rows.
+ */
+@property (readonly) int rowCount;
+
+/**
+ * Is the index of the first row with changes.  Will be equal to the number
+ * of rows if there are no changes.
+ */
+@property (readonly) int firstChangedRow;
+
+/**
+ * Is the index of the last row with changes.  Will be equal to -1 if there
+ * are no changes.
+ */
+@property (readonly) int lastChangedRow;
+
+@end
+
+@implementation TerminalChanges
+
+- (id)init
+{
+    return [self initWithColumns:0 rows:0];
+}
+
+- (id)initWithColumns:(int)nCol rows:(int)nRow
+{
+    if (self = [super init]) {
+       self->colBounds = malloc(2 * nRow * sizeof(int));
+       self->marks = malloc(nCol * nRow * sizeof(BOOL));
+       self->_columnCount = nCol;
+       self->_rowCount = nRow;
+       [self clear];
+    }
+    return self;
+}
+
+- (void)dealloc
+{
+    if (self->marks != 0) {
+       free(self->marks);
+       self->marks = 0;
+    }
+    if (self->colBounds != 0) {
+       free(self->colBounds);
+       self->colBounds = 0;
+    }
+}
+
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow
+{
+    int* newColBounds = malloc(2 * nRow * sizeof(int));
+    BOOL* newMarks = malloc(nCol * nRow * sizeof(BOOL));
+    int nRowCommon = (nRow < self.rowCount) ? nRow : self.rowCount;
+
+    if (self.firstChangedRow <= self.lastChangedRow &&
+       self.firstChangedRow < nRowCommon) {
+       BOOL* marksOutCursor = newMarks + self.firstChangedRow * nCol;
+       const BOOL* marksInCursor =
+           self->marks + self.firstChangedRow * self.columnCount;
+       int nColCommon = (nCol < self.columnCount) ? nCol : self.columnCount;
+
+       if (self.lastChangedRow >= nRowCommon) {
+           self->_lastChangedRow = nRowCommon - 1;
+       }
+       for (int i = self.firstChangedRow; i <= self.lastChangedRow; ++i) {
+           if (self->colBounds[i + i] < nColCommon) {
+               newColBounds[i + i] = self->colBounds[i + i];
+               newColBounds[i + i + 1] =
+                   (self->colBounds[i + i + 1] < nColCommon) ?
+                   self->colBounds[i + i + 1] : nColCommon - 1;
+               (void) memcpy(
+                   marksOutCursor + self->colBounds[i + i],
+                   marksInCursor + self->colBounds[i + i],
+                   (newColBounds[i + i + 1] - newColBounds[i + i] + 1) *
+                       sizeof(BOOL));
+               marksInCursor += self.columnCount;
+               marksOutCursor += nCol;
+           } else {
+               self->colBounds[i + i] = nCol;
+               self->colBounds[i + i + 1] = -1;
+           }
+       }
+    } else {
+       self->_firstChangedRow = nRow;
+       self->_lastChangedRow = -1;
+    }
+
+    free(self->colBounds);
+    self->colBounds = newColBounds;
+    free(self->marks);
+    self->marks = newMarks;
+    self->_columnCount = nCol;
+    self->_rowCount = nRow;
+}
+
+- (void)clear
+{
+    self->_firstChangedRow = self.rowCount;
+    self->_lastChangedRow = -1;
+}
+
+- (BOOL)isChangedAtColumn:(int)icol row:(int)irow
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
+       return NO;
+    }
+    if (icol < self->colBounds[irow + irow] ||
+       icol > self->colBounds[irow + irow + 1]) {
+       return NO;
+    }
+    return self->marks[icol + irow * self.columnCount];
+}
+
+- (int)scanForChangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow ||
+       icol0 > self->colBounds[irow + irow + 1]) {
+       return icol1;
+    }
+
+    int i = (icol0 > self->colBounds[irow + irow]) ?
+       icol0 : self->colBounds[irow + irow];
+    int i1 = (icol1 <= self->colBounds[irow + irow + 1]) ?
+       icol1 : self->colBounds[irow + irow + 1] + 1;
+    const BOOL* marksCursor = self->marks + irow * self.columnCount;
+    while (1) {
+       if (i >= i1) {
+           return icol1;
+       }
+       if (marksCursor[i]) {
+           return i;
+       }
+       ++i;
+    }
+}
+
+- (int)scanForUnchangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow ||
+       icol0 < self->colBounds[irow + irow] ||
+       icol0 > self->colBounds[irow + irow + 1]) {
+       return icol0;
+    }
+
+    int i = icol0;
+    int i1 = (icol1 <= self->colBounds[irow + irow + 1]) ?
+       icol1 : self->colBounds[irow + irow + 1] + 1;
+    const BOOL* marksCursor = self->marks + irow * self.columnCount;
+    while (1) {
+       if (i >= i1 || ! marksCursor[i]) {
+           return i;
+       }
+       ++i;
+    }
+}
+
+- (void)markChangedAtColumn:(int)icol row:(int)irow
+{
+    [self markChangedBlockAtColumn:icol row:irow width:1 height:1];
+}
+
+- (void)markChangedRangeAtColumn:(int)icol row:(int)irow width:(int)w
+{
+    [self markChangedBlockAtColumn:icol row:irow width:w height:1];
+}
+
+- (void)markChangedBlockAtColumn:(int)icol
+                            row:(int)irow
+                          width:(int)w
+                         height:(int)h
+{
+    if (irow + h <= self.firstChangedRow) {
+       /* All prior marked regions are on rows after the requested block. */
+       if (self.firstChangedRow > self.lastChangedRow) {
+           self->_lastChangedRow = irow + h - 1;
+       } else {
+           for (int i = irow + h; i < self.firstChangedRow; ++i) {
+               self->colBounds[i + i] = self.columnCount;
+               self->colBounds[i + i + 1] = -1;
+           }
+       }
+       self->_firstChangedRow = irow;
+
+       BOOL* marksCursor = self->marks + irow * self.columnCount;
+       for (int i = irow; i < irow + h; ++i) {
+           self->colBounds[i + i] = icol;
+           self->colBounds[i + i + 1] = icol + w - 1;
+           for (int j = icol; j < icol + w; ++j) {
+               marksCursor[j] = YES;
+           }
+           marksCursor += self.columnCount;
+       }
+    } else if (irow > self.lastChangedRow) {
+       /* All prior marked regions are on rows before the requested block. */
+       int i;
+
+       for (i = self.lastChangedRow + 1; i < irow; ++i) {
+           self->colBounds[i + i] = self.columnCount;
+           self->colBounds[i + i + 1] = -1;
+       }
+       self->_lastChangedRow = irow + h - 1;
+
+       BOOL* marksCursor = self->marks + irow * self.columnCount;
+       for (i = irow; i < irow + h; ++i) {
+           self->colBounds[i + i] = icol;
+           self->colBounds[i + i + 1] = icol + w - 1;
+           for (int j = icol; j < icol + w; ++j) {
+               marksCursor[j] = YES;
+           }
+           marksCursor += self.columnCount;
+       }
+    } else {
+       /*
+        * There's overlap between the rows of the requested block and prior
+        * marked regions.
+        */
+       BOOL* marksCursor = self->marks + irow * self.columnCount;
+       int irow0, h0;
+
+       if (irow < self.firstChangedRow) {
+           /* Handle any leading rows where there's no overlap. */
+           for (int i = irow; i < self.firstChangedRow; ++i) {
+               self->colBounds[i + i] = icol;
+               self->colBounds[i + i + 1] = icol + w - 1;
+               for (int j = icol; j < icol + w; ++j) {
+                   marksCursor[j] = YES;
+               }
+               marksCursor += self.columnCount;
+           }
+           irow0 = self.firstChangedRow;
+           h0 = irow + h - self.firstChangedRow;
+           self->_firstChangedRow = irow;
+       } else {
+           irow0 = irow;
+           h0 = h;
+       }
+
+       /* Handle potentially overlapping rows */
+       if (irow0 + h0 > self.lastChangedRow + 1) {
+           h0 = self.lastChangedRow + 1 - irow0;
+           self->_lastChangedRow = irow + h - 1;
+       }
+
+       int i;
+       for (i = irow0; i < irow0 + h0; ++i) {
+           if (icol + w <= self->colBounds[i + i]) {
+               int j;
+
+               for (j = icol; j < icol + w; ++j) {
+                   marksCursor[j] = YES;
+               }
+               if (self->colBounds[i + i] > self->colBounds[i + i + 1]) {
+                   self->colBounds[i + i + 1] = icol + w - 1;
+               } else {
+                   for (j = icol + w; j < self->colBounds[i + i]; ++j) {
+                       marksCursor[j] = NO;
+                   }
+               }
+               self->colBounds[i + i] = icol;
+           } else if (icol > self->colBounds[i + i + 1]) {
+               int j;
+
+               for (j = self->colBounds[i + i + 1] + 1; j < icol; ++j) {
+                   marksCursor[j] = NO;
+               }
+               for (j = icol; j < icol + w; ++j) {
+                   marksCursor[j] = YES;
+               }
+               self->colBounds[i + i + 1] = icol + w - 1;
+           } else {
+               if (icol < self->colBounds[i + i]) {
+                   self->colBounds[i + i] = icol;
+               }
+               if (icol + w > self->colBounds[i + i + 1]) {
+                   self->colBounds[i + i + 1] = icol + w - 1;
+               }
+               for (int j = icol; j < icol + w; ++j) {
+                   marksCursor[j] = YES;
+               }
+           }
+           marksCursor += self.columnCount;
+       }
+
+       /* Handle any trailing rows where there's no overlap. */
+       for (i = irow0 + h0; i < irow + h; ++i) {
+           self->colBounds[i + i] = icol;
+           self->colBounds[i + i + 1] = icol + w - 1;
+           for (int j = icol; j < icol + w; ++j) {
+               marksCursor[j] = YES;
+           }
+           marksCursor += self.columnCount;
+       }
+    }
+}
+
+- (int)getFirstChangedColumnInRow:(int)irow
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
+       return self.columnCount;
+    }
+    return self->colBounds[irow + irow];
+}
+
+- (int)getLastChangedColumnInRow:(int)irow
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
+       return -1;
+    }
+    return self->colBounds[irow + irow + 1];
+}
+
+@end
+
+
+/**
+ * Draws one tile as a helper function for AngbandContext's drawRect.
+ */
+static void draw_image_tile(
+    NSGraphicsContext* nsContext,
+    CGContextRef cgContext,
+    CGImageRef image,
+    NSRect srcRect,
+    NSRect dstRect,
+    NSCompositingOperation op)
+{
+    /* Flip the source rect since the source image is flipped */
+    CGAffineTransform flip = CGAffineTransformIdentity;
+    flip = CGAffineTransformTranslate(flip, 0.0, CGImageGetHeight(image));
+    flip = CGAffineTransformScale(flip, 1.0, -1.0);
+    CGRect flippedSourceRect =
+       CGRectApplyAffineTransform(NSRectToCGRect(srcRect), flip);
+
+    /*
+     * When we use high-quality resampling to draw a tile, pixels from outside
+     * the tile may bleed in, causing graphics artifacts. Work around that.
+     */
+    CGImageRef subimage =
+       CGImageCreateWithImageInRect(image, flippedSourceRect);
+    [nsContext setCompositingOperation:op];
+    CGContextDrawImage(cgContext, NSRectToCGRect(dstRect), subimage);
+    CGImageRelease(subimage);
+}
+
+
+/* The max number of glyphs we support.  Currently this only affects
+ * updateGlyphInfo() for the calculation of the tile size, fontAscender,
+ * fontDescender, nColPre, and nColPost.  The rendering in drawWChar() will
+ * work for a glyph not in updateGlyphInfo()'s set, and that is used for
+ * rendering Japanese characters, though there may be clipping or clearing
+ * artifacts because it wasn't included in updateGlyphInfo()'s calculations.
+ */
+#define GLYPH_COUNT 256
+
+/* An AngbandContext represents a logical Term (i.e. what Angband thinks is
+ * a window). */
+@interface AngbandContext : NSObject <NSWindowDelegate>
+{
+@public
+
+    /* The Angband term */
+    term *terminal;
+
+@private
+    /* Is the last time we drew, so we can throttle drawing. */
+    CFAbsoluteTime lastRefreshTime;
+
+    /* Flags whether or not a fullscreen transition is in progress. */
+    BOOL inFullscreenTransition;
+
+    /* Our view */
+    AngbandView *angbandView;
+}
+
+/* Column and row counts, by default 80 x 24 */
+@property (readonly) int cols;
+@property (readonly) int rows;
+
+/* The size of the border between the window edge and the contents */
+@property (readonly) NSSize borderSize;
 
 /* The font of this context */
 @property NSFont *angbandViewFont;
@@ -996,12 +1851,27 @@ struct PendingCellChange {
 /* If this context owns a window, here it is. */
 @property NSWindow *primaryWindow;
 
-/* Is the record of changes to the contents for the next update. */
-@property PendingTermChanges *changes;
+/* Holds our version of the contents of the terminal. */
+@property TerminalContents *contents;
+
+/*
+ * Marks which locations have been changed by the text_hook, pict_hook,
+ * wipe_hook, curs_hook, and bigcurs_hhok callbacks on the terminal since
+ * the last call to xtra_hook with TERM_XTRA_FRESH.
+ */
+@property TerminalChanges *changes;
 
 @property (nonatomic, assign) BOOL hasSubwindowFlags;
 @property (nonatomic, assign) BOOL windowVisibilityChecked;
 
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow;
+
+/**
+ * Based on what has been marked as changed, inform AppKit of the bounding
+ * rectangles for the changed areas.
+ */
+- (void)computeInvalidRects;
+
 - (void)drawRect:(NSRect)rect inView:(NSView *)view;
 
 /* Called at initialization to set the term */
@@ -1010,49 +1880,44 @@ struct PendingCellChange {
 /* Called when the context is going down. */
 - (void)dispose;
 
-/* Returns the size of the image. */
-- (NSSize)imageSize;
-
-/* Return the rect for a tile at given coordinates. */
-- (NSRect)rectInImageForTileAtX:(int)x Y:(int)y;
+/*
+ * Return the rect in view coordinates for the block of cells whose upper
+ * left corner is (x,y).
+ */
+- (NSRect)viewRectForCellBlockAtX:(int)x y:(int)y width:(int)w height:(int)h;
 
 /* Draw the given wide character into the given tile rect. */
-- (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx;
-
-/* Locks focus on the Angband image, and scales the CTM appropriately. */
-- (CGContextRef)lockFocus;
-
-/* Locks focus on the Angband image but does NOT scale the CTM. Appropriate
- * for drawing hairlines. */
-- (CGContextRef)lockFocusUnscaled;
-
-/* Unlocks focus. */
-- (void)unlockFocus;
+- (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile screenFont:(NSFont*)font
+         context:(CGContextRef)ctx;
 
 /* Returns the primary window for this angband context, creating it if
  * necessary */
 - (NSWindow *)makePrimaryWindow;
 
-/* Called to add a new Angband view */
-- (void)addAngbandView:(AngbandView *)view;
-
-/* Make the context aware that one of its views changed size */
-- (void)angbandViewDidScale:(AngbandView *)view;
-
 /* Handle becoming the main window */
 - (void)windowDidBecomeMain:(NSNotification *)notification;
 
 /* Return whether the context's primary window is ordered in or not */
 - (BOOL)isOrderedIn;
 
-/* Return whether the context's primary window is key */
+/*
+ * Return whether the context's primary window is the main window.
+ * Since the terminals other than terminal 0 are configured as panels in
+ * Hengband, this will only be true for terminal 0.
+ */
 - (BOOL)isMainWindow;
 
+/*
+ * Return whether the context's primary window is the destination for key
+ * input.
+ */
+- (BOOL)isKeyWindow;
+
 /* Invalidate the whole image */
 - (void)setNeedsDisplay:(BOOL)val;
 
-/* Invalidate part of the image, with the rect expressed in base coordinates */
-- (void)setNeedsDisplayInBaseRect:(NSRect)rect;
+/* Invalidate part of the image, with the rect expressed in view coordinates */
+- (void)setNeedsDisplayInRect:(NSRect)rect;
 
 /* Display (flush) our Angband views */
 - (void)displayIfNeeded;
@@ -1062,15 +1927,12 @@ struct PendingCellChange {
 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
 
 /*
- * Change the minimum size for the window associated with the context.
- * termIdx is the index for the terminal:  pass it so this function can be
- * used when self->terminal has not yet been set.
+ * Change the minimum size and size increments for the window associated with
+ * the context.  termIdx is the index for the terminal:  pass it so this
+ * function can be used when self->terminal has not yet been set.
  */
-- (void)setMinimumWindowSize:(int)termIdx;
+- (void)constrainWindowSize:(int)termIdx;
 
-/* Called from the view to indicate that it is starting or ending live resize */
-- (void)viewWillStartLiveResize:(AngbandView *)view;
-- (void)viewDidEndLiveResize:(AngbandView *)view;
 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible;
 - (BOOL)windowVisibleUsingDefaults;
 
@@ -1086,8 +1948,6 @@ struct PendingCellChange {
 + (void)setDefaultFont:(NSFont*)font;
 
 /* Internal methods */
-- (AngbandView *)activeView;
-
 /* Set the title for the primary window. */
 - (void)setDefaultTitle:(int)termIdx;
 
@@ -1189,7 +2049,6 @@ static void AngbandUpdateWindowVisibility(void)
     [mainWindow.primaryWindow makeKeyAndOrderFront: nil];
 }
 
-
 /**
  * ------------------------------------------------------------------------
  * Graphics support
@@ -1224,12 +2083,26 @@ static BOOL graphics_are_enabled(void)
 }
 
 /**
+ * Like graphics_are_enabled(), but test the requested graphics mode.
+ */
+static BOOL graphics_will_be_enabled(void)
+{
+    if (graf_mode_req == GRAPHICS_NONE) {
+       return NO;
+    }
+
+    graphics_mode *new_mode = get_graphics_mode(graf_mode_req);
+    return new_mode && new_mode->grafID != GRAPHICS_NONE;
+}
+
+/**
  * Hack -- game in progress
  */
 static Boolean game_in_progress = FALSE;
 
 
 #pragma mark Prototypes
+static BOOL redraw_for_tiles_or_term0_font(void);
 static void wakeup_event_loop(void);
 static void hook_plog(const char *str);
 static void hook_quit(const char * str);
@@ -1237,14 +2110,14 @@ static NSString* get_lib_directory(void);
 static NSString* get_doc_directory(void);
 static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath);
 static void prepare_paths_and_directories(void);
+static void load_prefs(void);
+static void init_windows(void);
 static void handle_open_when_ready(void);
 static void play_sound(int event);
 static BOOL check_events(int wait);
 static BOOL send_event(NSEvent *event);
+static void set_color_for_index(int idx);
 static void record_current_savefile(void);
-#ifdef JP
-static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp);
-#endif
 
 /**
  * Available values for 'wait'
@@ -1278,13 +2151,13 @@ static bool initialized = FALSE;
 @end
 
 /* The NSView subclass that draws our Angband image */
-@interface AngbandView : NSView
-{
-    AngbandContext *angbandContext;
+@interface AngbandView : NSView {
+@private
+    NSBitmapImageRep *cacheForResize;
+    NSRect cacheBounds;
 }
 
-- (void)setAngbandContext:(AngbandContext *)context;
-- (AngbandContext *)angbandContext;
+@property (nonatomic, weak) AngbandContext *angbandContext;
 
 @end
 
@@ -1304,22 +2177,14 @@ static bool initialized = FALSE;
 
 @implementation AngbandContext
 
-@synthesize hasSubwindowFlags=_hasSubwindowFlags;
-@synthesize windowVisibilityChecked=_windowVisibilityChecked;
-
-- (BOOL)useLiveResizeOptimization
-{
-    /* If we have graphics turned off, text rendering is fast enough that we
-        * don't need to use a live resize optimization. */
-    return self->inLiveResize && graphics_are_enabled();
-}
-
 - (NSSize)baseSize
 {
-    /* We round the base size down. If we round it up, I believe we may end up
-        * with pixels that nobody "owns" that may accumulate garbage. In general
-        * rounding down is harmless, because any lost pixels may be sopped up by
-        * the border. */
+    /*
+     * We round the base size down. If we round it up, I believe we may end up
+     * with pixels that nobody "owns" that may accumulate garbage. In general
+     * rounding down is harmless, because any lost pixels may be sopped up by
+     * the border.
+     */
     return NSMakeSize(
        floor(self.cols * self.tileSize.width + 2 * self.borderSize.width),
        floor(self.rows * self.tileSize.height + 2 * self.borderSize.height));
@@ -1356,7 +2221,8 @@ static int compare_advances(const void *ap, const void *bp)
     for (i=0; i < GLYPH_COUNT; i++) latinString[i] = (unsigned char)i;
 
     /* Turn that into unichar. Angband uses ISO Latin 1. */
-    NSString *allCharsString = [[NSString alloc] initWithBytes:latinString length:sizeof latinString encoding:NSISOLatin1StringEncoding];
+    NSString *allCharsString = [[NSString alloc] initWithBytes:latinString
+        length:GLYPH_COUNT encoding:NSISOLatin1StringEncoding];
     unichar *unicharString = malloc(GLYPH_COUNT * sizeof(unichar));
     if (unicharString == 0) {
        free(latinString);
@@ -1408,8 +2274,10 @@ static int compare_advances(const void *ap, const void *bp)
         glyphWidths[i] = advances[i].width;
     }
 
-    /* For good non-mono-font support, use the median advance. Start by sorting
-        * all advances. */
+    /*
+     * For good non-mono-font support, use the median advance. Start by sorting
+     * all advances.
+     */
     qsort(advances, GLYPH_COUNT, sizeof *advances, compare_advances);
 
     /* Skip over any initially empty run */
@@ -1421,9 +2289,9 @@ static int compare_advances(const void *ap, const void *bp)
 
     /* Pick the center to find the median */
     CGFloat medianAdvance = 0;
+    /* In case we have all zero advances for some reason */
     if (startIdx < GLYPH_COUNT)
     {
-               /* In case we have all zero advances for some reason */
         medianAdvance = advances[(startIdx + GLYPH_COUNT)/2].width;
     }
 
@@ -1497,75 +2365,20 @@ static int compare_advances(const void *ap, const void *bp)
            beyond_right = v;
        }
     }
-    free(boxes);
-    self->_nColPre = ceil(-beyond_left / self.tileSize.width);
-    if (beyond_right > self.tileSize.width) {
-       self->_nColPost =
-           ceil((beyond_right - self.tileSize.width) / self.tileSize.width);
-    } else {
-       self->_nColPost = 0;
-    }
-
-    free(glyphWidths);
-    free(glyphArray);
-}
-
-- (void)updateImage
-{
-    NSSize size = NSMakeSize(1, 1);
-    
-    AngbandView *activeView = [self activeView];
-    if (activeView)
-    {
-        /* If we are in live resize, draw as big as the screen, so we can scale
-                * nicely to any size. If we are not in live resize, then use the
-                * bounds of the active view. */
-        NSScreen *screen;
-        if ([self useLiveResizeOptimization] && (screen = [[activeView window] screen]) != NULL)
-        {
-            size = [screen frame].size;
-        }
-        else
-        {
-            size = [activeView bounds].size;
-        }
-    }
-
-    CGLayerRelease(self.angbandLayer);
-    
-    /* Use the highest monitor scale factor on the system to work out what
-     * scale to draw at - not the recommended method, but works where we
-     * can't easily get the monitor the current draw is occurring on. */
-    float angbandLayerScale = 1.0;
-    if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) {
-        for (NSScreen *screen in [NSScreen screens]) {
-            angbandLayerScale = fmax(angbandLayerScale, [screen backingScaleFactor]);
-        }
-    }
-
-    /* Make a bitmap context as an example for our layer */
-    CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
-    CGContextRef exampleCtx = CGBitmapContextCreate(NULL, 1, 1, 8 /* bits per component */, 48 /* bytesPerRow */, cs, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host);
-    CGColorSpaceRelease(cs);
-
-    /* Create the layer at the appropriate size */
-    size.width = fmax(1, ceil(size.width * angbandLayerScale));
-    size.height = fmax(1, ceil(size.height * angbandLayerScale));
-    self.angbandLayer =
-       CGLayerCreateWithContext(exampleCtx, *(CGSize *)&size, NULL);
-
-    CFRelease(exampleCtx);
-
-    /* Set the new context of the layer to draw at the correct scale */
-    CGContextRef ctx = CGLayerGetContext(self.angbandLayer);
-    CGContextScaleCTM(ctx, angbandLayerScale, angbandLayerScale);
+    free(boxes);
+    self->_nColPre = ceil(-beyond_left / self.tileSize.width);
+    if (beyond_right > self.tileSize.width) {
+       self->_nColPost =
+           ceil((beyond_right - self.tileSize.width) / self.tileSize.width);
+    } else {
+       self->_nColPost = 0;
+    }
 
-    [self lockFocus];
-    [[NSColor blackColor] set];
-    NSRectFill((NSRect){NSZeroPoint, [self baseSize]});
-    [self unlockFocus];
+    free(glyphWidths);
+    free(glyphArray);
 }
 
+
 - (void)requestRedraw
 {
     if (! self->terminal) return;
@@ -1590,38 +2403,6 @@ static int compare_advances(const void *ap, const void *bp)
     self->terminal = t;
 }
 
-- (void)viewWillStartLiveResize:(AngbandView *)view
-{
-#if USE_LIVE_RESIZE_CACHE
-    if (self->inLiveResize < INT_MAX) self->inLiveResize++;
-    else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize overflow"];
-    
-    if (self->inLiveResize == 1 && graphics_are_enabled())
-    {
-        [self updateImage];
-        
-        [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
-        [self requestRedraw];
-    }
-#endif
-}
-
-- (void)viewDidEndLiveResize:(AngbandView *)view
-{
-#if USE_LIVE_RESIZE_CACHE
-    if (self->inLiveResize > 0) self->inLiveResize--;
-    else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize underflow"];
-    
-    if (self->inLiveResize == 0 && graphics_are_enabled())
-    {
-        [self updateImage];
-        
-        [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
-        [self requestRedraw];
-    }
-#endif
-}
-
 /**
  * If we're trying to limit ourselves to a certain number of frames per second,
  * then compute how long it's been since we last drew, and then wait until the
@@ -1642,21 +2423,24 @@ static int compare_advances(const void *ap, const void *bp)
     self->lastRefreshTime = CFAbsoluteTimeGetCurrent();
 }
 
-- (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx
+- (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile screenFont:(NSFont*)font
+         context:(CGContextRef)ctx
 {
     CGFloat tileOffsetY = self.fontAscender;
     CGFloat tileOffsetX = 0.0;
-    NSFont *screenFont = [self.angbandViewFont screenFont];
     UniChar unicharString[2] = {(UniChar)wchar, 0};
 
     /* Get glyph and advance */
     CGGlyph thisGlyphArray[1] = { 0 };
     CGSize advances[1] = { { 0, 0 } };
-    CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, thisGlyphArray, 1);
+    CTFontGetGlyphsForCharacters(
+       (CTFontRef)font, unicharString, thisGlyphArray, 1);
     CGGlyph glyph = thisGlyphArray[0];
-    CTFontGetAdvancesForGlyphs((CTFontRef)screenFont, kCTFontHorizontalOrientation, thisGlyphArray, advances, 1);
+    CTFontGetAdvancesForGlyphs(
+       (CTFontRef)font, kCTFontHorizontalOrientation, thisGlyphArray,
+       advances, 1);
     CGSize advance = advances[0];
-    
+
     /* If our font is not monospaced, our tile width is deliberately not big
         * enough for every character. In that event, if our glyph is too wide, we
         * need to compress it horizontally. Compute the compression ratio.
@@ -1675,7 +2459,6 @@ static int compare_advances(const void *ap, const void *bp)
         tileOffsetX = 0;
     }
 
-    
     /* Now draw it */
     CGAffineTransform textMatrix = CGContextGetTextMatrix(ctx);
     CGFloat savedA = textMatrix.a;
@@ -1690,61 +2473,24 @@ static int compare_advances(const void *ap, const void *bp)
         textMatrix.a *= compressionRatio;
     }
 
-    textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
     CGContextSetTextMatrix(ctx, textMatrix);
     CGContextShowGlyphsAtPositions(ctx, &glyph, &CGPointZero, 1);
-    
+
     /* Restore the text matrix if we messed with the compression ratio */
     if (compressionRatio != 1.)
     {
         textMatrix.a = savedA;
-        CGContextSetTextMatrix(ctx, textMatrix);
     }
 
-    textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
     CGContextSetTextMatrix(ctx, textMatrix);
 }
 
-/* Lock and unlock focus on our image or layer, setting up the CTM
- * appropriately. */
-- (CGContextRef)lockFocusUnscaled
-{
-    /* Create an NSGraphicsContext representing this CGLayer */
-    CGContextRef ctx = CGLayerGetContext(self.angbandLayer);
-    NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO];
-    [NSGraphicsContext saveGraphicsState];
-    [NSGraphicsContext setCurrentContext:context];
-    CGContextSaveGState(ctx);
-    return ctx;
-}
-
-- (void)unlockFocus
-{
-    /* Restore the graphics state */
-    CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
-    CGContextRestoreGState(ctx);
-    [NSGraphicsContext restoreGraphicsState];
-}
-
-- (NSSize)imageSize
-{
-    /* Return the size of our layer */
-    CGSize result = CGLayerGetSize(self.angbandLayer);
-    return NSMakeSize(result.width, result.height);
-}
-
-- (CGContextRef)lockFocus
-{
-    return [self lockFocusUnscaled];
-}
-
-
-- (NSRect)rectInImageForTileAtX:(int)x Y:(int)y
+- (NSRect)viewRectForCellBlockAtX:(int)x y:(int)y width:(int)w height:(int)h
 {
-    int flippedY = y;
-    return NSMakeRect(x * self.tileSize.width + self.borderSize.width,
-                     flippedY * self.tileSize.height + self.borderSize.height,
-                     self.tileSize.width, self.tileSize.height);
+    return NSMakeRect(
+       x * self.tileSize.width + self.borderSize.width,
+       y * self.tileSize.height + self.borderSize.height,
+       w * self.tileSize.width, h * self.tileSize.height);
 }
 
 - (void)setSelectionFont:(NSFont*)font adjustTerminal: (BOOL)adjustTerminal
@@ -1763,7 +2509,7 @@ static int compare_advances(const void *ap, const void *bp)
            [self.primaryWindow
                 contentRectForFrameRect: [self.primaryWindow frame]];
 
-       [self setMinimumWindowSize:[self terminalIndex]];
+       [self constrainWindowSize:[self terminalIndex]];
        NSSize size = self.primaryWindow.contentMinSize;
        BOOL windowNeedsResizing = NO;
        if (contentRect.size.width < size.width) {
@@ -1781,9 +2527,6 @@ static int compare_advances(const void *ap, const void *bp)
        }
         [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
     }
-
-    /* Update our image */
-    [self updateImage];
 }
 
 - (id)init
@@ -1797,22 +2540,18 @@ static int compare_advances(const void *ap, const void *bp)
         /* Default border size */
         self->_borderSize = NSMakeSize(2, 2);
 
-        /* Allocate our array of views */
-        self->_angbandViews = [[NSMutableArray alloc] init];
-
        self->_nColPre = 0;
        self->_nColPost = 0;
 
+       self->_contents =
+           [[TerminalContents alloc] initWithColumns:self->_cols
+                                     rows:self->_rows];
        self->_changes =
-           [[PendingTermChanges alloc] initWithColumnsRows:self->_cols
-                                       rows:self->_rows];
+           [[TerminalChanges alloc] initWithColumns:self->_cols
+                                    rows:self->_rows];
        self->lastRefreshTime = CFAbsoluteTimeGetCurrent();
-       self->inLiveResize = 0;
        self->inFullscreenTransition = NO;
 
-        /* Make the image. Since we have no views, it'll just be a puny 1x1 image. */
-        [self updateImage];
-
         self->_windowVisibilityChecked = NO;
     }
     return self;
@@ -1826,13 +2565,9 @@ static int compare_advances(const void *ap, const void *bp)
 {
     self->terminal = NULL;
 
-    /* Disassociate ourselves from our angbandViews */
-    [self.angbandViews makeObjectsPerformSelector:@selector(setAngbandContext:) withObject:nil];
-    self.angbandViews = nil;
-
-    /* Destroy the layer/image */
-    CGLayerRelease(self.angbandLayer);
-    self.angbandLayer = NULL;
+    /* Disassociate ourselves from our view. */
+    [self->angbandView setAngbandContext:nil];
+    self->angbandView = nil;
 
     /* Font */
     self.angbandViewFont = nil;
@@ -1842,7 +2577,8 @@ static int compare_advances(const void *ap, const void *bp)
     [self.primaryWindow close];
     self.primaryWindow = nil;
 
-    /* Pending changes */
+    /* Contents and pending changes */
+    self.contents = nil;
     self.changes = nil;
 }
 
@@ -1852,252 +2588,1117 @@ static int compare_advances(const void *ap, const void *bp)
     [self dispose];
 }
 
-#if 0
-/* From the Linux mbstowcs(3) man page:
- *   If dest is NULL, n is ignored, and the conversion  proceeds  as  above,
- *   except  that  the converted wide characters are not written out to mem‐
- *   ory, and that no length limit exists.
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow
+{
+    [self.contents resizeWithColumns:nCol rows:nRow];
+    [self.changes resizeWithColumns:nCol rows:nRow];
+    self->_cols = nCol;
+    self->_rows = nRow;
+}
+
+/**
+ * For defaultFont and setDefaultFont.
  */
-static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
+static __strong NSFont* gDefaultFont = nil;
+
++ (NSFont*)defaultFont
 {
-    int i;
-    int count = 0;
+    return gDefaultFont;
+}
 
-    /* Unicode code point to UTF-8
-     *  0x0000-0x007f:   0xxxxxxx
-     *  0x0080-0x07ff:   110xxxxx 10xxxxxx
-     *  0x0800-0xffff:   1110xxxx 10xxxxxx 10xxxxxx
-     * 0x10000-0x1fffff: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
-     * Note that UTF-16 limits Unicode to 0x10ffff. This code is not
-     * endian-agnostic.
-     */
-    for (i = 0; i < n || dest == NULL; i++) {
-        if ((src[i] & 0x80) == 0) {
-            if (dest != NULL) dest[count] = src[i];
-            if (src[i] == 0) break;
-        } else if ((src[i] & 0xe0) == 0xc0) {
-            if (dest != NULL) dest[count] = 
-                            (((unsigned char)src[i] & 0x1f) << 6)| 
-                            ((unsigned char)src[i+1] & 0x3f);
-            i++;
-        } else if ((src[i] & 0xf0) == 0xe0) {
-            if (dest != NULL) dest[count] = 
-                            (((unsigned char)src[i] & 0x0f) << 12) | 
-                            (((unsigned char)src[i+1] & 0x3f) << 6) |
-                            ((unsigned char)src[i+2] & 0x3f);
-            i += 2;
-        } else if ((src[i] & 0xf8) == 0xf0) {
-            if (dest != NULL) dest[count] = 
-                            (((unsigned char)src[i] & 0x0f) << 18) | 
-                            (((unsigned char)src[i+1] & 0x3f) << 12) |
-                            (((unsigned char)src[i+2] & 0x3f) << 6) |
-                            ((unsigned char)src[i+3] & 0x3f);
-            i += 3;
++ (void)setDefaultFont:(NSFont*)font
+{
+    gDefaultFont = font;
+}
+
+- (void)setDefaultTitle:(int)termIdx
+{
+    NSMutableString *title =
+       [NSMutableString stringWithCString:angband_term_name[termIdx]
+#ifdef JP
+                        encoding:NSJapaneseEUCStringEncoding
+#else
+                        encoding:NSMacOSRomanStringEncoding
+#endif
+       ];
+    [title appendFormat:@" %dx%d", self.cols, self.rows];
+    [[self makePrimaryWindow] setTitle:title];
+}
+
+- (NSWindow *)makePrimaryWindow
+{
+    if (! self.primaryWindow)
+    {
+        /* This has to be done after the font is set, which it already is in
+                * term_init_cocoa() */
+        NSSize sz = self.baseSize;
+        NSRect contentRect = NSMakeRect( 0.0, 0.0, sz.width, sz.height );
+
+        NSUInteger styleMask = NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask;
+
+        /*
+        * Make every window other than the main window closable, also create
+        * them as utility panels to get the thinner title bar and other
+        * attributes that already match up with how those windows are used.
+        */
+        if ((__bridge AngbandContext*) (angband_term[0]->data) != self)
+        {
+           NSPanel *panel =
+               [[NSPanel alloc] initWithContentRect:contentRect
+                                styleMask:(styleMask | NSClosableWindowMask |
+                                           NSUtilityWindowMask)
+                                backing:NSBackingStoreBuffered defer:YES];
+
+           panel.floatingPanel = NO;
+           self.primaryWindow = panel;
         } else {
-            /* Found an invalid multibyte sequence */
-            return (size_t)-1;
-        }
-        count++;
+           self.primaryWindow =
+               [[NSWindow alloc] initWithContentRect:contentRect
+                                 styleMask:styleMask
+                                 backing:NSBackingStoreBuffered defer:YES];
+       }
+
+        /* Not to be released when closed */
+        [self.primaryWindow setReleasedWhenClosed:NO];
+        [self.primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
+
+        /* Make the view */
+        self->angbandView = [[AngbandView alloc] initWithFrame:contentRect];
+        [angbandView setAngbandContext:self];
+        [angbandView setNeedsDisplay:YES];
+        [self.primaryWindow setContentView:angbandView];
+
+        /* We are its delegate */
+        [self.primaryWindow setDelegate:self];
+    }
+    return self.primaryWindow;
+}
+
+
+- (void)computeInvalidRects
+{
+    for (int irow = self.changes.firstChangedRow;
+        irow <= self.changes.lastChangedRow;
+        ++irow) {
+       int icol = [self.changes scanForChangedInRow:irow
+                       col0:0 col1:self.cols];
+
+       while (icol < self.cols) {
+           /* Find the end of the changed region. */
+           int jcol =
+               [self.changes scanForUnchangedInRow:irow col0:(icol + 1)
+                    col1:self.cols];
+
+           /*
+            * If the last column is a character, extend the region drawn
+            * because characters can exceed the horizontal bounds of the cell
+            * and those parts will need to be cleared.  Don't extend into a
+            * tile because the clipping is set while drawing to never
+            * extend text into a tile.  For a big character that's been
+            * partially overwritten, allow what comes after the point
+            * where the overwrite occurred to influence the stuff before
+            * but not vice versa.  If extending the region reaches another
+            * changed block, find the end of that block and repeat the
+            * process.
+            */
+           /*
+            * A value of zero means checking for a character immediately
+            * prior to the column, isrch.  A value of one means checking for
+            * something past the end that could either influence the changed
+            * region (within nColPre of it and no intervening tile) or be
+            * influenced by it (within nColPost of it and no intervening
+            * tile or partially overwritten big character).  A value of two
+            * means checking for something past the end which is both changed
+            * and could affect the part of the unchanged region that has to
+            * be redrawn because it is affected by the prior changed region
+            * Values of three and four are like one and two, respectively,
+            * but indicate that a partially overwritten big character was
+            * found.
+            */
+           int stage = 0;
+           int isrch = jcol;
+           int irng0 = jcol;
+           int irng1 = jcol;
+           while (1) {
+               if (stage == 0) {
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:(isrch - 1) row:irow];
+                   if ((pcell->form &
+                        (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                       break;
+                   } else {
+                       irng0 = isrch + self.nColPre;
+                       if (irng0 > self.cols) {
+                           irng0 = self.cols;
+                       }
+                       irng1 = isrch + self.nColPost;
+                       if (irng1 > self.cols) {
+                           irng1 = self.cols;
+                       }
+                       if (isrch < irng0 || isrch < irng1) {
+                           stage = isPartiallyOverwrittenBigChar(pcell) ?
+                               3 : 1;
+                       } else {
+                           break;
+                       }
+                   }
+               }
+
+               if (stage == 1) {
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:isrch row:irow];
+
+                   if ((pcell->form &
+                        (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                       /*
+                        * Check if still in the region that could be
+                        * influenced by the changed region.  If so,
+                        * everything up to the tile will be redrawn anyways
+                        * so combine the regions if the tile has changed
+                        * as well.  Otherwise, terminate the search since
+                        * the tile doesn't allow influence to propagate
+                        * through it and don't want to affect what's in the
+                        * tile.
+                        */
+                       if (isrch < irng1) {
+                           if ([self.changes isChangedAtColumn:isrch
+                                    row:irow]) {
+                               jcol = [self.changes scanForUnchangedInRow:irow
+                                           col0:(isrch + 1) col1:self.cols];
+                               if (jcol < self.cols) {
+                                   stage = 0;
+                                   isrch = jcol;
+                                   continue;
+                               }
+                           }
+                       }
+                       break;
+                   } else {
+                       /*
+                        * With a changed character, combine the regions (if
+                        * still in the region affected by the changed region
+                        * am going to redraw everything up to this new region
+                        * anyway; if only in the region that can affect the
+                        * changed region, this changed text could influence
+                        * the current changed region).
+                        */
+                       if ([self.changes isChangedAtColumn:isrch row:irow]) {
+                           jcol = [self.changes scanForUnchangedInRow:irow
+                                       col0:(isrch + 1) col1:self.cols];
+                           if (jcol < self.cols) {
+                               stage = 0;
+                               isrch = jcol;
+                               continue;
+                           }
+                           break;
+                       }
+
+                       if (isrch < irng1) {
+                           /*
+                            * Can be affected by the changed region so
+                            * has to be redrawn.
+                            */
+                           ++jcol;
+                       }
+                       ++isrch;
+                       if (isrch >= irng1) {
+                           irng0 = jcol + self.nColPre;
+                           if (irng0 > self.cols) {
+                               irng0 = self.cols;
+                           }
+                           if (isrch >= irng0) {
+                               break;
+                           }
+                           stage = isPartiallyOverwrittenBigChar(pcell) ?
+                               4 : 2;
+                       } else if (isPartiallyOverwrittenBigChar(pcell)) {
+                           stage = 3;
+                       }
+                   }
+               }
+
+               if (stage == 2) {
+                   /*
+                    * Looking for a later changed region that could influence
+                    * the region that has to be redrawn.  The region that has
+                    * to be redrawn ends just before jcol.
+                    */
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:isrch row:irow];
+
+                   if ((pcell->form &
+                        (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                       /* Can not spread influence through a tile. */
+                       break;
+                   }
+                   if ([self.changes isChangedAtColumn:isrch row:irow]) {
+                       /*
+                        * Found one.  Combine with the one ending just before
+                        * jcol.
+                        */
+                       jcol = [self.changes scanForUnchangedInRow:irow
+                                   col0:(isrch + 1) col1:self.cols];
+                       if (jcol < self.cols) {
+                           stage = 0;
+                           isrch = jcol;
+                           continue;
+                       }
+                       break;
+                   }
+
+                   ++isrch;
+                   if (isrch >= irng0) {
+                       break;
+                   }
+                   if (isPartiallyOverwrittenBigChar(pcell)) {
+                       stage = 4;
+                   }
+               }
+
+               if (stage == 3) {
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:isrch row:irow];
+
+                   /*
+                    * Have encountered a partially overwritten big character
+                    * but still may be in the region that could be influenced
+                    * by the changed region.  That influence can not extend
+                    * past the past the padding for the partially overwritten
+                    * character.
+                    */
+                   if ((pcell->form & (TERM_CELL_CHAR | TERM_CELL_TILE |
+                                       TERM_CELL_TILE_PADDING)) != 0) {
+                       if (isrch < irng1) {
+                           /*
+                            * Still can be affected by the changed region
+                            * so everything up to isrch will be redrawn
+                            * anyways.  If this location has changed,
+                            * merge the changed regions.
+                            */
+                           if ([self.changes isChangedAtColumn:isrch
+                                    row:irow]) {
+                               jcol = [self.changes scanForUnchangedInRow:irow
+                                           col0:(isrch + 1) col1:self.cols];
+                               if (jcol < self.cols) {
+                                   stage = 0;
+                                   isrch = jcol;
+                                   continue;
+                               }
+                               break;
+                           }
+                       }
+                       if ((pcell->form &
+                            (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                           /*
+                            * It's a tile.  That blocks influence in either
+                            * direction.
+                            */
+                           break;
+                       }
+
+                       /*
+                        * The partially overwritten big character was
+                        * overwritten by a character.  Check to see if it
+                        * can either influence the unchanged region that
+                        * has to redrawn or the changed region prior to
+                        * that.
+                        */
+                       if (isrch >= irng0) {
+                           break;
+                       }
+                       stage = 4;
+                   } else {
+                       if (isrch < irng1) {
+                           /*
+                            * Can be affected by the changed region so has to
+                            * be redrawn.
+                            */
+                           ++jcol;
+                       }
+                       ++isrch;
+                       if (isrch >= irng1) {
+                           irng0 = jcol + self.nColPre;
+                           if (irng0 > self.cols) {
+                               irng0 = self.cols;
+                           }
+                           if (isrch >= irng0) {
+                               break;
+                           }
+                           stage = 4;
+                       }
+                   }
+               }
+
+               if (stage == 4) {
+                   /*
+                    * Have already encountered a partially overwritten big
+                    * character.  Looking for a later changed region that
+                    * could influence the region that has to be redrawn
+                    * The region that has to be redrawn ends just before jcol.
+                    */
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:isrch row:irow];
+
+                   if ((pcell->form &
+                        (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                       /* Can not spread influence through a tile. */
+                       break;
+                   }
+                   if (pcell->form == TERM_CELL_CHAR) {
+                       if ([self.changes isChangedAtColumn:isrch row:irow]) {
+                           /*
+                            * Found a changed region.  Combine with the one
+                            * ending just before jcol.
+                            */
+                           jcol = [self.changes scanForUnchangedInRow:irow
+                                       col0:(isrch + 1) col1:self.cols];
+                           if (jcol < self.cols) {
+                               stage = 0;
+                               isrch = jcol;
+                               continue;
+                           }
+                           break;
+                       }
+                   }
+                   ++isrch;
+                   if (isrch >= irng0) {
+                       break;
+                   }
+               }
+           }
+
+           /*
+            * Check to see if there's characters before the changed region
+            * that would have to be redrawn because it's influenced by the
+            * changed region.  Do not have to check for merging with a prior
+            * region because of the screening already done.
+            */
+           if (self.nColPre > 0 &&
+               ([self.contents getCellAtColumn:icol row:irow]->form &
+                (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
+               int irng = icol - self.nColPre;
+
+               if (irng < 0) {
+                   irng = 0;
+               }
+               while (icol > irng &&
+                      ([self.contents getCellAtColumn:(icol - 1)
+                            row:irow]->form &
+                       (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
+                   --icol;
+               }
+           }
+
+           NSRect r = [self viewRectForCellBlockAtX:icol y:irow
+                            width:(jcol - icol) height:1];
+           [self setNeedsDisplayInRect:r];
+
+           icol = [self.changes scanForChangedInRow:irow col0:jcol
+                       col1:self.cols];
+       }
     }
-    return count;
 }
-#endif
 
-- (void)addAngbandView:(AngbandView *)view
+
+#pragma mark View/Window Passthrough
+
+/*
+ * This is a qsort-compatible compare function for NSRect, to get them in
+ * ascending order by y origin.
+ */
+static int compare_nsrect_yorigin_greater(const void *ap, const void *bp)
 {
-    if (! [self.angbandViews containsObject:view])
-    {
-        [self.angbandViews addObject:view];
-        [self updateImage];
-        [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
-        [self requestRedraw];
-    }
+    const NSRect *arp = ap;
+    const NSRect *brp = bp;
+    return (arp->origin.y > brp->origin.y) - (arp->origin.y < brp->origin.y);
 }
 
 /**
- * For defaultFont and setDefaultFont.
+ * This is a helper function for drawRect.
  */
-static __strong NSFont* gDefaultFont = nil;
+- (void)renderTileRunInRow:(int)irow col0:(int)icol0 col1:(int)icol1
+                    nsctx:(NSGraphicsContext*)nsctx ctx:(CGContextRef)ctx
+                grafWidth:(int)graf_width grafHeight:(int)graf_height
+              overdrawRow:(int)overdraw_row overdrawMax:(int)overdraw_max
+{
+    /* Save the compositing mode since it is modified below. */
+    NSCompositingOperation op = nsctx.compositingOperation;
+
+    while (icol0 < icol1) {
+       const struct TerminalCell *pcell =
+           [self.contents getCellAtColumn:icol0 row:irow];
+       NSRect destinationRect =
+           [self viewRectForCellBlockAtX:icol0 y:irow
+                 width:pcell->hscl height:pcell->vscl];
+       NSRect fgdRect = NSMakeRect(
+           graf_width * (pcell->v.ti.fgdCol +
+                         pcell->hoff_n / (1.0 * pcell->hoff_d)),
+           graf_height * (pcell->v.ti.fgdRow +
+                          pcell->voff_n / (1.0 * pcell->voff_d)),
+           graf_width * pcell->hscl / (1.0 * pcell->hoff_d),
+           graf_height * pcell->vscl / (1.0 * pcell->voff_d));
+       NSRect bckRect = NSMakeRect(
+           graf_width * (pcell->v.ti.bckCol +
+                         pcell->hoff_n / (1.0 * pcell->hoff_d)),
+           graf_height * (pcell->v.ti.bckRow +
+                          pcell->voff_n / (1.0 * pcell->voff_d)),
+           graf_width * pcell->hscl / (1.0 * pcell->hoff_d),
+           graf_height * pcell->vscl / (1.0 * pcell->voff_d));
+       int dbl_height_bck = overdraw_row && (irow > 2) &&
+           (pcell->v.ti.bckRow >= overdraw_row &&
+            pcell->v.ti.bckRow <= overdraw_max);
+       int dbl_height_fgd = overdraw_row && (irow > 2) &&
+           (pcell->v.ti.fgdRow >= overdraw_row) &&
+           (pcell->v.ti.fgdRow <= overdraw_max);
+       int aligned_row = 0, aligned_col = 0;
+       int is_first_piece = 0, simple_upper = 0;
+
+       /* Initialize stuff for handling a double-height tile. */
+       if (dbl_height_bck || dbl_height_fgd) {
+           if (self->terminal == angband_term[0]) {
+               aligned_col = ((icol0 - COL_MAP) / pcell->hoff_d) *
+                   pcell->hoff_d + COL_MAP;
+           } else {
+               aligned_col = (icol0 / pcell->hoff_d) * pcell->hoff_d;
+           }
+           aligned_row = ((irow - ROW_MAP) / pcell->voff_d) *
+               pcell->voff_d + ROW_MAP;
 
-+ (NSFont*)defaultFont
-{
-    return gDefaultFont;
-}
+           /*
+            * If the lower half has been broken into multiple pieces, only
+            * do the work of rendering whatever is necessary for the upper
+            * half when drawing the first piece (the one closest to the
+            * upper left corner).
+            */
+           struct TerminalCellLocation curs = { 0, 0 };
 
-+ (void)setDefaultFont:(NSFont*)font
-{
-    gDefaultFont = font;
+           [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
+                row:aligned_row width:pcell->hoff_d height:pcell->voff_d
+                mask:TERM_CELL_TILE cursor:&curs];
+           if (curs.col + aligned_col == icol0 &&
+               curs.row + aligned_row == irow) {
+               is_first_piece = 1;
+
+               /*
+                * Hack:  lookup the previous row to determine how much of the
+                * tile there is shown to apply it the upper half of the
+                * double-height tile.  That will do the right thing if there
+                * is a menu displayed in that row but isn't right if there's
+                * an object/creature/feature there that doesn't have a
+                * mapping to the tile set and is rendered with a character.
+                */
+               curs.col = 0;
+               curs.row = 0;
+               [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
+                    row:(aligned_row - pcell->voff_d) width:pcell->hoff_d
+                    height:pcell->voff_d mask:TERM_CELL_TILE cursor:&curs];
+               if (curs.col == 0 && curs.row == 0) {
+                   const struct TerminalCell *pcell2 =
+                       [self.contents
+                            getCellAtColumn:(aligned_col + curs.col)
+                            row:(aligned_row + curs.row - pcell->voff_d)];
+
+                   if (pcell2->hscl == pcell2->hoff_d &&
+                       pcell2->vscl == pcell2->voff_d) {
+                       /*
+                        * The tile in the previous row hasn't been clipped
+                        * or partially overwritten.  Use a streamlined
+                        * rendering procedure.
+                        */
+                       simple_upper = 1;
+                   }
+               }
+           }
+       }
+
+       /*
+        * Draw the background.  For a double-height tile, this is only the
+        * the lower half.
+        */
+       draw_image_tile(
+           nsctx, ctx, pict_image, bckRect, destinationRect, NSCompositeCopy);
+       if (dbl_height_bck && is_first_piece) {
+           /* Combine upper half with previously drawn row. */
+           if (simple_upper) {
+               const struct TerminalCell *pcell2 =
+                   [self.contents getCellAtColumn:aligned_col
+                        row:(aligned_row - pcell->voff_d)];
+               NSRect drect2 =
+                   [self viewRectForCellBlockAtX:aligned_col
+                         y:(aligned_row - pcell->voff_d)
+                         width:pcell2->hscl height:pcell2->vscl];
+               NSRect brect2 = NSMakeRect(
+                   graf_width * pcell->v.ti.bckCol,
+                   graf_height * (pcell->v.ti.bckRow - 1),
+                   graf_width, graf_height);
+
+               draw_image_tile(nsctx, ctx, pict_image, brect2, drect2,
+                               NSCompositeSourceOver);
+           } else {
+               struct TerminalCellLocation curs = { 0, 0 };
+
+               [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
+                    row:(aligned_row - pcell->voff_d) width:pcell->hoff_d
+                    height:pcell->voff_d mask:TERM_CELL_TILE
+                    cursor:&curs];
+               while (curs.col < pcell->hoff_d &&
+                      curs.row < pcell->voff_d) {
+                   const struct TerminalCell *pcell2 =
+                       [self.contents getCellAtColumn:(aligned_col + curs.col)
+                            row:(aligned_row + curs.row - pcell->voff_d)];
+                   NSRect drect2 =
+                       [self viewRectForCellBlockAtX:(aligned_col + curs.col)
+                             y:(aligned_row + curs.row - pcell->voff_d)
+                             width:pcell2->hscl height:pcell2->vscl];
+                   /*
+                    * Column and row in the tile set are from the
+                    * double-height tile at *pcell, but the offsets within
+                    * that and size are from what's visible for *pcell2.
+                    */
+                   NSRect brect2 = NSMakeRect(
+                       graf_width * (pcell->v.ti.bckCol +
+                                     pcell2->hoff_n / (1.0 * pcell2->hoff_d)),
+                       graf_height * (pcell->v.ti.bckRow - 1 +
+                                      pcell2->voff_n /
+                                      (1.0 * pcell2->voff_d)),
+                       graf_width * pcell2->hscl / (1.0 * pcell2->hoff_d),
+                       graf_height * pcell2->vscl / (1.0 * pcell2->voff_d));
+
+                   draw_image_tile(nsctx, ctx, pict_image, brect2, drect2,
+                                   NSCompositeSourceOver);
+                   curs.col += pcell2->hscl;
+                   [self.contents
+                        scanForTypeMaskInBlockAtColumn:aligned_col
+                        row:(aligned_row - pcell->voff_d)
+                        width:pcell->hoff_d height:pcell->voff_d
+                        mask:TERM_CELL_TILE cursor:&curs];
+               }
+           }
+       }
+
+       /* Skip drawing the foreground if it is the same as the background. */
+       if (fgdRect.origin.x != bckRect.origin.x ||
+           fgdRect.origin.y != bckRect.origin.y) {
+           if (is_first_piece && dbl_height_fgd) {
+               if (simple_upper) {
+                   if (pcell->hoff_n == 0 && pcell->voff_n == 0 &&
+                       pcell->hscl == pcell->hoff_d) {
+                       /*
+                        * Render upper and lower parts as one since they
+                        * are contiguous.
+                        */
+                       fgdRect.origin.y -= graf_height;
+                       fgdRect.size.height += graf_height;
+                       destinationRect.origin.y -=
+                           destinationRect.size.height;
+                       destinationRect.size.height +=
+                           destinationRect.size.height;
+                   } else {
+                       /* Not contiguous.  Render the upper half. */
+                       NSRect drect2 =
+                           [self viewRectForCellBlockAtX:aligned_col
+                                 y:(aligned_row - pcell->voff_d)
+                                 width:pcell->hoff_d height:pcell->voff_d];
+                       NSRect frect2 = NSMakeRect(
+                           graf_width * pcell->v.ti.fgdCol,
+                           graf_height * (pcell->v.ti.fgdRow - 1),
+                           graf_width, graf_height);
+
+                       draw_image_tile(
+                           nsctx, ctx, pict_image, frect2, drect2,
+                           NSCompositeSourceOver);
+                   }
+               } else {
+                   /* Render the upper half pieces. */
+                   struct TerminalCellLocation curs = { 0, 0 };
+
+                   while (1) {
+                       [self.contents
+                            scanForTypeMaskInBlockAtColumn:aligned_col
+                            row:(aligned_row - pcell->voff_d)
+                            width:pcell->hoff_d height:pcell->voff_d
+                            mask:TERM_CELL_TILE cursor:&curs];
+
+                       if (curs.col >= pcell->hoff_d ||
+                           curs.row >= pcell->voff_d) {
+                           break;
+                       }
+
+                       const struct TerminalCell *pcell2 =
+                           [self.contents
+                                getCellAtColumn:(aligned_col + curs.col)
+                                row:(aligned_row + curs.row - pcell->voff_d)];
+                       NSRect drect2 =
+                           [self viewRectForCellBlockAtX:(aligned_col + curs.col)
+                                 y:(aligned_row + curs.row - pcell->voff_d)
+                                 width:pcell2->hscl height:pcell2->vscl];
+                       NSRect frect2 = NSMakeRect(
+                           graf_width * (pcell->v.ti.fgdCol +
+                                         pcell2->hoff_n /
+                                         (1.0 * pcell2->hoff_d)),
+                           graf_height * (pcell->v.ti.fgdRow - 1 +
+                                          pcell2->voff_n /
+                                          (1.0 * pcell2->voff_d)),
+                           graf_width * pcell2->hscl / (1.0 * pcell2->hoff_d),
+                           graf_height * pcell2->vscl /
+                               (1.0 * pcell2->voff_d));
+
+                       draw_image_tile(nsctx, ctx, pict_image, frect2, drect2,
+                                       NSCompositeSourceOver);
+                       curs.col += pcell2->hscl;
+                   }
+               }
+           }
+           /*
+            * Render the foreground (if a double height tile and the bottom
+            * part is contiguous with the upper part this also render the
+            * upper part.
+            */
+           draw_image_tile(
+               nsctx, ctx, pict_image, fgdRect, destinationRect,
+               NSCompositeSourceOver);
+       }
+       icol0 = [self.contents scanForTypeMaskInRow:irow mask:TERM_CELL_TILE
+                    col0:(icol0+pcell->hscl) col1:icol1];
+    }
+
+    /* Restore the compositing mode. */
+    nsctx.compositingOperation = op;
 }
 
 /**
- * We have this notion of an "active" AngbandView, which is the largest - the
- * idea being that in the screen saver, when the user hits Test in System
- * Preferences, we don't want to keep driving the AngbandView in the
- * background.  Our active AngbandView is the widest - that's a hack all right.
- * Mercifully when we're just playing the game there's only one view.
+ * This is what our views call to get us to draw to the window
  */
-- (AngbandView *)activeView
+- (void)drawRect:(NSRect)rect inView:(NSView *)view
 {
-    if ([self.angbandViews count] == 1)
-        return [self.angbandViews objectAtIndex:0];
+    /* Take this opportunity to throttle so we don't flush faster than desired.
+        */
+    [self throttle];
 
-    AngbandView *result = nil;
-    float maxWidth = 0;
-    for (AngbandView *angbandView in self.angbandViews)
-    {
-        float width = [angbandView frame].size.width;
-        if (width > maxWidth)
-        {
-            maxWidth = width;
-            result = angbandView;
-        }
-    }
-    return result;
-}
+    CGFloat bottomY =
+       self.borderSize.height + self.tileSize.height * self.rows;
+    CGFloat rightX =
+       self.borderSize.width + self.tileSize.width * self.cols;
 
-- (void)setDefaultTitle:(int)termIdx
-{
-    NSMutableString *title =
-       [NSMutableString stringWithCString:angband_term_name[termIdx]
-#ifdef JP
-                        encoding:NSJapaneseEUCStringEncoding
-#else
-                        encoding:NSMacOSRomanStringEncoding
-#endif
-       ];
-    [title appendFormat:@" %dx%d", self.cols, self.rows];
-    [[self makePrimaryWindow] setTitle:title];
-}
+    const NSRect *invalidRects;
+    NSInteger invalidCount;
+    [view getRectsBeingDrawn:&invalidRects count:&invalidCount];
 
-- (void)angbandViewDidScale:(AngbandView *)view
-{
-    /* If we're live-resizing with graphics, we're using the live resize
-        * optimization, so don't update the image. Otherwise do it. */
-    if (! (self->inLiveResize && graphics_are_enabled()) && view == [self activeView])
-    {
-        [self updateImage];
-        
-        [self setNeedsDisplay:YES]; /*we'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
-        [self requestRedraw];
+    /*
+     * If the non-border areas need rendering, set some things up so they can
+     * be reused for each invalid rectangle.
+     */
+    NSGraphicsContext *nsctx = nil;
+    CGContextRef ctx = 0;
+    NSFont* screenFont = nil;
+    int graf_width = 0, graf_height = 0;
+    int overdraw_row = 0, overdraw_max = 0;
+    wchar_t blank = 0;
+    if (rect.origin.x < rightX &&
+       rect.origin.x + rect.size.width > self.borderSize.width &&
+       rect.origin.y < bottomY &&
+       rect.origin.y + rect.size.height > self.borderSize.height) {
+       nsctx = [NSGraphicsContext currentContext];
+       ctx = [nsctx graphicsPort];
+       screenFont = [self.angbandViewFont screenFont];
+       [screenFont set];
+       blank = [TerminalContents getBlankChar];
+       if (use_graphics) {
+           graf_width = current_graphics_mode->cell_width;
+           graf_height = current_graphics_mode->cell_height;
+           overdraw_row = current_graphics_mode->overdrawRow;
+           overdraw_max = current_graphics_mode->overdrawMax;
+       }
     }
-}
-
 
-- (void)removeAngbandView:(AngbandView *)view
-{
-    if ([self.angbandViews containsObject:view])
-    {
-        [self.angbandViews removeObject:view];
-        [self updateImage];
-        [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
-        if ([self.angbandViews count]) [self requestRedraw];
+    /*
+     * With double height tiles, need to have rendered prior rows (i.e.
+     * smaller y) before the current one.  Since the invalid rectanges are
+     * processed in order, ensure that by sorting the invalid rectangles in
+     * increasing order of y origin (AppKit guarantees the invalid rectanges
+     * are non-overlapping).
+     */
+    NSRect* sortedRects = 0;
+    const NSRect* workingRects;
+    if (overdraw_row && invalidCount > 1) {
+       sortedRects = malloc(invalidCount * sizeof(NSRect));
+       if (sortedRects == 0) {
+           NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
+                                           reason:@"sorted rects in drawRect"
+                                           userInfo:nil];
+           @throw exc;
+       }
+       (void) memcpy(
+           sortedRects, invalidRects, invalidCount * sizeof(NSRect));
+       qsort(sortedRects, invalidCount, sizeof(NSRect),
+             compare_nsrect_yorigin_greater);
+       workingRects = sortedRects;
+    } else {
+       workingRects = invalidRects;
     }
-}
 
+    /*
+     * Use -2 for unknown.  Use -1 for Cocoa's blackColor.  All others are the
+     * Angband color index.
+     */
+    int alast = -2;
+    int redrawCursor = 0;
+
+    for (NSInteger irect = 0; irect < invalidCount; ++irect) {
+       NSRect modRect, clearRect;
+       CGFloat edge;
+       int iRowFirst, iRowLast;
+       int iColFirst, iColLast;
+
+       /* Handle the top border. */
+       if (workingRects[irect].origin.y < self.borderSize.height) {
+           edge =
+               workingRects[irect].origin.y + workingRects[irect].size.height;
+           if (edge <= self.borderSize.height) {
+               if (alast != -1) {
+                   [[NSColor blackColor] set];
+                   alast = -1;
+               }
+               NSRectFill(workingRects[irect]);
+               continue;
+           }
+           clearRect = workingRects[irect];
+           clearRect.size.height =
+               self.borderSize.height - workingRects[irect].origin.y;
+           if (alast != -1) {
+               [[NSColor blackColor] set];
+               alast = -1;
+           }
+           NSRectFill(clearRect);
+           modRect.origin.x = workingRects[irect].origin.x;
+           modRect.origin.y = self.borderSize.height;
+           modRect.size.width = workingRects[irect].size.width;
+           modRect.size.height = edge - self.borderSize.height;
+       } else {
+           modRect = workingRects[irect];
+       }
 
-- (NSWindow *)makePrimaryWindow
-{
-    if (! self.primaryWindow)
-    {
-        /* This has to be done after the font is set, which it already is in
-                * term_init_cocoa() */
-        NSSize sz = self.baseSize;
-        NSRect contentRect = NSMakeRect( 0.0, 0.0, sz.width, sz.height );
+       /* Handle the left border. */
+       if (modRect.origin.x < self.borderSize.width) {
+           edge = modRect.origin.x + modRect.size.width;
+           if (edge <= self.borderSize.width) {
+               if (alast != -1) {
+                   alast = -1;
+                   [[NSColor blackColor] set];
+               }
+               NSRectFill(modRect);
+               continue;
+           }
+           clearRect = modRect;
+           clearRect.size.width = self.borderSize.width - clearRect.origin.x;
+           if (alast != -1) {
+               alast = -1;
+               [[NSColor blackColor] set];
+           }
+           NSRectFill(clearRect);
+           modRect.origin.x = self.borderSize.width;
+           modRect.size.width = edge - self.borderSize.width;
+       }
 
-        NSUInteger styleMask = NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask;
+       iRowFirst = floor((modRect.origin.y - self.borderSize.height) /
+                         self.tileSize.height);
+       iColFirst = floor((modRect.origin.x - self.borderSize.width) /
+                         self.tileSize.width);
+       edge = modRect.origin.y + modRect.size.height;
+       if (edge <= bottomY) {
+           iRowLast =
+               ceil((edge - self.borderSize.height) / self.tileSize.height);
+       } else {
+           iRowLast = self.rows;
+       }
+       edge = modRect.origin.x + modRect.size.width;
+       if (edge <= rightX) {
+           iColLast =
+               ceil((edge - self.borderSize.width) / self.tileSize.width);
+       } else {
+           iColLast = self.cols;
+       }
 
-        /*
-        * Make every window other than the main window closable, also create
-        * them as utility panels to get the thinner title bar and other
-        * attributes that already match up with how those windows are used.
-        */
-        if ((__bridge AngbandContext*) (angband_term[0]->data) != self)
-        {
-           self.primaryWindow =
-               [[NSPanel alloc] initWithContentRect:contentRect
-                                styleMask:(styleMask | NSClosableWindowMask |
-                                           NSUtilityWindowMask)
-                                backing:NSBackingStoreBuffered defer:YES];
-        } else {
-           self.primaryWindow =
-               [[NSWindow alloc] initWithContentRect:contentRect
-                                 styleMask:styleMask
-                                 backing:NSBackingStoreBuffered defer:YES];
+       if (self.contents.cursorColumn != -1 &&
+           self.contents.cursorRow != -1 &&
+           self.contents.cursorColumn + self.contents.cursorWidth - 1 >=
+           iColFirst &&
+           self.contents.cursorColumn < iColLast &&
+           self.contents.cursorRow + self.contents.cursorHeight - 1 >=
+           iRowFirst &&
+           self.contents.cursorRow < iRowLast) {
+           redrawCursor = 1;
        }
 
+       for (int irow = iRowFirst; irow < iRowLast; ++irow) {
+           int icol =
+               [self.contents scanForTypeMaskInRow:irow
+                    mask:(TERM_CELL_CHAR | TERM_CELL_TILE)
+                    col0:iColFirst col1:iColLast];
 
-        /* Not to be released when closed */
-        [self.primaryWindow setReleasedWhenClosed:NO];
-        [self.primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
+           while (1) {
+               if (icol >= iColLast) {
+                   break;
+               }
 
-        /* Make the view */
-        AngbandView *angbandView = [[AngbandView alloc] initWithFrame:contentRect];
-        [angbandView setAngbandContext:self];
-        [self.angbandViews addObject:angbandView];
-        [self.primaryWindow setContentView:angbandView];
+               if ([self.contents getCellAtColumn:icol row:irow]->form ==
+                   TERM_CELL_TILE) {
+                   /*
+                    * It is a tile.  Identify how far the run of tiles goes.
+                    */
+                   int jcol = [self.contents scanForPredicateInRow:irow
+                                   predicate:isTileTop desired:1
+                                   col0:(icol + 1) col1:iColLast];
+
+                   [self renderTileRunInRow:irow col0:icol col1:jcol
+                         nsctx:nsctx ctx:ctx
+                         grafWidth:graf_width grafHeight:graf_height
+                         overdrawRow:overdraw_row overdrawMax:overdraw_max];
+                   icol = jcol;
+               } else {
+                   /*
+                    * It is a character.  Identify how far the run of
+                    * characters goes.
+                    */
+                   int jcol = [self.contents scanForPredicateInRow:irow
+                                   predicate:isCharNoPartial desired:1
+                                   col0:(icol + 1) col1:iColLast];
+                   int jcol2;
+
+                   if (jcol < iColLast &&
+                       isPartiallyOverwrittenBigChar(
+                           [self.contents getCellAtColumn:jcol row:irow])) {
+                       jcol2 = [self.contents scanForTypeMaskInRow:irow
+                                    mask:~TERM_CELL_CHAR_PADDING
+                                    col0:(jcol + 1) col1:iColLast];
+                   } else {
+                       jcol2 = jcol;
+                   }
 
-        /* We are its delegate */
-        [self.primaryWindow setDelegate:self];
+                   /*
+                    * Set up clipping rectangle for text.  Save the
+                    * graphics context so the clipping rectangle can be
+                    * forgotten.  Use CGContextBeginPath to clear the current
+                    * path so it does not affect clipping.  Do not call
+                    * CGContextSetTextDrawingMode() to include clipping since
+                    * that does not appear to necessary on 10.14 and is
+                    * actually detrimental:  when displaying more than one
+                    * character, only the first is visible.
+                    */
+                   CGContextSaveGState(ctx);
+                   CGContextBeginPath(ctx);
+                   NSRect r = [self viewRectForCellBlockAtX:icol y:irow
+                                    width:(jcol2 - icol) height:1];
+                   CGContextClipToRect(ctx, r);
 
-        /* Update our image, since this is probably the first angband view
-                * we've gotten. */
-        [self updateImage];
-    }
-    return self.primaryWindow;
-}
+                   /*
+                    * See if the region to be rendered needs to be expanded:
+                    * adjacent text that could influence what's in the clipped
+                    * region.
+                    */
+                   int isrch = icol;
+                   int irng = icol - self.nColPost;
+                   if (irng < 1) {
+                       irng = 1;
+                   }
 
+                   while (1) {
+                       if (isrch <= irng) {
+                           break;
+                       }
 
+                       const struct TerminalCell *pcell2 =
+                           [self.contents getCellAtColumn:(isrch - 1)
+                                row:irow];
+                       if (pcell2->form == TERM_CELL_CHAR) {
+                           --isrch;
+                           if (pcell2->v.ch.glyph != blank) {
+                               icol = isrch;
+                           }
+                       } else if (pcell2->form == TERM_CELL_CHAR_PADDING) {
+                           /*
+                            * Only extend the rendering if this is padding
+                            * for a character that hasn't been partially
+                            * overwritten.
+                            */
+                           if (! isPartiallyOverwrittenBigChar(pcell2)) {
+                               if (isrch - pcell2->v.pd.hoff >= 0) {
+                                   const struct TerminalCell* pcell3 =
+                                       [self.contents
+                                            getCellAtColumn:(isrch - pcell2->v.pd.hoff)
+                                            row:irow];
+
+                                   if (pcell3->v.ch.glyph != blank) {
+                                       icol = isrch - pcell2->v.pd.hoff;
+                                       isrch = icol - 1;
+                                   } else {
+                                       isrch = isrch - pcell2->v.pd.hoff - 1;
+                                   }
+                               } else {
+                                   /* Should not happen, corrupt offset. */
+                                   --isrch;
+                               }
+                           } else {
+                               break;
+                           }
+                       } else {
+                           /*
+                            * Tiles or tile padding block anything before
+                            * them from rendering after them.
+                            */
+                           break;
+                       }
+                   }
 
-#pragma mark View/Window Passthrough
+                   isrch = jcol2;
+                   irng = jcol2 + self.nColPre;
+                   if (irng > self.cols) {
+                       irng = self.cols;
+                   }
+                   while (1) {
+                       if (isrch >= irng) {
+                           break;
+                       }
 
-/**
- * This is what our views call to get us to draw to the window
- */
-- (void)drawRect:(NSRect)rect inView:(NSView *)view
-{
-    /* Take this opportunity to throttle so we don't flush faster than desired.
-        */
-    BOOL viewInLiveResize = [view inLiveResize];
-    if (! viewInLiveResize) [self throttle];
+                       const struct TerminalCell *pcell2 =
+                           [self.contents getCellAtColumn:isrch row:irow];
+                       if (pcell2->form == TERM_CELL_CHAR) {
+                           if (pcell2->v.ch.glyph != blank) {
+                               jcol2 = isrch;
+                           }
+                           ++isrch;
+                       } else if (pcell2->form == TERM_CELL_CHAR_PADDING) {
+                           ++isrch;
+                       } else {
+                           break;
+                       }
+                   }
+
+                   /* Render text. */
+                   /* Clear where rendering will be done. */
+                   if (alast != -1) {
+                       [[NSColor blackColor] set];
+                       alast = -1;
+                   }
+                   r = [self viewRectForCellBlockAtX:icol y:irow
+                             width:(jcol - icol) height:1];
+                   NSRectFill(r);
+
+                   while (icol < jcol) {
+                       const struct TerminalCell *pcell =
+                           [self.contents getCellAtColumn:icol row:irow];
+
+                       /*
+                        * For blanks, clearing was all that was necessary.
+                        * Don't redraw them.
+                        */
+                       if (pcell->v.ch.glyph != blank) {
+                           int a = pcell->v.ch.attr % MAX_COLORS;
+
+                           if (alast != a) {
+                               alast = a;
+                               set_color_for_index(a);
+                           }
+                           r = [self viewRectForCellBlockAtX:icol
+                                     y:irow width:pcell->hscl
+                                     height:1];
+                           [self drawWChar:pcell->v.ch.glyph inRect:r
+                                 screenFont:screenFont context:ctx];
+                       }
+                       icol += pcell->hscl;
+                   }
+
+                   /*
+                    * Forget the clipping rectangle.  As a side effect, lose
+                    * the color.
+                    */
+                   CGContextRestoreGState(ctx);
+                   alast = -2;
+               }
+               icol =
+                   [self.contents scanForTypeMaskInRow:irow
+                        mask:(TERM_CELL_CHAR | TERM_CELL_TILE)
+                        col0:icol col1:iColLast];
+           }
+       }
+
+       /* Handle the right border. */
+       edge = modRect.origin.x + modRect.size.width;
+       if (edge > rightX) {
+           if (modRect.origin.x >= rightX) {
+               if (alast != -1) {
+                   alast = -1;
+                   [[NSColor blackColor] set];
+               }
+               NSRectFill(modRect);
+               continue;
+           }
+           clearRect = modRect;
+           clearRect.origin.x = rightX;
+           clearRect.size.width = edge - rightX;
+           if (alast != -1) {
+               alast = -1;
+               [[NSColor blackColor] set];
+           }
+           NSRectFill(clearRect);
+           modRect.size.width = edge - modRect.origin.x;
+       }
+
+       /* Handle the bottom border. */
+       edge = modRect.origin.y + modRect.size.height;
+       if (edge > bottomY) {
+           if (modRect.origin.y < bottomY) {
+               modRect.origin.y = bottomY;
+               modRect.size.height = edge - bottomY;
+           }
+           if (alast != -1) {
+               alast = -1;
+               [[NSColor blackColor] set];
+           }
+           NSRectFill(modRect);
+       }
+    }
 
-    /* With a GLayer, use CGContextDrawLayerInRect */
-    CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
-    NSRect bounds = [view bounds];
-    if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationLow);
-    CGContextSetBlendMode(context, kCGBlendModeCopy);
-    CGContextDrawLayerInRect(context, *(CGRect *)&bounds, self.angbandLayer);
-    if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
+    if (redrawCursor) {
+       NSRect r = [self viewRectForCellBlockAtX:self.contents.cursorColumn
+                        y:self.contents.cursorRow
+                        width:self.contents.cursorWidth
+                        height:self.contents.cursorHeight];
+       [[NSColor yellowColor] set];
+       NSFrameRectWithWidth(r, 1);
+    }
+
+    free(sortedRects);
 }
 
 - (BOOL)isOrderedIn
 {
-    return [[[self.angbandViews lastObject] window] isVisible];
+    return [[self->angbandView window] isVisible];
 }
 
 - (BOOL)isMainWindow
 {
-    return [[[self.angbandViews lastObject] window] isMainWindow];
+    return [[self->angbandView window] isMainWindow];
+}
+
+- (BOOL)isKeyWindow
+{
+    return [[self->angbandView window] isKeyWindow];
 }
 
 - (void)setNeedsDisplay:(BOOL)val
 {
-    for (NSView *angbandView in self.angbandViews)
-    {
-        [angbandView setNeedsDisplay:val];
-    }
+    [self->angbandView setNeedsDisplay:val];
 }
 
-- (void)setNeedsDisplayInBaseRect:(NSRect)rect
+- (void)setNeedsDisplayInRect:(NSRect)rect
 {
-    for (NSView *angbandView in self.angbandViews)
-    {
-        [angbandView setNeedsDisplayInRect: rect];
-    }
+    [self->angbandView setNeedsDisplayInRect:rect];
 }
 
 - (void)displayIfNeeded
 {
-    [[self activeView] displayIfNeeded];
+    [self->angbandView displayIfNeeded];
 }
 
 - (int)terminalIndex
@@ -2120,14 +3721,12 @@ static __strong NSFont* gDefaultFont = nil;
     CGFloat newRows = floor(
        (contentRect.size.height - (self.borderSize.height * 2.0)) /
        self.tileSize.height);
-    CGFloat newColumns = ceil(
+    CGFloat newColumns = floor(
        (contentRect.size.width - (self.borderSize.width * 2.0)) /
        self.tileSize.width);
 
     if (newRows < 1 || newColumns < 1) return;
-    self->_cols = newColumns;
-    self->_rows = newRows;
-    [self.changes resize:self.cols rows:self.rows];
+    [self resizeWithColumns:newColumns rows:newRows];
 
     int termIndex = [self terminalIndex];
     [self setDefaultTitle:termIndex];
@@ -2158,7 +3757,7 @@ static __strong NSFont* gDefaultFont = nil;
     Term_activate( old );
 }
 
-- (void)setMinimumWindowSize:(int)termIdx
+- (void)constrainWindowSize:(int)termIdx
 {
     NSSize minsize;
 
@@ -2174,6 +3773,7 @@ static __strong NSFont* gDefaultFont = nil;
     minsize.height =
         minsize.height * self.tileSize.height + self.borderSize.height * 2.0;
     [[self makePrimaryWindow] setContentMinSize:minsize];
+    self.primaryWindow.contentResizeIncrements = self.tileSize;
 }
 
 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
@@ -2300,7 +3900,13 @@ static __strong NSFont* gDefaultFont = nil;
 
 - (void)windowWillClose: (NSNotification *)notification
 {
+    /*
+     * If closing only because the application is terminating, don't update
+     * the visible state for when the application is relaunched.
+     */
+    if (! quit_when_ready) {
        [self saveWindowVisibleToDefaults: NO];
+    }
 }
 
 @end
@@ -2318,46 +3924,131 @@ static __strong NSFont* gDefaultFont = nil;
     return YES;
 }
 
-- (void)drawRect:(NSRect)rect
-{
-    if (! angbandContext)
-    {
+- (void)drawRect:(NSRect)rect
+{
+    if ([self inLiveResize]) {
+       /*
+        * Always anchor the cached area to the upper left corner of the view.
+        * Any parts on the right or bottom that can't be drawn from the cached
+        * area are simply cleared.  Will fill them with appropriate content
+        * when resizing is done.
+        */
+       const NSRect *rects;
+       NSInteger count;
+
+       [self getRectsBeingDrawn:&rects count:&count];
+       if (count > 0) {
+           NSRect viewRect = [self visibleRect];
+
+           [[NSColor blackColor] set];
+           while (count-- > 0) {
+               CGFloat drawTop = rects[count].origin.y - viewRect.origin.y;
+               CGFloat drawBottom = drawTop + rects[count].size.height;
+               CGFloat drawLeft = rects[count].origin.x - viewRect.origin.x;
+               CGFloat drawRight = drawLeft + rects[count].size.width;
+               /*
+                * modRect and clrRect, like rects[count], are in the view
+                * coordinates with y flipped.  cacheRect is in the bitmap
+                * coordinates and y is not flipped.
+                */
+               NSRect modRect, clrRect, cacheRect;
+
+               /*
+                * Clip by bottom edge of cached area.  Clear what's below
+                * that.
+                */
+               if (drawTop >= self->cacheBounds.size.height) {
+                   NSRectFill(rects[count]);
+                   continue;
+               }
+               modRect.origin.x = rects[count].origin.x;
+               modRect.origin.y = rects[count].origin.y;
+               modRect.size.width = rects[count].size.width;
+               cacheRect.origin.y = drawTop;
+               if (drawBottom > self->cacheBounds.size.height) {
+                   CGFloat excess =
+                       drawBottom - self->cacheBounds.size.height;
+
+                   modRect.size.height = rects[count].size.height - excess;
+                   cacheRect.origin.y = 0;
+                   clrRect.origin.x = modRect.origin.x;
+                   clrRect.origin.y = modRect.origin.y + modRect.size.height;
+                   clrRect.size.width = modRect.size.width;
+                   clrRect.size.height = excess;
+                   NSRectFill(clrRect);
+               } else {
+                   modRect.size.height = rects[count].size.height;
+                   cacheRect.origin.y = self->cacheBounds.size.height -
+                       rects[count].size.height;
+               }
+               cacheRect.size.height = modRect.size.height;
+
+               /*
+                * Clip by right edge of cached area.  Clear what's to the
+                * right of that and copy the remainder from the cache.
+                */
+               if (drawLeft >= self->cacheBounds.size.width) {
+                   NSRectFill(modRect);
+                   continue;
+               }
+               cacheRect.origin.x = drawLeft;
+               if (drawRight > self->cacheBounds.size.width) {
+                   CGFloat excess = drawRight - self->cacheBounds.size.width;
+
+                   modRect.size.width -= excess;
+                   cacheRect.size.width =
+                       self->cacheBounds.size.width - drawLeft;
+                   clrRect.origin.x = modRect.origin.x + modRect.size.width;
+                   clrRect.origin.y = modRect.origin.y;
+                   clrRect.size.width = excess;
+                   clrRect.size.height = modRect.size.height;
+                   NSRectFill(clrRect);
+               } else {
+                   cacheRect.size.width = drawRight - drawLeft;
+               }
+               [self->cacheForResize drawInRect:modRect fromRect:cacheRect
+                    operation:NSCompositeCopy fraction:1.0
+                    respectFlipped:YES hints:nil];
+           }
+       }
+    } else if (! self.angbandContext) {
         /* Draw bright orange, 'cause this ain't right */
         [[NSColor orangeColor] set];
         NSRectFill([self bounds]);
-    }
-    else
-    {
+    } else {
         /* Tell the Angband context to draw into us */
-        [angbandContext drawRect:rect inView:self];
+        [self.angbandContext drawRect:rect inView:self];
     }
 }
 
-- (void)setAngbandContext:(AngbandContext *)context
-{
-    angbandContext = context;
-}
-
-- (AngbandContext *)angbandContext
-{
-    return angbandContext;
-}
-
-- (void)setFrameSize:(NSSize)size
-{
-    BOOL changed = ! NSEqualSizes(size, [self frame].size);
-    [super setFrameSize:size];
-    if (changed) [angbandContext angbandViewDidScale:self];
-}
-
+/**
+ * Override NSView's method to set up a cache that's used in drawRect to
+ * handle drawing during a resize.
+ */
 - (void)viewWillStartLiveResize
 {
-    [angbandContext viewWillStartLiveResize:self];
+    [super viewWillStartLiveResize];
+    self->cacheBounds = [self visibleRect];
+    self->cacheForResize =
+       [self bitmapImageRepForCachingDisplayInRect:self->cacheBounds];
+    if (self->cacheForResize != nil) {
+       [self cacheDisplayInRect:self->cacheBounds
+             toBitmapImageRep:self->cacheForResize];
+    } else {
+       self->cacheBounds.size.width = 0.;
+       self->cacheBounds.size.height = 0.;
+    }
 }
 
+/**
+ * Override NSView's method to release the cache set up in
+ * viewWillStartLiveResize.
+ */
 - (void)viewDidEndLiveResize
 {
-    [angbandContext viewDidEndLiveResize:self];
+    [super viewDidEndLiveResize];
+    self->cacheForResize = nil;
+    [self setNeedsDisplay:YES];
 }
 
 @end
@@ -2503,16 +4194,14 @@ static void Term_init_cocoa(term *t)
            if (defaultColumns > 0) columns = defaultColumns;
        }
 
-       context.cols = columns;
-       context.rows = rows;
-       [context.changes resize:columns rows:rows];
+       [context resizeWithColumns:columns rows:rows];
 
        /* Get the window */
        NSWindow *window = [context makePrimaryWindow];
 
        /* Set its title and, for auxiliary terms, tentative size */
        [context setDefaultTitle:termIdx];
-       [context setMinimumWindowSize:termIdx];
+       [context constrainWindowSize:termIdx];
 
        /*
         * If this is the first term, and we support full screen (Mac OS X Lion
@@ -2547,10 +4236,9 @@ static void Term_init_cocoa(term *t)
                 * This is a bit of a trick to allow us to display multiple
                 * windows in the "standard default" window position in OS X:
                 * the upper center of the screen.  The term sizes set in
-                * AngbandAppDelegate's loadPrefs() are based on a 5-wide by
-                * 3-high grid, with the main term being 4/5 wide by 2/3 high
-                * (hence the scaling to find what the containing rect would
-                * be).
+                * load_prefs() are based on a 5-wide by 3-high grid, with the
+                * main term being 4/5 wide by 2/3 high (hence the scaling to
+                * find what the containing rect would be).
                 */
                NSRect originalMainTermFrame = [window frame];
                NSRect scaledFrame = originalMainTermFrame;
@@ -2665,8 +4353,8 @@ static void Term_nuke_cocoa(term *t)
 }
 
 /**
- * Returns the CGImageRef corresponding to an image with the given name in the
- * resource directory, transferring ownership to the caller
+ * Returns the CGImageRef corresponding to an image with the given path.
+ * Transfers ownership to the caller.
  */
 static CGImageRef create_angband_image(NSString *path)
 {
@@ -2682,16 +4370,20 @@ static CGImageRef create_angband_image(NSString *path)
             CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
             if (source)
             {
-                /* We really want the largest image, but in practice there's
-                                * only going to be one */
+                /*
+                 * We really want the largest image, but in practice there's
+                 * only going to be one
+                 */
                 decodedImage = CGImageSourceCreateImageAtIndex(source, 0, (CFDictionaryRef)options);
                 CFRelease(source);
             }
         }
     }
     
-    /* Draw the sucker to defeat ImageIO's weird desire to cache and decode on
-        * demand. Our images aren't that big! */
+    /*
+     * Draw the sucker to defeat ImageIO's weird desire to cache and decode on
+     * demand. Our images aren't that big!
+     */
     if (decodedImage)
     {
         size_t width = CGImageGetWidth(decodedImage), height = CGImageGetHeight(decodedImage);
@@ -2706,645 +4398,158 @@ static CGImageRef create_angband_image(NSString *path)
             case kCGImageAlphaNoneSkipFirst:
                 /* No alpha */
                 contextBitmapInfo |= kCGImageAlphaNone;
-                break;
-            default:
-                /* Some alpha, use premultiplied last which is most efficient. */
-                contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
-                break;
-        }
-
-        /* Draw the source image flipped, since the view is flipped */
-        CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(decodedImage), CGImageGetBytesPerRow(decodedImage), CGImageGetColorSpace(decodedImage), contextBitmapInfo);
-       if (ctx) {
-           CGContextSetBlendMode(ctx, kCGBlendModeCopy);
-           CGContextTranslateCTM(ctx, 0.0, height);
-           CGContextScaleCTM(ctx, 1.0, -1.0);
-           CGContextDrawImage(
-               ctx, CGRectMake(0, 0, width, height), decodedImage);
-           result = CGBitmapContextCreateImage(ctx);
-           CFRelease(ctx);
-       }
-
-        CGImageRelease(decodedImage);
-    }
-    return result;
-}
-
-/**
- * React to changes
- */
-static errr Term_xtra_cocoa_react(void)
-{
-    /* Don't actually switch graphics until the game is running */
-    if (!initialized || !game_in_progress) return (-1);
-
-    @autoreleasepool {
-       AngbandContext *angbandContext =
-           (__bridge AngbandContext*) (Term->data);
-
-       /* Handle graphics */
-       int expected_graf_mode = (current_graphics_mode) ?
-           current_graphics_mode->grafID : GRAPHICS_NONE;
-       if (graf_mode_req != expected_graf_mode)
-       {
-           graphics_mode *new_mode;
-           if (graf_mode_req != GRAPHICS_NONE) {
-               new_mode = get_graphics_mode(graf_mode_req);
-           } else {
-               new_mode = NULL;
-           }
-
-           /* Get rid of the old image. CGImageRelease is NULL-safe. */
-           CGImageRelease(pict_image);
-           pict_image = NULL;
-
-           /* Try creating the image if we want one */
-           if (new_mode != NULL)
-           {
-               NSString *img_path =
-                   [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
-               pict_image = create_angband_image(img_path);
-
-               /* If we failed to create the image, revert to ASCII. */
-               if (! pict_image) {
-                   new_mode = NULL;
-                   if (use_bigtile) {
-                       arg_bigtile = FALSE;
-                   }
-                   [[NSUserDefaults angbandDefaults]
-                       setInteger:GRAPHICS_NONE
-                       forKey:AngbandGraphicsDefaultsKey];
-
-                   NSString *msg = NSLocalizedStringWithDefaultValue(
-                       @"Error.TileSetLoadFailed",
-                       AngbandMessageCatalog,
-                       [NSBundle mainBundle],
-                       @"Failed to Load Tile Set",
-                       @"Alert text for failed tile set load");
-                   NSString *info = NSLocalizedStringWithDefaultValue(
-                       @"Error.TileSetRevertToASCII",
-                       AngbandMessageCatalog,
-                       [NSBundle mainBundle],
-                       @"Could not load the tile set.  Switched back to ASCII.",
-                       @"Alert informative message for failed tile set load");
-                   NSAlert *alert = [[NSAlert alloc] init];
-
-                   alert.messageText = msg;
-                   alert.informativeText = info;
-                   [alert runModal];
-               }
-           }
-
-           /* Record what we did */
-           use_graphics = new_mode ? new_mode->grafID : 0;
-           ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
-           current_graphics_mode = new_mode;
-
-           /*
-            * Enable or disable higher picts. Note: this should be done for
-            * all terms.
-            */
-           angbandContext->terminal->higher_pict = !! use_graphics;
-
-           if (pict_image && current_graphics_mode)
-           {
-               /*
-                * Compute the row and column count via the image height and
-                * width.
-                */
-               pict_rows = (int)(CGImageGetHeight(pict_image) /
-                                 current_graphics_mode->cell_height);
-               pict_cols = (int)(CGImageGetWidth(pict_image) /
-                                 current_graphics_mode->cell_width);
-           }
-           else
-           {
-               pict_rows = 0;
-               pict_cols = 0;
-           }
-
-           /* Reset visuals */
-           if (arg_bigtile == use_bigtile && character_generated)
-           {
-               reset_visuals();
-           }
-       }
-
-       if (arg_bigtile != use_bigtile) {
-           if (character_generated)
-           {
-               /* Reset visuals */
-               reset_visuals();
-           }
-
-           Term_activate(angband_term[0]);
-           Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
-       }
-    }
-
-    /* Success */
-    return (0);
-}
-
-
-/**
- * Draws one tile as a helper function for Term_xtra_cocoa_fresh().
- */
-static void draw_image_tile(
-    NSGraphicsContext* nsContext,
-    CGContextRef cgContext,
-    CGImageRef image,
-    NSRect srcRect,
-    NSRect dstRect,
-    NSCompositingOperation op)
-{
-    /* Flip the source rect since the source image is flipped */
-    CGAffineTransform flip = CGAffineTransformIdentity;
-    flip = CGAffineTransformTranslate(flip, 0.0, CGImageGetHeight(image));
-    flip = CGAffineTransformScale(flip, 1.0, -1.0);
-    CGRect flippedSourceRect =
-       CGRectApplyAffineTransform(NSRectToCGRect(srcRect), flip);
-
-    /*
-     * When we use high-quality resampling to draw a tile, pixels from outside
-     * the tile may bleed in, causing graphics artifacts. Work around that.
-     */
-    CGImageRef subimage =
-       CGImageCreateWithImageInRect(image, flippedSourceRect);
-    [nsContext setCompositingOperation:op];
-    CGContextDrawImage(cgContext, NSRectToCGRect(dstRect), subimage);
-    CGImageRelease(subimage);
-}
-
-
-/**
- * This is a helper function for Term_xtra_cocoa_fresh():  look before a block
- * of text on a row to see if the bounds for rendering and clipping need to be
- * extended.
- */
-static void query_before_text(
-    PendingTermChanges *tc, int iy, int npre, int* pclip, int* prend)
-{
-    int start = *prend;
-    int i = start - 1;
-
-    while (1) {
-       if (i < 0 || i < start - npre) {
-           break;
-       }
-       enum PendingCellChangeType ctype = [tc getCellChangeType:i row:iy];
-
-       if (ctype == CELL_CHANGE_TILE) {
-           /*
-            * The cell has been rendered with a tile.  Do not want to modify
-            * its contents so the clipping and rendering region can not be
-            * extended.
-            */
-           break;
-       } else if (ctype == CELL_CHANGE_NONE) {
-           /*
-            * It has not changed (or using big tile mode and it is within
-            * a changed tile but is not the left cell for that tile) so
-            * inquire what it is.
-            */
-           TERM_COLOR a[2];
-           char c[2];
-
-           Term_what(i, iy, a + 1, c + 1);
-           if (use_graphics && (a[1] & 0x80) && (c[1] & 0x80)) {
-               /*
-                * It is an unchanged location rendered with a tile.  Do not
-                * want to modify its contents so the clipping and rendering
-                * region can not be extended.
-                */
-               break;
-           }
-           if (use_bigtile && i > 0) {
-               Term_what(i - 1, iy, a, c);
-               if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
-                   /*
-                    * It is the right cell of a location rendered with a tile.
-                    * Do not want to modify its contents so the clipping and
-                    * rendering region can not be exteded.
-                    */
-                   break;
-               }
-           }
-           /*
-            * It is unchanged text.  A character from the changed region
-            * may have extended into it so render it to clear that.
-            */
-#ifdef JP
-           /* Check to see if it is the second part of a kanji character. */
-           if (i > 0) {
-               Term_what(i - 1, iy, a, c);
-               if (iskanji(c)) {
-                   [tc markTextChange:i-1 row:iy
-                       glyph:convert_two_byte_eucjp_to_utf16_native(c)
-                       color:a[0] isDoubleWidth:YES];
-                   *pclip = i - 1;
-                   *prend = i - 1;
-                   --i;
-               } else {
-                   [tc markTextChange:i row:iy
-                       glyph:c[1] color:a[1] isDoubleWidth:NO];
-                   *pclip = i;
-                   *prend = i;
-               }
-           } else {
-               [tc markTextChange:i row:iy
-                   glyph:c[1] color:a[1] isDoubleWidth:NO];
-               *pclip = i;
-               *prend = i;
-           }
-#else
-           [tc markTextChange:i row:iy
-               glyph:c[1] color:a[1] isDoubleWidth:NO];
-           *pclip = i;
-           *prend = i;
-#endif
-           --i;
-       } else {
-           /*
-            * The cell has been wiped or had changed text rendered.  Do
-            * not need to render.  Can extend the clipping rectangle into it.
-            */
-           *pclip = i;
-           --i;
-       }
-    }
-}
-
-
-/**
- * This is a helper function for Term_xtra_cocoa_fresh():  look after a block
- * of text on a row to see if the bounds for rendering and clipping need to be
- * extended.
- */
-static void query_after_text(
-    PendingTermChanges *tc, int iy, int npost, int* pclip, int* prend)
-{
-    int end = *prend;
-    int i = end + 1;
-    int ncol = tc.columnCount;
-
-    while (1) {
-       if (i >= ncol) {
-           break;
-       }
-
-       enum PendingCellChangeType ctype = [tc getCellChangeType:i row:iy];
-
-       /*
-        * Be willing to consolidate this block with the one after it.  This
-        * logic should be sufficient to avoid redraws of the region between
-        * changed blocks of text if angbandContext.nColPre is zero or one.
-        * For larger values of nColPre, would need to do something more to
-        * avoid extra redraws.
-        */
-       if (i > end + npost && ctype != CELL_CHANGE_TEXT &&
-           ctype != CELL_CHANGE_WIPE) {
-           break;
-       }
-
-       if (ctype == CELL_CHANGE_TILE) {
-           /*
-            * The cell has been rendered with a tile.  Do not want to modify
-            * its contents so the clipping and rendering region can not be
-            * extended.
-            */
-           break;
-       } else if (ctype == CELL_CHANGE_NONE) {
-           /* It has not changed so inquire what it is. */
-           TERM_COLOR a[2];
-           char c[2];
+                break;
+            default:
+                /* Some alpha, use premultiplied last which is most efficient. */
+                contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
+                break;
+        }
 
-           Term_what(i, iy, a, c);
-           if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
-               /*
-                * It is an unchanged location rendered with a tile.  Do not
-                * want to modify its contents so the clipping and rendering
-                * region can not be extended.
-                */
-               break;
-           }
-           /*
-            * It is unchanged text.  A character from the changed region
-            * may have extended into it so render it to clear that.
-            */
-#ifdef JP
-           /* Check to see if it is the first part of a kanji character. */
-           if (i < ncol - 1) {
-               Term_what(i + 1, iy, a + 1, c + 1);
-               if (iskanji(c)) {
-                   [tc markTextChange:i row:iy
-                       glyph:convert_two_byte_eucjp_to_utf16_native(c)
-                       color:a[0] isDoubleWidth:YES];
-                   *pclip = i + 1;
-                   *prend = i + 1;
-                   ++i;
-               } else {
-                   [tc markTextChange:i row:iy
-                       glyph:c[0] color:a[0] isDoubleWidth:NO];
-                   *pclip = i;
-                   *prend = i;
-               }
-           } else {
-               [tc markTextChange:i row:iy
-                   glyph:c[0] color:a[0] isDoubleWidth:NO];
-               *pclip = i;
-               *prend = i;
-           }
-#else
-           [tc markTextChange:i row:iy
-               glyph:c[0] color:a[0] isDoubleWidth:NO];
-           *pclip = i;
-           *prend = i;
-#endif
-           ++i;
-       } else {
-           /*
-            * Have come to another region of changed text or another region
-            * to wipe.  Combine the regions to minimize redraws.
-            */
-           *pclip = i;
-           *prend = i;
-           end = i;
-           ++i;
+        /* Draw the source image flipped, since the view is flipped */
+        CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(decodedImage), CGImageGetBytesPerRow(decodedImage), CGImageGetColorSpace(decodedImage), contextBitmapInfo);
+       if (ctx) {
+           CGContextSetBlendMode(ctx, kCGBlendModeCopy);
+           CGContextTranslateCTM(ctx, 0.0, height);
+           CGContextScaleCTM(ctx, 1.0, -1.0);
+           CGContextDrawImage(
+               ctx, CGRectMake(0, 0, width, height), decodedImage);
+           result = CGBitmapContextCreateImage(ctx);
+           CFRelease(ctx);
        }
+
+        CGImageRelease(decodedImage);
     }
+    return result;
 }
 
-
 /**
- * Draw the pending changes saved in angbandContext->changes.
+ * React to changes
  */
-static void Term_xtra_cocoa_fresh(AngbandContext* angbandContext)
+static errr Term_xtra_cocoa_react(void)
 {
-    int graf_width, graf_height, alphablend;
-
-    if (angbandContext.changes.hasTileChanges) {
-       CGImageAlphaInfo ainfo = CGImageGetAlphaInfo(pict_image);
-
-       graf_width = current_graphics_mode->cell_width;
-       graf_height = current_graphics_mode->cell_height;
-       /*
-        * As of this writing, a value of zero for
-        * current_graphics_mode->alphablend can mean either that the tile set
-        * doesn't have an alpha channel or it does but it only takes on values
-        * of 0 or 255.  For main-cocoa.m's purposes, the latter is rendered
-        * using the same procedure as if alphablend was nonzero.  The former
-        * is handled differently, but alphablend doesn't distinguish it from
-        * the latter.  So ignore alphablend and directly test whether an
-        * alpha channel is present.
-        */
-       alphablend = (ainfo & (kCGImageAlphaPremultipliedFirst |
-                              kCGImageAlphaPremultipliedLast)) ? 1 : 0;
-    } else {
-       graf_width = 0;
-       graf_height = 0;
-       alphablend = 0;
-    }
-
-    CGContextRef ctx = [angbandContext lockFocus];
-
-    if (angbandContext.changes.hasTextChanges ||
-       angbandContext.changes.hasWipeChanges) {
-       NSFont *selectionFont = [angbandContext.angbandViewFont screenFont];
-       [selectionFont set];
-    }
-
-    for (int iy = angbandContext.changes.firstChangedRow;
-        iy <= angbandContext.changes.lastChangedRow;
-        ++iy) {
-       /* Skip untouched rows. */
-       if ([angbandContext.changes getFirstChangedColumnInRow:iy] >
-           [angbandContext.changes getLastChangedColumnInRow:iy]) {
-           continue;
-       }
-       int ix = [angbandContext.changes getFirstChangedColumnInRow:iy];
-       int ixmax = [angbandContext.changes getLastChangedColumnInRow:iy];
-
-       while (1) {
-           int jx;
+    /* Don't actually switch graphics until the game is running */
+    if (!initialized || !game_in_progress) return (-1);
 
-           if (ix > ixmax) {
-               break;
+    @autoreleasepool {
+       /* Handle graphics */
+       int expected_graf_mode = (current_graphics_mode) ?
+           current_graphics_mode->grafID : GRAPHICS_NONE;
+       if (graf_mode_req != expected_graf_mode)
+       {
+           graphics_mode *new_mode;
+           if (graf_mode_req != GRAPHICS_NONE) {
+               new_mode = get_graphics_mode(graf_mode_req);
+           } else {
+               new_mode = NULL;
            }
 
-           switch ([angbandContext.changes getCellChangeType:ix row:iy]) {
-           case CELL_CHANGE_NONE:
-               ++ix;
-               break;
+           /* Get rid of the old image. CGImageRelease is NULL-safe. */
+           CGImageRelease(pict_image);
+           pict_image = NULL;
 
-           case CELL_CHANGE_TILE:
-               {
-                   /*
-                    * Because changes are made to the compositing mode, save
-                    * the incoming value.
-                    */
-                   NSGraphicsContext *nsContext =
-                       [NSGraphicsContext currentContext];
-                   NSCompositingOperation op = nsContext.compositingOperation;
-                   int step = (use_bigtile) ? 2 : 1;
-
-                   jx = ix;
-                   while (jx <= ixmax &&
-                          [angbandContext.changes getCellChangeType:jx row:iy]
-                          == CELL_CHANGE_TILE) {
-                       NSRect destinationRect =
-                           [angbandContext rectInImageForTileAtX:jx Y:iy];
-                       struct PendingTileChange tileIndices =
-                           [angbandContext.changes
-                                          getCellTileChange:jx row:iy];
-                       NSRect sourceRect, terrainRect;
-
-                       destinationRect.size.width *= step;
-                       sourceRect.origin.x = graf_width * tileIndices.fgdCol;
-                       sourceRect.origin.y = graf_height * tileIndices.fgdRow;
-                       sourceRect.size.width = graf_width;
-                       sourceRect.size.height = graf_height;
-                       terrainRect.origin.x = graf_width * tileIndices.bckCol;
-                       terrainRect.origin.y = graf_height *
-                           tileIndices.bckRow;
-                       terrainRect.size.width = graf_width;
-                       terrainRect.size.height = graf_height;
-                       if (alphablend) {
-                           draw_image_tile(
-                               nsContext,
-                               ctx,
-                               pict_image,
-                               terrainRect,
-                               destinationRect,
-                               NSCompositeCopy);
-                           /*
-                            * Skip drawing the foreground if it is the same
-                            * as the background.
-                            */
-                           if (sourceRect.origin.x != terrainRect.origin.x ||
-                               sourceRect.origin.y != terrainRect.origin.y) {
-                               draw_image_tile(
-                                   nsContext,
-                                   ctx,
-                                   pict_image,
-                                   sourceRect,
-                                   destinationRect,
-                                   NSCompositeSourceOver);
-                           }
-                       } else {
-                           draw_image_tile(
-                               nsContext,
-                               ctx,
-                               pict_image,
-                               sourceRect,
-                               destinationRect,
-                               NSCompositeCopy);
-                       }
-                       jx += step;
-                   }
+           /* Try creating the image if we want one */
+           if (new_mode != NULL)
+           {
+               NSString *img_path =
+                   [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
+               pict_image = create_angband_image(img_path);
 
-                   [nsContext setCompositingOperation:op];
+               /* If we failed to create the image, revert to ASCII. */
+               if (! pict_image) {
+                   new_mode = NULL;
+                   if (use_bigtile) {
+                       arg_bigtile = FALSE;
+                   }
+                   [[NSUserDefaults angbandDefaults]
+                       setInteger:GRAPHICS_NONE
+                       forKey:AngbandGraphicsDefaultsKey];
 
-                   NSRect rect =
-                       [angbandContext rectInImageForTileAtX:ix Y:iy];
-                   rect.size.width =
-                       angbandContext.tileSize.width * (jx - ix);
-                   [angbandContext setNeedsDisplayInBaseRect:rect];
+                   NSString *msg = NSLocalizedStringWithDefaultValue(
+                       @"Error.TileSetLoadFailed",
+                       AngbandMessageCatalog,
+                       [NSBundle mainBundle],
+                       @"Failed to Load Tile Set",
+                       @"Alert text for failed tile set load");
+                   NSString *info = NSLocalizedStringWithDefaultValue(
+                       @"Error.TileSetRevertToASCII",
+                       AngbandMessageCatalog,
+                       [NSBundle mainBundle],
+                       @"Could not load the tile set.  Switched back to ASCII.",
+                       @"Alert informative message for failed tile set load");
+                   NSAlert *alert = [[NSAlert alloc] init];
+                   alert.messageText = msg;
+                   alert.informativeText = info;
+                   [alert runModal];
                }
-               ix = jx;
-               break;
+           }
 
-           case CELL_CHANGE_WIPE:
-           case CELL_CHANGE_TEXT:
+           if (graphics_are_enabled()) {
                /*
-                * For a wiped region, treat it as if it had text (the only
-                * loss if it was not is some extra work rendering
-                * neighboring unchanged text).
+                * The contents stored in the AngbandContext may have
+                * references to the old tile set.  Out of an abundance
+                * of caution, clear those references in case there's an
+                * attempt to redraw the contents before the core has the
+                * chance to update it via the text_hook, pict_hook, and
+                * wipe_hook.
                 */
-               jx = ix + 1;
-               while (1) {
-                   if (jx >= angbandContext.cols) {
-                       break;
-                   }
-                   enum PendingCellChangeType ctype =
-                       [angbandContext.changes getCellChangeType:jx row:iy];
-                   if (ctype != CELL_CHANGE_TEXT &&
-                       ctype != CELL_CHANGE_WIPE) {
-                       break;
-                   }
-                   ++jx;
-               }
-               {
-                   int isclip = ix;
-                   int ieclip = jx - 1;
-                   int isrend = ix;
-                   int ierend = jx - 1;
-                   int set_color = 1;
-                   TERM_COLOR alast = 0;
-                   NSRect r;
-                   int k;
-
-                   query_before_text(
-                       angbandContext.changes,
-                       iy,
-                       angbandContext.nColPre,
-                       &isclip,
-                       &isrend);
-                   query_after_text(
-                       angbandContext.changes,
-                       iy,
-                       angbandContext.nColPost,
-                       &ieclip,
-                       &ierend
-                   );
-                   ix = ierend + 1;
-
-                   /* Save the state since the clipping will be modified. */
-                   CGContextSaveGState(ctx);
-
-                   /* Clear the area where rendering will be done. */
-                   r = [angbandContext rectInImageForTileAtX:isrend Y:iy];
-                   r.size.width = angbandContext.tileSize.width *
-                       (ierend - isrend + 1);
-                   [[NSColor blackColor] set];
-                   NSRectFill(r);
-
-                   /*
-                    * Clear the current path so it does not affect clipping.
-                    * Then set the clipping rectangle.  Using
-                    * CGContextSetTextDrawingMode() to include clipping does
-                    * not appear to be necessary on 10.14 and is actually
-                    * detrimental:  when displaying more than one character,
-                    * only the first is visible.
-                    */
-                   CGContextBeginPath(ctx);
-                   r = [angbandContext rectInImageForTileAtX:isclip Y:iy];
-                   r.size.width = angbandContext.tileSize.width *
-                       (ieclip - isclip + 1);
-                   CGContextClipToRect(ctx, r);
+               for (int iterm = 0; iterm < ANGBAND_TERM_MAX; ++iterm) {
+                   AngbandContext* aContext =
+                       (__bridge AngbandContext*) (angband_term[iterm]->data);
 
-                   /* Render. */
-                   k = isrend;
-                   while (k <= ierend) {
-                       if ([angbandContext.changes getCellChangeType:k row:iy]
-                           == CELL_CHANGE_WIPE) {
-                           /* Skip over since no rendering is necessary. */
-                           ++k;
-                           continue;
-                       }
+                   [aContext.contents wipeTiles];
+               }
+           }
 
-                       struct PendingTextChange textChange =
-                           [angbandContext.changes getCellTextChange:k
-                                          row:iy];
-                       int anew = textChange.color % MAX_COLORS;
-                       if (set_color || alast != anew) {
-                           set_color = 0;
-                           alast = anew;
-                           set_color_for_index(anew);
-                       }
+           /* Record what we did */
+           use_graphics = new_mode ? new_mode->grafID : 0;
+           ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
+           current_graphics_mode = new_mode;
 
-                       NSRect rectToDraw =
-                           [angbandContext rectInImageForTileAtX:k Y:iy];
-                       if (textChange.doubleWidth) {
-                           rectToDraw.size.width *= 2.0;
-                           [angbandContext drawWChar:textChange.glyph
-                                           inRect:rectToDraw context:ctx];
-                           k += 2;
-                       } else {
-                           [angbandContext drawWChar:textChange.glyph
-                                           inRect:rectToDraw context:ctx];
-                           ++k;
-                       }
-                   }
+           /* Enable or disable higher picts.  */
+           for (int iterm = 0; iterm < ANGBAND_TERM_MAX; ++iterm) {
+               if (angband_term[iterm]) {
+                   angband_term[iterm]->higher_pict = !! use_graphics;
+               }
+           }
 
-                   /*
-                    * Inform the context that the area in the clipping
-                    * rectangle needs to be redisplayed.
-                    */
-                   [angbandContext setNeedsDisplayInBaseRect:r];
+           if (pict_image && current_graphics_mode)
+           {
+               /*
+                * Compute the row and column count via the image height and
+                * width.
+                */
+               pict_rows = (int)(CGImageGetHeight(pict_image) /
+                                 current_graphics_mode->cell_height);
+               pict_cols = (int)(CGImageGetWidth(pict_image) /
+                                 current_graphics_mode->cell_width);
+           }
+           else
+           {
+               pict_rows = 0;
+               pict_cols = 0;
+           }
 
-                   CGContextRestoreGState(ctx);
-               }
-               break;
+           /* Reset visuals */
+           if (arg_bigtile == use_bigtile && character_generated)
+           {
+               reset_visuals();
            }
        }
-    }
 
-    if (angbandContext.changes.cursorColumn >= 0 &&
-       angbandContext.changes.cursorRow >= 0) {
-       NSRect rect = [angbandContext
-                         rectInImageForTileAtX:angbandContext.changes.cursorColumn
-                         Y:angbandContext.changes.cursorRow];
+       if (arg_bigtile != use_bigtile) {
+           if (character_generated)
+           {
+               /* Reset visuals */
+               reset_visuals();
+           }
 
-       rect.size.width *= angbandContext.changes.cursorWidth;
-       rect.size.height *= angbandContext.changes.cursorHeight;
-       [[NSColor yellowColor] set];
-       NSFrameRectWithWidth(rect, 1);
-       /* Invalidate that rect */
-       [angbandContext setNeedsDisplayInBaseRect:rect];
+           Term_activate(angband_term[0]);
+           Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
+       }
     }
 
-    [angbandContext unlockFocus];
+    /* Success */
+    return (0);
 }
 
 
@@ -3405,16 +4610,9 @@ static errr Term_xtra_cocoa(int n, int v)
 
            /* Clear the screen */
         case TERM_XTRA_CLEAR:
-           {
-               [angbandContext lockFocus];
-               [[NSColor blackColor] set];
-               NSRect imageRect = {NSZeroPoint, [angbandContext imageSize]};
-               NSRectFillUsingOperation(imageRect, NSCompositeCopy);
-               [angbandContext unlockFocus];
-               [angbandContext setNeedsDisplay:YES];
-               /* Success */
-               break;
-           }
+           [angbandContext.contents wipe];
+           [angbandContext setNeedsDisplay:YES];
+           break;
 
            /* React to changes */
         case TERM_XTRA_REACT:
@@ -3442,8 +4640,21 @@ static errr Term_xtra_cocoa(int n, int v)
 
            /* Draw the pending changes. */
         case TERM_XTRA_FRESH:
-           Term_xtra_cocoa_fresh(angbandContext);
-           [angbandContext.changes clear];
+           {
+               /*
+                * Check the cursor visibility since the core will tell us
+                * explicitly to draw it, but tells us implicitly to forget it
+                * by simply telling us to redraw a location.
+                */
+               int isVisible = 0;
+
+               Term_get_cursor(&isVisible);
+               if (! isVisible) {
+                   [angbandContext.contents removeCursor];
+               }
+               [angbandContext computeInvalidRects];
+               [angbandContext.changes clear];
+           }
             break;
 
         default:
@@ -3460,7 +4671,14 @@ static errr Term_curs_cocoa(TERM_LEN x, TERM_LEN y)
 {
     AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
 
-    [angbandContext.changes markCursor:x row:y];
+    [angbandContext.contents setCursorAtColumn:x row:y width:1 height:1];
+    /*
+     * Unfortunately, this (and the same logic in Term_bigcurs_cocoa) will
+     * also trigger what's under the cursor to be redrawn as well, even if
+     * it has not changed.  In the current drawing implementation, that
+     * inefficiency seems unavoidable.
+     */
+    [angbandContext.changes markChangedAtColumn:x row:y];
 
     /* Success */
     return 0;
@@ -3475,7 +4693,8 @@ static errr Term_bigcurs_cocoa(TERM_LEN x, TERM_LEN y)
 {
     AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
 
-    [angbandContext.changes markBigCursor:x row:y cellsWide:2 cellsHigh:1];
+    [angbandContext.contents setCursorAtColumn:x row:y width:2 height:1];
+    [angbandContext.changes markChangedBlockAtColumn:x row:y width:2 height:1];
 
     /* Success */
     return 0;
@@ -3490,7 +4709,8 @@ static errr Term_wipe_cocoa(TERM_LEN x, TERM_LEN y, int n)
 {
     AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
 
-    [angbandContext.changes markWipeRange:x row:y n:n];
+    [angbandContext.contents wipeBlockAtColumn:x row:y width:n height:1];
+    [angbandContext.changes markChangedRangeAtColumn:x row:y width:n];
 
     /* Success */
     return 0;
@@ -3500,79 +4720,152 @@ static errr Term_pict_cocoa(TERM_LEN x, TERM_LEN y, int n,
                            TERM_COLOR *ap, concptr cp,
                            const TERM_COLOR *tap, concptr tcp)
 {
-    /* Paranoia: Bail if we don't have a current graphics mode */
-    if (! current_graphics_mode) return -1;
+    /* Paranoia: Bail if graphics aren't enabled */
+    if (! graphics_are_enabled()) return -1;
 
     AngbandContext* angbandContext = (__bridge AngbandContext*) (Term->data);
     int step = (use_bigtile) ? 2 : 1;
 
-    /*
-     * In bigtile mode, it is sufficient that the bounds for the modified
-     * region only encompass the left cell for the region affected by the
-     * tile and that only that cell has to have the details of the changes.
-     */
-    for (int i = x; i < x + n * step; i += step) {
-       TERM_COLOR a = *ap++;
-       char c = *cp++;
-       TERM_COLOR ta = *tap++;
-       char tc = *tcp++;
+    int alphablend;
+    if (use_graphics) {
+       CGImageAlphaInfo ainfo = CGImageGetAlphaInfo(pict_image);
+
+       alphablend = (ainfo & (kCGImageAlphaPremultipliedFirst |
+                              kCGImageAlphaPremultipliedLast)) ? 1 : 0;
+    } else {
+       alphablend = 0;
+    }
 
+    for (int i = x; i < x + n * step; i += step) {
+       TERM_COLOR a = *ap;
+       char c = *cp;
+       TERM_COLOR ta = *tap;
+       char tc = *tcp;
+
+       ap += step;
+       cp += step;
+       tap += step;
+       tcp += step;
        if (use_graphics && (a & 0x80) && (c & 0x80)) {
-           [angbandContext.changes markTileChange:i row:y
-                          foregroundCol:((byte)c & 0x7F) % pict_cols
-                          foregroundRow:((byte)a & 0x7F) % pict_rows
-                          backgroundCol:((byte)tc & 0x7F) % pict_cols
-                          backgroundRow:((byte)ta & 0x7F) % pict_rows];
+           char fgdRow = ((byte)a & 0x7F) % pict_rows;
+           char fgdCol = ((byte)c & 0x7F) % pict_cols;
+           char bckRow, bckCol;
+
+           if (alphablend) {
+               bckRow = ((byte)ta & 0x7F) % pict_rows;
+               bckCol = ((byte)tc & 0x7F) % pict_cols;
+           } else {
+               /*
+                * Not blending so make the background the same as the
+                * the foreground.
+                */
+               bckRow = fgdRow;
+               bckCol = fgdCol;
+           }
+           [angbandContext.contents setTileAtColumn:i row:y
+                          foregroundColumn:fgdCol
+                          foregroundRow:fgdRow
+                          backgroundColumn:bckCol
+                          backgroundRow:bckRow
+                          tileWidth:step
+                          tileHeight:1];
+           [angbandContext.changes markChangedBlockAtColumn:i row:y
+                          width:step height:1];
        }
     }
-
-    /* Success */
-    return (0);
+
+    /* Success */
+    return (0);
+}
+
+/**
+ * Low level graphics.  Assumes valid input.
+ *
+ * Draw several ("n") chars, with an attr, at a given location.
+ */
+static errr Term_text_cocoa(
+    TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr cp)
+{
+    AngbandContext* angbandContext = (__bridge AngbandContext*) (Term->data);
+
+    [angbandContext.contents setUniformAttributeTextRunAtColumn:x
+                  row:y n:n glyphs:cp attribute:a];
+    [angbandContext.changes markChangedRangeAtColumn:x row:y width:n];
+
+    /* Success */
+    return 0;
+}
+
+#if 0
+/* From the Linux mbstowcs(3) man page:
+ *   If dest is NULL, n is ignored, and the conversion  proceeds  as  above,
+ *   except  that  the converted wide characters are not written out to mem‐
+ *   ory, and that no length limit exists.
+ */
+static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
+{
+    int i;
+    int count = 0;
+
+    /* Unicode code point to UTF-8
+     *  0x0000-0x007f:   0xxxxxxx
+     *  0x0080-0x07ff:   110xxxxx 10xxxxxx
+     *  0x0800-0xffff:   1110xxxx 10xxxxxx 10xxxxxx
+     * 0x10000-0x1fffff: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
+     * Note that UTF-16 limits Unicode to 0x10ffff. This code is not
+     * endian-agnostic.
+     */
+    for (i = 0; i < n || dest == NULL; i++) {
+        if ((src[i] & 0x80) == 0) {
+            if (dest != NULL) dest[count] = src[i];
+            if (src[i] == 0) break;
+        } else if ((src[i] & 0xe0) == 0xc0) {
+            if (dest != NULL) dest[count] =
+                            (((unsigned char)src[i] & 0x1f) << 6)|
+                            ((unsigned char)src[i+1] & 0x3f);
+            i++;
+        } else if ((src[i] & 0xf0) == 0xe0) {
+            if (dest != NULL) dest[count] =
+                            (((unsigned char)src[i] & 0x0f) << 12) |
+                            (((unsigned char)src[i+1] & 0x3f) << 6) |
+                            ((unsigned char)src[i+2] & 0x3f);
+            i += 2;
+        } else if ((src[i] & 0xf8) == 0xf0) {
+            if (dest != NULL) dest[count] =
+                            (((unsigned char)src[i] & 0x0f) << 18) |
+                            (((unsigned char)src[i+1] & 0x3f) << 12) |
+                            (((unsigned char)src[i+2] & 0x3f) << 6) |
+                            ((unsigned char)src[i+3] & 0x3f);
+            i += 3;
+        } else {
+            /* Found an invalid multibyte sequence */
+            return (size_t)-1;
+        }
+        count++;
+    }
+    return count;
 }
+#endif
 
 /**
- * Low level graphics.  Assumes valid input.
- *
- * Draw several ("n") chars, with an attr, at a given location.
+ * Handle redrawing for a change to the tile set, tile scaling, or main window
+ * font.  Returns YES if the redrawing was initiated.  Otherwise returns NO.
  */
-static errr Term_text_cocoa(
-    TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr cp)
+static BOOL redraw_for_tiles_or_term0_font(void)
 {
-    AngbandContext* angbandContext = (__bridge AngbandContext*) (Term->data);
-    int i = 0;
-
-    while (i < n) {
-#ifdef JP
-       if (iskanji(*cp)) {
-           if (i == n - 1) {
-               /*
-                * The second byte of the character is past the end.  Ignore
-                * the character.
-                */
-               break;
-           } else {
-               [angbandContext.changes markTextChange:i+x row:y
-                              glyph:convert_two_byte_eucjp_to_utf16_native(cp)
-                              color:a isDoubleWidth:YES];
-               cp += 2;
-               i += 2;
-           }
-       } else {
-           [angbandContext.changes markTextChange:i+x row:y
-                          glyph:*cp color:a isDoubleWidth:NO];
-           ++cp;
-           ++i;
-       }
-#else
-       [angbandContext.changes markTextChange:i+x row:y
-                      glyph:*cp color:a isDoubleWidth:NO];
-       ++cp;
-       ++i;
-#endif
+    /*
+     * In Angband 4.2, do_cmd_redraw() will always clear, but only provides
+     * something to replace the erased content if a character has been
+     * generated.  In Hengband, do_cmd_redraw() isn't safe to call unless a
+     * character has been generated.  Therefore, only call it if a character
+     * has been generated.
+     */
+    if (character_generated) {
+       do_cmd_redraw();
+       wakeup_event_loop();
+       return YES;
     }
-
-    /* Success */
-    return 0;
+    return NO;
 }
 
 /**
@@ -3685,10 +4978,12 @@ static void AngbandHandleEventMouseDown( NSEvent *event )
                /* Coordinates run from (0,0) to (cols-1, rows-1). */
                BOOL mouseInMapSection = (x > 13 && x <= cols - 1 && y > 0  && y <= rows - 2);
 
-               /* If we are displaying a menu, allow clicks anywhere; if we are
-                * displaying the main game interface, only allow clicks in the map
-                * section */
-               if (!displayingMapInterface || (displayingMapInterface && mouseInMapSection))
+               /* If we are displaying a menu, allow clicks anywhere within
+                * the terminal bounds; if we are displaying the main game
+                * interface, only allow clicks in the map section */
+               if ((!displayingMapInterface && x >= 0 && x < cols &&
+                    y >= 0 && y < rows) ||
+                    (displayingMapInterface && mouseInMapSection))
                {
                        /* [event buttonNumber] will return 0 for left click,
                         * 1 for right click, but this is safer */
@@ -3939,7 +5234,7 @@ static NSString* get_lib_directory(void)
 
     if( !libExists || !isDirectory )
     {
-       NSLog( @"Hengband: can't find %@/ in bundle: isDirectory: %d libExists: %d", AngbandDirectoryNameLib, isDirectory, libExists );
+       NSLog( @"%@: can't find %@/ in bundle: isDirectory: %d libExists: %d", @VERSION_NAME, AngbandDirectoryNameLib, isDirectory, libExists );
 
        NSString *msg = NSLocalizedStringWithDefaultValue(
            @"Error.MissingResources",
@@ -3957,7 +5252,6 @@ static NSString* get_lib_directory(void)
            @"Label.Quit", AngbandMessageCatalog, [NSBundle mainBundle],
            @"Quit", @"Quit");
        NSAlert *alert = [[NSAlert alloc] init];
-
        /*
         * Note that NSCriticalAlertStyle was deprecated in 10.10.  The
         * replacement is NSAlertStyleCritical.
@@ -3967,76 +5261,262 @@ static NSString* get_lib_directory(void)
        alert.informativeText = info;
        [alert addButtonWithTitle:quit_label];
        [alert runModal];
-       exit( 0 );
+       exit(0);
     }
 
     return bundleLibPath;
 }
 
-/**
- * Return the path for the directory where Angband should look for its standard
- * user file tree.
- */
-static NSString* get_doc_directory(void)
-{
-       NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
+/**
+ * Return the path for the directory where Angband should look for its standard
+ * user file tree.
+ */
+static NSString* get_doc_directory(void)
+{
+       NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
+
+#if defined(SAFE_DIRECTORY)
+       NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
+       return [documents stringByAppendingPathComponent: versionedDirectory];
+#else
+       return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
+#endif
+}
+
+/**
+ * Adjust directory paths as needed to correct for any differences needed by
+ * Angband.  init_file_paths() currently requires that all paths provided have
+ * a trailing slash and all other platforms honor this.
+ *
+ * \param originalPath The directory path to adjust.
+ * \return A path suitable for Angband or nil if an error occurred.
+ */
+static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath)
+{
+       if ([originalPath length] == 0) {
+               return nil;
+       }
+
+       if (![originalPath hasSuffix: @"/"]) {
+               return [originalPath stringByAppendingString: @"/"];
+       }
+
+       return originalPath;
+}
+
+/**
+ * Give Angband the base paths that should be used for the various directories
+ * it needs. It will create any needed directories.
+ */
+static void prepare_paths_and_directories(void)
+{
+       char libpath[PATH_MAX + 1] = "\0";
+       NSString *libDirectoryPath =
+           AngbandCorrectedDirectoryPath(get_lib_directory());
+       [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
+
+       char basepath[PATH_MAX + 1] = "\0";
+       NSString *angbandDocumentsPath =
+           AngbandCorrectedDirectoryPath(get_doc_directory());
+       [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
+
+       init_file_paths(libpath, basepath);
+       create_needed_dirs();
+}
+
+/**
+ * Create and initialize Angband terminal number "i".
+ */
+static term *term_data_link(int i)
+{
+    NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults]
+                                   valueForKey: AngbandTerminalsDefaultsKey];
+    NSInteger rows = 24;
+    NSInteger columns = 80;
+
+    if (i < (int)[terminalDefaults count]) {
+        NSDictionary *term = [terminalDefaults objectAtIndex:i];
+        rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey]
+                  integerValue];
+        columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey]
+                     integerValue];
+    }
+
+    /* Allocate */
+    term *newterm = ZNEW(term);
+
+    /* Initialize the term */
+    term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
+
+    /* Use a "software" cursor */
+    newterm->soft_cursor = TRUE;
+
+    /* Disable the per-row flush notifications since they are not used. */
+    newterm->never_frosh = TRUE;
+
+    /*
+     * Differentiate between BS/^h, Tab/^i, ... so ^h and ^j work under the
+     * roguelike command set.
+     */
+    /* newterm->complex_input = TRUE; */
+
+    /* Erase with "white space" */
+    newterm->attr_blank = TERM_WHITE;
+    newterm->char_blank = ' ';
+
+    /* Prepare the init/nuke hooks */
+    newterm->init_hook = Term_init_cocoa;
+    newterm->nuke_hook = Term_nuke_cocoa;
+
+    /* Prepare the function hooks */
+    newterm->xtra_hook = Term_xtra_cocoa;
+    newterm->wipe_hook = Term_wipe_cocoa;
+    newterm->curs_hook = Term_curs_cocoa;
+    newterm->bigcurs_hook = Term_bigcurs_cocoa;
+    newterm->text_hook = Term_text_cocoa;
+    newterm->pict_hook = Term_pict_cocoa;
+    /* newterm->mbcs_hook = Term_mbcs_cocoa; */
+
+    /* Global pointer */
+    angband_term[i] = newterm;
+
+    return newterm;
+}
+
+/**
+ * Load preferences from preferences file for current host+current user+
+ * current application.
+ */
+static void load_prefs(void)
+{
+    NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
+
+    /* Make some default defaults */
+    NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
+
+    /*
+     * The following default rows/cols were determined experimentally by first
+     * finding the ideal window/font size combinations. But because of awful
+     * temporal coupling in Term_init_cocoa(), it's impossible to set up the
+     * defaults there, so we do it this way.
+     */
+    for (NSUInteger i = 0; i < ANGBAND_TERM_MAX; i++) {
+       int columns, rows;
+       BOOL visible = YES;
+
+       switch (i) {
+       case 0:
+           columns = 129;
+           rows = 32;
+           break;
+       case 1:
+           columns = 84;
+           rows = 20;
+           break;
+       case 2:
+           columns = 42;
+           rows = 24;
+           break;
+       case 3:
+           columns = 42;
+           rows = 20;
+           break;
+       case 4:
+           columns = 42;
+           rows = 16;
+           break;
+       case 5:
+           columns = 84;
+           rows = 20;
+           break;
+       default:
+           columns = 80;
+           rows = 24;
+           visible = NO;
+           break;
+       }
+
+       NSDictionary *standardTerm =
+           [NSDictionary dictionaryWithObjectsAndKeys:
+                         [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
+                         [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
+                         [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
+                         nil];
+        [defaultTerms addObject: standardTerm];
+    }
+
+    NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:
+#ifdef JP
+                              @"Osaka", @"FontName",
+#else
+                              @"Menlo", @"FontName",
+#endif
+                              [NSNumber numberWithFloat:13.f], @"FontSize",
+                              [NSNumber numberWithInt:60], AngbandFrameRateDefaultsKey,
+                              [NSNumber numberWithBool:YES], AngbandSoundDefaultsKey,
+                              [NSNumber numberWithInt:GRAPHICS_NONE], AngbandGraphicsDefaultsKey,
+                              [NSNumber numberWithBool:YES], AngbandBigTileDefaultsKey,
+                              defaultTerms, AngbandTerminalsDefaultsKey,
+                              nil];
+    [defs registerDefaults:defaults];
+
+    /* Preferred graphics mode */
+    graf_mode_req = [defs integerForKey:AngbandGraphicsDefaultsKey];
+    if (graphics_will_be_enabled() &&
+       [defs boolForKey:AngbandBigTileDefaultsKey] == YES) {
+       use_bigtile = TRUE;
+       arg_bigtile = TRUE;
+    } else {
+       use_bigtile = FALSE;
+       arg_bigtile = FALSE;
+    }
+
+    /* Use sounds; set the Angband global */
+    if ([defs boolForKey:AngbandSoundDefaultsKey] == YES) {
+       use_sound = TRUE;
+       [AngbandSoundCatalog sharedSounds].enabled = YES;
+    } else {
+       use_sound = FALSE;
+       [AngbandSoundCatalog sharedSounds].enabled = NO;
+    }
+
+    /* fps */
+    frames_per_second = [defs integerForKey:AngbandFrameRateDefaultsKey];
 
-#if defined(SAFE_DIRECTORY)
-       NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
-       return [documents stringByAppendingPathComponent: versionedDirectory];
-#else
-       return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
-#endif
+    /* Font */
+    [AngbandContext
+       setDefaultFont:[NSFont fontWithName:[defs valueForKey:@"FontName-0"]
+                              size:[defs floatForKey:@"FontSize-0"]]];
+    if (! [AngbandContext defaultFont])
+       [AngbandContext
+           setDefaultFont:[NSFont fontWithName:@"Menlo" size:13.]];
 }
 
 /**
- * Adjust directory paths as needed to correct for any differences needed by
- * Angband.  init_file_paths() currently requires that all paths provided have
- * a trailing slash and all other platforms honor this.
- *
- * \param originalPath The directory path to adjust.
- * \return A path suitable for Angband or nil if an error occurred.
+ * Play sound effects asynchronously.  Select a sound from any available
+ * for the required event, and bridge to Cocoa to play it.
  */
-static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath)
+static void play_sound(int event)
 {
-       if ([originalPath length] == 0) {
-               return nil;
-       }
-
-       if (![originalPath hasSuffix: @"/"]) {
-               return [originalPath stringByAppendingString: @"/"];
-       }
-
-       return originalPath;
+    [[AngbandSoundCatalog sharedSounds] playSound:event];
 }
 
 /**
- * Give Angband the base paths that should be used for the various directories
- * it needs. It will create any needed directories.
+ * Allocate the primary Angband terminal and activate it.  Allocate the other
+ * Angband terminals.
  */
-static void prepare_paths_and_directories(void)
+static void init_windows(void)
 {
-       char libpath[PATH_MAX + 1] = "\0";
-       NSString *libDirectoryPath =
-           AngbandCorrectedDirectoryPath(get_lib_directory());
-       [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
-
-       char basepath[PATH_MAX + 1] = "\0";
-       NSString *angbandDocumentsPath =
-           AngbandCorrectedDirectoryPath(get_doc_directory());
-       [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
+    /* Create the primary window */
+    term *primary = term_data_link(0);
 
-       init_file_paths(libpath, basepath);
-       create_needed_dirs();
-}
+    /* Prepare to create any additional windows */
+    for (int i = 1; i < ANGBAND_TERM_MAX; i++) {
+        term_data_link(i);
+    }
 
-/**
- * Play sound effects asynchronously.  Select a sound from any available
- * for the required event, and bridge to Cocoa to play it.
- */
-static void play_sound(int event)
-{
-    [[AngbandSoundCatalog sharedSounds] playSound:event];
+    /* Activate the primary term */
+    Term_activate(primary);
 }
 
 /**
@@ -4066,7 +5546,7 @@ static void play_sound(int event)
     for (i=0; i < ANGBAND_TERM_MAX; i++) {
        AngbandContext *context =
            (__bridge AngbandContext*) (angband_term[i]->data);
-        if ([context isMainWindow]) {
+        if ([context isKeyWindow]) {
             termFont = [context angbandViewFont];
             break;
         }
@@ -4088,7 +5568,7 @@ static void play_sound(int event)
     for (mainTerm=0; mainTerm < ANGBAND_TERM_MAX; mainTerm++) {
        AngbandContext *context =
            (__bridge AngbandContext*) (angband_term[mainTerm]->data);
-        if ([context isMainWindow]) {
+        if ([context isKeyWindow]) {
             break;
         }
     }
@@ -4121,11 +5601,7 @@ static void play_sound(int event)
 
     NSEnableScreenUpdates();
 
-    if (mainTerm == 0 && game_in_progress && character_generated) {
-       /* Mimics the logic in setGraphicsMode(). */
-       do_cmd_redraw();
-       wakeup_event_loop();
-    } else {
+    if (mainTerm != 0 || ! redraw_for_tiles_or_term0_font()) {
        [(id)angbandContext requestRedraw];
     }
 }
@@ -4190,186 +5666,12 @@ static void play_sound(int event)
     /* Save the game */
     do_cmd_save_game(FALSE);
     
-    /* Record the current save file so we can select it by default next time.
-        * It's a little sketchy that this only happens when we save through the
-        * menu; ideally game-triggered saves would trigger it too. */
-    record_current_savefile();
-}
-
-/**
- * Create and initialize Angband terminal number "termIndex".
- */
-- (void)linkTermData:(int)termIndex
-{
-    NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults]
-                                   valueForKey: AngbandTerminalsDefaultsKey];
-    NSInteger rows = 24;
-    NSInteger columns = 80;
-
-    if (termIndex < (int)[terminalDefaults count]) {
-        NSDictionary *term = [terminalDefaults objectAtIndex:termIndex];
-        rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey]
-                  integerValue];
-        columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey]
-                     integerValue];
-    }
-
-    /* Allocate */
-    term *newterm = ZNEW(term);
-
-    /* Initialize the term */
-    term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
-
-    /* Differentiate between BS/^h, Tab/^i, etc. */
-    /* newterm->complex_input = TRUE; */
-
-    /* Use a "software" cursor */
-    newterm->soft_cursor = TRUE;
-
-    /* Disable the per-row flush notifications since they are not used. */
-    newterm->never_frosh = TRUE;
-
-    /* Erase with "white space" */
-    newterm->attr_blank = TERM_WHITE;
-    newterm->char_blank = ' ';
-
-    /* Prepare the init/nuke hooks */
-    newterm->init_hook = Term_init_cocoa;
-    newterm->nuke_hook = Term_nuke_cocoa;
-
-    /* Prepare the function hooks */
-    newterm->xtra_hook = Term_xtra_cocoa;
-    newterm->wipe_hook = Term_wipe_cocoa;
-    newterm->curs_hook = Term_curs_cocoa;
-    newterm->bigcurs_hook = Term_bigcurs_cocoa;
-    newterm->text_hook = Term_text_cocoa;
-    newterm->pict_hook = Term_pict_cocoa;
-    /* newterm->mbcs_hook = Term_mbcs_cocoa; */
-
-    /* Global pointer */
-    angband_term[termIndex] = newterm;
-}
-
-/**
- * Allocate the primary Angband terminal and activate it.  Allocate the other
- * Angband terminals.
- */
-- (void)initWindows {
-    for (int i = 0; i < ANGBAND_TERM_MAX; i++) {
-       [self linkTermData:i];
-    }
-
-    Term_activate(angband_term[0]);
-}
-
-/**
- * Load preferences from preferences file for current host+current user+
- * current application.
- */
-- (void)loadPrefs
-{
-    NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
-
-    /* Make some default defaults */
-    NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
-
     /*
-     * The following default rows/cols were determined experimentally by first
-     * finding the ideal window/font size combinations. But because of awful
-     * temporal coupling in Term_init_cocoa(), it's impossible to set up the
-     * defaults there, so we do it this way.
+     * Record the current save file so we can select it by default next time.
+     * It's a little sketchy that this only happens when we save through the
+     * menu; ideally game-triggered saves would trigger it too.
      */
-    for (NSUInteger i = 0; i < ANGBAND_TERM_MAX; i++) {
-       int columns, rows;
-       BOOL visible = YES;
-
-       switch (i) {
-       case 0:
-           columns = 129;
-           rows = 32;
-           break;
-       case 1:
-           columns = 84;
-           rows = 20;
-           break;
-       case 2:
-           columns = 42;
-           rows = 24;
-           break;
-       case 3:
-           columns = 42;
-           rows = 20;
-           break;
-       case 4:
-           columns = 42;
-           rows = 16;
-           break;
-       case 5:
-           columns = 84;
-           rows = 20;
-           break;
-       default:
-           columns = 80;
-           rows = 24;
-           visible = NO;
-           break;
-       }
-
-       NSDictionary *standardTerm =
-           [NSDictionary dictionaryWithObjectsAndKeys:
-                         [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
-                         [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
-                         [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
-                         nil];
-        [defaultTerms addObject: standardTerm];
-    }
-
-    NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:
-#ifdef JP
-                              @"Osaka", @"FontName",
-#else
-                              @"Menlo", @"FontName",
-#endif
-                              [NSNumber numberWithFloat:13.f], @"FontSize",
-                              [NSNumber numberWithInt:60], AngbandFrameRateDefaultsKey,
-                              [NSNumber numberWithBool:YES], AngbandSoundDefaultsKey,
-                              [NSNumber numberWithInt:GRAPHICS_NONE], AngbandGraphicsDefaultsKey,
-                              [NSNumber numberWithBool:YES], AngbandBigTileDefaultsKey,
-                              defaultTerms, AngbandTerminalsDefaultsKey,
-                              nil];
-    [defs registerDefaults:defaults];
-
-    /* Preferred graphics mode */
-    graf_mode_req = [defs integerForKey:AngbandGraphicsDefaultsKey];
-    if (graf_mode_req != GRAPHICS_NONE &&
-       get_graphics_mode(graf_mode_req)->grafID != GRAPHICS_NONE &&
-       [defs boolForKey:AngbandBigTileDefaultsKey] == YES) {
-       use_bigtile = TRUE;
-       arg_bigtile = TRUE;
-    } else {
-       use_bigtile = FALSE;
-       arg_bigtile = FALSE;
-    }
-
-    /* Use sounds; set the Angband global */
-    if ([defs boolForKey:AngbandSoundDefaultsKey] == YES) {
-       use_sound = TRUE;
-       [AngbandSoundCatalog sharedSounds].enabled = YES;
-    } else {
-       use_sound = FALSE;
-       [AngbandSoundCatalog sharedSounds].enabled = NO;
-    }
-
-    /* fps */
-    frames_per_second = [defs integerForKey:AngbandFrameRateDefaultsKey];
-
-    /* Font */
-    [AngbandContext
-       setDefaultFont:[NSFont fontWithName:[defs valueForKey:@"FontName-0"]
-                              size:[defs floatForKey:@"FontSize-0"]]];
-    if (! [AngbandContext defaultFont])
-       [AngbandContext
-           setDefaultFont:[NSFont fontWithName:@"Menlo" size:13.]];
+    record_current_savefile();
 }
 
 /**
@@ -4392,10 +5694,10 @@ static void play_sound(int event)
        init_graphics_modes();
 
        /* Load preferences */
-       [self loadPrefs];
+       load_prefs();
 
        /* Prepare the windows */
-       [self initWindows];
+       init_windows();
 
        /* Set up game event handlers */
        /* init_display(); */
@@ -4403,12 +5705,13 @@ static void play_sound(int event)
        /* Register the sound hook */
        /* sound_hook = play_sound; */
 
-       /* Initialise game */
-       init_angband();
-
        /* Initialize some save file stuff */
+       player_euid = geteuid();
        player_egid = getegid();
 
+       /* Initialise game */
+       init_angband();
+
        /* We are now initialized */
        initialized = TRUE;
 
@@ -4457,7 +5760,7 @@ static void play_sound(int event)
 /**
  * Implement NSObject's validateMenuItem() method to override enabling or
  * disabling a menu item.  Note that, as of 10.14, validateMenuItem() is
- * deprecated in NSObject - it will be removed at some point and  the
+ * deprecated in NSObject - it will be removed at some point and the
  * application delegate will have to be declared as implementing the
  * NSMenuItemValidation protocol.
  */
@@ -4522,6 +5825,13 @@ static void play_sound(int event)
        [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
        return YES;
     }
+    else if (sel == @selector(toggleWideTiles:)) {
+       BOOL is_on = [[NSUserDefaults standardUserDefaults]
+                        boolForKey:AngbandBigTileDefaultsKey];
+
+       [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
+       return YES;
+    }
     else if( sel == @selector(sendAngbandCommand:) ||
             sel == @selector(saveGame:) )
     {
@@ -4549,8 +5859,7 @@ static void play_sound(int event)
     /* Stash it in UserDefaults */
     [[NSUserDefaults angbandDefaults] setInteger:graf_mode_req forKey:AngbandGraphicsDefaultsKey];
 
-    if (graf_mode_req == GRAPHICS_NONE ||
-       get_graphics_mode(graf_mode_req) == GRAPHICS_NONE) {
+    if (! graphics_will_be_enabled()) {
        if (use_bigtile) {
            arg_bigtile = FALSE;
        }
@@ -4559,19 +5868,11 @@ static void play_sound(int event)
        arg_bigtile = TRUE;
     }
 
-    if (game_in_progress && character_generated)
-    {
-       if (arg_bigtile != use_bigtile) {
-           Term_activate(angband_term[0]);
-           Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
-       }
-
-        /* Hack -- Force redraw */
-        do_cmd_redraw();
-
-        /* Wake up the event loop so it notices the change */
-        wakeup_event_loop();
+    if (arg_bigtile != use_bigtile) {
+       Term_activate(angband_term[0]);
+       Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
     }
+    redraw_for_tiles_or_term0_font();
 }
 
 - (void)selectWindow: (id)sender
@@ -4612,13 +5913,10 @@ static void play_sound(int event)
                                      forKey:AngbandBigTileDefaultsKey];
     if (graphics_are_enabled()) {
        arg_bigtile = (is_on) ? FALSE : TRUE;
-       /* Mimics the logic in setGraphicsMode(). */
-       if (game_in_progress && character_generated &&
-           arg_bigtile != use_bigtile) {
+       if (arg_bigtile != use_bigtile) {
            Term_activate(angband_term[0]);
            Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
-           do_cmd_redraw();
-           wakeup_event_loop();
+           redraw_for_tiles_or_term0_font();
        }
     }
 }
@@ -4780,6 +6078,7 @@ static void play_sound(int event)
 {
     if (p_ptr->playing == FALSE || game_is_finished == TRUE)
     {
+        quit_when_ready = true;
         return NSTerminateNow;
     }
     else if (! inkey_flag)