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

📄 images.cpp

📁 DOS游戏编程中处理Int 13的工具包
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//**************************************************************************
// IMAGES.CPP -source file for basic video operations in mode 0x13.        *
// Copyright 1991 by the Gamers Programming Workshop, a function of the    *
// GAMERS forum, Compuserve. For more info e-mail 76605,2346.              *
//                                                                         *
// License is granted for use or modification of this code as long as      *
// this notice remains intact, and all improvements are listed in the      *
// version history below, and uploaded to the GAMERS forum. This code      *
// may not be used for any commercial purpose.                             *
//                                                                         *
// PCX stuff adapted from "BitMapped Graphics"  by Steve Rimmer (1990      *
// Windcrest)                                                              *
//**************************************************************************

//**************************************************************************
// Version history:                                                        *
//                                                                         *
// Version 1.0                                                             *
// Developed: May 2, 1991                                                  *
// Author:    Mark Betz, 76605, 2346                                       *
// Last update: July 5, 1991                                               *
//**************************************************************************

// INCLUDES ****************************************************************

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <mem.h>
#include <alloc.h>
#include <io.h>
#include <fcntl.h>
#include "images.hpp"
#include "keyboard.hpp"

// *************************************************************************

// local constants *********************************************************

const int DacWrite = 0x3C8;        // DAC write index register
const int DacRead  = 0x3C7;        // DAC read index register
const int DacData  = 0x3C9;        // DAC data register
const int input_status_1 = 0x3DA;  // Port addr. of VGA input status 1 reg.
const int vbi_mask = 0x8;          // test bit 3 of input_status_1

// *************************************************************************

// Variables ***************************************************************

static unsigned int GRAPH_SEG = 0xA000; // segment for video operations
static p_rec current;                   // palette record for fades

// *************************************************************************

// *************************************************************************
// *************************************************************************
// NOTE:
//        The assembler functions in this module do not save any of the
//        registers they use, with the exception of di and si, which are
//        handled by the standard stack frame. When calling them expect
//        register values to be altered.
//
// *************************************************************************
// *************************************************************************

// *************************************************************************
// reporterr() should be called anytime one of the routines in this module
// returns a code other that NoErr. Pass it the error code returned by the
// offending subroutine, and a max. 30 character string describing the loc-
// ation of the error.
// *************************************************************************

void reporterr(char type, char mess[30])
	{
	settextmode();
	sound(300);
	delay(200);
	sound(250);
	delay(150);
	nosound();
	switch (type)
		{
		case MemErr        : printf("An error occured allocating memory...");
							 break;
		case FileReadErr   : printf("An error occured reading a file...");
							 break;
		case FileWriteErr  : printf("An error occured writing a file...");
							 break;
		case FileMakeErr   : printf("An error occured creating a file...");
							 break;
		case FileOpenErr   : printf("An error occured opening a file...");
							 break;
		case FileFormatErr : printf("A file format error has occured...");
							 break;
		case SecondaryErr  : printf("An error has occured in a subroutine...");
							 break;
		case UnknownErr    : printf("An unknown error has occured...");
		}
	printf(" error occured in ");
	printf(mess);
	return;
	}

// *************************************************************************
// UnpackPcx() unpacks the RLE encoded PCX data from a file if pcx != NULL,
// or the source buffer if pcx==NULL, to the Dest buffer. Unpacks NumBytes
// bytes of decompressed data. (max 64000) This is a pretty dumb routine.
// It's fast, but it expects you to know the uncompressed size of the data
// (the NumBytes parameter), and you have to do all of the file and buffer
// set-up elsewhere. Basically it's a core decoding engine. The InitPcxFile()
// function above is provided for opening a PCX file and saving the header
// and palette in data. For buffering and decoding of pcx's with various
// fade types see the fadeinpcx() function below.
// *************************************************************************

// *************************************************************************
// NOTE: this function isn't called by the PVC class objects when they dis-
// play themselves. It is an earlier version of the algrotihm, and they have
// their own version of it. I left this in in case anyone wanted to use it.
// **************************************************************************

void  unpackpcx(FILE *pcx, const char far *source, char far *dest,
				unsigned int num_bytes)
	{
	unsigned int     bytes=0;       // counts unpacked bytes
	unsigned char    c;             // byte being processed
	unsigned int     src_cntr=0;    // pointer into source buffer
	unsigned int     runlen;        // length of packet

	do
		{
		// get a key byte
		if (pcx!=NULL)
		   c=fgetc(pcx);
		else
		   c=source[src_cntr++] & 0xff;
		// check if it's a packet
		if ((c & 0xc0) == 0xc0)
			{
			// and off the high bits
			runlen = (c & 0x3f);
			// get the run value
			if (pcx!=NULL)
				c=fgetc(pcx);
			else
				c = source[src_cntr++];
			// run it out
			while(runlen--) (dest[bytes++]=c);
			}
		else
			dest[bytes++]=c;
		} while (bytes<num_bytes);
	}

// *************************************************************************
// sets the segment address that will be used in all graphics operations
// within this module. Can be used to operate on a buffer offscreen. This
// is a fairly powerfull feature, as any of the functions provided which
// write or read video memory will act on the segment contained in GRAPH_
// SEG. If you allocate a 64k buffer, and call _setgraphseg() with it's
// segment you can then create the screen there, and block copy it to
// video ram when you're finished.
// *************************************************************************

void setgraphseg(unsigned int newseg)
	{
	GRAPH_SEG = newseg;
	}

// *************************************************************************
// call bios interrupt 0x10, function 13, subfunction 00 to set up mode
// 0x13 graphics.
// *************************************************************************

void setgraphmode()
	{
	asm	{
		xor ah, ah          // zero out ah, subfunction 00
		mov al, 0x13        // function 0x13
		int 0x10            // interrupt 0x10
		}
	}

// *************************************************************************
// call bios interrupt 0x10 in same manner as previous function to restore
// text mode
// *************************************************************************

void settextmode()
	{
	asm	{
		xor ah, ah
		mov al, 0x03
		int 0x10
		}
	}

// **************************************************************************
// wait_vbi() waits twice for the vertical blanking interval. Once to make
// sure any current vbi is completed, and once for the start of the next vbi
// **************************************************************************

void wait_vbi()
	{
	asm mov dx, input_status_1;
	test_1:
	asm	{
		in al, dx
		test al,0x8
		jnz test_1
		}
	test_2:
	asm	{
		in al, dx
		test al,0x8
		jz test_2
		}
	}


// *************************************************************************
// loads the vga dac by direct method via the DAC access ports. Disables
// interrupts during load of each triplet. Does not do snow checking. Call
// with start = first register to load, number = number of registers to
// load, and palette pointing to a structure of type PRec containing the
// palette data. If start or number out of range, or palette is null then
// the function returns.
// *************************************************************************

void loadpalette(int start, int number, const p_rec palette)
	{
	unsigned int i;
	if ((start>256) || (start<0) || ((start+number)>256))
		return;
	for (i=start;i<(start+number);i++)
		{
		asm cli;
		outportb(DacWrite,i);
		outportb(DacData,palette[i][Red]);
		outportb(DacData,palette[i][Green]);
		outportb(DacData,palette[i][Blue]);
		asm sti;
		}
	}

// *************************************************************************
// same as previous function only reads the dac data into the supplied
// palette structure.
// *************************************************************************

void readpalette(int start, int number, p_rec palette)
	{
	unsigned int i;
	if ((start>256) | (start<0) | ((start+number)>256))
		return;
	for (i=start;i<(start+number);i++)
		{
		asm cli;
		outportb(DacRead,i);
		palette[i][Red] = inportb(DacData);
		palette[i][Green] = inportb(DacData);
		palette[i][Blue] = inportb(DacData);
		asm sti;
		}
	}

// **************************************************************************
// clears a range of palette registers to zero
// **************************************************************************

void clrpalette(int start, int number)
	{
	unsigned int i;
	if ((start>256) | (start<0) | ((start+number)>256))
		return;
	for (i=start;i<(start+number);i++)
		{
		asm cli;
		outportb(DacWrite,i);
		outportb(DacData,0);
		outportb(DacData,0);
		outportb(DacData,0);
		asm sti;
		}
	}

// **************************************************************************
// fadepalettein() fades the dac palette from 0 (black) to the default pal-
// ette values passed in palette structure. Fade is done in 64 passes, and
// is very smooth. Operates on count registers starting with register start.
// NOTE: this funtion expect the dac palette registers to be zero'd before
// it is called. Call clrpalette() to clear the registers.
// **************************************************************************

void fadepalettein(int start, int count, const p_rec palette)
	{
	int i, j, cycle;
	memset((void *)current,0,sizeof(p_rec));
	for (cycle=0;cycle<64;cycle++)
		{
		i=start;
		wait_vbi();
		outportb(DacWrite,i);
		asm cli;
		for (;i<((start+count)/2);i++)
			{
			for (j=0;j<3;j++)
				{
				if ((64-cycle)<=palette[i][j])
					current[i][j]++;
				outportb(DacData,current[i][j]);
				}
			}
		asm sti;
		wait_vbi();
		asm cli;
		for (;i<(start+count);i++)
			{
			for (j=0;j<3;j++)
				{
				if ((64-cycle)<=palette[i][j])
					current[i][j]++;
				outportb(DacData,current[i][j]);
				}
			}
		asm sti;
		}
	}

// **************************************************************************
// fadepaletteout() fades the current palette to black. It operates on count
// registers starting with register start. NOTE: this function destroys the
// current contents of temp_pal (images.cpp internal palette structure).
// **************************************************************************

void fadepaletteout(int start, int count)
	{
	int i, j, cycle;
	readpalette(0,256,current);
	for (cycle=64;cycle>0;cycle--)
		{
		i=start;
		wait_vbi();
		outportb(DacWrite,i);
		asm cli;
		for (;i<((start+count)/2);i++)
			{
			for (j=0;j<3;j++)
				{
				if (current[i][j]!=0)
					current[i][j]--;
				outportb(DacData,current[i][j]);
				}
			}
		asm sti;
		wait_vbi();
		asm cli;
		for (;i<(start+count);i++)
			{
			for (j=0;j<3;j++)
				{
				if (current[i][j]!=0)
					current[i][j]--;
				outportb(DacData,current[i][j]);
				}
			}
		asm sti;
		}
	clearscr(0);
	}

// *************************************************************************
// clear the graphics screen to the color provided
// *************************************************************************

void clearscr(int color)
	{
	asm {
		mov es, GRAPH_SEG
		xor di, di
		mov ax, color
		mov cx, 32000
		rep stosw
		}
	}

// *************************************************************************
// fill the rectangular area bounded by (tlx,tly)(brx,bry) with color.
// tlx = top left x, tly = top left y, brx = bottom right x, bry = bottom
// right y.
//
// This function needs to be recoded in assembler
// *************************************************************************

void barfill(int tlx, int tly, int brx, int bry, int color)
	{
	int row;
	void far *line;
	for (row=tly;row<bry;row++)
		{
		line = MK_FP(GRAPH_SEG,((row*320)+tlx));
		_fmemset(line,color,(brx-tlx));
		}
	}

⌨️ 快捷键说明

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