在C中绘制像素:OSdev

时间:2018-12-14 09:31:39

标签: c osdev vga

我完全是osdeving的初学者。目前,我刚刚按照osdevwiki实施了键盘和VGA屏幕。现在,我要像这样绘制适当的像素

void drawPixel(int x, int y, int rgb)

独立C语言。 现在,在vga模式下,用于打印文本和颜色的地址为0xB8000。要在屏幕上绘制像素怎么办?我没有任何线索。

2 个答案:

答案 0 :(得分:3)

此处讨论文本模式:

https://wiki.osdev.org/Text_mode

这里有一个在文本模式下写彩色字符的示例:

void WriteCharacter(unsigned char c, unsigned char forecolour, unsigned char backcolour, int x, int y)
{
     uint16_t attrib = (backcolour << 4) | (forecolour & 0x0F);
     volatile uint16_t * where;
     where = (volatile uint16_t *)0xB8000 + (y * 80 + x) ;
     *where = c | (attrib << 8);
}

如果要在图形模式下写入RGB像素,则必须首先切换到其他视频模式。

在这里解释:

https://wiki.osdev.org/Drawing_In_Protected_Mode

以下是该页面上有关如何在图形模式下绘制像素的代码:

/* only valid for 800x600x16M */
static void putpixel(unsigned char* screen, int x,int y, int color) {
    unsigned where = x*3 + y*2400;
    screen[where] = color & 255;              // BLUE
    screen[where + 1] = (color >> 8) & 255;   // GREEN
    screen[where + 2] = (color >> 16) & 255;  // RED
}

/* only valid for 800x600x32bpp */
static void putpixel(unsigned char* screen, int x,int y, int color) {
    unsigned where = x*4 + y*3200;
    screen[where] = color & 255;              // BLUE
    screen[where + 1] = (color >> 8) & 255;   // GREEN
    screen[where + 2] = (color >> 16) & 255;  // RED
}

基本上,您需要将三个颜色值从视频内存开始写入三个字节,并以坐标乘以某些值的偏移量得出正确的行和列。

对于不同的视频模式,值是不同的。

请注意,甚至在VGA / CGA / EGA模式下,视频存储地址也不同!

答案 1 :(得分:1)

我使用此方法从文本模式绘制像素。 将字符设置为空格字符,然后使用颜色作为像素的颜色。

   char* video  = (char*)0xb8000;
   video [0] = 0x20; // space character
   video [1] = 0x12; // color of the pixel