linux下的framebuffer(2)

news/2024/7/8 5:20:24 标签: linux, struct, gcc, function, 图形, file

目前根据自己对linux中framebuffer的了解写了如下的代码:

下面是用到的一些文件,在linux下编译通过,显示图形效果。

 效果:主要代码在fb.c中,在显卡x,y=(1100,100)出显示x0,y0=(130,150)大小的颜色,再在x0,y0的坐标基础上添加红色图像,如代码所所示。

main.c

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <time.h>
#include <sys/mman.h>
#include"fb.h"

 

int main()
{

  init_fb();//init framebuffer

 
 
 return 0;
}

 

fb.c

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <time.h>
#include <sys/mman.h>
#include"fb.h"

color_t color={0,0,150,100};   //the color of background
color_t color1={0,255,0,0};    //in window   red

int init_fb()
 {
  int fbfd = 0;
 struct fb_var_screeninfo vinfo;
 struct fb_fix_screeninfo finfo;
 long int screensize = 0;
 char *fbp = 0;
 int x = 0, y = 0;
 long int location = 0;
 long int location1 = 0;
 fbfd = open("/dev/fb0", O_RDWR); // Open the file for reading and writing
  if (!fbfd)
  {
  printf("Error: cannot open framebuffer device.\n");
  exit(0);
  }
  printf("The framebuffer device was opened successfully.\n");
  // Get fixed screen information
  if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
  {
  printf("Error reading fixed information.\n");
   exit(0);
  }
  // Get variable screen information
  if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
  {          
  printf("Error reading variable information.\n");
   exit(0);
  }
  printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
        // Figure out the size of the screen in bytes
  screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  printf("screensize is %ldKB\n",screensize/1024);
        // Map the device to memory
  fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  if((int)fbp == -1)
  {
  printf("Error: failed to map framebuffer device to memory.\n");
  exit(0);
  }
  printf("The framebuffer device was mapped to memory successfully.\n");

 for (x = 0; x < DISPLAY_WIDE; x++)
  for (y = 0; y < DISPLAY_HIGH; y++)
  {
  location = (x+1100+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +(y+100+vinfo.yoffset) * finfo.line_length;
 //if (vinfo.bits_per_pixel == 32)
  
   *(fbp + location + 3) = color.t;// No transparency
   *(fbp + location + 2) = color.r;// A  red
   *(fbp + location + 1) = color.g;// A  green
   *(fbp + location) = color.b;    // Some blue 
     
  } 
  /*
    function:in window display red
    author  :sjmp
     time   :2012.06.02
  */
  for (x = 0; x < DISPLAY_WIDE-10; x++)
  for (y = 0; y < DISPLAY_HIGH-10; y++)
  {
  location1 = (x+5+(1100+vinfo.xoffset)) * (vinfo.bits_per_pixel/8) +(y+(5+100+vinfo.yoffset)) * finfo.line_length;
   *(fbp + location1 + 3) = color1.t;
   *(fbp + location1 + 2) = color1.r;
   *(fbp + location1 + 1) = color1.g;
   *(fbp + location1) = color1.b; 
   
   }
  munmap(fbp, screensize);
  return 0;
  close(fbfd);
 }

 

fb.h

#ifndef FBI_H_
#define FBI_H_

#define DISPLAY_WIDE  130      //display wide
#define DISPLAY_HIGH  150      //display high


typedef struct
 {
   int t;
   int r;
   int g;
   int b;
 } color_t;

int  init_fb();

#endif

 

Makefile


%.o : %.c        
 @gcc -Wall -c $< -o $@

OBJS= main.o fb.o                //only this need change

all: $(OBJS)
 @gcc $(OBJS) -o fb
 @strip fb                              //put 'fb ' in the first place
 
clean:
  @rm -f *.o
 @rm -f *.c~
  @rm -f fb
 

 


http://www.niftyadmin.cn/n/1262345.html

相关文章

MVVM之Event and Command

Event&#xff1a; 在Silverlight和WPF中没有使用.net的LCR事件&#xff0c;而是使用Routed路由事件&#xff0c;根本原因是因为Silverlight控件的节点树。 一个简单的示例&#xff1a; public static readonly RoutedEvent MyRoutedEvent EventManager.RegisterRoutedEvent(&…

js和as的稳定传值问题

最近在实现flash的播放音乐的功能&#xff0c;这就涉及到js和as交互的问题&#xff0c;因为要实现动态改变音乐文件的功能。但是在实现js传值给flash时&#xff0c;flash在获取值存在几率性&#xff0c;有时可以获得到&#xff0c;有时有获取不到。后来发现&#xff0c;其实是由…

linux内核struct链表程序与分析

struct是编程的重要部分&#xff0c;学好struct非常重要&#xff0c;本文参考网友与自己总结&#xff0c;适合初学者&#xff0c;或者想了解linux内核对struct的利用。 编写的代码在linux下gcc可编译通过。 # gcc mylist.c -o mylist [jinyonglocalhost 888]$ lslist.h myli…

Windows消息+GlobalAddAtom 实现两程序通信

WinAPI: AddAtom、DeleteAtom、FindAtom、GetAtomName、GlobalAddAtom、GlobalDeleteAtom、GlobalFindAtom、GlobalGetAtomName这是储存字符串的一组 API.通过 AddAtom 储存一个字符串, 返回一个 ID;GetAtomName 通过这个 ID 可返回储存的字符串;还可以用 FindAtom 查找、用 De…

《转》linux内存管理

内存管理模块是操作系统的心脏&#xff1b;它对应用程序和系统管理非常重要。今后的几篇文章中&#xff0c;我将着眼于实际的内存问题&#xff0c;但也不避讳其中的技术内幕。由于不少概念是通用的&#xff0c;所以文中大部分例子取自32位x86平台的Linux和Windows系统。本系列第…

Windows Phone SDK for Windows 8

首先安装Games for Windows – LIVE Redistributable即可。 前两天刚好装了Windows 8 消费者预览版&#xff0c;用来学习Metro style Apps 开发。本来呢&#xff0c;也还正在学习Windows Phone 7开发。双系统&#xff0c;继续学习WP7的话&#xff0c;还得重启切换回Win7&#x…

《转》linux内核如何管理内存

在仔细审视了进程的虚拟地址布局之后&#xff0c;让我们把目光转向内核以及其管理用户内存的机制。再次从gonzo图示开始&#xff1a; Linux进程在内核中是由task_struct的实例来表示的&#xff0c;即进程描述符。task_struct的mm字段指向内存描述符&#xff08;memory descript…

Qt Creator中的snippets

最近公司的一个项目使用Qt/QML进行开发。 项目有点小大&#xff0c;团队人员比较多。想规范大家的文件头、函数注释等。以前一直用VC的&#xff0c;刚开始使用Qt Creator&#xff0c;还在到处找类似VC助手的插件。 后来发现在Qt Creator本身就提供了snippets的功能&#xff0c;…