Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "vga.hxx"
- namespace vga{
- vga::vga(){ // Constructor
- this->row = 0;
- this->column = 0;
- this->height = 25;
- this->width = 80;
- this->color = this->make_color(COLOR_WHITE, COLOR_BLACK);
- // Above here is just initializations.
- this->buffer = (uint16_t*) 0xB8000; // Sets the buffer to point to VGA memory.
- this->clear();
- }
- uint8_t vga::make_color(enum color fg, enum color bg){ // Generates a color scheme.
- return fg | bg << 4;
- }
- uint16_t vga::make_entry(char c, uint8_t color){ // Makes an entry (character and color).
- return (uint16_t)c | (uint16_t)color << 8;
- }
- void vga::putc(char c, int x, int y){ // Puts a character on the screen with co-ordinates.
- if(x > this->width){
- x = 0;
- }
- if(y > this->height){
- y = 0;
- }
- this->buffer[y * this->width + x] = this->make_entry(c, this->color);
- }
- void vga::putc(char c){
- if(c == '\n'){ // If newline's encountered...
- if(this->row < (this->height - 1)){ // If we're not already at the bottom of the screen...
- this->row++;
- this->column = 0;
- }
- else{ // If we've hit the bottom of the screen, let's scroll...
- this->column = 0;
- for(int y = 1; y < this->height; ++y){ // Move all the rows up by one.
- for(int x = 0; x < this->width; ++x){
- this->buffer[(y - 1) * this->width + x] = this->buffer[y * this->width + x];
- }
- }
- this->clear(this->row);
- }
- }
- else{
- this->putc(c, this->column, this->row);
- if(++this->column == this->width){ // If we reach the edge of the screen...
- this->column = 0;
- if(this->row == (this->height - 1)){ // If we're at the bottom of the screen
- for(int y = 1; y < this->height; ++y){ // Move all rows up by one.
- for(int x = 0; x < this->width; ++x){
- this->buffer[(y - 1) * this->width + x] = this->buffer[y * this->width + x];
- }
- }
- this->clear(this->row);
- }
- else{
- this->row++;
- }
- }
- }
- }
- void vga::write(const char *data){ // Write a string to the screen.
- int index = 0;
- while(data[index] != 0) // Stand-in for strlen.
- index++;
- for(int i = 0; i < index; ++i){
- this->putc(data[i]);
- }
- }
- void vga::write(const char *data, enum color fg, enum color bg){
- // Write a string to the screen with color arguments.
- this->color = this->make_color(fg, bg);
- this->write(data);
- this->color = this->make_color(COLOR_WHITE, COLOR_BLACK);
- }
- void vga::write(int val) {
- if(val == 0) {
- this->write("NULL");
- }
- else {
- char* r = itoa(val, 10);
- this->write(r);
- }
- }
- void vga::clear(){
- for(int y = 0; y < this->height; y++){
- for(int x = 0; x < this->width; ++x){
- this->buffer[y * this->width + x] = this->make_entry(' ', this->color); // Clear the screen.
- }
- }
- }
- void vga::clear(int line){
- for(int x = 0; x < this->width; ++x){
- this->buffer[line * this->width + x] = this->make_entry(' ', this->color);
- }
- }
- void vga::nl() {
- this->write("\n");
- }
- char* vga::itoa(int value, int base) {
- char* rc;
- char* ptr;
- char* low;
- char* str = (char*)"\0";
- // Check for supported base.
- if ( base < 2 || base > 36 ) {
- *str = '\0';
- return str;
- }
- rc = ptr = str;
- // Set '-' for negative decimals.
- if ( value < 0 && base == 10 ) {
- *ptr++ = '-';
- }
- // Remember where the numbers start.
- low = ptr;
- // The actual conversion.
- do {
- // Modulo is negative for negative value. This trick makes abs() unnecessary.
- *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
- value /= base;
- } while ( value );
- // Terminating the string.
- *ptr-- = '\0';
- // Invert the numbers.
- while ( low < ptr ) {
- char tmp = *low;
- *low++ = *ptr;
- *ptr-- = tmp;
- }
- return rc;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement