回复:(econoy)c中putimage()函数的问题
good !我还以为这样的废贴没人回了呢。:-)
首先贴运行正常的:两个圆碰撞:
#include<stdio.h>
#include<graphics.h>
int main()
{
int i, gdriver, gmode, size;
void *buf;
gdriver=DETECT;
initgraph(&gdriver, &gmode, "");
setbkcolor(BLUE);
cleardevice();
setcolor(LIGHTRED);
setlinestyle(0,0,1);
setfillstyle(1, 10);
circle(100, 200, 30);
floodfill(100, 200, 12);
size=imagesize(69,169,131,231);
buf=malloc(size);
getimage(69, 169, 131,231,buf);
getch();
for(i=0; i<185; i++){
putimage(70+i, 170, buf, COPY_PUT);
putimage(500-i, 170, buf, COPY_PUT);
}
for(i=0;i<185; i++){
putimage(255-i, 170, buf, COPY_PUT);
putimage(315+i, 170, buf, COPY_PUT);
}
getch();
closegraph();
}
再贴不能运行的:(在此附上16色bmp图像,将它置于turbo c目录下)
#include<stdio.h>
#include<dir.h>
#include<dos.h>
#include<graphics.h>
char *buffer;
char *malloc();
char bmp_to_dat(char *bmp,char *dat)
{
unsigned char c[8],scan_times,scan_pixs;
unsigned char workpos;
int i,j,k,n,nowpos,iw,ih;
static int color[16]={0,4,2,6,1,5,3,7,8,12,10,14,9,13,11,15};
unsigned char workline[640],scanline[640];
FILE *fp,*targetfp;
union
{
unsigned char value ;
struct
{
unsigned cl:4;
unsigned ch:4;
}color;
}mycolor;
if((fp=fopen(bmp,"rb"))==NULL)return 0;
targetfp=fopen(dat,"wb");
fseek(fp,18,SEEK_SET);
iw=0;ih=0;
fread(&iw,4,1,fp);
fread(&ih,4,1,fp);
if(iw==0&&ih==0&&iw>640&&ih>480)
{
fclose(fp);fclose(targetfp);return 0;
}
iw--;ih--;
scan_times=iw/8+1;
scan_pixs=scan_times*4;
fputc(iw%256,targetfp);
fputc(iw/256,targetfp);
fputc(ih%256,targetfp);
fputc(ih/256,targetfp);
fseek(fp,-scan_pixs,SEEK_END);
for(j=0;j<=ih;j++)
{
nowpos=0;
fread(scanline,scan_pixs,1,fp);
fseek(fp,-scan_pixs*2,SEEK_CUR);
for(n=3;n>=0;n--)
{
for(i=0;i<scan_times;i++)
{
workpos=0;
for(k=0;k<4;k++)
{
mycolor.value=scanline[i*4+k];
c[k*2]=color[mycolor.color.ch];
c[k*2+1]=color[mycolor.color.cl];
}
for(k=0;k<8;k++) workpos+=(c[k]>>n&1)<<(7-k);
workline[nowpos]=workpos;nowpos++;
}
}
fwrite(workline,scan_pixs,1,targetfp);
}
fclose(fp);
fclose(targetfp);
return 1;
}
main()
{
int gd=DETECT,gm,n;
char bmpfile[13],_16file[13]={0};
FILE *fp;
struct ffblk *ff;
initgraph(&gd,&gm,"");
setbkcolor(BLUE);
printf("Now begin!\n");
puts("Please select:");
printf("0--exit,1--bitmap file\n");
while(1)
{
n=getch();
switch(n)
{
case 0:continue;
case '0':exit(0);
case '1':{printf("Input bmp file:");gets(bmpfile);goto OUT;}
}
}
OUT:
strcpy(_16file,"dat");
if(!bmp_to_dat(bmpfile,_16file))
{
puts("Can not open file!\n");
getch();
closegraph();
exit(0);
}
fp=fopen(_16file,"rb");
findfirst(_16file,ff,FA_ARCH);
if((buffer=malloc(ff->ff_fsize))==NULL) exit(0);
n=0;
while(!feof(fp))
{
buffer[n]=fgetc(fp);
n++;
}
cleardevice();
for(n=2;n<600;n++)
{putimage(1+n,100,buffer,COPY_PUT);}
getch();
closegraph();
free(buffer);
fclose(fp);
}
借此帖再问:能不能动态获得视频缓冲区的首地址?正想自己做个lib库,让c搞得很郁闷。