我正在做一个小的操作系统,我已经到了我想要画字符到屏幕的部分。我在运行代码时遇到了问题(这可能是由于我生疏的C++技能造成的),当我这么做的时候,屏幕上没有看到所要求的字符。我复制了一个8x8ASCII字符位图,并尝试使用OSDev维基中的代码作为我的文本显示功能的基础。到目前为止,这是我的C++代码:
#define LOCHAR 32
#define HICHAR 127
#define FONT_HEIGHT 8
//bitmap
unsigned char font[HICHAR-LOCHAR+1][FONT_HEIGHT] = {
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },{ 0x00,0x00,0x00,0x00,0x02,0x02,0x00,0x02 },{ 0x00,0x00,0x00,0x00,0x05,0x05,0x00,0x00 },{ 0x00,0x00,0x00,0x00,0x06,0x0F,0x0F,0x06 },{ 0x00,0x00,0x00,0x00,0x02,0x07,0x07,0x02 },{ 0x00,0x00,0x00,0x00,0x09,0x04,0x02,0x09 },{ 0x00,0x00,0x00,0x00,0x03,0x0D,0x07,0x0B },{ 0x00,0x00,0x00,0x00,0x04,0x02,0x00,0x00 },{ 0x00,0x00,0x00,0x00,0x04,0x02,0x02,0x04 },{ 0x00,0x00,0x00,0x00,0x02,0x04,0x04,0x02 },{ 0x00,0x00,0x00,0x00,0x02,0x07,0x02,0x05 },{ 0x00,0x00,0x00,0x00,0x00,0x02,0x07,0x02 },{ 0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x04 },{ 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00 },{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02 },{ 0x00,0x00,0x00,0x00,0x04,0x04,0x02,0x02 },{ 0x00,0x00,0x00,0x00,0x07,0x05,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x03,0x02,0x02,0x07 },{ 0x00,0x00,0x00,0x00,0x07,0x04,0x03,0x07 },{ 0x00,0x00,0x00,0x00,0x07,0x06,0x04,0x07 },{ 0x00,0x00,0x00,0x00,0x05,0x05,0x07,0x04 },{ 0x00,0x00,0x00,0x00,0x07,0x03,0x04,0x07 },{ 0x00,0x00,0x00,0x00,0x03,0x01,0x07,0x07 },{ 0x00,0x00,0x00,0x00,0x07,0x04,0x02,0x02 },{ 0x00,0x00,0x00,0x00,0x06,0x07,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x07,0x07,0x04,0x06 },{ 0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x02 },{ 0x00,0x00,0x00,0x00,0x02,0x00,0x06,0x04 },{ 0x00,0x00,0x00,0x00,0x00,0x04,0x02,0x04 },{ 0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x07 },{ 0x00,0x00,0x00,0x00,0x00,0x02,0x04,0x02 },{ 0x00,0x00,0x00,0x00,0x07,0x06,0x00,0x02 },{ 0x00,0x00,0x00,0x00,0x0F,0x09,0x0A,0x0F },{ 0x00,0x00,0x00,0x00,0x06,0x05,0x07,0x05 },{ 0x00,0x00,0x00,0x00,0x03,0x07,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x06,0x01,0x01,0x07 },{ 0x00,0x00,0x00,0x00,0x03,0x05,0x05,0x03 },{ 0x00,0x00,0x00,0x00,0x07,0x03,0x01,0x07 },{ 0x00,0x00,0x00,0x00,0x07,0x03,0x01,0x01 },{ 0x00,0x00,0x00,0x00,0x06,0x01,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x05,0x05,0x07,0x05 },{ 0x00,0x00,0x00,0x00,0x07,0x02,0x02,0x07 },{ 0x00,0x00,0x00,0x00,0x06,0x04,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x05,0x03,0x03,0x05 },{ 0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x07 },{ 0x00,0x00,0x00,0x00,0x07,0x07,0x07,0x05 },{ 0x00,0x00,0x00,0x00,0x07,0x05,0x05,0x05 },{ 0x00,0x00,0x00,0x00,0x07,0x05,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x07,0x05,0x07,0x01 },{ 0x00,0x00,0x00,0x00,0x07,0x05,0x07,0x0F },{ 0x00,0x00,0x00,0x00,0x07,0x05,0x03,0x05 },{ 0x00,0x00,0x00,0x00,0x07,0x01,0x06,0x07 },{ 0x00,0x00,0x00,0x00,0x07,0x02,0x02,0x02 },{ 0x00,0x00,0x00,0x00,0x05,0x05,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x05,0x05,0x03,0x01 },{ 0x00,0x00,0x00,0x00,0x05,0x07,0x07,0x07 },{ 0x00,0x00,0x00,0x00,0x05,0x02,0x05,0x05 },{ 0x00,0x00,0x00,0x00,0x05,0x07,0x02,0x02 },{ 0x00,0x00,0x00,0x00,0x07,0x04,0x02,0x07 },{ 0x00,0x00,0x00,0x00,0x06,0x02,0x02,0x06 },{ 0x00,0x00,0x00,0x00,0x02,0x02,0x04,0x04 },{ 0x00,0x00,0x00,0x00,0x06,0x04,0x04,0x06 },{ 0x00,0x00,0x00,0x00,0x02,0x05,0x00,0x00 },{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F },{ 0x00,0x00,0x00,0x00,0x01,0x02,0x00,0x00 },{ 0x00,0x00,0x00,0x00,0x00,0x06,0x07,0x07 },{ 0x00,0x00,0x00,0x00,0x01,0x07,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x07 },{ 0x00,0x00,0x00,0x00,0x04,0x07,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x00,0x07,0x07,0x03 },{ 0x00,0x00,0x00,0x00,0x06,0x02,0x07,0x02 },{ 0x00,0x00,0x00,0x00,0x06,0x07,0x04,0x03 },{ 0x00,0x00,0x00,0x00,0x01,0x07,0x05,0x05 },{ 0x00,0x00,0x00,0x00,0x02,0x00,0x02,0x02 },{ 0x00,0x00,0x00,0x00,0x02,0x00,0x02,0x03 },{ 0x00,0x00,0x00,0x00,0x01,0x05,0x03,0x05 },{ 0x00,0x00,0x00,0x00,0x03,0x02,0x02,0x06 },{ 0x00,0x00,0x00,0x00,0x00,0x07,0x07,0x05 },{ 0x00,0x00,0x00,0x00,0x00,0x07,0x05,0x05 },{ 0x00,0x00,0x00,0x00,0x00,0x07,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x02,0x05,0x03,0x01 },{ 0x00,0x00,0x00,0x00,0x02,0x05,0x06,0x04 },{ 0x00,0x00,0x00,0x00,0x00,0x07,0x01,0x01 },{ 0x00,0x00,0x00,0x00,0x00,0x06,0x02,0x03 },{ 0x00,0x00,0x00,0x00,0x02,0x06,0x02,0x06 },{ 0x00,0x00,0x00,0x00,0x00,0x05,0x05,0x07 },{ 0x00,0x00,0x00,0x00,0x00,0x05,0x07,0x02 },{ 0x00,0x00,0x00,0x00,0x00,0x05,0x07,0x07 },{ 0x00,0x00,0x00,0x00,0x00,0x05,0x02,0x05 },{ 0x00,0x00,0x00,0x00,0x00,0x05,0x06,0x03 },{ 0x00,0x00,0x00,0x00,0x00,0x03,0x02,0x06 },{ 0x00,0x00,0x00,0x00,0x06,0x03,0x02,0x06 },{ 0x00,0x00,0x00,0x00,0x02,0x02,0x02,0x02 },{ 0x00,0x00,0x00,0x00,0x03,0x06,0x02,0x03 },{ 0x00,0x00,0x00,0x00,0x04,0x07,0x01,0x00 }};
void draw_pixel(int pos_x, int pos_y, unsigned char colour)
{
unsigned char* location = (unsigned char*)0xA0000 + 320 * pos_y + pos_x;
*location = colour;
}
void draw_char(unsigned char c, int x, int y, int color)
{
int cx,cy;
int mask[8]={1,2,4,8,16,32,64,128};
unsigned char *glyph[8]=font[(int)c-LOCHAR];
for(cy=0;cy<8;cy++){
for(cx=0;cx<8;cx++){
if((int)glyph[cy]&mask[cx]==true){
draw_pixel(color,x+cx,y+cy-12);
}
}
}
}
extern "C" void main(){
for(int h = 0; h<200; h++)
{
for(int w = 0; w<320; w++)
{
draw_pixel(w,h,h);
}
}
draw_char(97, 100, 100, 4);
return;
}至于我到目前为止所做的故障排除,我已经尽可能地修改了最初的'draw_char‘函数,以满足我的需求,但是,凭借我对C++的一点经验,我确信我只是使它变得更糟了。
发布于 2022-07-18 04:04:16
你误解了运算符优先。下面的声明不是您所期望的:
if((int)glyph[cy] & mask[cx] == true) ...用括号积极隔离所有操作符,这意味着:
((int)(glyph[cy])) & ( (mask[cx]) == true )换句话说,首先计算值mask[cx] == true,然后将其用于按位-和。这是因为运算符==比&具有更高的优先级。
你可以这样做,如果你必须(但不要,并继续读):
if (((int)glyph[cy] & mask[cx]) == true) ...这里的问题是,现在直接将整数与布尔值进行比较,依赖于编译器显式转换。不仅如此,您还比较真实的值,这是我们倾向于避免的东西(历史上,任何非零值都被认为是正确的)。您会发现,大多数生产代码要么完全忽略布尔比较,要么总是倾向于使用与虚值相关的比较。
这听起来可能有点迂腐,如果是这样的话,那是不幸的,但也很好。但结果是..。你真正需要的是:
if (glyph[cy] & mask[cx]) ...现在,让我们来看看glyph。我真的不知道你的编译器为什么不对你大喊大叫。这是错误的,并且可能也对你的程序的怪异行为负责。您已经将其定义为:
// incorrect: you didn't want an array of 8 pointers!!
unsigned char *glyph[8] = font[(int)c - LOCHAR];正确的定义是:
const unsigned char *glyph = font[(int)c - LOCHAR];您的mask表上的最后一个注意事项。这并不是必要的,除非你打算用它做些花哨的事。您也可以轻松地通过轮班来实现它:
if (glyph[cy] & ((unsigned char)1U << cx)) ...https://stackoverflow.com/questions/73017012
复制相似问题