Advertisement
Guest User

Untitled

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