轉到內核用 C語言寫東西,速度就快了,先實現畫圖:ide
/* graphics.c 圖形函數 四彩 2015-12-12 */ #include "../kernel/include/graphics.h" // 圖形函數相關全局參數 static unsigned short *pVideo, Color = 0; static unsigned short Screen_X, Screen_Y; static unsigned char BytePerPix; /* 畫一條線 */ void line(unsigned x0, unsigned y0, unsigned x1, unsigned y1) { int x, y; if(x0 == x1) for(y = y0; y <= y1; y++) pVideo[Screen_X * y + x] = Color; else if(x0 < x1) for(x = x0; x <= x1; x++) { y = ((x - x0) * y1 + (x1 - x) * y0) / (x1 - x0); pVideo[Screen_X * y + x] = Color; } else // if(x0 > x1) for(x = x1; x <= x0; x++) { y = ((x0 - x) * y1 + (x - x1) * y0) / (x0 - x1); pVideo[Screen_X * y + x] = Color; } } /* 畫一個方框並填色 */ void rectangle(unsigned x0, unsigned y0, unsigned x1, unsigned y1) { unsigned x, y; for(y = y0; y <= y1; y++) for(x = x0; x <= x1; x++) pVideo[Screen_X * y + x] = Color; } // 初始化圖形參數 void InitGraphics(GRAPHICS_PARAM *ptGraphics) { pVideo = (unsigned short *)ptGraphics->dwPhyAddrOfVideo; Screen_X = ptGraphics->wScreenX; Screen_Y = ptGraphics->wScreenY; BytePerPix = ptGraphics->bBitsPerPixel; } // 設置顏色 void SetColor(unsigned color) { Color = color; } unsigned GetColor() { return Color; } unsigned GetScreenX() { return Screen_X; } unsigned GetScreenY() { return Screen_Y; }
kernel 作了調整,圖形相關定義、函數調整到圖形模塊裏去了。這個頭文件還把圖形函數引出來(這裏定義的顏色但是費老勁收集到的):函數
/* graphics.h 圖形出函數 四彩 2015-12-12 */ #ifndef _GRAPHICS_H #define _GRAPHICS_H typedef struct tag_Graphics_Param { unsigned dwPhyAddrOfVideo; // 顯存基址 unsigned short wScreenX; // 分辨率 X unsigned short wScreenY; // 分辨率 Y unsigned char bBitsPerPixel; // 顏色數 } GRAPHICS_PARAM; // RGB565 合成 16 位色宏 // r = 紅(高 5 位,0 ~ 31),g = 綠(中 6 位,0 ~ 63),b = 藍(低 5 位, 0 ~ 31) #define RGB565(r, g, b) (unsigned short)(((r) << 11) | ((g) << 5) | (b)) // 基本色 #define COLOR16_BLACK 0b0000000000000000 #define COLOR16_WHITE 0b1111111111111111 #define COLOR16_RED 0b1111100000000000 // 紅 #define COLOR16_GREEN 0b0000011111100000 // 綠 #define COLOR16_BLUE 0b0000000000011111 // 藍 #define COLOR16_YELLOW (COLOR16_RED | COLOR16_GREEN) // 黃 #define COLOR16_MAGENTA (COLOR16_RED | COLOR16_BLUE) // 洋紅 #define COLOR16_CYAN (COLOR16_GREEN | COLOR16_BLUE) // 青 // 常見 RGB565 合成色 // 紅色系 #define COLOR16_DARKRED RGB565(17, 0, 0) // 暗紅 // 綠色系 #define COLOR16_PALEGREEN RGB565(19, 62, 19) // 淡綠 #define COLOR16_GREENYELLOW RGB565(21, 63, 5) // 黃綠 // 藍色系 #define COLOR16_LIGHTBLUE RGB565(21, 54, 28) // 亮藍 #define COLOR16_MEDIUMBLUE RGB565( 0, 0, 25) // 中藍 #define COLOR16_DARKBLUE RGB565( 0, 0, 17) // 暗藍 #define COLOR16_BABYBLUE RGB565(28, 63, 31) // 淺藍 #define COLOR16_SKYBLUE RGB565(16, 51, 29) // 天藍 #define COLOR16_LIGHTSKYBLUE RGB565(16, 51, 31) // 亮天藍 #define COLOR16_DEEPSKYBLUE RGB565( 0, 41, 31) // 暗天藍 // 黃色系 // 橙色系 #define COLOR16_ORANGE RGB565(31, 41, 0) // 橙 // 灰色系 #define COLOR16_GRAY RGB565(16, 32, 16) // 灰 #define COLOR16_LIGHTGRAY RGB565(26, 52, 26) // 亮灰 #define COLOR16_DARKGRAY RGB565(21, 42, 21) // 暗灰 // 紫色系 #define COLOR16_PURPLE RGB565(16, 0, 16) // 紫 #define COLOR16_LIGHTVIOLET RGB565(29, 32, 29) // 亮紫 #define COLOR16_MEDIUMPURPLE RGB565(18, 28, 27) // 中紫 #define COLOR16_DARKVIOLET RGB565(18, 0, 26) // 暗紫 // 褐色系 #define COLOR16_BROWN RGB565(20, 10, 5) // 棕褐 // 設置部分 void InitGraphics(GRAPHICS_PARAM *ptGraphics); void SetColor(unsigned color); // 取參數部分 unsigned GetColor(); unsigned GetScreenX(); unsigned GetScreenY(); // 繪圖部分 void line(unsigned x0, unsigned y0, unsigned x1, unsigned y1); void rectangle(unsigned x0, unsigned y0, unsigned x1, unsigned y1); #endif
kernel 增長了一個簡單的界面spa
/* kernel.c 內核 whoozit 2012-12-12 */ #include "../lib/include/stddef.h" #include "../lib/include/string.h" #include "../kernel/include/PMode.h" #include "../kernel/include/graphics.h" // 存放全局變量的物理地址(const.inc 中的 SEGMENTOFGPARAM * 0x10) #define PHYADDROFGPARAM 0x500 // 全局變量結構 typedef struct tag_Global_param { unsigned int dwMemorySize; GRAPHICS_PARAM tGraphics; } GLOBAL_PARAM; // 引用匯編指令函數 extern void asm_hlt(); void c_main() { unsigned i, j; unsigned short Screen_X, Screen_Y; char string[80], number[33]; GLOBAL_PARAM g_Param; // 初始化 GDT 表 InitGdt(); // 取全局變量 memcpy((void *)&g_Param, (void *)(PHYADDROFGPARAM), sizeof(GLOBAL_PARAM)); // 初始化圖形參數 InitGraphics(&(g_Param.tGraphics)); Screen_X = GetScreenX(); Screen_Y = GetScreenY(); // 畫界面 SetColor(COLOR16_DEEPSKYBLUE); rectangle(0, 0, Screen_X - 1, Screen_Y - 1); SetColor(COLOR16_LIGHTGRAY); rectangle(0, Screen_Y - 28, Screen_X - 1, Screen_Y - 28); rectangle(0, Screen_Y - 26, Screen_X - 1, Screen_Y - 1); SetColor(COLOR16_WHITE); rectangle(0, Screen_Y - 27, Screen_X - 1, Screen_Y - 27); rectangle(3, Screen_Y - 24, 59, Screen_Y - 24); rectangle(2, Screen_Y - 24, 2, Screen_Y - 4); rectangle(2, Screen_Y - 3, 59, Screen_Y - 3); rectangle(60, Screen_Y - 24, 60, Screen_Y - 3); rectangle(Screen_X - 47, Screen_Y - 3, Screen_X - 4, Screen_Y - 3); rectangle(Screen_X - 3, Screen_Y - 24, Screen_X - 3, Screen_Y - 3); SetColor(COLOR16_DARKGRAY); rectangle(3, Screen_Y - 4, 59, Screen_Y - 4); rectangle(59, Screen_Y - 23, 59, Screen_Y - 5); rectangle(Screen_X - 47, Screen_Y - 24, Screen_X - 4, Screen_Y - 24); rectangle(Screen_X - 47, Screen_Y - 23, Screen_X - 47, Screen_Y - 4); while(1) asm_hlt(); }
Makefile 裏增長了圖形文件(一個.c、一個.h)的編譯,其餘文件沒有變化。運行效果圖:code