在c中使用bmp文件拍摄屏幕截图?

在c中使用bmp文件拍摄屏幕截图?,c,bmp,C,Bmp,我正在尝试用c创建一个3D游戏 添加标志“--save”时 游戏将截图保存下来 所以要做到这一点(从游戏中截图)。 我必须使用BMP文件,所以我必须使用c 守则: #include "../includes/cub3d.h" t_screenshot *ft_init_shot(t_mlx *mlx) { t_screenshot *takeshot; takeshot = malloc(1 * sizeof(t_screenshot)

我正在尝试用c创建一个3D游戏

添加标志“--save”时 游戏将截图保存下来

所以要做到这一点(从游戏中截图)。 我必须使用BMP文件,所以我必须使用c

守则:

#include "../includes/cub3d.h"

t_screenshot        *ft_init_shot(t_mlx *mlx)
{
    t_screenshot    *takeshot;

    takeshot = malloc(1 * sizeof(t_screenshot));
    takeshot->width = w;
    takeshot->height = h;
    takeshot->bitcount = 24;
    takeshot->width_in_bytes = ((takeshot->width * takeshot->bitcount + 31) / 32) * 4;
    takeshot->imagesize = takeshot->width_in_bytes * takeshot->height;
    takeshot->filesize = 54 + takeshot->imagesize;
    return (takeshot);
}

unsigned char       *ft_bitheader(t_mlx *mlx)
{
    unsigned char    *header;
    uint32_t bisize;
    uint32_t bfoffbits;
    uint16_t biplanes;
    
    header =(unsigned char *)malloc(54 * sizeof(char));
    bisize = 40;
    bfoffbits = 54;
    biplanes = 1;
    memcpy(header, "BM", 2);
    memcpy(header + 2 , &mlx->shot->filesize, 4);
    memcpy(header + 10, &bfoffbits, 4);
    memcpy(header + 14, &bisize, 4);
    memcpy(header + 18, &mlx->shot->width, 4);
    memcpy(header + 22, &mlx->shot->height, 4);
    memcpy(header + 26, &biplanes, 2);
    memcpy(header + 28, &mlx->shot->bitcount, 2);
    memcpy(header + 34, &mlx->shot->imagesize, 4);
    return (header);
}

void                screen_shot(t_mlx *mlx)
{
    ft_move(mlx);
    ft_update(mlx, YES);
    mlx->shot = ft_init_shot(mlx);
    mlx->shot->header = ft_bitheader(mlx);
    screno(mlx);
}
 
void                screno(t_mlx *mlx)
{
    int x ;
    int y ;
    int row;
    int col;

    x = 0;
    y = 0;
    row = mlx->shot->height - 1;
    col = 0;
    unsigned char* buf = malloc(mlx->shot->imagesize);
    while (row >= 0)
    {
        y = 0;
        col = 0;
        while (col < mlx->shot->width)
        {
            int red = (mlx->tex.img_data[x * w + y] >> 16) & 0xFF;
            int green = (mlx->tex.img_data[x * w + y] >> 8) & 0xFF;
            int blue = mlx->tex.img_data[x * w + y] & 0xFF;
            buf[row * mlx->shot->width_in_bytes + col * 3 + 0] = blue;
            buf[row * mlx->shot->width_in_bytes + col * 3 + 1] = green;
            buf[row * mlx->shot->width_in_bytes + col * 3 + 2] = red;
            col++;
            y++;
        }
        row--;
        x++;
    }
    ft_printf("Taking ScreenShoot....\n");
    FILE *image = fopen("screenshot.bmp", "wb");
    ft_printf("ScreenShot Has been saved under The name 'screenshot.bmp']\n");
    fwrite(mlx->shot->header, 1, 54, image);
    fwrite((char*)buf, 1, mlx->shot->imagesize, image);
    fclose(image);
    free(buf);
}
#include“./includes/cub3d.h”
t_屏幕截图*ft_初始截图(t_mlx*mlx)
{
t_截图*截图;
takeshot=malloc(1*sizeof(t_屏幕截图));
拍摄->宽度=w;
拍摄->高度=h;
拍摄->比特数=24;
takeshot->width_,单位为字节=((takeshot->width*takeshot->bitcount+31)/32)*4;
拍摄->图像大小=拍摄->宽度(以字节为单位)*拍摄->高度;
拍摄->文件大小=54+拍摄->图像大小;
回击(投篮);
}
无符号字符*ft\u比特头(t\u mlx*mlx)
{
无符号字符*头;
uint32_t bisize;
uint32_t bOffbit;
uint16双飞机;
头=(无符号字符*)malloc(54*sizeof(字符));
bisize=40;
bOffBits=54;
双翼飞机=1;
memcpy(标题“BM”,2);
memcpy(header+2,&mlx->shot->filesize,4);
memcpy(标头+10和bOffBits,4);
memcpy(页眉+14,和bisize,4);
memcpy(页眉+18,&mlx->shot->width,4);
memcpy(收割台+22,&mlx->放炮->高度,4);
memcpy(收割台+26和双平面,2);
memcpy(页眉+28,&mlx->shot->bitcount,2);
memcpy(页眉+34,&mlx->shot->imagesize,4);
返回(标题);
}
无效屏幕截图(t_mlx*mlx)
{
ft_移动(mlx);
ft_更新(mlx,是);
mlx->shot=ft_init_shot(mlx);
mlx->shot->header=ft_比特头(mlx);
斯克雷诺(mlx);
}
无效screno(t_mlx*mlx)
{
int x;
int-y;
int行;
int col;
x=0;
y=0;
row=mlx->放炮->高度-1;
col=0;
无符号字符*buf=malloc(mlx->shot->imagesize);
而(行>=0)
{
y=0;
col=0;
同时(镜头镜头->宽度)
{
int red=(mlx->tex.img_数据[x*w+y]>>16)&0xFF;
int绿色=(mlx->tex.img_数据[x*w+y]>>8)&0xFF;
int blue=mlx->tex.img_数据[x*w+y]&0xFF;
buf[row*mlx->shot->宽度(以字节为单位)+列*3+0]=蓝色;
buf[行*mlx->快照->宽度(以字节为单位)+列*3+1]=绿色;
buf[行*mlx->快照->宽度(以字节为单位)+列*3+2]=红色;
col++;
y++;
}
行--;
x++;
}
ft_printf(“拍摄截图…\n”);
FILE*image=fopen(“screenshot.bmp”、“wb”);
ft_printf(“屏幕截图已以'ScreenShot.bmp'的名称保存]\n”);
fwrite(mlx->shot->header,1,54,图像);
fwrite((char*)buf,1,mlx->shot->imagesize,image);
fclose(图像);
免费(buf);
}
问题是,当我编译此代码时,图像有时会出现,有时会出现以下错误:


谢谢你的帮助。

与你的问题无关,这个程序到处都在泄漏内存。为每个分配的块调用free()本身可能会揭示堆损坏,而这又可能是由数组越界、无效指针等引起的。文件格式可能是错误的,google bmp file format在哪里分配了
mlx
?关于bmp格式,它是否假定/需要很少的endian?因为这里的一些数据是使用endianess独立代码计算的,比如
int red=(mlx->tex.img_data[x*w+y]>>16)&0xFF,而其他数据是硬编码的小尾端,如
memcpy(header+10,&bfoffbits,4)您是否检查了所创建文件的标题以获得正确的值?与您的问题无关,此程序到处泄漏内存。为每个分配的块调用free()本身可能会揭示堆损坏,而这又可能是由数组越界、无效指针等引起的。文件格式可能是错误的,google bmp file format在哪里分配了
mlx
?关于bmp格式,它是否假定/需要很少的endian?因为这里的一些数据是使用endianess独立代码计算的,比如
int red=(mlx->tex.img_data[x*w+y]>>16)&0xFF,而其他数据是硬编码的小尾端,如
memcpy(header+10,&bfoffbits,4)是否已检查所创建文件的标题以获取正确的值?