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

📄 graph.c

📁 坦克大战,实现基本的游戏功能
💻 C
字号:
/*
    graph.c

    图形库相关函数实现
    最后修改: 2007-06-19

*/


#include "graph.h"
#include <dos.h>

unsigned char far *g_pbyVideoBuffer=(char far *)0xA0000000L;  /* 显示缓存首地址 */

#ifdef VESA101H

void InitGraph()  /* 进入 VESA 101h 模式 */
{
    union REGS r;

    r.x.ax=0x4f02;
    r.x.bx=0x0101;
    int86(0x10, &r, &r);
}

void CloseGraph()  /* 恢复文本模式 */
{
    union REGS r;

    r.x.ax=0x0003;
    int86(0x10, &r, &r);
}

void SelectPage(char byPage)  /* 换页函数 */
{
    union REGS r;
    r.x.ax=0x4f05;
    r.x.bx=0;
    r.x.dx=byPage;
    int86(0x10, &r, &r);
}

int GetOffset(int nX, int nY)  /* 返回相对于显存首地址的偏移量 */
{
    static int s_byPage=0;
    long offset;
    int t;

    offset=nY;
    offset=(offset<<9)+(offset<<7)+nX;
    t=offset>>16;  /* /65536 */

    if(t!=s_byPage)
    {
        SelectPage(t);
        s_byPage=t;
    }

    return (offset&0xffff);  /* %65536 */
}

void PutPix(int nX, int nY, unsigned char byColor)  /* 画点函数 */
{
    if(byColor!=13)  /* 13 视为透明色 */
    {
        g_pbyVideoBuffer[GetOffset(nX, nY)]=byColor;   /* 直接写屏 */
    }
}

char GetPix(int nX, int nY)
{
    return g_pbyVideoBuffer[GetOffset(nX, nY)];
}

void SetColorPal(char byColor, char byRed, char byGreen, char byBlue)  /* 设调色板 */
{
    outportb(0x03c8, byColor);
    outportb(0x03c9, byRed);
    outportb(0x03c9, byGreen);
    outportb(0x03c9, byBlue);
}

void GetColorPal (char byColor, char *pbyRed, char *pbyGreen, char *pbyBlue)  /* 读调色板 */
{
    outportb(0x03c7, byColor);
    *pbyRed=inportb(0x03c9);
    *pbyGreen=inportb(0x03c9);
    *pbyBlue=inportb(0x03c9);
}

#else

void InitGraph()  /* 进入 VGA 12h 模式 */
{
    union REGS r;

    r.x.ax=0x0012;
    int86(0x10, &r, &r);
    outport(0x3ce, 0x0205);
    outportb(0x3ce, 8);
}

void CloseGraph()  /* 恢复文本模式 */
{
    union REGS r;

    outport(0x3ce, 0xff08);
    outport(0x3ce, 0x0005);
    outport(0x3ce, 0x0003);
    r.x.ax=0x0003;
    int86(0x10, &r, &r);
}

unsigned char far *g_pbyTheBuffer;

char GetPix(int nX, int nY)  /* 读点函数 */
{
    char color=0x00;

    g_pbyTheBuffer=g_pbyVideoBuffer+(nY<<6)+(nY<<4)+(nX>>3);
    outportb(0x3ce, 4);
    outportb(0x3cf, 3);
    color|=(*g_pbyTheBuffer>>(7-(nX&0x07)))&1;
    color=(color<<1);
    outportb(0x3cf, 2);
    color|=(*g_pbyTheBuffer>>(7-(nX&0x07)))&1;
    color=(color<<1);
    outportb(0x3cf, 1);
    color|=(*g_pbyTheBuffer>>(7-(nX&0x07)))&1;
    color=(color<<1);
    outportb(0x3cf, 0);
    color|=(*g_pbyTheBuffer>>(7-(nX&0x07)))&1;
    outportb(0x3ce, 8);

    return color;
}

void PutPix(int nX, int nY, unsigned char byColor)  /* 画点函数 */
{
    static unsigned char temp;
    static unsigned char mask=0;

    if(byColor!=13)  /* 13 视为透明色 */
    {
        temp=(0x80>>(nX&0x07));
        if(mask!=temp)
        {
            mask=temp;
            outportb(0x3cf, mask);
        }
        g_pbyTheBuffer=g_pbyVideoBuffer+(nY<<6)+(nY<<4)+(nX>>3);
        temp=*g_pbyTheBuffer;
        *g_pbyTheBuffer=byColor;
    }
}

#endif

void Rect(int nX1, int nY1, int nX2, int nY2, char byColor) /* 画空矩形 */
{
    int i;

    if(nX1>nX2) i=nX1, nX1=nX2, nX2=i;
    if(nY1>nY2) i=nY1, nY1=nY2, nY2=i;

    for(i=nX1;i<=nX2;i++)
    {
        PutPix(i, nY1, byColor);
        PutPix(i, nY2, byColor);
    }
    for(i=nY1+1;i<=nY2-1;i++)
    {
        PutPix(nX1, i, byColor);
        PutPix(nX2, i, byColor);
    }
}

void Box(int nX1, int nY1, int nX2, int nY2, char byColor) /* 画实矩形 */
{
    int i, j;

    if(nX1>nX2) i=nX1, nX1=nX2, nX2=i;
    if(nY1>nY2) i=nY1, nY1=nY2, nY2=i;

    for(i=nX1;i<=nX2;i++)
    {
        for(j=nY1;j<=nY2;j++)
        {
            PutPix(i, j, byColor);
        }
    }
}

⌨️ 快捷键说明

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