Advertisement
Guest User

Framebuffer

a guest
Jul 13th, 2023
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.31 KB | None | 0 0
  1. #include <sysdefs.h>
  2. #include <dev/framebuffer.h>
  3.  
  4. CURSOR_INFO cInfo;
  5. FRAMEBUFFER* dFb;
  6. PSF1_FONT* dFont;
  7. uint32_t numRowsUsed = 0;
  8.  
  9. void scrollFramebuffer(uint32_t rows) {
  10.     if (rows >= numRowsUsed) {
  11.         return; // Do not scroll beyond the number of used rows
  12.     }
  13.  
  14.     uint32_t* dest = (uint32_t*)dFb->baseAddress;
  15.     uint32_t* src = (uint32_t*)dFb->baseAddress + (rows * dFb->width);
  16.     uint32_t pixelSize = (dFb->height - rows) * dFb->width;
  17.     uint32_t byteSize = pixelSize * sizeof(uint32_t);
  18.     memmove(dest, src, byteSize);
  19.  
  20.     // Clear the newly visible portion at the bottom
  21.     uint32_t* clearStart = (uint32_t*)dFb->baseAddress + pixelSize;
  22.     uint32_t clearBytes = rows * dFb->width * sizeof(uint32_t);
  23.     memset(clearStart, 0, clearBytes);
  24.  
  25.     numRowsUsed -= rows;
  26. }
  27.  
  28. INTERNAL void FrameBufferPutCharInternal(FRAMEBUFFER* fb, PSF1_FONT* font, char chr, uint xOff, uint yOff, HEXCLR clr) {
  29.       uint* pixPtr = (uint*)fb->baseAddress;
  30.       char* fontPtr = (char*)font->charBuffer + (chr * font->header->size);
  31.  
  32.       for (int64_t y = yOff; y < yOff + DEFAULT_FONT_SIZE;y++ ) {
  33.  
  34.  
  35.          for (int64_t x = xOff; x < xOff + (DEFAULT_FONT_SIZE / 2); x++) {
  36.  
  37.  
  38.              if (x >= 0 && x < fb->width && y >= 0 && y < fb->height) {
  39.  
  40.  
  41.  
  42.                 if ((*fontPtr & (0b10000000 >> (x - xOff))) > 0) {
  43.                        *(uint*)(pixPtr + x + (y * fb->scanLinePixels)) = clr;
  44.                 }
  45.             }
  46.         }
  47.     fontPtr++;
  48.   }
  49.  
  50. }
  51. bool IsFramebufferBinded() {
  52.   if (dFb == NULL || dFont == NULL) {
  53.     //for now return -1. In future return. NK_FB_EBIND when we introduce error handling
  54.     return false;
  55.   }
  56.   return true;
  57. }
  58.  
  59.  
  60.  
  61. CURSOR_INFO GetCursorInfo() {
  62.   return cInfo;
  63.  
  64. }
  65. bool SetDefaultFramebuffer(FRAMEBUFFER* fb) {
  66.   if (fb == NULL) {
  67.     return false;
  68.   }
  69.   dFb = fb;
  70.   return true;
  71. }
  72.  
  73. bool SetDefaulFont(PSF1_FONT* font) {
  74.   if (font == NULL) {
  75.     return false;
  76.   }
  77.   dFont = font;
  78.   return true;
  79. }
  80. HEXCLR RGB2Hex(ushort r, ushort g, ushort b)
  81. {  
  82.     return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
  83. }
  84.  
  85. void FrameBufferPutString(const char* str) {
  86.   FrameBufferPutClrString(str, 0xFFFFFF);
  87. }
  88.  
  89.  
  90. void FrameBufferPutClrString(const char* str, HEXCLR iclr) {
  91.   if (!IsFramebufferBinded()) {
  92.     return;
  93.   }
  94.  
  95.  
  96.   char* chr = (char*)str;
  97.   while (*chr != 0) {
  98.    
  99.         FrameBufferPutChar(*chr, iclr);
  100.         cInfo.x += (DEFAULT_FONT_SIZE / 2);
  101.        
  102.    
  103.  
  104.    
  105.     chr++;
  106.   }
  107. }
  108. void FrameBufferPutChar(char c, HEXCLR clr) {
  109.     if (!IsFramebufferBinded()) {
  110.         return;
  111.     }
  112.  
  113.     switch (c) {
  114.          case '\n':
  115.             cInfo.x = 0;
  116.             cInfo.y += DEFAULT_FONT_SIZE;
  117.             break;
  118.         case '\t':
  119.             cInfo.x += TABSPACE;
  120.             break;
  121.         case '\b':
  122.             if (cInfo.x > 0) {
  123.                 cInfo.x -= (DEFAULT_FONT_SIZE / 2);
  124.                 FrameBufferPutCharInternal(dFb, dFont, ' ', cInfo.x, cInfo.y, clr);
  125.             } else if (cInfo.y >= DEFAULT_FONT_SIZE) {
  126.                 cInfo.x = (dFb->width - (DEFAULT_FONT_SIZE / 2));
  127.                 cInfo.y -= DEFAULT_FONT_SIZE;
  128.                 FrameBufferPutCharInternal(dFb, dFont, ' ', cInfo.x, cInfo.y, clr);
  129.             }
  130.             break;
  131.         default:
  132.             if (cInfo.x >= dFb->width) {
  133.                 cInfo.x = 0;
  134.                 cInfo.y += DEFAULT_FONT_SIZE;
  135.             }
  136.  
  137.             if (cInfo.y + DEFAULT_FONT_SIZE >= dFb->height) {
  138.                 uint32_t numScrollRows = (cInfo.y + DEFAULT_FONT_SIZE - dFb->height + 1) / DEFAULT_FONT_SIZE;
  139.                 scrollFramebuffer(numScrollRows);
  140.                 cInfo.y -= numScrollRows * DEFAULT_FONT_SIZE;
  141.             }
  142.  
  143.             FrameBufferPutCharInternal(dFb, dFont, c, cInfo.x, cInfo.y, clr);
  144.             cInfo.x += (DEFAULT_FONT_SIZE / 2);
  145.             break;
  146.     }
  147. }
  148.  
  149.  
  150. void FrameBufferClearScreen() {
  151.   uint* pixPtr = (uint*)dFb->baseAddress;
  152.                
  153.   for (int64_t x = 0; x < dFb->width; ++x) {
  154.     for (int64_t y = 0; y < dFb->height; ++y) {
  155.      //  FrameBufferPutCharInternal(dFb, dFont, 'g', x, y, FB_CLR_BLACK);
  156.      *(uint*)(pixPtr + x + (y * dFb->scanLinePixels)) = 0;
  157.     }
  158.   }
  159.   cInfo.x = 0;
  160.   cInfo.y = 0;
  161.    
  162.  // *(uint*)(pixPtr + x + (y * fb->scanLinePixels)) = clr;
  163.                
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement