频 道 直 达 - 新闻 - 读书 - 培训 - 教程 - 前沿 - 组网 - 系统应用 - 安全 - 编程 - 存储 - 操作系统 - 数据库 - 服务器 - 专题 - 产品 - 案例库 - 技术圈 - 博客 - BBS
51CTO.COM_中国领先的IT技术网站
找资料:

我--曾经的C语言痴迷者,就算C死了,也永远活在我心里(2)

作者: 老李 出处:Csdn博客  (  ) 砖  (  ) 好  评论 ( ) 条  进入论坛
更新时间:2007-01-23 11:07
关 键 词:C语言
阅读提示:C语言已死?这个论题成为近期的热点。C语言到底有没有死?其实我也说不清楚,因为我现在从事的工作是用C++开发图形程序,但是我仍然有必要写这样一篇文章,如果你觉得C没有死,并且正准备学习她,那么希望我的经历对你有启示作用,如果你是曾经的C痴迷者并且现在不再用C,那么就和我一起怀念逝去的痴迷吧!

一个dos下的简单图形函数库

/*
=============0x13h display mode version 1.1 Header
=============CopyRight (C) LiGungcun
=============QQ:66855618
=============E-Mail: hoya-lee@sohu.com
--------Compiler is Borland TC++3.0 for DOS 
---------The last modification 08/02/2004 
*/
#ifndef __V_13__
#define __V_13__
/*===========head file include============*/
#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <mem.h>
#include <math.h>
#include <stdarg.h>
#include <process.h>
#include <bios.h>
/*===========macro defined================*/
/*===========globe variable===============*/
unsigned char g_current_color;    //setcolor current color for draw
unsigned char g_back_color;        //setcolor backgrand color for draw
int g_image_height;                //get image from screen height
int g_image_width;                //get image from screen width
void far *g_bitmap;                //a bit_image pointer
/*===========data structure===============*/
typedef struct {
    unsigned char r;    //Dos VGA 13 Mode 
    unsigned char g;    //256Color
    unsigned char b;    //wei cai
} palette_type;            //palette
typedef struct{
    unsigned long fsize;        /*2文件大小4B*/
    unsigned long offset;        /*10图象数据在文件中的偏移地址4B*/
    unsigned long width,height;    /*18,22宽高(像素)4B+4B*/
    unsigned int bit;            /*28指明每个像素点需要的位数2B*/
    unsigned long compres;        /*30压缩标志0为不压1为256色压4B*/
    unsigned long bsize;        /*34图象数据所占用的字节数4B*/
}BMP;        // Load Bitmap to this struct
/*===========function declare=============*/
void    init_graph          (void);
void    close_graph         (void);
void    draw_pixel          (unsigned x,unsigned y,unsigned char color);
void    draw_line           (unsigned x1,unsigned y1,unsigned x2,unsigned y2);
void    draw_rectangle      (unsigned left, unsigned top, unsigned right, 
unsigned bottom);
void    draw_bar            (unsigned left, unsigned top, unsigned right, 
unsigned bottom);
void    draw_circle         (unsigned x, unsigned y, unsigned r);
void    draw_solid_circle    (unsigned x, unsigned y, unsigned r);
void    clear_device        (unsigned left,unsigned top,unsigned right,
unsigned bottom);
void    set_back_color        (unsigned char color);
unsigned char     get_back_color        (void);
void    set_current_color       (unsigned char color);
unsigned char     get_current_color       (void);
void far * get_image           (unsigned left, unsigned top, unsigned right, 
unsigned bottom);
void    put_image           (unsigned left,unsigned top,void far *bitmap);
int     get_max_x           (void);
int     get_max_y           (void);
unsigned get_pixel          (unsigned x,unsigned y);
void    out_text_xy         (unsigned x,unsigned y,const unsigned char *text);
void    set_palette         (unsigned color_index,palette_type rgb);
palette_type get_palette    (unsigned color_index);
int     show_bmp            (int x,int y,char *path);
/*===========function implement===========*/
void init_graph(void){
    union REGS r;
    r.x.ax=0x13;
    int86(0x10,&r,&r);
    g_current_color=0;
    g_image_height=0;
    g_image_width=0;
    g_bitmap=NULL;
}
void close_graph(void){
    union REGS r;
    r.x.ax=0x03;
    int86(0x10,&r,&r);
}
void draw_pixel (unsigned x,unsigned y,unsigned char color){
    unsigned char *video_buffer=NULL;
    if(x<320&&y<200){
        *(video_buffer=MK_FP(0xa000,((y<<8)+(y<<6))+x))=color;
    }
    else
        return;
}
//these algorithm reference my <>
void draw_line (unsigned x1,unsigned y1,unsigned x2,unsigned y2){
    int dx,dy,index,x_inc,y_inc;
    int error=0;
    if(x1>319||x2>319return ;
    if(y1>199||y2>199return ;
    dx=x2-x1;     dy=y2-y1;
    if(dx>=0){
        x_inc=1;
    }
    else{
        x_inc=-1;
        dx=-dx;
    }
    if(dy>=0){
        y_inc=1;
    }
    else{
        y_inc=-1;
        dy=-dy;
    }
if(dx>dy){
        for(index=0;index<=dx;index++){
            draw_pixel(x1,y1,g_current_color);
            error+=dy;
            if(error>dx){
                error-=dx;
                y1+=y_inc;
            }
            x1+=x_inc;
        }
    }
    else{
        for(index=0;index<=dy;index++) {
            draw_pixel(x1,y1,g_current_color);
            error+=dx;
            if(error>0){
                error-=dy;
                x1+=x_inc;
            }
            y1+=y_inc;
        }
    }
}
void draw_rectangle (unsigned left, unsigned top, unsigned right, 
unsigned bottom){
    unsigned t;
    if(left>319||right>319return ;
    if(top>199||bottom>199return ;
    if(left>right){
        t=left; left=right; right=t;
    }
    if(top>bottom){
        t=top; top=bottom; bottom=t;
    }
    draw_line(left,top,right,top);
    draw_line(left,top,left,bottom);
    draw_line(left,bottom,right,bottom);
    draw_line(right,top,right,bottom);
}
void draw_bar (unsigned left, unsigned top, unsigned right, 
unsigned bottom){
    int i;
    unsigned x,y,t;
    if(left>319||right>319return ;
    if(top>199||bottom>199return ;
    if(left>right){
        t=left; left=right; right=t;
    }
    if(top>bottom){
        t=top; top=bottom; bottom=t;
    }
    for(y=top;y<=bottom;y++){
        /*draw_line(left,y,right,y);*/
        for(i=left;i<=right;i++)
            draw_pixel(i,y,g_current_color);
    }
}
void draw_circle (unsigned x, unsigned y, unsigned r){
     int t_x,t_y,i;
     float pi_2=6.283;
     if(x>319||y>199return ;
     if(r>319return ;
     for(i=1;i<720;i++){
        t_x=x+r*cos(pi_2/720*i);
        t_y=y+r*sin(pi_2/720*i);
        draw_pixel(t_x,t_y,g_current_color);
     }
}
void draw_solid_circle(unsigned x, unsigned y, unsigned r){
     int i;
     if(x>319||y>199return ;
     if(r>319return ;
     for(i=1;i<r;i++)
        draw_circle(x,y,i);
}
void clear_device (unsigned left,unsigned top,unsigned right,unsigned bottom){
     unsigned char t_color;
     t_color=g_current_color;
     set_current_color(get_back_color());
     draw_bar(left,top,right,bottom);
     set_current_color(t_color);
}
void set_back_color (unsigned char color){
    unsigned char t_color;
    g_back_color=color;
    t_color=get_current_color();
    set_current_color(color);
    draw_bar(0,0,get_max_x(),get_max_y());
    set_current_color(t_color);
}
unsigned char get_back_color (void){
    return g_back_color;
}
void set_current_color (unsigned char color){
    g_current_color=color;
}
unsigned char get_current_color (void){
    return g_current_color;
}
void far *get_image (unsigned left, unsigned top, unsigned right, 
unsigned bottom){ 
    unsigned char *t_video=NULL,*t_bitmap=NULL;
    int i; unsigned t;
    if(left>319||right>319return NULL;
    if(top>199||bottom>199return NULL;
    if(left>right){
        t=left; left=right; right=t;
    }
    if(top>bottom){
        t=top; top=bottom; bottom=t;
    }
    g_image_height=bottom-top+1;  g_image_width=right-left+1;
    if((g_bitmap=(unsigned char *)malloc(g_image_height*g_image_width))==NULL)
        return NULL;
    t_video=MK_FP(0xa000,((top<<8)+(top<<6))+left);
    t_bitmap=g_bitmap;
    for(i=0;i<g_image_height;i++){
        memcpy(t_bitmap,t_video,g_image_width);
        t_bitmap+=g_image_width;
        t_video+=320;
    }
    return g_bitmap;
}
void put_image (unsigned left,unsigned top,void far *bitmap){
    unsigned char *t_video=NULL,*t_bitmap=NULL;
    int i;
    if(bitmap==NULL) return ;
    if(left>319||top>199return ;
    if(left+g_image_width>321||top+g_image_height>201return ;
    t_video=MK_FP(0xa000,((top<<8)+(top<<6))+left);
    t_bitmap=bitmap;
    for(i=0;i<g_image_height;i++){
        memcpy(t_video,t_bitmap,g_image_width);
        t_bitmap+=g_image_width;
        t_video+=320;
    }
    free(bitmap);  bitmap=NULL;
    g_image_width=0;
    g_image_height=0;
}
int get_max_x (void){
    return 319;
}
int get_max_y (void){
    return 199;
}
unsigned get_pixel (unsigned x,unsigned y){
    unsigned char *video_buffer=NULL;
    if(x<320){
        video_buffer=MK_FP(0xa000,((y<<8)+(y<<6))+x);
        return *(video_buffer);
    }
    else
        return 300;
}
/*==========out_text function=============*/
void print_hz(char *zimo,int size,int x,int y){
    int i,j,k,n;
    if(x<0||x>303||y<0||y>183return ;
    n=(size-1)/8+1;
    for(j=0;j<size;j++)
        for(i=0;i<n;i++)
            for(k=0;k<8;k++)
                if(zimo[j*n+i]&(0x80>>k))
                    draw_pixel(x+i*8+k,y+j,g_current_color);
}
void print_hzk(char *hz,int x,int y,const char *path){
     int c1,c2;
     FILE *fp=NULL;
     char buf[32];
     unsigned long offset;
     if(x<0||x>303||y<0||y>183return ;
     fp=fopen(path,"rb");
     if(fp==NULL){ close_graph(); printf("Can't find file hzk16"); exit(0);}
     c1=hz[0]-0xa0;  //c1=(c1<<8)>>8;
     c2=hz[1]-0xa0;  //c2=(c2<<8)>>8;
     offset=(94*(c1-1)+(c2-1))*32L;
     fseek(fp,offset,0);
     fread(buf,32,1,fp);
     print_hz(buf,16,x,y);
     fclose(fp);
}
void print_ascii(char c,int x,int y){
 /*ascii 8*8 字模点阵地址=0xF000:0xFA6E*/
    char m[8];
    int i,j;
    char far *p=(char far*)0xf000fa6e;
    if(x<0||x>311||y<0||y>191return ;
    p=p+c*8;
    memcpy(m,p,8);
    if((x<311)&&(y<191)){
        for(i=0;i<8;i++)
            for(j=0;j<8;j++){
                if(m[i]&(0x80>>j))
                    draw_pixel(x+j,y+i,g_current_color);
            }
    }
}
/*==========out_text function END=========*/
void out_text_xy (unsigned x,unsigned y,const unsigned char *text){
    int t_x,t_y;
    int i;
    unsigned char *t_text=NULL;
    if(x>311||y>191return ;
    t_x=x;  t_y=y;  t_text = (unsigned char *) text;
    while(*t_text!='

来源链接: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1477960

(责任编辑 火凤凰 sunsj@51cto.com  TEL:(010)68476636-8007)



共2页: 上一页 [1] 2
【内容导航】
发表
查看
我也说两句

匿名发表

(如果看不清请点击图片进行更换)


中 国 领 先 的 IT 技 术 网 站 ·
技 术 成 就 梦 想
·Java基础教程 (查看52473次)
·UML类图详解 (查看46951次)
·Java编程开发手册 (查看25172次)
·UML统一建模语言 (查看24155次)
·C#技术开发指南 (查看22515次)
·Java编程开发手册 (1195个砖)
·Java基础教程 (429个砖)
·C#技术开发指南 (304个砖)
·PB开发教程 (220个砖)
·.NET开发手册 (217个砖)
·Java编程开发手册 (653个好)
·Java基础教程 (569个好)
·.NET开发手册 (251个好)
·PB开发教程 (209个好)
·Delphi开发技术手册 (174个好)
订阅技术快讯
电子杂志下载
名称:网络安全精品应用黄皮书
简介:《2007精品网络安全黄皮书》包括了9个大类24个小类, 800余篇文章,内容包含了熊猫烧香病毒、DDOS攻击、ARP病等热点问题的介绍及解决方案。从病毒查杀、防范、系统、数据等各方面的安全设置到黑客技术的了解、防范,涉及到了安全应用的全部领域, 由浅至深内容全面。
名称:Vista精品应用黄皮书
简介:《Vista精品应用黄皮书》囊括了Vista的各方面内容。此次的精简版,是将里面的内容做了提取,便于用户下载和使用。内容包含了各种Vista的安装与实施、技巧与解析以及各种Vista相关学习文档和相关软件的安全下载。该电子书是了解和应用Vista人员必备的工具手册,并且也是第一本
名称:2006中国IT论坛精品集合
简介:本书由“51CTO论坛推广联盟”制作完成。书中所有内容均来自各联盟成员的论坛(网站)。制作本书的目的是为了集中大家的优势资源,将更多更精彩的内容带给广大技术爱好者。本书是联盟成立以来制作的第一本书。
关键字阅读
频道精选
主编信箱 热线:010-66476606 告诉我们您想看的:专题 文章
关于我们 | 诚聘英才 | 联系我们 | 网站大事 | 意见反馈 | 网站地图
Copyright©2005-2007 51CTO.COM 版权所有