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

前端之家收集整理的这篇文章主要介绍了c – 如何使用c语言在framebuffer中绘制图形?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是这个 linux framebuffer的新手,所以任何人都可以指导我在framebuffer中画线形图.我有代码在turbo c中绘制图形,但现在在linux中.所以请帮我

谢谢,
拉胡尔

解决方法

在/ 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检索的结构;这是你的工作,决定如何读/写它们.

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

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

  1. #include <sys/types.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/mman.h>
  4.  
  5. #include <linux/fb.h>
  6.  
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9.  
  10. #include <stdio.h>
  11.  
  12. int main()
  13. {
  14. struct fb_var_screeninfo screen_info;
  15. struct fb_fix_screeninfo fixed_info;
  16. char *buffer = NULL;
  17. size_t buflen;
  18. int fd = -1;
  19. int r = 1;
  20.  
  21. fd = open("/dev/fb0",O_RDWR);
  22. if (fd >= 0)
  23. {
  24. if (!ioctl(fd,FBIOGET_VSCREENINFO,&screen_info) &&
  25. !ioctl(fd,FBIOGET_FSCREENINFO,&fixed_info))
  26. {
  27. buflen = screen_info.yres_virtual * fixed_info.line_length;
  28. buffer = mmap(NULL,buflen,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
  29. if (buffer != MAP_Failed)
  30. {
  31. /*
  32. * TODO: something interesting here.
  33. * "buffer" now points to screen pixels.
  34. * Each individual pixel might be at:
  35. * buffer + x * screen_info.bits_per_pixel/8
  36. * + y * fixed_info.line_length
  37. * Then you can write pixels at locations such as that.
  38. */
  39.  
  40. r = 0; /* Indicate success */
  41. }
  42. else
  43. {
  44. perror("mmap");
  45. }
  46. }
  47. else
  48. {
  49. perror("ioctl");
  50. }
  51. }
  52. else
  53. {
  54. perror("open");
  55. }
  56.  
  57. /*
  58. * Clean up
  59. */
  60. if (buffer && buffer != MAP_Failed)
  61. munmap(buffer,buflen);
  62. if (fd >= 0)
  63. close(fd);
  64.  
  65. return r;
  66. }

猜你在找的C&C++相关文章