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

📄 my_tools.c

📁 公司vc培训的时候,给大家几个例子,比较实用,拿来分享
💻 C
字号:
/*............................ MY_TOOLS.C .................. 4-1-95 ........*/
/* A collection of commonly used code.                                      */
/*..........................................................................*/
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<bios.h>

#define MYTLS_VARS

#include<my_tools.h>
#include<vsa.h>
#include<tiff.h>

float SINE_LUT[1024];
/*..........................................................................*/
/*                      Initialize sin look up table.                       */
/*  Index 'i' goes from 0 to 1023 and is equivalent to 0 to 360 degrees.    */
/*..........................................................................*/
/*.....
	for(i=0;i<1024;i++)
		SINE_LUT[i] = sin(i*6.28/1024.0);
.....*/

void gray_lut(void)
{
	int i,start,count;
	unsigned char color_array[768];
	for(i=0;i<256;i++)
		{
				color_array[3*i]   = i/4;
				color_array[3*i+1] = i/4;
				color_array[3*i+2] = i/4;
		}
	start = 0;
	count = 256;
	vsa_write_color_block(start,count,color_array);
	return;
}

/*.......................... TRUE_COLOR_LUT.C ............. 5-15-94 ........*/
/*  This routine generates a 'true color' LUT.  An 8 bit index into the LUT */
/*  represents 3 bits of RED, 3 bits of GREEN, and 2 bits of BLUE.  The     */
/*  3 msbs of the 8 bit index are the RED field, next 3 are GREEN, and      */
/*  the 2 lsbs are the BLUE field.                                          */
/*..........................................................................*/
void true_color_lut(void)
{
	int i;
	unsigned char color_array[768];
	for(i=0;i<256;i++)
		{
			color_array[3*i+0]= ((i & 0x00e0) >> 5) * 9;
			color_array[3*i+1]= ((i & 0x001c) >> 2) * 9;
			color_array[3*i+2]= (i & 0x0003) * 21;
		}
	vsa_write_color_block(0,256,color_array);
	return;
}                                   /*.....       End true_color_lut   .....*/


void rainbow_lut(void)
{
	int i,start,count;
	unsigned char color_array[768];
	for(i=0;i<224;i++)
		{
			color_array[3*i+2]=0;
			color_array[3*i+1]=0;
			color_array[3*i]=0;
		}
/*................................ RED .....................................*/
	for(i=0;i<56;i++)
		{
				color_array[3*i] = 63*sin((i*6.28)/112.0);
		}
/*............................... BLUE .....................................*/
	for(i=0;i<126;i++)
		{
				color_array[3*i+2] = 63*sin((i*6.28)/252.0);
		}
/*............................... GREEN ....................................*/
	for(i=96;i<210;i++)
		{
				color_array[3*i+1] = 63*sin(((i-90)*6.28)/252.0);
		}
/*................................ RED .....................................*/
	for(i=140;i<224;i++)
		{
				color_array[3*i]   = 63*sin(((i-140)*6.28)/280.0);
		}
	start = 32;
	count = 224;
	vsa_write_color_block(start,count,color_array);
	return;
}

void special_lut(void)
{
	int i,start,count;
	unsigned char color_array[768];
	for(i=0;i<256;i++)
		{
			color_array[3*i+2]=0;
			color_array[3*i+1]=0;
			color_array[3*i]=0;
		}
/*............................... BLUE .....................................*/
	for(i=0;i<128;i++)
		{
				color_array[3*i+2] = 63*sin((i*6.28)/256.0);
		}
/*............................... GREEN ....................................*/
	for(i=48;i<240;i++)
		{
				color_array[3*i+1] = 63*sin(((i-48)*6.28)/384.0);
		}
/*................................ RED .....................................*/
	for(i=128;i<256;i++)
		{
				color_array[3*i]   = 63*sin(((i-128)*6.28)/512.0);
		}
	start = 0;
	count = 256;
	vsa_write_color_block(start,count,color_array);
	return;
}

/*.............................. SHADED_LUT.C ............. 2-25-96 ........*/
/*  This routine generates a lookup table holding 8 primary colors with 32  */
/*  shades of "gray" for each.                                              */
/*                                                                          */
/*           COLOR   |   INDEX RANGE                                        */
/*           -----------------------                                        */
/*            Gray   |     0-31                                             */
/*            Red    |    32-63                                             */
/*            Orange |    64-95                                             */
/*            Yellow |    96-127                                            */
/*            Green  |   128-159                                            */
/*            Auqa   |   160-191                                            */
/*            Blue   |   192-223                                            */
/*            Purple |   224-255                                            */
/*..........................................................................*/
void shaded_lut(void)
{
	int i,j;
	unsigned char color_array[768];
	float r[8]={1.0,1.0,0.917,1.0,0.0,0.0,0.0,1.0};
	float g[8]={1.0,0.0,0.436,1.0,1.0,1.0,0.0,0.0};
	float b[8]={1.0,0.0,0.000,0.0,0.0,1.0,1.0,1.0};
	float k;
	for(j=0;j<8;j++)
		{
			for(i=0;i<32;i++)
				{
					if(i < 12) k = pow((i/31.0),1.0);
					else       k = pow((11/31.0),1.0) + 0.645*pow(((i-9.0)/(31.0-9.0)),3.0);
					color_array[3*i+96*j+0]= (int)(k*63*r[j] + 0.5);
					color_array[3*i+96*j+1]= (int)(k*63*g[j] + 0.5);
					color_array[3*i+96*j+2]= (int)(k*63*b[j] + 0.5);
					i=i;
				}
		}
	vsa_write_color_block(0,256,color_array);
	return;
}                                   /*.....       End shaded_lut       .....*/

void color_bar(x0,y0)
int x0,y0;
{
	int i;
	unsigned xx,yy,a,b;
	float c;
	xx = XResolution;
	yy = YResolution;
/*..........................................................................*/
/*     Draw outline for color bar.                                          */
/*..........................................................................*/
	vsa_set_color(15);
	vsa_move_to(x0-1,y0-1);
	a = .75*xx;
	b = .065*yy;
	vsa_rect(x0+a+1,y0+b+1);
	c = (float)a/256;
	for(i=0;i<256;i++)
		{
			vsa_set_color((unsigned char)i);
			vsa_move_to(x0+(unsigned)(i*c),y0);
			vsa_rect_fill(x0+(unsigned)(c+i*c),y0+b);
		}
	return;
}

void image(int x,int y)
{
	int i,j;
	long ii,jj,z1,z2;
	unsigned char array[1024];
	unsigned xx,yy,a,b;
	xx = XResolution;
	yy = YResolution;
	a = .4*xx;
	b = .26*yy;
	z1 = 2*1024L/a;
	z2 = 1024L/b;
	vsa_move_to(x-2,y-2);
	vsa_set_color(250);
	vsa_rect(x+a+1,y+b+1);
	for(j=0;j<b;j++)
		{
			for(i=0;i<a;i++)
				{
					ii = (i*z1) & 0x000003ff;
					jj = (j*z2+256) & 0x000003ff;
/*.....
					array[i] = 144+112*sin(i*6.28/c)*cos(j*6.28/c);
.....*/
					array[i] = 144+112.0*(SINE_LUT[ii]*SINE_LUT[jj]);
				}
			vsa_raster_line(x,x+a-1,y+j,array);
		}
	return;
}

/*.............................. ANY_KEY.C ............... 10-25-94 ........*/
/*  This routine returns a 0 if NO key has been pressed.  If a key has been */
/*  pressed, this routine returns the key's value.  Whats special about     */
/*  this routine is that it doesn't stop execution, it always returns even  */
/*  if no key was entered.                                                  */
/*..........................................................................*/

int any_key(void)
{
	int result=0;
#ifdef _MSC_VER
/*.....             For Microsoft C, Use this line.                    .....*/
	if(_bios_keybrd(_KEYBRD_READY))
		result = (_bios_keybrd(_KEYBRD_READ)) & 0xff;
#else
/*.....             For Borland C, Use this line instead.              .....*/
	if(bioskey(1))
		result = (bioskey(0)) & 0xff;
#endif
	return result;
}

/*............................. TONE_GEN.C ................. 1-29-94 .......*/
/*  Tone generator sets up a tone of frequency = 'freq' (valid range = 19   */
/*  to 65535 Hz, 0 for silence).                                            */
/*..........................................................................*/
void tone_gen(unsigned freq)
{
	unsigned control;
/*..........................................................................*/
/*  Prepare timer by sending 10111100 to port 43, and read speaker value.   */
/*..........................................................................*/
	outp( 0x43, 0xb6 );
/*..........................................................................*/
/*      Divide input frequency by timer ticks per second and write          */
/*      (byte by byte) to timer.  Note: timer is 16 bits, 1193180/64k       */
/*      = approx 19 Hz.  (this is the lowest meaningful frequency value).   */
/*..........................................................................*/
	if(freq != 0)
		freq = (unsigned)(1193180L / freq);
	outp( 0x42, (char)freq );
	outp( 0x42, (char)(freq >> 8) );
/*..........................................................................*/
/*   Get current speaker byte.  Change 2 LSBs to 0x3 to turn on tone.       */
/*   Change 2 LSBs to --- to turn off.                                      */
/*..........................................................................*/
	control = inp(0x61);
	if(freq > 0)
		outp(0x61,control | 0x03);
	else
		outp(0x61,control & 0xfd);
	return;
}

/*................................ DELAY.C ................. 1-29-94 .......*/
/*  This routine waits 'delay' milliseconds and then returns.               */
/*..........................................................................*/
void delay(unsigned delay)
{
	clock_t start_time,delta_time;
	if((delay*CLOCKS_PER_SEC) < 1000)
		return;
	delta_time = ((long)delay*(long)CLOCKS_PER_SEC)/1000L;
	start_time = clock();
	while((clock()-start_time) < delta_time);
	return;
}

/*.............................. ASK_VIDEO ................. 4-1-95 ........*/
/*  This routine takes care of asking for video mode.                       */
/*..........................................................................*/
void ask_video()
{
	int vmode,err;
/*..........................................................................*/
/*               Initialize video mode and VSA256 environment.              */
/*               Valid modes are: 100h, 101h, 103h, and 105h.               */
/*..........................................................................*/
	printf("\n");
	printf("\n");
	printf("VESA standard Video Modes  =>   Mode | Resolution\n");
	printf("              (256 color)       -----|-----------\n");
	printf("                                100  |  640 x 400\n");
	printf("                                101  |  640 x 480\n");
	printf("                                103  |  800 x 600\n");
	printf("                                105  | 1024 x 768\n");
	printf("                                107  | 1280 x 1024\n");
	printf("Input Mode: ");
	scanf("%x",&vmode);
	if((err = vsa_init(vmode)) != 0)
		{
			printf("Error Initializing Requested Video Mode!\n");
			if(err==1) printf("  - Did You Load Correct VESA Driver (TSR) ??\n");
			if(err==2) printf("  - VESA BIOS Extensions (Driver) Not Loaded !!\n");
			if(err==3) printf("  - Requested Video Mode Not Supported by this Card!\n");
			if(err==4) printf("  - Mode Not an SVGA Mode Supported by this Card!\n");
			if(err==5) printf("  - VESA Driver Not Returning Mode Information!\n");
			exit(0);
		}
	return;
}

⌨️ 快捷键说明

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