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

📄 lsys4.cpp

📁 ldraw_DOS游戏开发包
💻 CPP
字号:
/***************************************************************************
 * Lin Timer
 */
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <lsys.h>
#define MAX_TIMERS      16

void lt_install_timer();
void lt_remove_timer();
long lt_clock(char swit); // 0: 1.19MHz 1:200Hz the same
char lt_install_int_ex(void (*proc)(),long speed);
char lt_install_int(void (*proc)(),long speed);
void lt_remove_int(void (*proc)());

static long tinstall=0,autoexit=0,Timer_Delay=65536L;
static long lt_clock0=0, lt_clock1=0;
short  lt_Intr=0x08;
static struct {                           /* list of active callbacks */
   void ((*proc)());
   long speed;
   long counter;
} my_int_queue[MAX_TIMERS+1];

#define TIMERS_PER_SECOND     1193181L
#define SECS_TO_TIMER(x)      ((long)(x) * TIMERS_PER_SECOND)
#define MSEC_TO_TIMER(x)      ((long)(x) * (TIMERS_PER_SECOND / 1000))
#define BPS_TO_TIMER(x)       (TIMERS_PER_SECOND / (long)(x))
#define BPM_TO_TIMER(x)       ((60 * TIMERS_PER_SECOND) / (long)(x))
#define LOVE_TIME_SPEED       BPS_TO_TIMER(200)

static void interrupt MyInt8();
static void interrupt far (*OldInt8)();
static void SetTimerRate(unsigned v);
static int  FindProc(void (*proc)());
static void UpdateRate();


static int  FindProc(void (*proc)())
{ int i;
  for (i=0;i<MAX_TIMERS;i++) if (proc==my_int_queue[i].proc) return i;
  i=0; while (my_int_queue[i].proc&&i<MAX_TIMERS) i++; // Find a new space
  my_int_queue[i].speed=my_int_queue[i].counter=0;
  return i;
}
void lt_remove_int(void (*proc)())
{ int i=FindProc(proc);
  my_int_queue[i].proc=NULL; 
  my_int_queue[i].speed=my_int_queue[i].counter=0;
  //UpdateRate();
}
char lt_install_int_ex(void (*proc)(), long speed)
{ int i=FindProc(proc); 
  my_int_queue[i].proc=proc; 
  my_int_queue[i].counter-=my_int_queue[i].speed;
  my_int_queue[i].counter+=speed; my_int_queue[i].speed=speed;
  //UpdateRate();
  if (i<MAX_TIMERS) return 1;
  return 0;
}
static void UpdateRate()
{ int i; long new_speed=65536L;
  for (i=0;i<MAX_TIMERS;i++)
      if (my_int_queue[i].proc&&my_int_queue[i].speed<new_speed)
         new_speed=my_int_queue[i].speed;
  if (new_speed!=Timer_Delay)
   { Timer_Delay=new_speed;
     if (Timer_Delay>65536L) Timer_Delay=65536L;
     SetTimerRate(Timer_Delay);
   }
}
char lt_install_int(void (*proc)(),long speed)
{ return lt_install_int_ex(proc,MSEC_TO_TIMER(speed));
}
long lt_clock(char swit)
{ if (!tinstall) lt_install_timer();
  if (swit==0) return lt_clock0;
  return lt_clock1;
}
static void interrupt MyInt8()
{ register short i; 
  static long  old_delay=65536L, old_counter=65536L;
  static short callback[MAX_TIMERS];

  lt_clock0+=Timer_Delay; lt_clock1++; // add clock

  for (i=0;i<MAX_TIMERS;i++) {
   callback[i]=0;
   if ((my_int_queue[i].proc) && (my_int_queue[i].speed>0))
    { my_int_queue[i].counter-=Timer_Delay; 
      while (my_int_queue[i].counter<=0) 
       { my_int_queue[i].counter+=my_int_queue[i].speed;
         callback[i]++;
       }
    }
  }
  for (i=0;i<MAX_TIMERS;i++)
   { while(callback[i]>0) 
      { if (my_int_queue[i].proc) (*my_int_queue[i].proc)();
        callback[i]--;
      }
   }

  old_counter-=Timer_Delay;
  if (old_counter<=0) { 
     old_counter+=old_delay; 
     if (lt_Intr==0x08) _chain_intr(OldInt8); 
  }

  _enable();
  if (lt_Intr==0x08) outp(0x20,0x20);
}
static void SetTimerRate(unsigned v)
{ if (v>65536||v==0) v=65536;
  _disable();
  outp(0x43, 0x34);
  outp(0x40, v & 0xff);
  outp(0x40, v >> 8);  
  _enable(); Timer_Delay=v; 
}
void lt_install_timer()
{ int i;
  if (tinstall) return;
  OldInt8=_dos_getvect(lt_Intr);
  _disable();
  _dos_setvect(lt_Intr,MyInt8);
  _enable();
  tinstall=1; SetTimerRate(LOVE_TIME_SPEED);
  for (i=0;i<MAX_TIMERS;i++) my_int_queue[i].proc=NULL;
  if (!autoexit) { atexit(lt_remove_timer); autoexit=1; }
}
long lt_report_delay() { return Timer_Delay; }
void lt_remove_timer()
{ if (!tinstall) return;
  tinstall=0;
  _disable();
  _dos_setvect(lt_Intr,OldInt8);
  _enable();
  SetTimerRate(0);
}

⌨️ 快捷键说明

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