OSDN Git Service

renamed: ptmp0.pcx -> data/ptmp0.pcx
[proj16/16.git] / src / v2 / source / MAPED / TILEED.C
diff --git a/src/v2/source/MAPED/TILEED.C b/src/v2/source/MAPED/TILEED.C
new file mode 100644 (file)
index 0000000..807f67c
--- /dev/null
@@ -0,0 +1,2164 @@
+/*\r
+Copyright (C) 1998 BJ Eirich (aka vecna)\r
+This program is free software; you can redistribute it and/or\r
+modify it under the terms of the GNU General Public License\r
+as published by the Free Software Foundation; either version 2\r
+of the License, or (at your option) any later version.\r
+This program is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
+See the GNU General Public Lic\r
+See the GNU General Public License for more details.\r
+You should have received a copy of the GNU General Public License\r
+along with this program; if not, write to the Free Software\r
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
+*/\r
+\r
+// Additional code by Richard Lau (aka Ric)\r
+\r
+/* Date started: 08/Jul/98 */\r
+/* -- 29/Jul/98 --\r
+ * Heh, I've actually done quite a lot of work on this even though I intend\r
+ * to replace it. Oh well :)\r
+ *\r
+ * I've now made clicking on the mask indicator button (labelled with a small\r
+ * "m") drop back to draw mode if you're in select mode (since it makes no\r
+ * sense to select an area with the mask turned off). Holding down shift\r
+ * and dragging the mouse when you're in select mode will allow you to select\r
+ * a rectangle. The PgUp/PgDn colour shifters will now work in paste mode\r
+ * (affecting the image in the copy buffer).\r
+ *\r
+ * The fill routine is now in place and I've put a button in for the pick\r
+ * colour/eyedropper tool so that I don't get asked to put in a function for\r
+ * like I continuously did with ACE. Due to the extra buttons I've had to\r
+ * shift some of the other buttons around to make them all fit onto the\r
+ * screen. It's probably not the best arrangement but if anyone comes up with\r
+ * a better layout I'm open to suggestions.\r
+ *\r
+ * The wierd button labelling problem I had last time has been corrected - the\r
+ * problem was traced back to the pixels routine in SMALFONT.C. Aen pointed\r
+ * out that I wasn't allocating space for the NULL terminator for all the\r
+ * strings so that has now been fixed (by adding 1 to strlen).\r
+ *\r
+ * - Ric\r
+ */\r
+/*\r
+ * Keys:\r
+ *      CTRL/S + mouse click - (over edit window) Pick up colour under mouse\r
+ *      SHIFT+mb down+drag mouse - select rectangle (select mode only)\r
+ *      SPACEBAR    - toggle between draw mode and select mode\r
+ *      CTRL+Z or U - one level undo\r
+ *      CTRL+A      - select all\r
+ *      CTRL+U      - unselect all\r
+ *      CTRL+C      - copy selected area\r
+ *      CTRL+X      - cut selected area\r
+ *      CTRL+V      - paste\r
+ *      DEL         - clears selected area\r
+ *      CTRL+R      - reverts the tile to the original (like undo in maped 1)\r
+ *                    (self note: modify to ask for confirmation)\r
+ *      G           - toggles grid on/off\r
+ *      H           - toggles hilight on/off\r
+ *      M           - toggles mask on/off\r
+ *      I           - inverts mask\r
+ *      X           - mirrors image\r
+ *      Y           - flips image\r
+ *      L/R         - Rotates tile 90 degrees anti-clockwise/clockwise\r
+ *      Q/W         - Steps through animation (in tile edit mode) if any\r
+ *      A/Z and S/X - changes the left or right drawing colours\r
+ *      PGUP/PGDN   - Colour shift up/down\r
+ *      [ and ]     - toggle screen resolutions\r
+ *      ALT+X       - quits to DOS\r
+ */\r
+/* -- 26/Jul/98 --\r
+ * I've actually decided to start the editor over in a new file so that the\r
+ * code is a bit more elegant. As it stands some bits of this are messy but\r
+ * it currently works ;). Anyway I've decided that what I'll do is to in\r
+ * effect write two tile editors - continuing work on this one and working\r
+ * on the rewrite as a "side project". The eventual aim will be to replace\r
+ * this editor with the rewrite but if factors intervene preventing me from\r
+ * completing the rewrite then there'll always be this editor to fall back on.\r
+ *\r
+ * New in this release is a copy/cut/paste system. New buttons have been added.\r
+ * For some reason I haven't figured out yet I can't call the two rotate\r
+ * buttons "Rotate ACW" and "Rotate CW" (it causes MapEd to crash) so I've had\r
+ * to resort to calling them "Turn_L" and "Turn_R".\r
+ * - Ric\r
+ */\r
+\r
+#include <string.h>\r
+#include <malloc.h>\r
+\r
+#include "config.h"\r
+#include "guicomp.h"\r
+#include "keyboard.h"\r
+#include "maped.h"\r
+#include "mouse.h"\r
+#include "render.h"\r
+#include "vdriver.h"\r
+\r
+#include "smalfont.h"\r
+\r
+#undef free\r
+\r
+#define TRUE  1\r
+#define FALSE 0\r
+// #define NULL ((void *)0)\r
+\r
+#define MOUSENONE    0    // No mouse buttons pressed\r
+#define MOUSERELEASE 1    // Mouse button released\r
+#define MOUSEPRESSED 2    // Mouse button held down\r
+#define MOUSECLICKED 4    // Mouse button pressed\r
+\r
+// Setup some defines for the regions/areas/windows where everything goes\r
+#define MAX_REGIONS 29    // Number of regions\r
+#define BASEREG 0         // Window coords + title\r
+#define HELPBAR 1         // Helpbar - put helpful messages here :)\r
+#define PALETTE 2         // The palette\r
+#define EDITREG 3         // Main edit window\r
+#define PREVIEW 4         // Preview of image being edited\r
+#define SCOLBOX 5         // Selected colour box\r
+#define SCTXTBX 6         // Text box to show info on the selected colours\r
+#define QUITBUT 7         // Quit button\r
+#define DRAWBUT 8         // Draw mode indicator\r
+#define FILLBUT 9         // Fill mode indicator\r
+#define PICKBUT 10        // Pick mode indicator\r
+#define MASKBUT 11        // Mask mode indicator\r
+#define PSTEBUT 12        // Paste mode indicator\r
+#define EDMDFRM 13        // Frame for edit mode radio buttons\r
+#define MASKIND 14        // Indicator showing if mask is shown\r
+#define GRIDIND 15        // Indicator showing if grid is shown\r
+#define ANIMIND 16        // Indicator showing if animations are shown\r
+#define PXHLIND 17        // Indicator showing if pixel highlight shown\r
+#define IND_FRM 18        // Frame for indicators (push buttons)\r
+#define BUT_FRM 19        // Frame for buttons\r
+#define UNDOBUT 20        // Undo button\r
+#define COPYBUT 21        // Copy button\r
+#define CUT_BUT 22        // Cut button\r
+#define CLR_BUT 23        // Clear button\r
+#define TOOLFRM 24        // Frame for tools\r
+#define FLIPBUT 25        // Flip image button\r
+#define MIRRBUT 26        // Mirror image button\r
+#define RIACBUT 27        // Rotate anticlockwise button\r
+#define RIC_BUT 28        // Rotate clockwise button\r
+\r
+// Define TEbitflags values\r
+#define TEQUITCUR 1\r
+#define TEANIMATE 2\r
+#define TEGRID    4\r
+#define TEHILIGHT 8\r
+#define TESHOWMSK 16\r
+\r
+// Define TEREGION->bitflags values\r
+#define REGVISIBLE  1\r
+#define REGACTIVE   2\r
+#define REGNOCLICK  4\r
+#define REGTWOCLICK 8          // wait for a second click\r
+\r
+// TEeditmode defs\r
+#define EDDRAW  1\r
+#define EDFILL  2\r
+#define EDPICK  4\r
+#define EDMASK  8\r
+#define EDPASTE 16\r
+\r
+// TEtype defs\r
+#define TEUNKNOWN 0\r
+#define TETILE    1\r
+\r
+// DATABUTTON buttontypes:\r
+#define CLICKBUTTON 1          // normal button\r
+#define PUSHBUTTON  2          // can be pushed in/out (toggle bit bitvalue)\r
+#define RADIOBUTTON 4          // for button banks (set *bitflag=bitvalue)\r
+\r
+// DATABASEREGION windowtypes:\r
+#define DBRSTDWINDOW 0         // window and title bar\r
+#define DBRPANEL     1         // no title bar\r
+\r
+// INFOTYPEs:\r
+#define DITREGION     1\r
+#define DITBASEREGION 2\r
+#define DITBUTTON     4\r
+#define DITEDITREG    8\r
+\r
+// Button flag action - has this button been clicked?\r
+#define BFANONE   0\r
+#define BFAUNDO   1\r
+#define BFACOPY   2\r
+#define BFACUT    4\r
+#define BFACLEAR  8\r
+#define BFARIAC   16\r
+#define BFARIC    32\r
+#define BFAMIRROR 64\r
+#define BFAFLIP   128\r
+\r
+// Variables\r
+typedef struct TEREGION\r
+  {\r
+  int infotype;                 // type of struct\r
+  unsigned int x1, x2, y1, y2;  // bounding coordinates\r
+  char *caption;                // helpbar text\r
+  int bitflags;                 // see defines above\r
+  void (*drawproc)(struct TEREGION *);  // region's draw procedure\r
+                                        // - allows nice and short RenderTileEdit proc ;)\r
+  void *data;        // extended information\r
+  } TEREGION;\r
+typedef struct DATABASEREGION\r
+  {\r
+  int infotype;                 // type of struct\r
+  int numofregs;                // total number of regions (including base)\r
+  int windowtype;               // type of window to draw\r
+  } DATABASEREGION;\r
+typedef struct DATABUTTON\r
+  {\r
+  int infotype;                 // type of struct\r
+  int *bitflag;                 // address of bitflag\r
+  char *text;                   // addition text (eg helptext)\r
+  int bitvalue;                 // 1st bit/value\r
+  int buttontype;               // type\r
+  } DATABUTTON;\r
+typedef struct DATAEDITREG\r
+  {\r
+  int infotype;                 // type of struct\r
+  int firstx, firsty;           // first coords for two-click operation\r
+  } DATAEDITREG;\r
+\r
+\r
+static TEREGION *TEregs=NULL;       // pointer to the regions\r
+static unsigned char lcolor=255;    // left  mouse colour\r
+static unsigned char rcolor=0;      // right mouse colour\r
+static TEREGION *TEOldReg=NULL;\r
+static unsigned int MBStatus[3]={MOUSENONE,MOUSENONE,MOUSENONE};\r
+static int TEanimstrand=0;        // Animation strand\r
+static int TEbitflags=TEANIMATE;  // Set default bitflags\r
+static int TEbfa=BFANONE;         // Set default button action flags\r
+static int TEeditmode=EDDRAW;     // Default edit region mode\r
+static int TExoffs=16;            // X offset for region "windows"\r
+static int TEyoffs=16;            // Y offset for region "windows"\r
+unsigned int TEtype=TEUNKNOWN;    // type of graphic being edited\r
+unsigned int TEtile; // tile to edit - may possibly expand this to an array for simultaneous editing\r
+unsigned int TExsize=0;   // width of graphic being edited\r
+unsigned int TEysize=0;   // height of graphic being edited\r
+unsigned int TEcopyxsize=0;   // width of copy buffer\r
+unsigned int TEcopyysize=0;   // height of copy buffer\r
+unsigned char *TEsource=NULL; // pointer to original image being edited\r
+static unsigned char *TEimage=NULL;  // pointer to image being edited\r
+static unsigned char *TEmask=NULL;   // pointer to image mask\r
+static unsigned char *TEundo=NULL;   // pointer to undo buffer\r
+static unsigned char *TEcopy=NULL;   // pointer to copy buffer\r
+static unsigned char *TEcmsk=NULL;   // pointer to copy mask\r
+static char TEtempstr[256];          // Temporary string buffer\r
+\r
+// Function prototypes\r
+static void *TEAlloc(unsigned long amount, char *whatfor);\r
+\r
+#define TEFree(ptr) if (ptr) free(ptr); ptr=NULL;\r
+#define TEFreeReg(ptr) DestroyRegions(&ptr); if (ptr) free(ptr); ptr=NULL;\r
+#define TENewText(text, whatfor) (char *) strcpy(TEAlloc(strlen(text)+1, whatfor), text);\r
+//static void TEFree(void *TETmpBuf);\r
+static void MBClickHandler(TEREGION *TEBaseReg);\r
+static void AboutTE(void);\r
+static void DestroyRegions(TEREGION **TEBaseReg);\r
+static void SetupRegions(void);\r
+static void InitTEImage(void);\r
+static void DeinitTEImage(void);\r
+static int  MouseOverTEreg(TEREGION *TEr);\r
+static void RenderTileEdit(TEREGION *TEBaseReg);\r
+static void RestartTileEditor(void);\r
+static void SetCaption(TEREGION *TEr, char *helptext);\r
+static void TEDrawHilight(int x1, int y1, int width, int height, unsigned char colour);\r
+static void TEDrawCheckeredBox(int x1, int y1, int width, int height, unsigned char colour);\r
+static void TERegFill(TEREGION *TEr, int colour);\r
+static void TERegText(TEREGION *TEr, char *message);\r
+static void TEClearImage(void);\r
+static void TECopyImage(void);\r
+static void TEFlipImage(unsigned char *TERILsrc, int TERILxsize, int TERILysize);\r
+static void TEMirrorImage(unsigned char *TERILsrc, int TERILxsize, int TERILysize);\r
+static void TERotateImageAntiClockwise(unsigned char *TERILsrc, int TERILxsize, int TERILysize);\r
+static void TERotateImageClockwise(unsigned char *TERILsrc, int TERILxsize, int TERILysize);\r
+static void TEFillImage(char *TEFIimage, char *TEFImask, int TEFIxsize, int TEFIysize, int TEFIx, int TEFIy, char TEFIcolour);\r
+static void TERFillImage(char *TERFIimage, char *TERFImask, int TERFIxsize, int TERFIysize, int TERFIx, int TERFIy);\r
+static void TEShiftColours(char *TESCimage, char *TESCmask, int TESCxsize, int TESCysize, int amount);\r
+static void TEUndo(void);\r
+static void DrawButton(TEREGION *TEr);\r
+static void DrawEditReg(TEREGION *TEr);\r
+static void DrawHelpBar(TEREGION *TEr);\r
+static void DrawPalette(TEREGION *TEr);\r
+static void DrawPreview(TEREGION *TEr);\r
+static void DrawSColBox(TEREGION *TEr);\r
+static void DrawSCTxtBx(TEREGION *TEr);\r
+static void DrawSunkRegion(TEREGION *TEr);\r
+void TileEdit(void);\r
+\r
+\r
+static int MouseOverTEreg(TEREGION *TEr)\r
+  {\r
+  unsigned int testx=mx-TExoffs;\r
+  unsigned int testy=my-TEyoffs;\r
+  unsigned int width=(TEr->x2)-(TEr->x1);\r
+  unsigned int height=(TEr->y2)-(TEr->y1);\r
+  testx-=(TEr->x1);\r
+  testy-=(TEr->y1);\r
+\r
+  if (testx >= width || testy >= height) return FALSE;\r
+  return TRUE;\r
+  }\r
+\r
+static void DrawButton(TEREGION *TEr)\r
+// -- ric: 20/Jul/98 - generic button routine\r
+  {\r
+  unsigned int x=TEr->x1+TExoffs;\r
+  unsigned int y=TEr->y1+TEyoffs;\r
+  unsigned int width=(TEr->x2)-(TEr->x1);\r
+  unsigned int height=(TEr->y2)-(TEr->y1);\r
+\r
+  strcpy(TEtempstr,"Button data not declared.");\r
+  if (!TEr->data)\r
+    err(TEtempstr);\r
+  if (((struct DATABUTTON *)(TEr->data))->infotype!=DITBUTTON)\r
+    err(TEtempstr);\r
+\r
+  // draw raised button\r
+  stdwindow(x-1, y-1, x+width+1, y+height+1);\r
+  if (TEr->caption)\r
+    {\r
+    GotoXY(x+(width-(pixels(TEr->caption)))/2, y+(height-6)/2);\r
+    printstring(TEr->caption);\r
+    }\r
+  switch (((struct DATABUTTON *)(TEr->data))->buttontype)\r
+    {\r
+    case CLICKBUTTON:\r
+      if (MouseOverTEreg(TEr) && TEr->bitflags&REGACTIVE)\r
+        {\r
+        if (((struct DATABUTTON *)(TEr->data))->text)\r
+          SetCaption(&TEregs[HELPBAR], (((struct DATABUTTON *)(TEr->data))->text));\r
+\r
+        // Handle mouse clicks\r
+        if (TEOldReg==TEr)\r
+          {\r
+          if ((MBStatus[0]|MBStatus[1])==MOUSEPRESSED)\r
+            {\r
+            // draw button down\r
+            FilledBox(x, y, width, height, winbg);\r
+            HLine(x, y, width, darkw);\r
+            VLine(x, y, height, darkw);\r
+            if (TEr->caption)\r
+              {\r
+              GotoXY(x+1+(width-(pixels(TEr->caption)))/2, y+1+(height-6)/2);\r
+              printstring(TEr->caption);\r
+              }\r
+            }\r
+          if ((MBStatus[0]|MBStatus[1])==MOUSERELEASE)\r
+            {\r
+            *(((struct DATABUTTON *)(TEr->data))->bitflag)^=((struct DATABUTTON *)(TEr->data))->bitvalue;\r
+            }\r
+          }\r
+        }\r
+      break;\r
+    case PUSHBUTTON:\r
+      if (*(((struct DATABUTTON *)(TEr->data))->bitflag)&(((struct DATABUTTON *)(TEr->data))->bitvalue))\r
+        {\r
+        // draw button down\r
+        FilledBox(x, y, width, height, winbg);\r
+        HLine(x, y, width, darkw);\r
+        VLine(x, y, height, darkw);\r
+        if (TEr->caption)\r
+          {\r
+          GotoXY(x+1+(width-(pixels(TEr->caption)))/2, y+1+(height-6)/2);\r
+          printstring(TEr->caption);\r
+          }\r
+        }\r
+      if (MouseOverTEreg(TEr) && TEr->bitflags&REGACTIVE)\r
+        {\r
+        if (((struct DATABUTTON *)(TEr->data))->text)\r
+          {\r
+          sprintf(TEtempstr, "Toggle %s on/off", ((struct DATABUTTON *)(TEr->data))->text);\r
+          SetCaption(&TEregs[HELPBAR], TEtempstr);\r
+          }\r
+\r
+        // Handle mouse clicks\r
+        if (TEOldReg==TEr)\r
+          {\r
+          if ((MBStatus[0]|MBStatus[1])==MOUSECLICKED)\r
+            {\r
+            *(((struct DATABUTTON *)(TEr->data))->bitflag)^=((struct DATABUTTON *)(TEr->data))->bitvalue;\r
+            }\r
+          }\r
+        }\r
+      break;\r
+    case RADIOBUTTON:\r
+      if (*(((struct DATABUTTON *)(TEr->data))->bitflag)==((struct DATABUTTON *)(TEr->data))->bitvalue)\r
+        {\r
+        // draw button down\r
+        FilledBox(x, y, width, height, winbg);\r
+        HLine(x, y, width, darkw);\r
+        VLine(x, y, height, darkw);\r
+        if (TEr->caption)\r
+          {\r
+          GotoXY(x+1+(width-(pixels(TEr->caption)))/2, y+1+(height-6)/2);\r
+          printstring(TEr->caption);\r
+          }\r
+        }\r
+      if (MouseOverTEreg(TEr) && TEr->bitflags&REGACTIVE)\r
+        {\r
+        if (((struct DATABUTTON *)(TEr->data))->text)\r
+          SetCaption(&TEregs[HELPBAR], (((struct DATABUTTON *)(TEr->data))->text));\r
+\r
+        // Handle mouse clicks\r
+        if (TEOldReg==TEr)\r
+          {\r
+          if ((MBStatus[0]|MBStatus[1])==MOUSECLICKED)\r
+            {\r
+            *(((struct DATABUTTON *)(TEr->data))->bitflag)=((struct DATABUTTON *)(TEr->data))->bitvalue;\r
+            }\r
+          }\r
+        }\r
+      break;\r
+    }\r
+  }\r
+\r
+static void DrawSunkRegion(TEREGION *TEr)\r
+  {\r
+  unsigned int x=TEr->x1+TExoffs;\r
+  unsigned int y=TEr->y1+TEyoffs;\r
+  unsigned int width=(TEr->x2)-(TEr->x1);\r
+  unsigned int height=(TEr->y2)-(TEr->y1);\r
+\r
+//  FilledBox(x, y, width, height, black);\r
+  HLine(x, y, width, darkw);\r
+  VLine(x, y, height, darkw);\r
+  HLine(x+1, y+height-1, width-1, brightw);\r
+  VLine(x+width-1, y+1, height-1, brightw);\r
+  }\r
+\r
+static void DrawEditReg(TEREGION *TEr)\r
+  {\r
+  int i,j,i2,j2,c;\r
+  int minx,miny,maxx,maxy;\r
+  int blocksize=8;\r
+  int visblocksize=blocksize;\r
+  char *img;\r
+  TEREGION tempr;\r
+  tempr.x1=TEr->x1+2;\r
+  tempr.x2=TEr->x2-2;\r
+  tempr.y1=TEr->y1+2;\r
+  tempr.y2=TEr->y2-2;\r
+\r
+  img=TEimage; //vsp+(256*TEtile);\r
+\r
+  TERegFill(TEr, black);\r
+  DrawSunkRegion(TEr);\r
+  FilledBox(TEr->x1+TExoffs+2, TEr->y1+TEyoffs+2, (blocksize<<4), (blocksize<<4), titlebg);\r
+\r
+  if (TEbitflags&TEGRID) visblocksize-=1;\r
+\r
+  for (j=0; j<TEysize; j++)\r
+    for (i=0; i<TExsize; i++)\r
+      {\r
+      FilledBox(TEr->x1+TExoffs+2+i*blocksize, TEr->y1+TEyoffs+2+j*blocksize, visblocksize, visblocksize, *img++);\r
+      if (TEbitflags&TESHOWMSK && !TEmask[j*TExsize+i])\r
+        TEDrawCheckeredBox(TEr->x1+TExoffs+2+i*blocksize, TEr->y1+TEyoffs+2+j*blocksize, visblocksize, visblocksize, titlebg);\r
+      }\r
+\r
+  img=TEimage;\r
+  if (TEr->bitflags&REGACTIVE && !(MouseOverTEreg(&tempr)) && (MBStatus[0]|MBStatus[1])==MOUSERELEASE)\r
+    if (TEr->bitflags&REGTWOCLICK) TEr->bitflags^=REGTWOCLICK;\r
+  if (MouseOverTEreg(&tempr) && TEr->bitflags&REGACTIVE)\r
+    {\r
+    i=(mx-TExoffs-(tempr.x1))/blocksize;\r
+    j=(my-TEyoffs-(tempr.y1))/blocksize;\r
+    c=img[(j*TExsize)+i];\r
+    sprintf(TEtempstr, "Tile: %d, (%02d, %02d), Color: %03d (rgb: %03d/%03d/%03d)", TEtile, i, j, c, pal[(c*3)], pal[(c*3)+1], pal[(c*3)+2]);\r
+    SetCaption(&TEregs[HELPBAR], TEtempstr);\r
+    if (TEbitflags&TEHILIGHT)\r
+      TEDrawHilight(TExoffs+i*blocksize+tempr.x1, TEyoffs+j*blocksize+tempr.y1, visblocksize, visblocksize, white);\r
+\r
+    // if pasting draw the image being pasted\r
+    if (TEeditmode==EDPASTE)\r
+      {\r
+      for (j2=0; j2<TEcopyysize; j2++)\r
+        if ((j+j2)<TEysize)\r
+          for (i2=0; i2<TEcopyxsize; i2++)\r
+            if ((i+i2)<TExsize)\r
+              if (TEcmsk[j2*TEcopyxsize+i2])\r
+                if (!(TEbitflags&TESHOWMSK))\r
+                  FilledBox(TEr->x1+TExoffs+2+(i+i2)*blocksize, TEr->y1+TEyoffs+2+(j+j2)*blocksize, visblocksize, visblocksize, TEcopy[j2*TEcopyxsize+i2]);\r
+                else\r
+                  if (TEmask[(j+j2)*TExsize+i+i2])\r
+                    FilledBox(TEr->x1+TExoffs+2+(i+i2)*blocksize, TEr->y1+TEyoffs+2+(j+j2)*blocksize, visblocksize, visblocksize, TEcopy[j2*TEcopyxsize+i2]);\r
+      }\r
+    if (TEeditmode==EDMASK)\r
+      {\r
+      // draw reg\r
+      if (TEr->bitflags&REGTWOCLICK)\r
+        for (j2=0; j2<TEysize; j2++)\r
+          for (i2=0; i2<TEysize; i2++)\r
+            if (((j2>=j && j2<=((struct DATAEDITREG *)(TEr->data))->firsty) ||\r
+                (j2>=((struct DATAEDITREG *)(TEr->data))->firsty && j2<=j)) &&\r
+                ((i2>=i && i2<=((struct DATAEDITREG *)(TEr->data))->firstx) ||\r
+                (i2>=((struct DATAEDITREG *)(TEr->data))->firstx && i2<=i)))\r
+              TEDrawCheckeredBox(TEr->x1+TExoffs+2+i2*blocksize, TEr->y1+TEyoffs+2+j2*blocksize, visblocksize, visblocksize, darkred);\r
+\r
+      if ((MBStatus[0]|MBStatus[1])==MOUSECLICKED && (key[SCAN_LSHIFT] || key[SCAN_RSHIFT]) && !(TEr->bitflags&REGTWOCLICK))\r
+        {\r
+        ((struct DATAEDITREG *)(TEr->data))->firstx=i;\r
+        ((struct DATAEDITREG *)(TEr->data))->firsty=j;\r
+        TEr->bitflags^=REGTWOCLICK;\r
+        }\r
+      if ((MBStatus[0]|MBStatus[1])==MOUSERELEASE && TEr->bitflags&REGTWOCLICK)\r
+        {\r
+        if (MBStatus[1]==MOUSERELEASE) c=0x0;\r
+        else c=0xf;\r
+        miny=j;\r
+        maxy=((struct DATAEDITREG *)(TEr->data))->firsty;\r
+        minx=i;\r
+        maxx=((struct DATAEDITREG *)(TEr->data))->firstx;\r
+        if (j>((struct DATAEDITREG *)(TEr->data))->firsty)\r
+          {\r
+          maxy=j;\r
+          miny=((struct DATAEDITREG *)(TEr->data))->firsty;\r
+          }\r
+        if (i>((struct DATAEDITREG *)(TEr->data))->firstx)\r
+          {\r
+          maxx=i;\r
+          minx=((struct DATAEDITREG *)(TEr->data))->firstx;\r
+          }\r
+        for (j2=miny; j2<=maxy; j2++)\r
+          for (i2=minx; i2<=maxx; i2++)\r
+            TEmask[j2*TExsize+i2]=(char)c;\r
+        TEr->bitflags^=REGTWOCLICK;\r
+        }\r
+      }\r
+    // Handle mouse clicks\r
+\r
+#ifdef JUNK\r
+    if ((MBStatus[0]|MBStatus[1])==MOUSECLICKED && (key[SCAN_CTRL]||key[SCAN_S]))\r
+      {\r
+      // put code to lock out draw mode here\r
+      }\r
+#endif\r
+    if (TEOldReg==TEr)\r
+      {\r
+      // Update undo buffer\r
+      if ((MBStatus[0]|MBStatus[1])==MOUSECLICKED && TEeditmode!=EDMASK && TEeditmode!=EDPICK && !(key[SCAN_CTRL]||key[SCAN_S]))\r
+        memcpy(TEundo, TEimage, TExsize*TEysize);\r
+      if (MBStatus[0]==MOUSECLICKED && TEeditmode==EDFILL)\r
+        TEFillImage(TEimage, (TEbitflags&TESHOWMSK ? TEmask : NULL), TExsize, TEysize, i, j, lcolor);\r
+      if (MBStatus[1]==MOUSECLICKED && TEeditmode==EDFILL)\r
+        TEFillImage(TEimage, (TEbitflags&TESHOWMSK ? TEmask : NULL), TExsize, TEysize, i, j, rcolor);\r
+\r
+      // Draw\r
+      if ((MBStatus[0]|MBStatus[1])==MOUSEPRESSED)\r
+        {\r
+        if (TEeditmode==EDPASTE)\r
+          {\r
+          for (j2=0; j2<TEcopyysize; j2++)\r
+            if ((j+j2)<TEysize)\r
+              for (i2=0; i2<TEcopyxsize; i2++)\r
+                if ((i+i2)<TExsize)\r
+                  if (TEcmsk[j2*TEcopyxsize+i2])\r
+                    if (!(TEbitflags&TESHOWMSK))\r
+                      TEimage[(j+j2)*TExsize+i+i2]=TEcopy[j2*TEcopyxsize+i2];\r
+                    else\r
+                      if (TEmask[(j+j2)*TExsize+i+i2])\r
+                        TEimage[(j+j2)*TExsize+i+i2]=TEcopy[j2*TEcopyxsize+i2];\r
+          }\r
+        }\r
+      if (MBStatus[0]==MOUSEPRESSED)\r
+        if (key[SCAN_CTRL]||key[SCAN_S]||(TEeditmode==EDPICK))\r
+          lcolor=TEimage[(j*TExsize)+i];\r
+        else\r
+          if ((TEeditmode==EDDRAW)&&!(TEbitflags&TESHOWMSK && !TEmask[j*TExsize+i]))\r
+            TEimage[(j*TExsize)+i]=lcolor;\r
+          else\r
+            if (TEeditmode==EDMASK && !(TEr->bitflags&REGTWOCLICK))\r
+              TEmask[(j*TExsize)+i]=0xf;\r
+      if (MBStatus[1]==MOUSEPRESSED)\r
+        if (key[SCAN_CTRL]||key[SCAN_S]||(TEeditmode==EDPICK))\r
+          rcolor=TEimage[(j*TExsize)+i];\r
+        else\r
+          if ((TEeditmode==EDDRAW)&&!(TEbitflags&TESHOWMSK && !TEmask[j*TExsize+i]))\r
+            TEimage[(j*TExsize)+i]=rcolor;\r
+          else\r
+            if (TEeditmode==EDMASK && !(TEr->bitflags&REGTWOCLICK))\r
+              TEmask[(j*TExsize)+i]=0x0;\r
+      }\r
+    }\r
+  }\r
+\r
+static void DrawHelpBar(TEREGION *TEr)\r
+  {\r
+  //int i, htext=0;\r
+  TERegFill(TEr, black);\r
+  DrawSunkRegion(TEr);\r
+  if (TEr->caption) TERegText(TEr, TEr->caption);\r
+  TEFree(TEr->caption);               // Blank caption\r
+  //TEr->caption=NULL;                  // why doesn't this work in TEFree?\r
+#ifdef JUNK\r
+  i=MAX_REGIONS;\r
+  do\r
+    {\r
+    i--;\r
+    if (MouseOverTEreg(&TEregs[i])) htext=i;\r
+    } while (i>0);\r
+  if (htext && TEregs[htext].helptext) TERegText(TEr, TEr->caption);\r
+#endif\r
+  }\r
+\r
+static void DrawPalette(TEREGION *TEr)\r
+  {\r
+  int i,j;\r
+  unsigned int width=(TEr->x2)-(TEr->x1);\r
+  unsigned int height=(TEr->y2)-(TEr->y1);\r
+  int xscale=(width-4)>>6;\r
+  int yscale=(height-4)>>2;\r
+  int x=(TEr->x1)+TExoffs;\r
+  int y=(TEr->y1)+TEyoffs;\r
+  TEREGION tempr;\r
+  tempr.x1=TEr->x1+2;\r
+  tempr.x2=TEr->x2-2;\r
+  tempr.y1=TEr->y1+2;\r
+  tempr.y2=TEr->y2-2;\r
+\r
+  x+=2;\r
+  y+=2;\r
+  TERegFill(TEr, black);\r
+  DrawSunkRegion(TEr);\r
+  i=64;\r
+  do\r
+    {\r
+    i--;\r
+    FilledBox(x+(i*xscale), y, xscale, yscale, i);\r
+    FilledBox(x+(i*xscale), y+yscale, xscale, yscale, i+64);\r
+    FilledBox(x+(i*xscale), y+yscale*2, xscale, yscale, i+128);\r
+    FilledBox(x+(i*xscale), y+yscale*3, xscale, yscale, i+192);\r
+    } while (i>0);\r
+\r
+  i=lcolor&63;\r
+  j=lcolor>>6;\r
+  TEDrawHilight(x+i*xscale, y+j*yscale, xscale, yscale, titlebg);\r
+\r
+  i=rcolor&63;\r
+  j=rcolor>>6;\r
+  TEDrawHilight(x+i*xscale, y+j*yscale, xscale, yscale, titlebg);\r
+\r
+  if (MouseOverTEreg(&tempr) && TEr->bitflags&REGACTIVE)\r
+    {\r
+    i=(mx-TExoffs-(tempr.x1))/xscale;\r
+    j=(my-TExoffs-(tempr.y1))/yscale;\r
+    i+=(j<<6);\r
+    j=i*3;\r
+    sprintf(TEtempstr, "Color: %03d (rgb: %03d/%03d/%03d)", i, pal[j], pal[j+1], pal[j+2]);\r
+    SetCaption(&TEregs[HELPBAR], TEtempstr);\r
+    // Handle mouse clicks\r
+    if (TEOldReg==TEr)\r
+      {\r
+      if (MBStatus[0]==MOUSEPRESSED) lcolor=i;\r
+      if (MBStatus[1]==MOUSEPRESSED) rcolor=i;\r
+      }\r
+    }\r
+  }\r
+\r
+static void DrawPreview(TEREGION *TEr)\r
+  {\r
+  char *img;\r
+  int i;\r
+  TERegFill(TEr, black);\r
+  DrawSunkRegion(TEr);\r
+//  img=vsp+(256*TEtile);\r
+  img=TEimage;\r
+  i=tileidx[TEtile];\r
+  if ((TEbitflags&TEANIMATE) && i!=TEtile) img=vsp+(256*i);\r
+  CopyTile(TEr->x1+TExoffs+2, TEr->y1+TEyoffs+2, img);\r
+\r
+  if (MouseOverTEreg(TEr) && TEr->bitflags&REGACTIVE)\r
+    {\r
+    strcpy(TEtempstr, (TEbitflags&TEANIMATE ? "Click to halt animation" : "Click to animate tile"));\r
+    SetCaption(&TEregs[HELPBAR], TEtempstr);\r
+\r
+    // Handle mouse clicks\r
+    if (TEOldReg==TEr)\r
+      if ((MBStatus[0]|MBStatus[1])==MOUSECLICKED && MouseOverTEreg(TEr))\r
+        TEbitflags^=TEANIMATE;\r
+    }\r
+  }\r
+\r
+static void DrawSColBox(TEREGION *TEr)\r
+// -- ric: 15/Jul/98 - Draw selected colour box\r
+  {\r
+  int halfwidth=TEr->x2-TEr->x1-4;\r
+  int height=TEr->y2-TEr->y1-4;\r
+  char DSCBtempcolor;\r
+  TERegFill(TEr, black);\r
+  DrawSunkRegion(TEr);\r
+  FilledBox(TExoffs+2+TEr->x1, TEyoffs+2+TEr->y1, halfwidth, height, titlebg);\r
+  height-=2;\r
+  halfwidth-=2;\r
+  halfwidth>>=1;\r
+  FilledBox(TExoffs+3+TEr->x1, TEyoffs+3+TEr->y1, halfwidth, height, lcolor);\r
+  FilledBox(TExoffs+3+TEr->x1+halfwidth, TEyoffs+3+TEr->y1, halfwidth, height, rcolor);\r
+\r
+  if (MouseOverTEreg(TEr) && TEr->bitflags&REGACTIVE)\r
+    {\r
+    strcpy(TEtempstr, "Click to swap colors");\r
+    SetCaption(&TEregs[HELPBAR], TEtempstr);\r
+\r
+    // Handle mouse clicks\r
+    if (TEOldReg==TEr)\r
+    if ((MBStatus[0]|MBStatus[1])==MOUSECLICKED && MouseOverTEreg(TEr))\r
+      {\r
+      DSCBtempcolor=lcolor;\r
+      lcolor=rcolor;\r
+      rcolor=DSCBtempcolor;\r
+      }\r
+    }\r
+  }\r
+\r
+static void DrawSCTxtBx(TEREGION *TEr)\r
+// -- ric: 15/Jul/98 - Draw selected colour text box\r
+  {\r
+//  char DSCTBTextBuf[256];\r
+  int i;\r
+  int DSCTBx=TEr->x1+TExoffs+2;\r
+  int DSCTBy=TEr->y1+TEyoffs+2;\r
+  TERegFill(TEr, black);\r
+  DrawSunkRegion(TEr);\r
+  i=lcolor*3;\r
+  sprintf(TEtempstr, "Left: %03d (rgb: %03d/%03d/%03d)", lcolor, pal[i], pal[i+1], pal[i+2]);\r
+  GotoXY(DSCTBx, DSCTBy);\r
+  printstring(TEtempstr);\r
+  i=rcolor*3;\r
+  sprintf(TEtempstr, "Right: %03d (rgb: %03d/%03d/%03d)", rcolor, pal[i], pal[i+1], pal[i+2]);\r
+  GotoXY(DSCTBx, DSCTBy+7);\r
+  printstring(TEtempstr);\r
+  }\r
+\r
+static void SetCaption(TEREGION *TEr, char *helptext)\r
+  {\r
+  TEFree(TEr->caption);\r
+  TEr->caption=(char *)strcpy((char *)TEAlloc(strlen(helptext)+1,helptext), helptext);\r
+  }\r
+\r
+static void TEDrawHilight(int x1, int y1, int width, int height, unsigned char colour)\r
+// -- ric: 15/Jul/98 - Draws hilight around given rectangle\r
+  {\r
+  VLine(x1-1, y1, height, colour);\r
+  VLine(x1+width, y1, height, colour);\r
+  HLine(x1-1, y1-1, width+2, colour);\r
+  HLine(x1-1, y1+height, width+2, colour);\r
+  }\r
+\r
+static void TEDrawCheckeredBox(int x1, int y1, int width, int height, unsigned char colour)\r
+// -- ric: 17/Jul/98 - Draws a checkered box\r
+  {\r
+  int i,j;\r
+  unsigned char c;\r
+  for (j=0; j<height; j++)\r
+   {\r
+   c=(y1+j+1)&1;\r
+   if (c) c=colour;\r
+   for (i=0; i<width; i++)\r
+     {\r
+     if (c) screen[(y1+j)*tsx+x1+i]=c;\r
+     c^=colour;\r
+     }\r
+   }\r
+  }\r
+\r
+static void TERegFill(TEREGION *TEr, int colour)\r
+// -- ric: 20/Jul/98 - Fills region with colour (colour==-1 for transparent)\r
+  {\r
+  if (colour>-1)\r
+    FilledBox(TEr->x1+TExoffs, TEr->y1+TEyoffs, (TEr->x2)-(TEr->x1), (TEr->y2)-(TEr->y1), colour);\r
+  }\r
+\r
+static void TERegText(TEREGION *TEr, char *message)\r
+// Prints given message in the specified region\r
+  {\r
+  int tempx, tempy;\r
+  tempx=(TEr->x1)+TExoffs+2;\r
+\r
+  tempy=(TEr->y2)-(TEr->y1);\r
+  tempy>>=1;\r
+  tempy+=(TEr->y1)-3;\r
+\r
+  GotoXY(tempx,tempy+TEyoffs);\r
+  printstring(message);\r
+\r
+  }\r
+\r
+static void TEClearImage(void)\r
+// -- ric: 26/Jul/98 - clear selected image\r
+  {\r
+  int j;\r
+  if (TEbitflags&TESHOWMSK)\r
+    {\r
+    j=TEysize*TExsize;\r
+    memcpy(TEundo, TEimage, j); // backup image to undo buffer\r
+    do\r
+      {\r
+      j--;\r
+      if (*(TEmask+j)) *(TEimage+j)=rcolor;\r
+      } while (j>0);\r
+    }\r
+  }\r
+\r
+static void TECopyImage(void)\r
+// -- ric: 26/Jul/98 - copy selected image to copy buffer\r
+  {\r
+  int i, j, TECIxstart, TECIystart, TECIxsize, TECIysize;\r
+  if (TEbitflags&TESHOWMSK)  // only makes sense if selected region is shown\r
+    {\r
+    TECIxstart=TExsize;\r
+    TECIystart=TEysize;\r
+    TECIxsize=-1;\r
+    TECIysize=-1;\r
+    // loop through image to find the starting and ending x and y coords\r
+    // of the image to copy\r
+    for (j=0; j<TEysize; j++)\r
+      for (i=0; i<TExsize; i++)\r
+        {\r
+        if (TEmask[j*TExsize+i])   // selected\r
+          {\r
+          if (i<TECIxstart) TECIxstart=i;\r
+          if (i>TECIxsize) TECIxsize=i;\r
+          if (j<TECIystart) TECIystart=j;\r
+          if (j>TECIysize) TECIysize=j;\r
+          }\r
+        }\r
+\r
+    TECIxsize-=TECIxstart-1;\r
+    TECIysize-=TECIystart-1;\r
+\r
+    if (TECIxsize>0 && TECIysize>0)\r
+      {\r
+      // free and create TEcopy and TEcmsk buffers\r
+      TEFree(TEcopy);\r
+      TEFree(TEcmsk);\r
+\r
+      // make copy buffer square (allows for rotation)\r
+      TEcopyxsize=TECIysize;\r
+      TEcopyysize=TECIysize;\r
+      if (TECIxsize>TECIysize)\r
+        {\r
+        TEcopyxsize=TECIxsize;\r
+        TEcopyysize=TECIxsize;\r
+        }\r
+      TEcopy=(char *)TEAlloc(TEcopyxsize*TEcopyysize, "copy buffer");\r
+      TEcmsk=(char *)TEAlloc(TEcopyxsize*TEcopyysize, "copy mask");\r
+\r
+      // copy image into buffer\r
+      for (j=0; j<TECIysize; j++)\r
+        for (i=0; i<TECIxsize; i++)\r
+          {\r
+          if (TEmask[(j+TECIystart)*TExsize+(i+TECIxstart)])\r
+            {\r
+            TEcopy[j*TEcopyxsize+i]=TEimage[(j+TECIystart)*TExsize+(i+TECIxstart)];\r
+            TEcmsk[j*TEcopyxsize+i]=0xf;\r
+            }\r
+          }\r
+      }\r
+    }\r
+  }\r
+\r
+static void TERotateImageAntiClockwise(unsigned char *TERILsrc, int TERILxsize, int TERILysize)\r
+// -- ric: 15/Jul/98 - rotates image (must be square) 90 degrees anti-clockwise\r
+  {\r
+//  unsigned char *TERILsrc=TEimage;\r
+  unsigned char *TERILimg=NULL;\r
+  int i, j, amount;\r
+  if (TERILxsize==TERILysize)\r
+    {\r
+    amount=TERILxsize*TERILysize;\r
+    TERILimg=(char *) TEAlloc(amount, "temporary image buffer for TERotateImageAntiClockwise");\r
+    memcpy(TERILimg, TERILsrc, amount);\r
+    j=TERILysize;\r
+    do\r
+      {\r
+      j--;\r
+      i=TERILxsize;\r
+      do\r
+        {\r
+        i--;\r
+        TERILsrc[(j*TERILxsize)+i]=TERILimg[(i*TERILxsize)+(TERILysize-1-j)];\r
+        } while (i>0);\r
+      } while (j>0);\r
+    TEFree(TERILimg);\r
+    }\r
+  }\r
+\r
+static void TERotateImageClockwise(unsigned char *TERILsrc, int TERILxsize, int TERILysize)\r
+// -- ric: 16/Jul/98 - rotates image (must be square) 90 degrees clockwise\r
+  {\r
+  unsigned char *TERILimg=NULL;\r
+  int i, j, amount;\r
+  if (TERILxsize==TERILysize)\r
+    {\r
+    amount=TERILxsize*TERILysize;\r
+    TERILimg=(char *) TEAlloc(amount, "temporary image buffer for TERotateImageClockwise");\r
+    memcpy(TERILimg, TERILsrc, amount);\r
+    j=TERILysize;\r
+    do\r
+      {\r
+      j--;\r
+      i=TERILxsize;\r
+      do\r
+        {\r
+        i--;\r
+        TERILsrc[(j*TERILxsize)+i]=TERILimg[((TERILxsize-1-i)*TERILxsize)+(j)];\r
+        } while (i>0);\r
+      } while (j>0);\r
+    TEFree(TERILimg);\r
+    }\r
+  }\r
+\r
+static void TEFlipImage(unsigned char *TERILsrc, int TERILxsize, int TERILysize)\r
+// -- ric: 15/Jul/98 - flips image\r
+  {\r
+  unsigned char *TERILimg=NULL;\r
+  int i, j, amount;\r
+  amount=TERILxsize*TERILysize;\r
+  TERILimg=(char *) TEAlloc(amount, "temporary image buffer for TEFlipImage");\r
+  memcpy(TERILimg, TERILsrc, amount);\r
+  j=TERILysize;\r
+  do\r
+    {\r
+    j--;\r
+    i=TERILxsize;\r
+    do\r
+      {\r
+      i--;\r
+      TERILsrc[(j*TERILxsize)+i]=TERILimg[((TERILysize-1-j)*TERILxsize)+i];\r
+      } while (i>0);\r
+    } while (j>0);\r
+  TEFree(TERILimg);\r
+  }\r
+\r
+static void TEMirrorImage(unsigned char *TERILsrc, int TERILxsize, int TERILysize)\r
+// -- ric: 15/Jul/98 - mirrors image\r
+  {\r
+  unsigned char *TERILimg=NULL;\r
+  int i, j, amount;\r
+  amount=TERILxsize*TERILysize;\r
+  TERILimg=(char *) TEAlloc(amount, "temporary image buffer for TEMirrorImage");\r
+  memcpy(TERILimg, TERILsrc, amount);\r
+  j=TERILysize;\r
+  do\r
+    {\r
+    j--;\r
+    i=TERILxsize;\r
+    do\r
+      {\r
+      i--;\r
+      TERILsrc[(j*TERILxsize)+i]=TERILimg[(j*TERILxsize)+(TERILxsize-1-i)];\r
+      } while (i>0);\r
+    } while (j>0);\r
+  TEFree(TERILimg);\r
+  }\r
+\r
+static void TEFillImage(char *TEFIimage, char *TEFImask, int TEFIxsize, int TEFIysize, int TEFIx, int TEFIy, char TEFIcolour)\r
+// -- ric: 29/Jul/98 - Fill procedure - sets up and calls recursive procedure below\r
+  {\r
+  int i,j;\r
+  unsigned char *TEFIfillmask=NULL;  // will eventually contain 0 for ignore and 1 for fill\r
+  TEFIfillmask=(char *) TEAlloc(TEFIxsize*TEFIysize, "temporary fill buffer for TEFillImage");\r
+  TERFillImage(TEFIimage, TEFIfillmask, TEFIxsize, TEFIysize, TEFIx, TEFIy);\r
+\r
+  if (TEFImask)\r
+    for (j=0; j<TEFIysize; j++)\r
+      for (i=0; i<TEFIxsize; i++)\r
+        TEFIfillmask[j*TEFIxsize+i]&=TEFImask[j*TEFIxsize+i];\r
+\r
+  for (j=0; j<TEFIysize; j++)\r
+    for (i=0; i<TEFIxsize; i++)\r
+      if (TEFIfillmask[j*TEFIxsize+i]) TEFIimage[j*TEFIxsize+i]=TEFIcolour;\r
+  TEFree(TEFIfillmask);\r
+  }\r
+\r
+static void TERFillImage(char *TERFIimage, char *TERFImask, int TERFIxsize, int TERFIysize, int TERFIx, int TERFIy)\r
+// -- ric: 29/Jul/98 - Recursive fill procedure\r
+  {\r
+  int x,y;\r
+  char TERFIcc;\r
+  TERFImask[TERFIy*TERFIxsize+TERFIx]=0xf;\r
+  TERFIcc=TERFIimage[TERFIy*TERFIxsize+TERFIx];\r
+  x=TERFIx-1;\r
+  y=TERFIy;\r
+  if (x>=0 && x<TERFIxsize && y>=0 && y<TERFIysize)\r
+    if (!(TERFImask[y*TERFIxsize+x]) && TERFIimage[y*TERFIxsize+x]==TERFIcc)\r
+      TERFillImage(TERFIimage, TERFImask, TERFIxsize, TERFIysize, x, y);\r
+  x=TERFIx;\r
+  y=TERFIy-1;\r
+  if (x>=0 && x<TERFIxsize && y>=0 && y<TERFIysize)\r
+    if (!(TERFImask[y*TERFIxsize+x]) && TERFIimage[y*TERFIxsize+x]==TERFIcc)\r
+      TERFillImage(TERFIimage, TERFImask, TERFIxsize, TERFIysize, x, y);\r
+  x=TERFIx+1;\r
+  y=TERFIy;\r
+  if (x>=0 && x<TERFIxsize && y>=0 && y<TERFIysize)\r
+    if (!(TERFImask[y*TERFIxsize+x]) && TERFIimage[y*TERFIxsize+x]==TERFIcc)\r
+      TERFillImage(TERFIimage, TERFImask, TERFIxsize, TERFIysize, x, y);\r
+  x=TERFIx;\r
+  y=TERFIy+1;\r
+  if (x>=0 && x<TERFIxsize && y>=0 && y<TERFIysize)\r
+    if (!(TERFImask[y*TERFIxsize+x]) && TERFIimage[y*TERFIxsize+x]==TERFIcc)\r
+      TERFillImage(TERFIimage, TERFImask, TERFIxsize, TERFIysize, x, y);\r
+  }\r
+\r
+static void TEShiftColours(char *TESCimage, char *TESCmask, int TESCxsize, int TESCysize, int amount)\r
+// -- ric: 17/Jul/98 - Colour shift idea by McGrue and Tarkuss\r
+  {\r
+  int i,j;\r
+  j=TESCysize;\r
+  do\r
+    {\r
+    j--;\r
+    i=TESCxsize;\r
+    do\r
+      {\r
+      i--;\r
+      if (!(TESCmask))\r
+        TESCimage[j*TESCxsize+i]+=amount;\r
+      else\r
+        if (TESCmask[j*TESCxsize+i])\r
+          TESCimage[j*TESCxsize+i]+=amount;\r
+      } while (i>0);\r
+    } while (j>0);\r
+  }\r
+\r
+\r
+static void TEUndo (void)\r
+  {\r
+  unsigned char *tempimg=TEimage;\r
+  TEimage=TEundo;\r
+  TEundo=tempimg;\r
+  }\r
+\r
+static void *TEAlloc(unsigned long amount, char *whatfor)\r
+// -- ric: 14/Jul/98 - TE's custom, error-handling memory allocation routine\r
+  {\r
+  char *TETmpBuf=NULL;\r
+\r
+  amount += 3; // aen\r
+  amount &= ~3;\r
+\r
+//  static char DebugBuf[256];\r
+  TETmpBuf=(char *) malloc(amount);\r
+  if (!TETmpBuf)\r
+    {\r
+    sprintf(TEtempstr, "Unable to allocate %ld bytes for %s.", amount, whatfor);\r
+    err(TEtempstr);\r
+    }\r
+  memset(TETmpBuf,0, amount); // Clear allocated mem\r
+  /*\r
+  __asm {  // aen\r
+    mov edi,TETmpBuf\r
+    mov ecx,amount\r
+    shr ecx,2\r
+    xor eax,eax\r
+    cld\r
+    rep stosd\r
+    } ;\r
+  */\r
+  return TETmpBuf;\r
+  }\r
+\r
+#ifdef JUNK\r
+static void TEFree(void *TETmpBuf)\r
+// -- ric: 14/Jul/98 - TE's custom memory freeing routine\r
+  {\r
+  if (TETmpBuf) free(TETmpBuf);  // Only free memory if previously allocated\r
+  TETmpBuf=NULL;                 // Set freed memory to point to NULL\r
+  }\r
+#endif\r
+\r
+static void MBClickHandler(TEREGION *TEBaseReg)\r
+// -- ric: 20/Jul/98 - Mouse button click handler\r
+  {\r
+  int i,j;\r
+  ReadMouse();\r
+  if (TEBaseReg)\r
+    {\r
+    if (!TEBaseReg->data) err("No region data defined.");\r
+    if (((struct DATABASEREGION *)TEBaseReg->data)->infotype!=DITBASEREGION)\r
+      err("MBClickHandler called with invalid base region.");\r
+    }\r
+\r
+  for (i=0; i<3; ++i)\r
+    {\r
+    if (mb & (i+1))\r
+      {\r
+      if (MBStatus[i] == MOUSECLICKED)\r
+        MBStatus[i]=MOUSEPRESSED;\r
+      else if (MBStatus[i] == MOUSENONE)\r
+        {\r
+        MBStatus[i] = MOUSECLICKED;\r
+        if (TEBaseReg)\r
+          {\r
+          TEOldReg = NULL;\r
+          for (j=0; j<((struct DATABASEREGION *)TEBaseReg->data)->numofregs; ++j)\r
+            if (MouseOverTEreg(TEBaseReg+j)&&!((TEBaseReg+j)->bitflags&REGNOCLICK))\r
+              TEOldReg = TEBaseReg+j;\r
+          }\r
+        }\r
+      }\r
+    else\r
+      {\r
+      if (MBStatus[i] <= MOUSERELEASE)\r
+        MBStatus[i] = MOUSENONE;\r
+      else if (MBStatus[i]>MOUSERELEASE)\r
+        MBStatus[i] = MOUSERELEASE;\r
+      }\r
+    }\r
+  }\r
+\r
+static void AboutTE (void)\r
+// Just here to test the mouse input loop - remove this later\r
+  {\r
+  TEREGION *TEr=NULL;\r
+  TEREGION *TEaboutwin=NULL;\r
+//  int amxsize=150, amysize=82;\r
+  int amxofs=60, amyofs=60;\r
+  int i,i2,j,j2;\r
+  int QuitAW=FALSE;\r
+  int moving=FALSE;\r
+\r
+  // Allocate memory\r
+  TEaboutwin=(struct TEREGION *) TEAlloc((sizeof (struct TEREGION))*4,"about window regions");\r
+  TEr=TEaboutwin;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=amxofs;\r
+  TEr->x2=TEr->x1+150;\r
+  TEr->y1=amyofs;\r
+  TEr->y2=TEr->y1+82;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE|REGNOCLICK;\r
+//  TEr->drawproc=NULL;\r
+  sprintf(strbuf, "About MapEd %s Tile Editor", ME2_VERSION);\r
+  TEr->caption=TENewText(strbuf, "About window");\r
+  TEr->data=(struct DATABASEREGION *) TEAlloc(sizeof (struct DATABASEREGION), "base region data");\r
+  ((struct DATABASEREGION *)(TEr->data))->infotype=DITBASEREGION;\r
+  ((struct DATABASEREGION *)(TEr->data))->numofregs=4;\r
+  ((struct DATABASEREGION *)(TEr->data))->windowtype=DBRSTDWINDOW;\r
+\r
+  TEr+=1;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=0;\r
+  TEr->x2=TEaboutwin->x2-TEaboutwin->x1-9;\r
+  TEr->y1=0;\r
+  TEr->y2=8;\r
+  TEr->bitflags=REGACTIVE;\r
+//  TEr->drawproc=NULL;\r
+//  TEr->caption=NULL;\r
+\r
+  TEr+=1;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEaboutwin->x2-TEaboutwin->x1-9;\r
+  TEr->x2=TEaboutwin->x2-TEaboutwin->x1-3;\r
+  TEr->y1=3;\r
+  TEr->y2=6;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+//  TEr->caption=NULL;\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "quit button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=NULL;\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&QuitAW;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=TRUE;\r
+\r
+  TEr+=1;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=60;\r
+  TEr->x2=90;\r
+  TEr->y1=62;\r
+  TEr->y2=72;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=TENewText("ok","ok button caption");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "quit button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=NULL;\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&QuitAW;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=TRUE;\r
+  TEr=TEaboutwin+1; // point to title bar area\r
+\r
+  while (!QuitAW)\r
+    {\r
+    // Mouse button click handler routines\r
+    MBClickHandler(TEaboutwin);\r
+\r
+    amxofs=TEaboutwin->x1;\r
+    amyofs=TEaboutwin->y1;\r
+    if ((MBStatus[0]|MBStatus[1])==MOUSERELEASE)\r
+      moving=FALSE;\r
+\r
+    // Draw loop\r
+    RenderTileEdit(TEregs);\r
+    RenderTileEdit(TEaboutwin);\r
+\r
+    sprintf(strbuf, "MapEd v.%s", ME2_VERSION);\r
+    GotoXY(amxofs+52, amyofs+16); printstring(strbuf);\r
+    GotoXY(amxofs+31, amyofs+22); printstring("Copyright (C) 1998 vecna");\r
+    GotoXY(amxofs+40, amyofs+28); printstring("All Rights Reserved");\r
+    sprintf(strbuf, "MapEd v.%s Tile Editor", ME2_VERSION);\r
+    GotoXY(amxofs+29, amyofs+41); printstring(strbuf);\r
+    GotoXY(amxofs+20, amyofs+47); printstring("Additional code by Richard Lau");\r
+    DrawMouse();\r
+    ShowPage();\r
+\r
+    // Move window?\r
+    if (MouseOverTEreg(TEr) && TEOldReg==TEr)\r
+      {\r
+      if ((MBStatus[0]|MBStatus[1])==MOUSECLICKED)\r
+        {\r
+        i2=TEaboutwin->x2-TEaboutwin->x1;\r
+        j2=TEaboutwin->y2-TEaboutwin->y1;\r
+        i=mx-TEaboutwin->x1;\r
+        j=my-TEaboutwin->y1;\r
+        moving=TRUE;\r
+        }\r
+      }\r
+      if ((MBStatus[0]|MBStatus[1])==MOUSEPRESSED && moving)\r
+        {\r
+        if (((mx-i)<16)) TEaboutwin->x1=16;\r
+        else if (((mx-i+i2)>=(16+sx))) TEaboutwin->x1=16+(sx-i2);\r
+        else TEaboutwin->x1=mx-i;\r
+        if (((my-j)<16)) TEaboutwin->y1=16;\r
+        else if (((my-j+j2)>=(16+sy))) TEaboutwin->y1=16+(sy-j2);\r
+        else TEaboutwin->y1=my-j;\r
+        TEaboutwin->x2=TEaboutwin->x1+i2;\r
+        TEaboutwin->y2=TEaboutwin->y1+j2;\r
+        }\r
+\r
+    if (key[SCAN_ESC])\r
+      {\r
+      key[SCAN_ESC]=0;\r
+      QuitAW=TRUE;\r
+      }\r
+\r
+    }\r
+  // free TEaboutwin\r
+  TEFreeReg(TEaboutwin);\r
+  }\r
+\r
+static void RestartTileEditor(void)\r
+// -- ric: 17/Jul/98 - reinit the tile editor\r
+  {\r
+  int i,j, amount;\r
+  unsigned char *TERTEmask=NULL;\r
+\r
+  switch (TEtype)\r
+    {\r
+    case TETILE:                 // edit tile TEtile\r
+          i=TExsize;\r
+          j=TEysize;\r
+          amount=i*j;\r
+          TERTEmask=(unsigned char *) TEAlloc(amount, "tile editor mask backup buffer");\r
+\r
+          // backup mask\r
+          memcpy(TERTEmask, TEmask, amount);\r
+\r
+          // Restart editor\r
+          DeinitTEImage();\r
+          TEFreeReg(TEregs);\r
+          TEtype=TETILE;\r
+          TExsize=i;\r
+          TEysize=j;\r
+          TEsource=vsp+(256*TEtile);\r
+          InitTEImage();\r
+          SetupRegions();\r
+\r
+          // Restore mask\r
+          memcpy(TEmask, TERTEmask, amount);\r
+          TEFree(TERTEmask);\r
+          break;\r
+    }\r
+  }\r
+\r
+static void InitTEImage (void)\r
+// Copy the image data being edited and allocate memory for the image buffers\r
+  {\r
+  int i;\r
+  int amount=TExsize*TEysize;\r
+  if (TEtype==TEUNKNOWN) err("Unrecognised tile editor graphics type.");\r
+  if (!TEsource) err("Invalid input image.");\r
+  TEimage=(unsigned char *) TEAlloc(amount, "tile editor image buffer");\r
+  TEmask=(unsigned char *) TEAlloc(amount, "tile editor mask buffer");\r
+  TEundo=(unsigned char *) TEAlloc(amount, "tile editor undo buffer");\r
+//  if (!TEimage || !TEundo) err("Cannot allocate memory for tile editor image buffers.");\r
+  memcpy(TEimage, TEsource, amount); // copy original image into edit buffer\r
+  memcpy(TEundo, TEimage, amount);   // copy original image into undo buffer\r
+\r
+  // if editing tile find the animation strand (if any) tile belongs to\r
+  TEanimstrand=0;       // Does not belong to an animation strand\r
+  if (TEtype==TETILE)\r
+    {\r
+    i=100;\r
+    do\r
+      {\r
+      i--;\r
+      if (vspanim[i].delay && TEtile<=vspanim[i].finish && TEtile>=vspanim[i].start)\r
+        TEanimstrand=i+1;\r
+      } while (i>0);\r
+    }\r
+  }\r
+\r
+static void DeinitTEImage(void)\r
+// Unallocate allocated image buffers - copy edited buffer into original\r
+  {\r
+  memcpy(TEsource, TEimage, (TExsize*TEysize));\r
+  TEFree(TEimage);\r
+  TEFree(TEmask);\r
+  TEFree(TEundo);\r
+  TEtype=TEUNKNOWN;         // Clear type info for error checking purposes\r
+  TExsize=0;\r
+  TEysize=0;\r
+  }\r
+\r
+static void SetupRegions (void)\r
+  {\r
+  int temp;\r
+  TEREGION *TEr=NULL;\r
+\r
+  // Allocate memory\r
+  TEregs=(struct TEREGION *) TEAlloc((sizeof (struct TEREGION))*MAX_REGIONS,"tile edit regions");\r
+\r
+  TEr=TEregs+BASEREG;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=16;\r
+  TEr->x2=TEr->x1+sx;\r
+  TEr->y1=16;\r
+  TEr->y2=TEr->y1+sy;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE|REGNOCLICK;\r
+  TEr->drawproc=NULL;\r
+  TEr->caption=NULL;\r
+  switch (TEtype)\r
+    {\r
+    case TETILE:\r
+      sprintf(TEtempstr, "MapEd %s Tile Editor - Tile: %d", ME2_VERSION, TEtile);\r
+      if (TEanimstrand)\r
+        sprintf(TEtempstr, "MapEd %s Tile Editor - Tile: %d (anim strand: %d, frame: %d/%d)",\r
+          ME2_VERSION,\r
+          TEtile, TEanimstrand-1, TEtile-vspanim[TEanimstrand-1].start+1,\r
+          vspanim[TEanimstrand-1].finish-vspanim[TEanimstrand-1].start+1);\r
+      SetCaption(TEr, TEtempstr);\r
+      break;\r
+    }\r
+  TEr->data=(struct DATABASEREGION *) TEAlloc(sizeof (struct DATABASEREGION), "base region data");\r
+  ((struct DATABASEREGION *)(TEr->data))->infotype=DITBASEREGION;\r
+  ((struct DATABASEREGION *)(TEr->data))->numofregs=MAX_REGIONS;\r
+  ((struct DATABASEREGION *)(TEr->data))->windowtype=DBRSTDWINDOW;\r
+\r
+  TEr=TEregs+HELPBAR;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=3;\r
+  TEr->x2=TEregs->x2-TEregs->x1-3;\r
+  TEr->y1=TEregs->y2-TEregs->y1-13;\r
+  TEr->y2=TEregs->y2-TEregs->y1-3;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE|REGNOCLICK;\r
+  TEr->drawproc=&DrawHelpBar;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  temp=(((TEregs->x2-TEregs->x1-8)>>6)<<5)+2;\r
+  TEr=TEregs+PALETTE;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=((TEregs->x2-TEregs->x1)>>1)-temp;\r
+  TEr->x2=((TEregs->x2-TEregs->x1)>>1)+temp;\r
+  TEr->y1=TEregs->y2-TEregs->y1-36;\r
+  TEr->y2=TEregs->y2-TEregs->y1-16;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawPalette;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  TEr=TEregs+EDITREG;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=3;\r
+  TEr->x2=3+128+4;\r
+  TEr->y1=10;\r
+  TEr->y2=10+128+4;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawEditReg;\r
+  TEr->caption=NULL;\r
+  TEr->data=(struct DATAEDITREG *) TEAlloc(sizeof (struct DATAEDITREG), "edit region data");\r
+  ((struct DATAEDITREG *)(TEr->data))->infotype=DITEDITREG;\r
+\r
+  TEr=TEregs+PREVIEW;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs->x2-TEregs->x1-23;\r
+  TEr->x2=TEregs->x2-TEregs->x1-3;\r
+  TEr->y1=10;\r
+  TEr->y2=10+20;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawPreview;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  TEr=TEregs+SCOLBOX;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[EDITREG].x1;\r
+  TEr->x2=TEr->x1+20;\r
+  TEr->y1=TEregs[EDITREG].y2+2;\r
+  TEr->y2=TEr->y1+16;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawSColBox;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  TEr=TEregs+SCTXTBX;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[SCOLBOX].x2+2;\r
+  TEr->x2=TEregs[EDITREG].x2;\r
+  TEr->y1=TEregs[SCOLBOX].y1;\r
+  TEr->y2=TEregs[SCOLBOX].y2;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE|REGNOCLICK;\r
+  TEr->drawproc=&DrawSCTxtBx;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  TEr=TEregs+QUITBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs->x2-TEregs->x1-9;\r
+  TEr->x2=TEregs->x2-TEregs->x1-3;\r
+  TEr->y1=3;\r
+  TEr->y2=6;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "quit button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Exit", "quit button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbitflags;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=TEQUITCUR;\r
+\r
+  TEr=TEregs+EDMDFRM;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[EDITREG].x2+3;\r
+  TEr->x2=TEr->x1+34;\r
+  TEr->y1=TEregs[EDITREG].y1;\r
+  TEr->y2=TEr->y1+11*5+3;     // 10*number of buttons\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE|REGNOCLICK;\r
+  TEr->drawproc=&DrawSunkRegion;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  TEr=TEregs+DRAWBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[EDMDFRM].x1+2;\r
+  TEr->x2=TEregs[EDMDFRM].x2-2;\r
+  TEr->y1=TEregs[EDMDFRM].y1+2;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"Draw");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "draw button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Draw mode", "draw button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=RADIOBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEeditmode;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=EDDRAW;\r
+\r
+  TEr=TEregs+FILLBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[EDMDFRM].x1+2;\r
+  TEr->x2=TEregs[EDMDFRM].x2-2;\r
+  TEr->y1=TEregs[EDMDFRM].y1+2+11;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"Fill");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "fill button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Fill mode", "fill button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=RADIOBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEeditmode;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=EDFILL;\r
+\r
+  TEr=TEregs+PICKBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[EDMDFRM].x1+2;\r
+  TEr->x2=TEregs[EDMDFRM].x2-2;\r
+  TEr->y1=TEregs[EDMDFRM].y1+2+22;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"Pick");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "pick button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Pick color mode", "pick button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=RADIOBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEeditmode;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=EDPICK;\r
+\r
+  TEr=TEregs+MASKBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[EDMDFRM].x1+2;\r
+  TEr->x2=TEregs[EDMDFRM].x2-2;\r
+  TEr->y1=TEregs[EDMDFRM].y1+2+33;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"Select");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "select button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Select mode", "select button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=RADIOBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEeditmode;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=EDMASK;\r
+\r
+  TEr=TEregs+PSTEBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[EDMDFRM].x1+2;\r
+  TEr->x2=TEregs[EDMDFRM].x2-2;\r
+  TEr->y1=TEregs[EDMDFRM].y1+2+44;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"Paste");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "paste button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Paste mode", "paste button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=RADIOBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEeditmode;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=EDPASTE;\r
+\r
+  TEr=TEregs+IND_FRM;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[SCTXTBX].x2+2;\r
+  TEr->x2=TEr->x1+12*4-1; // 10*number of buttons\r
+  TEr->y1=TEregs[SCTXTBX].y1;\r
+  TEr->y2=TEr->y1+13;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE|REGNOCLICK;\r
+  TEr->drawproc=&DrawSunkRegion;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  TEr=TEregs+MASKIND;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[IND_FRM].x1+2;\r
+  TEr->x2=TEr->x1+10;\r
+  TEr->y1=TEregs[IND_FRM].y1+2;\r
+  TEr->y2=TEregs[IND_FRM].y2-2;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"m");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "mask indicator data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("mask", "mask indicator text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=PUSHBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbitflags;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=TESHOWMSK;\r
+\r
+  TEr=TEregs+GRIDIND;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[MASKIND].x2+1;\r
+  TEr->x2=TEr->x1+10;\r
+  TEr->y1=TEregs[IND_FRM].y1+2;\r
+  TEr->y2=TEregs[IND_FRM].y2-2;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"g");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "grid button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("grid", "grid indicator text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=PUSHBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbitflags;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=TEGRID;\r
+\r
+  TEr=TEregs+ANIMIND;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[GRIDIND].x2+1;\r
+  TEr->x2=TEr->x1+10;\r
+  TEr->y1=TEregs[IND_FRM].y1+2;\r
+  TEr->y2=TEregs[IND_FRM].y2-2;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"a");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "animation button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("animation", "animation indicator text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=PUSHBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbitflags;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=TEANIMATE;\r
+\r
+  TEr=TEregs+PXHLIND;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[ANIMIND].x2+1;\r
+  TEr->x2=TEr->x1+10;\r
+  TEr->y1=TEregs[IND_FRM].y1+2;\r
+  TEr->y2=TEregs[IND_FRM].y2-2;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr,"h");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "highlight button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("pixel highlight", "highlight indicator text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=PUSHBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbitflags;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=TEHILIGHT;\r
+\r
+  TEr=TEregs+BUT_FRM;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[EDMDFRM].x1;\r
+  TEr->x2=TEregs[EDMDFRM].x2;\r
+  TEr->y1=TEregs[EDMDFRM].y2+2;\r
+  TEr->y2=TEr->y1+11*4+3;     // 10*number of buttons\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE|REGNOCLICK;\r
+  TEr->drawproc=&DrawSunkRegion;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  TEr=TEregs+UNDOBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[BUT_FRM].x1+2;\r
+  TEr->x2=TEregs[BUT_FRM].x2-2;\r
+  TEr->y1=TEregs[BUT_FRM].y1+2;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr, "Undo");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "undo button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Undo last action", "undo button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbfa;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=BFAUNDO;\r
+\r
+  TEr=TEregs+COPYBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[BUT_FRM].x1+2;\r
+  TEr->x2=TEregs[BUT_FRM].x2-2;\r
+  TEr->y1=TEregs[BUT_FRM].y1+2+11;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr, "Copy");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "copy button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Copy selected area", "copy button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbfa;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=BFACOPY;\r
+\r
+  TEr=TEregs+CUT_BUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[BUT_FRM].x1+2;\r
+  TEr->x2=TEregs[BUT_FRM].x2-2;\r
+  TEr->y1=TEregs[BUT_FRM].y1+2+22;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr, "Cut");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "cut button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Cut selected area", "cut button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbfa;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=BFACUT;\r
+\r
+  TEr=TEregs+CLR_BUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[BUT_FRM].x1+2;\r
+  TEr->x2=TEregs[BUT_FRM].x2-2;\r
+  TEr->y1=TEregs[BUT_FRM].y1+2+33;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr, "Clear");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "clear button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Clear selected area", "clear button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbfa;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=BFACLEAR;\r
+\r
+  TEr=TEregs+TOOLFRM;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[BUT_FRM].x2+2;\r
+  TEr->x2=TEr->x1+34;\r
+  TEr->y1=TEregs[BUT_FRM].y1;\r
+  TEr->y2=TEr->y1+11*4+3;     // 10*number of buttons\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE|REGNOCLICK;\r
+  TEr->drawproc=&DrawSunkRegion;\r
+  TEr->caption=NULL;\r
+  TEr->data=NULL;\r
+\r
+  TEr=TEregs+RIACBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[TOOLFRM].x1+2;\r
+  TEr->x2=TEregs[TOOLFRM].x2-2;\r
+  TEr->y1=TEregs[TOOLFRM].y1+2;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr, "Turn L");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "rotate button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Rotate anticlockwise", "rotate button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbfa;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=BFARIAC;\r
+\r
+  TEr=TEregs+RIC_BUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[TOOLFRM].x1+2;\r
+  TEr->x2=TEregs[TOOLFRM].x2-2;\r
+  TEr->y1=TEregs[TOOLFRM].y1+2+11;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr, "Turn R");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "rotate button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Rotate clockwise", "rotate button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbfa;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=BFARIC;\r
+\r
+  TEr=TEregs+FLIPBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[TOOLFRM].x1+2;\r
+  TEr->x2=TEregs[TOOLFRM].x2-2;\r
+  TEr->y1=TEregs[TOOLFRM].y1+2+22;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr, "Flip");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "flip button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Flip", "flip button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbfa;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=BFAFLIP;\r
+\r
+  TEr=TEregs+MIRRBUT;\r
+  TEr->infotype=DITREGION;\r
+  TEr->x1=TEregs[TOOLFRM].x1+2;\r
+  TEr->x2=TEregs[TOOLFRM].x2-2;\r
+  TEr->y1=TEregs[TOOLFRM].y1+2+33;\r
+  TEr->y2=TEr->y1+10;\r
+  TEr->bitflags=REGACTIVE|REGVISIBLE;\r
+  TEr->drawproc=&DrawButton;\r
+  TEr->caption=NULL;\r
+  SetCaption(TEr, "Mirror");\r
+  TEr->data=(struct DATABUTTON *) TEAlloc(sizeof (struct DATABUTTON), "mirror button data");\r
+  ((struct DATABUTTON *)(TEr->data))->infotype=DITBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->text=TENewText("Mirror", "mirror button text");\r
+  ((struct DATABUTTON *)(TEr->data))->buttontype=CLICKBUTTON;\r
+  ((struct DATABUTTON *)(TEr->data))->bitflag=&TEbfa;\r
+  ((struct DATABUTTON *)(TEr->data))->bitvalue=BFAMIRROR;\r
+\r
+  }\r
+\r
+static void DestroyRegions (TEREGION **TEBaseReg)\r
+  {\r
+  TEREGION *TEr=NULL;\r
+  int i;\r
+  if (!(*TEBaseReg)->data) err("No region data defined.");\r
+  if (((struct DATABASEREGION *)(*TEBaseReg)->data)->infotype!=DITBASEREGION)\r
+    err("DestroyRegions called with invalid base region.");\r
+\r
+  i=((struct DATABASEREGION *)(*TEBaseReg)->data)->numofregs;\r
+  do\r
+    {\r
+    i--;\r
+    TEr=(*TEBaseReg)+i;\r
+    TEFree(TEr->caption);\r
+    if (TEr->data)\r
+      {\r
+      switch (((struct DATABASEREGION *)(TEr->data))->infotype)\r
+        {\r
+        case DITBASEREGION:\r
+          break;\r
+        case DITBUTTON:\r
+          TEFree(((struct DATABUTTON *)TEr->data)->text);\r
+          break;\r
+        }\r
+      }\r
+    TEFree(TEr->data);\r
+    } while (i>0);\r
+  TEFree(*TEBaseReg);\r
+//  if (*TEBaseReg) err("Not freed");\r
+  }\r
+\r
+static void RenderTileEdit (TEREGION *TEBaseReg)\r
+// -- ric: 20/Jul/98 - updatedt to be a little more generic ;)\r
+  {\r
+  int i;\r
+  TExoffs=TEBaseReg->x1;\r
+  TEyoffs=TEBaseReg->y1;\r
+  if (!TEBaseReg->data) err("No region data defined.");\r
+  if (((struct DATABASEREGION *)TEBaseReg->data)->infotype!=DITBASEREGION)\r
+    err("RenderTileEdit called with invalid base region.");\r
+  switch (((struct DATABASEREGION *)TEBaseReg->data)->windowtype)\r
+    {\r
+    case DBRSTDWINDOW:\r
+      Window(TExoffs,TEyoffs,TEBaseReg->x2,TEBaseReg->y2, ((TEBaseReg->caption) ? TEBaseReg->caption : ""));\r
+      break;\r
+    case DBRPANEL:\r
+      stdwindow(TExoffs,TEyoffs,TEBaseReg->x2,TEBaseReg->y2);\r
+      break;\r
+    }\r
+\r
+  i=((struct DATABASEREGION *)TEBaseReg->data)->numofregs;\r
+  do\r
+    {\r
+    i--;\r
+    if ((TEBaseReg+i)->bitflags&REGVISIBLE) (TEBaseReg+i)->drawproc(TEBaseReg+i);\r
+    } while (i>1);\r
+  }\r
+\r
+void TileEdit (void)\r
+  {\r
+  int i,j;\r
+  int TEoldeditmode=EDDRAW;\r
+  if (TEbitflags&TEQUITCUR) TEbitflags^=TEQUITCUR;\r
+\r
+  InitTEImage();\r
+  SetupRegions();\r
+  TEeditmode=EDDRAW;  // Default to drawing mode\r
+  if (TEbitflags&TESHOWMSK) TEbitflags^=TESHOWMSK;\r
+\r
+  while (!(TEbitflags&TEQUITCUR))\r
+    {\r
+    // Mouse button click handler routines\r
+    MBClickHandler(TEregs);\r
+\r
+    TEbfa=BFANONE;          // Reset button action flag\r
+    if (TEeditmode==EDMASK && TEoldeditmode!=EDMASK) TEbitflags|=TESHOWMSK;\r
+    if (TEeditmode==EDMASK && TEoldeditmode==EDMASK && !(TEbitflags&TESHOWMSK))\r
+      TEeditmode=EDDRAW;\r
+    TEoldeditmode=TEeditmode;\r
+\r
+    // Draw loop\r
+    RenderTileEdit(TEregs);\r
+    DrawMouse();\r
+    ShowPage();\r
+\r
+    if (key[SCAN_ESC])\r
+      {\r
+      key[SCAN_ESC]=0;\r
+      TEbitflags|=TEQUITCUR;\r
+      }\r
+    if ((key[SCAN_CTRL] && key[SCAN_Z]) || (TEbfa&BFAUNDO) || (key[SCAN_U] && !(key[SCAN_CTRL])) && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_U]=0;\r
+      key[SCAN_Z]=0;\r
+      TEUndo();\r
+      }\r
+    if (key[SCAN_CTRL] && key[SCAN_R])\r
+      {\r
+      key[SCAN_R]=0;\r
+      memcpy(TEundo, TEimage, TExsize*TEysize);   // copy image into undo buffer\r
+      memcpy(TEimage, TEsource, TExsize*TEysize); // copy original image into edit buffer\r
+      }\r
+    if (key[SCAN_CTRL] && key[SCAN_A])\r
+      {\r
+      key[SCAN_A]=0;\r
+      j=TEysize*TExsize;\r
+      memset(TEmask, 0xf, j);\r
+      }\r
+    if (key[SCAN_CTRL] && key[SCAN_U])\r
+      {\r
+      key[SCAN_U]=0;\r
+      j=TEysize*TExsize;\r
+      memset(TEmask, 0x0, j);\r
+      }\r
+    if ((TEbfa&BFACOPY) || (key[SCAN_CTRL] && key[SCAN_C]) && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_C]=0;\r
+      TECopyImage();\r
+      }\r
+    if ((TEbfa&BFACUT) || (key[SCAN_CTRL] && key[SCAN_X]) && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_X]=0;\r
+      TECopyImage();\r
+      TEClearImage();\r
+      }\r
+    if (key[SCAN_CTRL] && key[SCAN_V] && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_V]=0;\r
+      TEeditmode=EDPASTE;\r
+      }\r
+    if ((TEbfa&BFACLEAR) || key[SCAN_DEL] && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_DEL]=0;\r
+      TEClearImage();\r
+      }\r
+    if (key[SCAN_Q] && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_Q]=0;\r
+      if (TEtype==TETILE && TEanimstrand)\r
+        {\r
+        if (TEtile>vspanim[TEanimstrand-1].start)\r
+          {\r
+          // Change tile index\r
+          TEtile-=1;\r
+          i=TExsize;\r
+          j=TEysize;\r
+\r
+          // Restart editor\r
+          RestartTileEditor();\r
+          }\r
+        }\r
+      }\r
+    if (key[SCAN_W] && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_W]=0;\r
+      if (TEtype==TETILE && TEanimstrand)\r
+        {\r
+        if (TEtile<vspanim[TEanimstrand-1].finish)\r
+          {\r
+          // Change tile index\r
+          TEtile+=1;\r
+          i=TExsize;\r
+          j=TEysize;\r
+\r
+          // Restart editor\r
+          RestartTileEditor();\r
+          }\r
+        }\r
+      }\r
+\r
+    if ((TEbfa&BFARIAC) || key[SCAN_L] && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_L]=0;\r
+      switch (TEeditmode)\r
+        {\r
+        case EDDRAW:\r
+          memcpy(TEundo, TEimage, TExsize*TEysize);\r
+          TERotateImageAntiClockwise(TEimage, TExsize, TEysize);\r
+          break;\r
+        case EDMASK:\r
+          TERotateImageAntiClockwise(TEmask, TExsize, TEysize);\r
+          break;\r
+        case EDPASTE:\r
+          if (TEcopy)\r
+            {\r
+            TERotateImageAntiClockwise(TEcopy, TEcopyxsize, TEcopyysize);\r
+            TERotateImageAntiClockwise(TEcmsk, TEcopyxsize, TEcopyysize);\r
+            }\r
+          break;\r
+        }\r
+      }\r
+    if ((TEbfa&BFARIC) || key[SCAN_R] && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_R]=0;\r
+      switch (TEeditmode)\r
+        {\r
+        case EDDRAW:\r
+          memcpy(TEundo, TEimage, TExsize*TEysize);\r
+          TERotateImageClockwise(TEimage, TExsize, TEysize);\r
+          break;\r
+        case EDMASK:\r
+          TERotateImageClockwise(TEmask, TExsize, TEysize);\r
+          break;\r
+        case EDPASTE:\r
+          if (TEcopy)\r
+            {\r
+            TERotateImageClockwise(TEcopy, TEcopyxsize, TEcopyysize);\r
+            TERotateImageClockwise(TEcmsk, TEcopyxsize, TEcopyysize);\r
+            }\r
+          break;\r
+        }\r
+      }\r
+    if ((TEbfa&BFAMIRROR) || key[SCAN_X] && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_X]=0;\r
+      switch (TEeditmode)\r
+        {\r
+        case EDDRAW:\r
+          memcpy(TEundo, TEimage, TExsize*TEysize);\r
+          TEMirrorImage(TEimage, TExsize, TEysize);\r
+          break;\r
+        case EDMASK:\r
+          TEMirrorImage(TEmask, TExsize, TEysize);\r
+          break;\r
+        case EDPASTE:\r
+          if (TEcopy)\r
+            {\r
+            TEMirrorImage(TEcopy, TEcopyxsize, TEcopyysize);\r
+            TEMirrorImage(TEcmsk, TEcopyxsize, TEcopyysize);\r
+            }\r
+          break;\r
+        }\r
+      }\r
+    if ((TEbfa&BFAFLIP) || key[SCAN_Y] && ((MBStatus[0]|MBStatus[1])==MOUSENONE))\r
+      {\r
+      key[SCAN_Y]=0;\r
+      switch (TEeditmode)\r
+        {\r
+        case EDDRAW:\r
+          memcpy(TEundo, TEimage, TExsize*TEysize);\r
+          TEFlipImage(TEimage, TExsize, TEysize);\r
+          break;\r
+        case EDMASK:\r
+          TEFlipImage(TEmask, TExsize, TEysize);\r
+          break;\r
+        case EDPASTE:\r
+          if (TEcopy)\r
+            {\r
+            TEFlipImage(TEcopy, TEcopyxsize, TEcopyysize);\r
+            TEFlipImage(TEcmsk, TEcopyxsize, TEcopyysize);\r
+            }\r
+          break;\r
+        }\r
+      }\r
+    if (key[SCAN_F1])\r
+      {\r
+      key[SCAN_F1]=0;\r
+\r
+      // Deacitvate tile editor\r
+      for (i=0; i<MAX_REGIONS; i++)\r
+        if (TEregs[i].bitflags&REGACTIVE) TEregs[i].bitflags^=REGACTIVE;\r
+\r
+      AboutTE();\r
+\r
+      // Reacitvate tile editor\r
+      for (i=0; i<MAX_REGIONS; i++)\r
+        TEregs[i].bitflags|=REGACTIVE;\r
+      }\r
+    if (key[SCAN_A])\r
+      {\r
+      key[SCAN_A]=0;\r
+      lcolor+=1;\r
+      lcolor&=255;\r
+      }\r
+    if (key[SCAN_D])\r
+      {\r
+      key[SCAN_D]=0;\r
+      rcolor+=1;\r
+      rcolor&=255;\r
+      }\r
+    if (key[SCAN_Z])\r
+      {\r
+      key[SCAN_Z]=0;\r
+      if (!lcolor) lcolor=255;\r
+      else lcolor-=1;\r
+      }\r
+    if (key[SCAN_C])\r
+      {\r
+      key[SCAN_C]=0;\r
+      if (!rcolor) rcolor=255;\r
+      else rcolor-=1;\r
+      }\r
+    if (key[SCAN_G])\r
+      {\r
+      key[SCAN_G]=0;\r
+      TEbitflags^=TEGRID;\r
+      }\r
+    if (key[SCAN_H])\r
+      {\r
+      key[SCAN_H]=0;\r
+      TEbitflags^=TEHILIGHT;\r
+      }\r
+    if (key[SCAN_I])\r
+      {\r
+      key[SCAN_I]=0;\r
+      j=TEysize*TExsize;\r
+      do\r
+        {\r
+        j--;\r
+        TEmask[j]^=0xf;\r
+        } while (j>0);\r
+      }\r
+    if (key[SCAN_M])\r
+      {\r
+      key[SCAN_M]=0;\r
+      if (TEeditmode==EDMASK)\r
+        TEeditmode=EDDRAW;\r
+      TEbitflags^=TESHOWMSK;\r
+      }\r
+    if (key[SCAN_SPACE])\r
+      {\r
+      key[SCAN_SPACE]=0;\r
+      if (TEeditmode==EDDRAW)\r
+        {\r
+        TEeditmode=EDMASK;\r
+        TEbitflags|=TESHOWMSK;\r
+        }\r
+      else\r
+        TEeditmode=EDDRAW;\r
+      }\r
+    if (key[SCAN_PGUP])\r
+      {\r
+      key[SCAN_PGUP]=0;\r
+      switch (TEeditmode)\r
+        {\r
+        case EDDRAW:\r
+        case EDMASK:\r
+          memcpy(TEundo, TEimage, TExsize*TEysize);\r
+          if (TEbitflags&TESHOWMSK)\r
+            TEShiftColours(TEimage, TEmask, TExsize, TEysize, 1);\r
+          else\r
+            TEShiftColours(TEimage, NULL, TExsize, TEysize, 1);\r
+          break;\r
+        case EDPASTE:\r
+          if (TEcopy)\r
+            TEShiftColours(TEcopy, TEcmsk, TEcopyxsize, TEcopyysize, 1);\r
+          break;\r
+        }\r
+      }\r
+    if (key[SCAN_PGDN])\r
+      {\r
+      key[SCAN_PGDN]=0;\r
+      switch (TEeditmode)\r
+        {\r
+        case EDDRAW:\r
+        case EDMASK:\r
+          memcpy(TEundo, TEimage, TExsize*TEysize);\r
+          if (TEbitflags&TESHOWMSK)\r
+            TEShiftColours(TEimage, TEmask, TExsize, TEysize, -1);\r
+          else\r
+            TEShiftColours(TEimage, NULL, TExsize, TEysize, -1);\r
+          break;\r
+        case EDPASTE:\r
+          if (TEcopy)\r
+            TEShiftColours(TEcopy, TEcmsk, TEcopyxsize, TEcopyysize, -1);\r
+          break;\r
+        }\r
+      }\r
+\r
+    if (key[SCAN_LANGLE])\r
+      {\r
+      key[SCAN_LANGLE]=0;\r
+      ShutdownVideo();\r
+      TEFreeReg(TEregs);\r
+      vm=0;\r
+      InitVideo(0);\r
+      SetupRegions();\r
+      set_intensity(63);\r
+      InitMouse();\r
+      }\r
+    if (key[SCAN_RANGLE])\r
+      {\r
+      key[SCAN_RANGLE]=0;\r
+      ShutdownVideo();\r
+      TEFreeReg(TEregs);\r
+      vm=1;\r
+      InitVideo(1);\r
+      SetupRegions();\r
+      set_intensity(63);\r
+      InitMouse();\r
+      }\r
+    }\r
+  DeinitTEImage();\r
+  TEFreeReg(TEregs);\r
+  }\r