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

📄 macro.c

📁 C++游戏开发书籍的实例非常适合初学但又又想往游戏开发方面发展的人学习哦
💻 C
📖 第 1 页 / 共 2 页
字号:


#include "tdestr.h"             /* tde types */
#include "common.h"
#include "define.h"
#include "tdefunc.h"


/*
 *              keystroke record functions
 */

/*
 * Name:    record_on_off
 * Purpose: save keystrokes in keystroke buffer
 * Date:    April 1, 1992
 * Passed:  window:  pointer to current window
 * Notes:   -1 in .next field indicates the end of a recording
 *          -1 in .key field indicates the initial, unassigned macro key
 *          STROKE_LIMIT+1 in .next field indicates an unused space.
 */
int  record_on_off( WINDOW *window )
{
register int next;
int  prev;
int  line;
int  key;
int  func;
char line_buff[(MAX_COLS+2)*2]; /* buffer for char and attribute  */

   mode.record = !mode.record;
   if (mode.record == TRUE) {
      line = window->bottom_line;
      show_avail_strokes( );
      save_screen_line( 0, line, line_buff );
      /*
       * press key that will play back recording
       */
      set_prompt( main11, line );

      /*
       * get the candidate macro key and look up the function assigned to it.
       */
      key = getkey( );
      func = getfunc( key );

      /*
       * the key must be an unused, recognized function key or a function
       *  key assigned to a previously defined macro.  we also need room
       *  in the macro structure.
       */
      if (key <= 256 || (func != 0 && func != PlayBack)) {
         /*
          * cannot assign a recording to this key
          */
         error( WARNING, line, main12 );
         mode.record = FALSE;
      } else if (g_status.stroke_count == 0) {
         /*
          * no more room in recording buffer
          */
         error( WARNING, line, main13 );
         mode.record = FALSE;
      } else {

         /*
          * everything is everything so far, just check for a prev macro
          */
         prev = OK;
         if (func == PlayBack) {
            /*
             * overwrite recording (y/n)?
             */
            set_prompt( main14, line );
            if (get_yn( ) == A_NO) {
               prev = ERROR;
               mode.record = FALSE;
            }
         }
         if (prev == OK) {
            g_status.recording_key = key;
            next = macro.first_stroke[key-256];

            /*
             * if key has already been assigned to a macro, clear macro def.
             */
            if (next != STROKE_LIMIT+1) {
               do {
                  prev = next;
                  next = macro.strokes[next].next;
                  macro.strokes[prev].key  = MAX_KEYS+1;
                  macro.strokes[prev].next = STROKE_LIMIT+1;
                  ++g_status.stroke_count;
               } while (next != -1);
               show_avail_strokes( );
            }

            /*
             * find the first open space and initialize
             */
            for (next=0; macro.strokes[next].next != STROKE_LIMIT+1;)
               next++;
            macro.first_stroke[key-256] = next;
            macro.strokes[next].key  = -1;
            macro.strokes[next].next = -1;
            key_func.key[key-256] = PlayBack;
            /*
             * recording
             */
            s_output( main15, g_display.mode_line, 22,
                      g_display.mode_color | 0x80 );
         }
      }
      restore_screen_line( 0, line, line_buff );
   }

   /*
    * the flashing "Recording" and the stroke count write over the modes.
    *  when we get thru defining a macro, redisplay the modes.
    */
   if (mode.record == FALSE) {
      memset( line_buff, ' ', 36 );
      line_buff[36] = '\0';
      s_output( line_buff, g_display.mode_line, 22, g_display.mode_color );
      show_tab_modes( );
      show_indent_mode( );
      show_sync_mode( );
      show_search_case( );
      show_wordwrap_mode( );

      /*
       * let's look at the macro.  if the first .key of the macro is
       *  still -1, which is the initial unassigned key in a macro, reset
       *  the macro so other keys may be assigned to this node.
       */
      key = g_status.recording_key;
      if (key != 0) {
         next = macro.first_stroke[key-256];
         if (macro.strokes[next].key == -1) {
            macro.strokes[next].key  = MAX_KEYS+1;
            macro.strokes[next].next = STROKE_LIMIT+1;
            macro.first_stroke[key-256] = STROKE_LIMIT+1;
            if (getfunc( key ) == PlayBack)
               key_func.key[key-256] = 0;
         }
      }
      g_status.recording_key = 0;
   }
   return( OK );
}


/*
 * 作用: 把键盘命令放到缓存中
 * 参数: line: 要提示显示的行
 * 注意: 如果next值是-1,表明到了记录的末尾;
 *       如果next值是STROKE_LIMIT+1表示空间已经用完
 */
void record_keys( int line )
{
register int next;
register int prev;
int  key;
int  func;

   if (mode.record == TRUE) {
      if (g_status.stroke_count == 0)
         /*
          * 宏记录已经没有空间记录更多的操作
          */
         error( WARNING, line, main13 );
      else {
         key = g_status.key_pressed;
         func = getfunc( key );
         if (func != RecordMacro && func != SaveMacro && func != LoadMacro &&
             func != ClearAllMacros) {

            /*
             * 如果next值是-1,表明到了记录的末尾
             */
            next = macro.first_stroke[g_status.recording_key - 256];
            if (macro.strokes[next].next != STROKE_LIMIT+1) {
               while (macro.strokes[next].next != -1)
                  next = macro.strokes[next].next;
            }
            prev = next;

            /*
             * 找到一个空间来记录当前的操作
             */
            if (macro.strokes[next].key != -1) {
               for (; next < STROKE_LIMIT &&
                            macro.strokes[next].next != STROKE_LIMIT+1;)
                  next++;
               if (next == STROKE_LIMIT) {
                  for (next=0; next < prev &&
                               macro.strokes[next].next != STROKE_LIMIT+1;)
                     next++;
               }
            }
            if (next == prev && macro.strokes[prev].key != -1)
               /*
                * 记录缓存没有记录
                */
               error( WARNING, line, main13 );
            else {
            /*
             * 如果我们正在记录第一个宏节点,那么next == prev
             */
               macro.strokes[prev].next = next;
               macro.strokes[next].next = -1;
               macro.strokes[next].key  = key;
               g_status.stroke_count--;
               show_avail_strokes( );
            }
         }
      }
   }
}


/*
 * Name:    show_avail_strokes
 * Purpose: show available free key strokes in lite bar at bottom of screen
 * Date:    April 1, 1992
 */
void show_avail_strokes( void )
{
char strokes[MAX_COLS];

   s_output( main18, g_display.mode_line, 33, g_display.mode_color );
   itoa( g_status.stroke_count, strokes, 10 );
   s_output( "      ", g_display.mode_line, 51, g_display.mode_color );
   s_output( strokes, g_display.mode_line, 51, g_display.mode_color );
}


/*
 * Name:    save_strokes
 * Purpose: save strokes to a file
 * Date:    April 1, 1992
 * Passed:  window:  pointer to current window
 */
int  save_strokes( WINDOW *window )
{
FILE *fp;                       /* file to be written */
char name[MAX_COLS+2];          /* file name */
char line_buff[(MAX_COLS+1)*2]; /* buffer for char and attribute  */
register int rc;
int  prompt_line;
int  fattr;

   name[0] = '\0';
   prompt_line = window->bottom_line;
   save_screen_line( 0, prompt_line, line_buff );
   /*
    * name for macro file
    */
   if ((rc = get_name( main19, prompt_line, name,
                 g_display.message_color )) == OK  &&  *name != '\0') {

      /*
       * make sure it is OK to overwrite any existing file
       */
      rc = get_fattr( name, &fattr );
      if (rc == OK) {
         /*
          * overwrite existing file
          */
         set_prompt( main20, prompt_line );
         if (get_yn( ) != A_YES  ||  change_mode( name, prompt_line ) == ERROR)
            rc = ERROR;
      }
      if (rc != ERROR) {
         if ((fp = fopen( name, "wb" )) != NULL) {
            fwrite( &macro.first_stroke[0], sizeof(int), MAX_KEYS, fp );
            fwrite( &macro.strokes[0], sizeof(STROKES), STROKE_LIMIT, fp );
            fclose( fp );
         }
      }
   }
   restore_screen_line( 0, prompt_line, line_buff );
   return( OK );
}


/*
 * Name:    load_strokes
 * Purpose: load strokes from a file
 * Date:    April 1, 1992
 * Passed:  window:  pointer to current window
 * Notes:   show the user a file pick list.  I can never remember macro
 *           file names or the directory in which they hide.  might as well
 *           give the user a file pick list.
 */
int  load_strokes( WINDOW *window )
{
register FILE *fp;      /* file to be read */
char dname[MAX_COLS];   /* directory search pattern */
char stem[MAX_COLS];    /* directory stem */
register int rc;

   dname[0] = '\0';
   /*
    * search path for macro file
    */
   if (get_name( main21, window->bottom_line, dname,
                 g_display.message_color ) == OK  &&  *dname != '\0') {
      if (validate_path( dname, stem ) == OK) {
         rc = list_and_pick( dname, stem, window );

         /*
          * if everything is everything, load in the file selected by user.
          */
         if (rc == OK) {
            if ((fp = fopen( dname, "rb" )) != NULL && ceh.flag != ERROR) {
               fwrite( &macro.first_stroke[0], sizeof(int), MAX_KEYS, fp );
               fwrite( &macro.strokes[0], sizeof(STROKES), STROKE_LIMIT, fp );
               fclose( fp );
            }
            if (ceh.flag == OK)
               connect_macros( );
         }
      } else
         /*
          * invalid path or file name
          */
         error( WARNING, window->bottom_line, main22 );
   }
   return( OK );
}


/*
 * Name:    clear_macro
 * Purpose: reset all macro buffers, pointers, functions.
 * Date:    April 1, 1992
 * Notes:   reset the available macro stroke count.  reset all fields in
 *           macro structure.  clear any keys assigned to macros in the
 *           function assignment array.
 */
int  clear_macros( WINDOW *arg_filler )
{
register int i;

   g_status.stroke_count = STROKE_LIMIT;
   for (i=0; i<STROKE_LIMIT; i++) {
      macro.strokes[i].next = STROKE_LIMIT+1;
      macro.strokes[i].key  = MAX_KEYS+1;
   }
   for (i=0; i<MAX_KEYS; i++) {
      macro.first_stroke[i] = STROKE_LIMIT+1;
      if (key_func.key[i] == PlayBack)
         key_func.key[i] = 0;
   }

⌨️ 快捷键说明

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