Advertisement
konalisp

vga.cxx

Nov 15th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. #include "vga.hxx"
  2.  
  3. namespace vga{
  4.  
  5.     vga::vga(){ // Constructor
  6.         this->row = 0;
  7.         this->column = 0;
  8.         this->height = 25;
  9.         this->width = 80;
  10.         this->color = this->make_color(COLOR_WHITE, COLOR_BLACK);
  11.         // Above here is just initializations.
  12.    
  13.         this->buffer = (uint16_t*) 0xB8000; // Sets the buffer to point to VGA memory.
  14.         this->clear();
  15.     }
  16.    
  17.     uint8_t vga::make_color(enum color fg, enum color bg){ // Generates a color scheme.
  18.         return fg | bg << 4;
  19.     }
  20.    
  21.     uint16_t vga::make_entry(char c, uint8_t color){ // Makes an entry (character and color).
  22.         return (uint16_t)c | (uint16_t)color << 8;
  23.     }
  24.    
  25.     void vga::putc(char c, int x, int y){ // Puts a character on the screen with co-ordinates.
  26.         if(x > this->width){
  27.             x = 0;
  28.         }
  29.    
  30.         if(y > this->height){
  31.             y = 0;
  32.         }
  33.    
  34.         this->buffer[y * this->width + x] = this->make_entry(c, this->color);
  35.     }
  36.    
  37.     void vga::putc(char c){
  38.         if(c == '\n'){ // If newline's encountered...
  39.             if(this->row < (this->height - 1)){ // If we're not already at the bottom of the screen...
  40.                 this->row++;
  41.                 this->column = 0;
  42.             }
  43.        
  44.             else{ // If we've hit the bottom of the screen, let's scroll...
  45.                 this->column = 0;
  46.    
  47.                 for(int y = 1; y < this->height; ++y){ // Move all the rows up by one.
  48.                     for(int x = 0; x < this->width; ++x){
  49.                         this->buffer[(y - 1) * this->width + x] = this->buffer[y * this->width + x];
  50.                     }
  51.                 }
  52.    
  53.                 this->clear(this->row);
  54.             }
  55.         }
  56.    
  57.         else{
  58.             this->putc(c, this->column, this->row);
  59.    
  60.             if(++this->column == this->width){ // If we reach the edge of the screen...
  61.                 this->column = 0;
  62.                 if(this->row == (this->height - 1)){ // If we're at the bottom of the screen
  63.    
  64.                     for(int y = 1; y < this->height; ++y){ // Move all rows up by one.
  65.                         for(int x = 0; x < this->width; ++x){
  66.                             this->buffer[(y - 1) * this->width + x] = this->buffer[y * this->width + x];
  67.                         }
  68.                     }
  69.                    
  70.    
  71.                     this->clear(this->row);
  72.                        
  73.                 }
  74.    
  75.                 else{
  76.                     this->row++;
  77.                 }
  78.             }
  79.         }
  80.     }
  81.  
  82.     void vga::write(const char *data){ // Write a string to the screen.
  83.         int index = 0;
  84.    
  85.         while(data[index] != 0) // Stand-in for strlen.
  86.             index++;
  87.    
  88.         for(int i = 0; i < index; ++i){
  89.             this->putc(data[i]);
  90.         }
  91.    
  92.     }
  93.    
  94.     void vga::write(const char *data, enum color fg, enum color bg){
  95.         // Write a string to the screen with color arguments.
  96.         this->color = this->make_color(fg, bg);
  97.        
  98.         this->write(data);
  99.    
  100.         this->color = this->make_color(COLOR_WHITE, COLOR_BLACK);
  101.     }
  102.    
  103.     void vga::write(int val) {
  104.         if(val == 0) {
  105.             this->write("NULL");
  106.         }
  107.         else {
  108.             char* r = itoa(val, 10);
  109.             this->write(r);
  110.         }
  111.     }
  112.    
  113.     void vga::clear(){
  114.         for(int y = 0; y < this->height; y++){
  115.             for(int x = 0; x < this->width; ++x){
  116.                 this->buffer[y * this->width + x] = this->make_entry(' ', this->color); // Clear the screen.
  117.             }
  118.         }
  119.     }
  120.    
  121.     void vga::clear(int line){
  122.         for(int x = 0; x < this->width; ++x){
  123.             this->buffer[line * this->width + x] = this->make_entry(' ', this->color);
  124.         }
  125.     }
  126.    
  127.     void vga::nl() {
  128.         this->write("\n");
  129.     }
  130.    
  131.     char* vga::itoa(int value, int base) {
  132.         char* rc;
  133.         char* ptr;
  134.         char* low;
  135.         char* str = (char*)"\0";
  136.         // Check for supported base.
  137.         if ( base < 2 || base > 36 ) {
  138.             *str = '\0';
  139.             return str;
  140.         }
  141.         rc = ptr = str;
  142.         // Set '-' for negative decimals.
  143.         if ( value < 0 && base == 10 ) {
  144.             *ptr++ = '-';
  145.         }
  146.         // Remember where the numbers start.
  147.         low = ptr;
  148.         // The actual conversion.
  149.         do {
  150.             // Modulo is negative for negative value. This trick makes abs() unnecessary.
  151.             *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
  152.             value /= base;
  153.         } while ( value );
  154.         // Terminating the string.
  155.         *ptr-- = '\0';
  156.         // Invert the numbers.
  157.         while ( low < ptr ) {
  158.             char tmp = *low;
  159.             *low++ = *ptr;
  160.             *ptr-- = tmp;
  161.         }
  162.         return rc;
  163.     }
  164. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement