This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 16th, 2013  |  syntax: None  |  size: 3.84 KB  |  views: 99  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "framebuffer.h"
  4. #include <termios.h>
  5. #include <unistd.h>
  6. #include <png++/png.hpp>
  7.  
  8. int mygetch(void)
  9. {
  10.     struct termios oldt,newt;
  11.     int ch;
  12.     tcgetattr( STDIN_FILENO, &oldt );
  13.     newt = oldt;
  14.     newt.c_lflag &= ~( ICANON | ECHO );
  15.     tcsetattr( STDIN_FILENO, TCSANOW, &newt );
  16.     ch = getchar();
  17.     tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
  18. return ch;
  19. }
  20. void draw_png(png::image <png::rgba_pixel> image, char *fbpointer,int xoffset=0, int yoffest=0)
  21. {
  22.     fbColor c;
  23.     for (size_t y = 0; y < image.get_height(); ++y)
  24.         {
  25.         for (size_t x = 0; x < image.get_width(); ++x)
  26.             {
  27.             c.r=image[y][x].blue;
  28.             c.g=image[y][x].green;
  29.             c.b=image[y][x].red;
  30.             c.alpha=image[y][x].alpha;
  31.             fbPutPixel(fbpointer,x+xoffset,y+yoffest,c);
  32.             }
  33.         }
  34. }
  35.  
  36. void draw_rectangle(png::image <png::rgba_pixel> image, int ,char *fbpointer)
  37. {
  38.  
  39. }
  40. int main()
  41. {
  42.     int fp=0;
  43.     char *fbpointer = initFrameBuffer("/dev/fb0",0,0,fp);
  44.     int xplace[3];
  45.     int midle;
  46.     png::image <png::rgba_pixel> arch("Arch.png");
  47.     png::image <png::rgba_pixel> android("Android.png");
  48.     png::image <png::rgba_pixel> fsck("Fsck.png");
  49.  
  50.     xplace[0]=vinfo.xres/2/2-android.get_width()/2;
  51.     xplace[1]=vinfo.xres/2-arch.get_width()/2;
  52.     xplace[2]=vinfo.xres/2/2*3-fsck.get_width()/2;
  53.     midle=vinfo.yres/2-200/2;
  54.  
  55.     while ((mygetch())!='q')
  56.     {
  57.         draw_png(android, fbpointer,xplace[0], midle);
  58.         draw_png(arch, fbpointer, xplace[1], midle);
  59.         draw_png(fsck, fbpointer, xplace[2], midle);
  60.     }
  61.     fbClean(fbpointer);
  62.     fbClose(fp);
  63.     return 0;
  64. }
  65.  
  66. And the framebuffer.h:
  67.  
  68. #ifndef FRAMEBUFFER_H_INCLUDED
  69. #define FRAMEBUFFER_H_INCLUDED
  70.  
  71. #include <unistd.h>
  72. #include <stdio.h>
  73. #include <stdlib.h>
  74. #include <fcntl.h>
  75. #include <linux/fb.h>
  76. #include <sys/mman.h>
  77. #include <sys/ioctl.h>
  78.  
  79. struct fb_var_screeninfo vinfo;
  80. struct fb_fix_screeninfo finfo;
  81. char *fbp = 0;
  82.  
  83. typedef struct fbColor
  84. {
  85.   int r;
  86.   int g;
  87.   int b;
  88.   int alpha;
  89. }fbColor;
  90.  
  91. char* initFrameBuffer(char *fbp,int xres,int yres,int fbfd)
  92. {
  93.   /* Open the file for reading and writing */
  94.   fbfd = open(fbp, O_RDWR);
  95.   if (!fbfd)
  96.   {
  97.      printf("Error: cannot open framebuffer device.\n");
  98.      exit(-1);
  99.   }
  100.   if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
  101.   {
  102.       printf("Error reading fixed information.\n");
  103.       exit(-1);
  104.   }
  105.   if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
  106.   {
  107.       printf("Error reading variable information.\n");
  108.       exit(-1);
  109.   }
  110.   fbp = ( char *)mmap(0,(vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8), PROT_READ | PROT_WRITE, MAP_SHARED,fbfd, 0);
  111.   //if ((int)fbp == -1)
  112.   //{
  113.   //   printf("Error: failed to map framebuffer device to memory.\n");
  114.   //   exit(-1);
  115.   //}
  116.   return fbp;
  117. }
  118. long int fbScreenSize()
  119. {
  120.     return vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  121. }
  122. int fbPutPixel(char *fbp,int x,int y,fbColor color)
  123. {
  124.     long int location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
  125.     if (vinfo.bits_per_pixel == 32)
  126.     {
  127.          *(fbp + location) =  color.b;        // Some blue
  128.          *(fbp + location + 1) = color.g;     // A little green
  129.          *(fbp + location + 2) = color.r;    // A lot of red
  130.          *(fbp + location + 3) = color.alpha;      // No transparency
  131.     }
  132.     else
  133.     {
  134.          int b = color.b;
  135.          int g = color.g;     // A little green
  136.          int r = color.r;    // A lot of red
  137.          unsigned short int t = r<<11 | g << 5 | b;
  138.          *((unsigned short int*)(fbp + location)) = t;
  139.     }
  140.     return 0;
  141. }
  142. void fbClose(int fbfd)
  143. {
  144.     close(fbfd);
  145. }
  146. void fbClean(char* fbp)
  147. {
  148.      munmap(fbp,fbScreenSize());
  149. }
  150.  
  151. #endif // FRAMEBUFFER_H_INCLUDED
clone this paste RAW Paste Data