#include "vga.h"

static size_t row = 0, col = 0;

void vga_clear(uint8_t color) {
    for (size_t r = 0; r < VGA_HEIGHT; r++) {
        for (size_t c = 0; c < VGA_WIDTH; c++) {
            VGA_MEM[r * VGA_WIDTH + c] = vga_entry(' ', color);
        }
    }
    row = col = 0;
}

static void putc(char c, uint8_t color) {
    if (c == '\n') {
        col = 0;
        if (++row >= VGA_HEIGHT) row = 0;
        return;
    }
    VGA_MEM[row * VGA_WIDTH + col] = vga_entry(c, color);
    if (++col >= VGA_WIDTH) {
        col = 0;
        if (++row >= VGA_HEIGHT) row = 0;
    }
}

void vga_write_str(const char* s, uint8_t color) {
    for (; *s; ++s) putc(*s, color);
}
