如何使用c语言在framebuffer中绘制图形..?

Rah*_*hul 11 c c++ linux framebuffer

我是这个linux framebuffer的新手,所以有人引导我在framebuffer中绘制线图.我有代码在turbo c中绘制图形但现在在linux中.所以请帮帮我.

谢谢你,拉胡尔

asv*_*kau 19

open()/dev(例如/dev/fb0)中使用右侧文件,然后使用mmap()它将其映射到内存中.如果您不知道如何使用它们,则联机帮助将有助于这些系统调用.

然后有一些ioctl()s的结构和常量<linux/fb.h>.像许多内核头文件一样,您只需浏览文件即可学到很多东西.

特别有趣的是IOCTL FBIOGET_VSCREENINFOstruct fb_var_screeninfo.注意这有xres,yres(分辨率)和bits_per_pixel.然后有FBIOGET_FSCREENINFO,struct fb_fix_screeninfo并有更多的信息,如typeline_length.

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

我已经用了一段时间了,所以我对更多细节有些模糊.

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

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include <linux/fb.h>

#include <unistd.h>
#include <fcntl.h>

#include <stdio.h>

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;
}
Run Code Online (Sandbox Code Playgroud)