embed\vs2008_bsp\bsp\bsp160160.h.c模拟160160灰色屏幕

来源:互联网 发布:gdp有月度数据吗 编辑:程序博客网 时间:2024/06/11 20:12

//模拟COG显示屏

/*****************************************************************************************
                            模拟COG显示屏
文件:bsp160160.h
编者:张永辉 2013年9月3日
*****************************************************************************************/
#ifndef _BSP160160_H_
#define _BSP160160_H_
#include "basetype.h"
//*********************************配置***************************************************
#define COG_SIZE_X 160
#define COG_SIZE_Y 160
//***************************************函数声明*****************************************
void    Bsp160160Test(void);
void    Bsp160160Init(void);
//图像界面接口
void    Bsp160160Point(u32 x,u32 y, bool Color);            //画一个点
void    Bsp160160LineH(u32 x0,u32 y0,u32 xlen,bool c);      //画竖线
void    Bsp160160LineL(u32 x0,u32 y0,u32 ylen,bool Color);  //画横线
bool    Bsp160160PointGet(u32 x,u32 y);                     //返回某点颜色
void    Bsp160160Clear(void);                               //清屏函数
void    Bsp160160DrawLine(int x1, int y1, int x2, int y2,bool color);   //画直线
void    Bsp160160DrawCircleFast(int xc , int yc , int r,bool color);    //画圆
//****************************************************************************************
#endif

/*****************************************************************************************
                                COG160160显示屏
文件:bsp160160.h
环境:vs2010
说明:使用电脑的320*320模拟真实的 160160界面
编者:张永辉 2013年9月3日
*****************************************************************************************/
#include    "opengl2.h"
#include    "bsp160160.h"
#include    "bsptime.h"
/*****************************************************************************************
                                函数集
*****************************************************************************************/
void    Bsp160160Test(void)
{
    Bsp160160LineH(0,0,160,1);
    Bsp160160LineH(0,159,160,1);
    Bsp160160LineH(100,100,20,1);
    Bsp160160LineL(100,100,20,1);
    Bsp160160DrawLine(30,30,130,130,1);
    Bsp160160DrawLine(30,30,50,130,1);
    Bsp160160DrawCircleFast(80,80,25,1);
    Bsp160160Point(2,2,1);
    while(1)
    {
        BspTimeDlyMs(100);
    }
}
//--------------------------------------基本函数-----------------------------------------
//初始化
void    Bsp160160Init(void)
{
    Gl2Init();
}
//画一个点
void    Bsp160160Point(u32 x,u32 y, bool Color)
{
    Gl2DisPoint(x*2  ,y*2  ,Color);
    Gl2DisPoint(x*2  ,y*2+1,Color);
    Gl2DisPoint(x*2+1,y*2  ,Color);
    Gl2DisPoint(x*2+1,y*2+1,Color);
}
//返回某点颜色
bool    Bsp160160PointGet(u32 x,u32 y)
{
    if(Gl2DisPointGet(x*2  ,y*2)> 0)
    {
        return 1;
    }
    return 0;
}
//使用同一颜色画一横线
void Bsp160160LineH(u32 x0,u32 y0,u32 xlen,bool c)
{
    for(;xlen >0;x0++,xlen--)
    {
        Bsp160160Point(x0,y0,c);
    }
}
//使用同一颜色画一竖线
void Bsp160160LineL(u32 x0,u32 y0,u32 ylen,bool c)
{
    for(;ylen >0;y0++,ylen--)
    {
        Bsp160160Point(x0,y0,c);
    }
}
//清屏
void Bsp160160Clear(void)
{
    Gl2DisClear();
}
//--------------------------------------画直线-------------------------------------------
void static SwapInt(int *x, int *y);
int  static abs(int a);
//方法:Bresenham算法
//参数:x1 y1   第一个点
//      x2 y2   第二个点
//      color   颜色
void Bsp160160DrawLine(int x1, int y1, int x2, int y2,bool color)
{
    int dx,dy,p,const1,const2,x,y,inc;
    int steep = (abs(y2 - y1) > abs(x2 - x1)) ? 1 : 0;
    if(steep == 1)
    {
        SwapInt(&x1, &y1);
        SwapInt(&x2, &y2);
    }
    if(x1 > x2)
    {
        SwapInt(&x1, &x2);
        SwapInt(&y1, &y2);
    }
    dx = abs(x2 - x1);
    dy = abs(y2 - y1);
    p = 2 * dy - dx;
    const1 = 2 * dy;
    const2 = 2 * (dy - dx);
    x = x1;
    y = y1;
    inc = (y1 < y2) ? 1 : -1;
    while(x <= x2)
    {
        if(steep == 1)
            Bsp160160Point(y, x,color);
        else
            Bsp160160Point(x, y,color);
        x++;
        if(p<0)
            p += const1;
        else
        {
            p += const2;
            y += inc;
        }
    }
}
//绝对值函数
int  static abs(int a)
{
    if(a<0)
    {
        return -a;
    }
    return a;
}
//值交换函数
void static SwapInt(int *x, int *y)
{
    int a = *x;
    *x = *y;
    *y = a;
}
//--------------------------------------画圆---------------------------------------------
//方法:快速画圆
//参数:xc,yc   圆心坐标
//      radius  半径
//      color   颜色
void static Draw8points(int xc,int yc,int x,int y,bool c);
void Bsp160160DrawCircleFast(int xc , int yc , int r,bool color)
{
    int x, y, d;
    x = 0;
    y = r;
    d = -r / 2;
    Draw8points(xc , yc , x , y,color);
    if(r % 2 == 0)
    {
        while(x < y)
        {
            x++;
            if(d < 0)
                d += x;
            else
            {
                y--;
                d += x - y;
            }
            Draw8points(xc , yc , x , y,color);
        }
    }else
    {
        while(x < y)
        {
            x++;
            if(d < 0)
                d += x + 1;
            else
            {
                y--;
                d += x - y + 1;
            }
            Draw8points(xc , yc , x , y,color);
        }
    }
}
//按8分法,每次调用将画出8个对称的点
void static Draw8points(int xc,int yc,int x,int y,bool c)
{
    Bsp160160Point(xc+x,yc+y,c);
    Bsp160160Point(xc-x,yc+y,c);
    Bsp160160Point(xc+x,yc-y,c);
    Bsp160160Point(xc-x,yc-y,c);
    Bsp160160Point(xc+y,yc+x,c);
    Bsp160160Point(xc-y,yc+x,c);
    Bsp160160Point(xc+y,yc-x,c);
    Bsp160160Point(xc-y,yc-x,c);
}
原创粉丝点击