c语言二维buffer,c – 如何使用c语言在framebuffer中绘制图形?

在/ dev(例如/ dev / fb0)中的正确文件上使用

open(),然后使用

mmap()将其映射到内存中.如果您不知道如何使用这些系统调用,联机帮助页将帮助您.

然后,< linux / fb.h>中的某些ioctl()有一些结构和常量.像许多内核头文件一样,您可以轻松学习浏览文件.

特别有趣的是ioctl FBIOGET_VSCREENINFO与struct fb_var_screeninfo.注意这里有xres,yres(resolution)和bits_per_pixel.那么有FBIOGET_FSCREENINFO和struct fb_fix_screeninfo,其中有更多的信息,如类型和line_length.

因此,(x,y)处的像素可能位于mmap_base_address x * bits_per_pixel / 8 y * line_length.像素的确切格式将取决于您通过ioctl检索的结构;这是你的工作,决定如何读/写它们.

已经有一段时间,因为我已经与这样做,所以我有点朦胧更多的细节..

这是一个快速而脏的代码示例,只是为了说明它的完成情况…我还没有测试过.

#include

#include

#include

#include

#include

#include

#include

int main()

{

struct fb_var_screeninfo screen_info;

struct fb_fix_screeninfo fixed_info;

char *buffer = NULL;

size_t buflen;

int fd = -1;

int r = 1;

fd = open("/dev/fb0", O_RDWR);

if (fd >= 0)

{

if (!ioctl(fd, FBIOGET_VSCREENINFO, &screen_info) &&

!ioctl(fd, FBIOGET_FSCREENINFO, &fixed_info))

{

buflen = screen_info.yres_virtual * fixed_info.line_length;

buffer = mmap(NULL,

buflen,

PROT_READ|PROT_WRITE,

MAP_SHARED,

fd,

0);

if (buffer != MAP_FAILED)

{

/*

* TODO: something interesting here.

* "buffer" now points to screen pixels.

* Each individual pixel might be at:

* buffer + x * screen_info.bits_per_pixel/8

* + y * fixed_info.line_length

* Then you can write pixels at locations such as that.

*/

r = 0; /* Indicate success */

}

else

{

perror("mmap");

}

}

else

{

perror("ioctl");

}

}

else

{

perror("open");

}

/*

* Clean up

*/

if (buffer && buffer != MAP_FAILED)

munmap(buffer, buflen);

if (fd >= 0)

close(fd);

return r;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值