⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editpal.c

📁 frasr200的win 版本源码(18.21),使用make文件,使用的vc版本较低,在我的环境下编译有问题! 很不错的分形程序代码!
💻 C
📖 第 1 页 / 共 5 页
字号:

/*
 * convert a range of colors to their inverse
 */


static void palrangetonegative(PALENTRY pal[], int first, int how_many)
   {
   PALENTRY      *curr;

   for (curr = &pal[first]; how_many>0; how_many--, curr++)
      {
      curr->red   = 63 - curr->red;
      curr->green = 63 - curr->green;
      curr->blue  = 63 - curr->blue;
      }
   }


/*
 * draw and horizontal/vertical dotted lines
 */


static void hdline(int x, int y, int width)
   {
   int ctr;
   BYTE *ptr;

   for (ctr=0, ptr=line_buff; ctr<width; ctr++, ptr++)
      *ptr = (ctr&2) ? bg_color : fg_color;

   putrow(x, y, width, (char *)line_buff);
   }


static void vdline(int x, int y, int depth)
   {
   int ctr;

   for (ctr=0; ctr<depth; ctr++, y++)
      clip_putcolor(x, y, (ctr&2) ? bg_color : fg_color);
   }


static void drect(int x, int y, int width, int depth)
   {
   hdline(x, y, width);
   hdline(x, y+depth-1, width);

   vdline(x, y, depth);
   vdline(x+width-1, y, depth);
   }


/*
 * A very simple memory "allocator".
 *
 * Each call to mem_alloc() returns size bytes from the array mem_block.
 *
 * Be sure to call mem_init() before using mem_alloc()!
 *
 */

static char     *mem_block;
static unsigned  mem_avail;


void mem_init(VOIDPTR block, unsigned size)
   {
   mem_block = (char *)block;
   mem_avail = size;
   }


VOIDPTR mem_alloc(unsigned size)
   {
   VOIDPTR block;

#ifndef XFRACT
   if (size & 1)
      ++size;   /* allocate even sizes */
#else
   size = (size+3)&~3; /* allocate word-aligned memory */
#endif

   if (mem_avail < size)   /* don't let this happen! */
      {
      static char far msg[] = "editpal.c: Out of memory!\n";

      stopmsg(0, msg);
      exit(1);
      }

   block = mem_block;
   mem_avail -= size;
   mem_block += size;

   return(block);
   }



/*
 * misc. routines
 *
 */


static BOOLEAN is_reserved(int color)
   {
   return ( (reserve_colors && (color==fg_color || color==bg_color) ) ? TRUE : FALSE );
   }



static BOOLEAN is_in_box(int x, int y, int bx, int by, int bw, int bd)
   {
   return ( (x >= bx) && (y >= by) && (x < bx+bw) && (y < by+bd) );
   }



static void draw_diamond(int x, int y, int color)
   {
   putcolor (x+2, y+0,	  color);
   hline    (x+1, y+1, 3, color);
   hline    (x+0, y+2, 5, color);
   hline    (x+1, y+3, 3, color);
   putcolor (x+2, y+4,	  color);
   }



/*
 * Class:     Cursor
 *
 * Purpose:   Draw the blinking cross-hair cursor.
 *
 * Note:      Only one Cursor can exist (referenced through the_cursor).
 *	      IMPORTANT: Call Cursor_Construct before you use any other
 *	      Cursor_ function!  Call Cursor_Destroy before exiting to
 *	      deallocate memory.
 */

struct _Cursor
   {

   int	   x, y;
   int	   hidden;	 /* >0 if mouse hidden */
   long    last_blink;
   BOOLEAN blink;
   char    t[CURSOR_SIZE],	  /* save line segments here */
	   b[CURSOR_SIZE],
	   l[CURSOR_SIZE],
	   r[CURSOR_SIZE];
   } ;

#define Cursor struct _Cursor

/* private: */

   static  void    Cursor__Draw      (void);
   static  void    Cursor__Save      (void);
   static  void    Cursor__Restore   (void);

/* public: */
#ifdef NOT_USED
   static  BOOLEAN Cursor_IsHidden  (void);
#endif



static Cursor *the_cursor = NULL;


BOOLEAN Cursor_Construct(void)
   {
   if (the_cursor != NULL)
      return(FALSE);

   the_cursor = new(Cursor);

   the_cursor->x	  = sxdots/2;
   the_cursor->y	  = sydots/2;
   the_cursor->hidden	  = 1;
   the_cursor->blink	  = FALSE;
   the_cursor->last_blink = 0;

   return (TRUE);
   }


void Cursor_Destroy(void)
   {
   if (the_cursor != NULL)
      delete(the_cursor);

   the_cursor = NULL;
   }



static void Cursor__Draw(void)
   {
   int color;

   find_special_colors();
   color = (the_cursor->blink) ? color_medium : color_dark;

   vline(the_cursor->x, the_cursor->y-CURSOR_SIZE-1, CURSOR_SIZE, color);
   vline(the_cursor->x, the_cursor->y+2,	     CURSOR_SIZE, color);

   hline(the_cursor->x-CURSOR_SIZE-1, the_cursor->y, CURSOR_SIZE, color);
   hline(the_cursor->x+2,	      the_cursor->y, CURSOR_SIZE, color);
   }


static void Cursor__Save(void)
   {
   vgetrow(the_cursor->x, the_cursor->y-CURSOR_SIZE-1, CURSOR_SIZE, the_cursor->t);
   vgetrow(the_cursor->x, the_cursor->y+2,	       CURSOR_SIZE, the_cursor->b);

   getrow(the_cursor->x-CURSOR_SIZE-1, the_cursor->y,  CURSOR_SIZE, the_cursor->l);
   getrow(the_cursor->x+2,	       the_cursor->y,  CURSOR_SIZE, the_cursor->r);
   }


static void Cursor__Restore(void)
   {
   vputrow(the_cursor->x, the_cursor->y-CURSOR_SIZE-1, CURSOR_SIZE, the_cursor->t);
   vputrow(the_cursor->x, the_cursor->y+2,	       CURSOR_SIZE, the_cursor->b);

   putrow(the_cursor->x-CURSOR_SIZE-1, the_cursor->y,  CURSOR_SIZE, the_cursor->l);
   putrow(the_cursor->x+2,	       the_cursor->y,  CURSOR_SIZE, the_cursor->r);
   }



void Cursor_SetPos(int x, int y)
   {
   if (!the_cursor->hidden)
      Cursor__Restore();

   the_cursor->x = x;
   the_cursor->y = y;

   if (!the_cursor->hidden)
      {
      Cursor__Save();
      Cursor__Draw();
      }
   }

#ifdef NOT_USED

static int Cursor_IsHidden(void)
   {
   return ( the_cursor->hidden );
   }


#endif


void Cursor_Move(int xoff, int yoff)
   {
   if ( !the_cursor->hidden )
      Cursor__Restore();

   the_cursor->x += xoff;
   the_cursor->y += yoff;

   if (the_cursor->x < 0)       the_cursor->x = 0;
   if (the_cursor->y < 0)       the_cursor->y = 0;
   if (the_cursor->x >= sxdots) the_cursor->x = sxdots-1;
   if (the_cursor->y >= sydots) the_cursor->y = sydots-1;

   if ( !the_cursor->hidden )
      {
      Cursor__Save();
      Cursor__Draw();
      }
   }


int Cursor_GetX(void)   { return(the_cursor->x); }

int Cursor_GetY(void)   { return(the_cursor->y); }


void Cursor_Hide(void)
   {
   if ( the_cursor->hidden++ == 0 )
      Cursor__Restore();
   }


void Cursor_Show(void)
   {
   if ( --the_cursor->hidden == 0)
      {
      Cursor__Save();
      Cursor__Draw();
      }
   }

#ifdef XFRACT
void Cursor_StartMouseTracking()
{
    editpal_cursor = 1;
}

void Cursor_EndMouseTracking()
{
    editpal_cursor = 0;
}
#endif

/* See if the cursor should blink yet, and blink it if so */
void Cursor_CheckBlink(void)
{
   long tick;
   tick = readticker();

   if ( (tick - the_cursor->last_blink) > CURSOR_BLINK_RATE )
      {
      the_cursor->blink = (the_cursor->blink) ? FALSE : TRUE;
      the_cursor->last_blink = tick;
      if ( !the_cursor->hidden )
	 Cursor__Draw();
      }
   else if ( tick < the_cursor->last_blink )
      the_cursor->last_blink = tick;
}

int Cursor_WaitKey(void)   /* blink cursor while waiting for a key */
   {

#ifndef XFRACT
   while ( !keypressed() ) {
       Cursor_CheckBlink();
   }
#else
   while ( !waitkeypressed(1) ) {
       Cursor_CheckBlink();
   }
#endif

   return( keypressed() );
   }



/*
 * Class:     MoveBox
 *
 * Purpose:   Handles the rectangular move/resize box.
 */

struct _MoveBox
   {
   int	    x, y;
   int	    base_width,
	    base_depth;
   int	    csize;
   BOOLEAN  moved;
   BOOLEAN  should_hide;
   char    *t, *b,
	   *l, *r;
   } ;

#define MoveBox struct _MoveBox

/* private: */

   static void	   MoveBox__Draw     (MoveBox *this);
   static void	   MoveBox__Erase    (MoveBox *this);
   static void	   MoveBox__Move     (MoveBox *this, int key);

/* public: */

   static MoveBox *MoveBox_Construct  (int x, int y, int csize, int base_width,
				      int base_depth);
   static void	   MoveBox_Destroy    (MoveBox *this);
   static BOOLEAN  MoveBox_Process    (MoveBox *this); /* returns FALSE if ESCAPED */
   static BOOLEAN  MoveBox_Moved      (MoveBox *this);
   static BOOLEAN  MoveBox_ShouldHide (MoveBox *this);
   static int	   MoveBox_X	      (MoveBox *this);
   static int	   MoveBox_Y	      (MoveBox *this);
   static int	   MoveBox_CSize      (MoveBox *this);

   static void	   MoveBox_SetPos     (MoveBox *this, int x, int y);
   static void	   MoveBox_SetCSize   (MoveBox *this, int csize);



static MoveBox *MoveBox_Construct(int x, int y, int csize, int base_width, int base_depth)
   {
   MoveBox *this = new(MoveBox);

   this->x	     = x;
   this->y	     = y;
   this->csize	     = csize;
   this->base_width  = base_width;
   this->base_depth  = base_depth;
   this->moved	     = FALSE;
   this->should_hide = FALSE;
   this->t	     = newx(sxdots);
   this->b	     = newx(sxdots);
   this->l	     = newx(sydots);
   this->r	     = newx(sydots);

   return(this);
   }


static void MoveBox_Destroy(MoveBox *this)
   {
   delete(this->t);
   delete(this->b);
   delete(this->l);
   delete(this->r);
   delete(this);
   }


static BOOLEAN MoveBox_Moved(MoveBox *this) { return(this->moved); }

static BOOLEAN MoveBox_ShouldHide(MoveBox *this) { return(this->should_hide); }

static int MoveBox_X(MoveBox *this)	 { return(this->x); }

static int MoveBox_Y(MoveBox *this)	 { return(this->y); }

static int MoveBox_CSize(MoveBox *this)  { return(this->csize); }


static void MoveBox_SetPos(MoveBox *this, int x, int y)
   {
   this->x = x;
   this->y = y;
   }


static void MoveBox_SetCSize(MoveBox *this, int csize)
   {
   this->csize = csize;
   }


static void MoveBox__Draw(MoveBox *this)  /* private */
   {
   int width = this->base_width + this->csize*16+1,
       depth = this->base_depth + this->csize*16+1;
   int x     = this->x,
       y     = this->y;


   getrow (x, y,	 width, this->t);
   getrow (x, y+depth-1, width, this->b);

   vgetrow(x,	      y, depth, this->l);
   vgetrow(x+width-1, y, depth, this->r);

   hdline(x, y,         width);
   hdline(x, y+depth-1, width);

   vdline(x,	     y, depth);
   vdline(x+width-1, y, depth);
   }


static void MoveBox__Erase(MoveBox *this)   /* private */
   {
   int width = this->base_width + this->csize*16+1,
       depth = this->base_depth + this->csize*16+1;

   vputrow(this->x,	    this->y, depth, this->l);
   vputrow(this->x+width-1, this->y, depth, this->r);

   putrow(this->x, this->y,	    width, this->t);
   putrow(this->x, this->y+depth-1, width, this->b);
   }


#define BOX_INC     1
#define CSIZE_INC   2

static void MoveBox__Move(MoveBox *this, int key)
   {
   BOOLEAN done  = FALSE;
   BOOLEAN first = TRUE;
   int	   xoff  = 0,
	   yoff  = 0;

   while ( !done )
      {
      switch(key)
	 {
	 case RIGHT_ARROW_2:	 xoff += BOX_INC*4;   break;
	 case RIGHT_ARROW:	 xoff += BOX_INC;     break;
	 case LEFT_ARROW_2:	 xoff -= BOX_INC*4;   break;
	 case LEFT_ARROW:	 xoff -= BOX_INC;     break;
	 case DOWN_ARROW_2:	 yoff += BOX_INC*4;   break;
	 case DOWN_ARROW:	 yoff += BOX_INC;     break;
	 case UP_ARROW_2:	 yoff -= BOX_INC*4;   break;
	 case UP_ARROW: 	 yoff -= BOX_INC;     break;

	 default:
	    done = TRUE;
	 }

      if (!done)
	 {
	 if (!first)
	    getakey();	     /* delete key from buffer */
	 else
	    first = FALSE;
	 key = keypressed();   /* peek at the next one... */
	 }
      }

   xoff += this->x;
   yoff += this->y;   /* (xoff,yoff) = new position */

   if (xoff < 0) xoff = 0;
   if (yoff < 0) yoff = 0;

   if (xoff+this->base_width+this->csize*16+1 > sxdots)
       xoff = sxdots - (this->base_width+this->csize*16+1);

   if (yoff+this->base_depth+this->csize*16+1 > sydots)
      yoff = sydots - (this->base_depth+this->csize*16+1);

   if ( xoff!=this->x || yoff!=this->y )
      {
      MoveBox__Erase(this);
      this->y = yoff;
      this->x = xoff;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -