用C语言在屏幕上绘制字体
我用SDL库模拟了一个屏幕。我想在此屏幕上绘制字体,只需使用我已经制作的“绘制像素”功能 我在网上搜索了很多。 我发现这个网站和代码工作得很好。但是,它不支持可变宽度字符 我只想使用源代码 你能告诉我是否有一个源代码或一个灯光库来绘制像素字体吗 编辑:这是我的代码,我从M Oehm答案中更改了用C语言在屏幕上绘制字体,c,fonts,C,Fonts,我用SDL库模拟了一个屏幕。我想在此屏幕上绘制字体,只需使用我已经制作的“绘制像素”功能 我在网上搜索了很多。 我发现这个网站和代码工作得很好。但是,它不支持可变宽度字符 我只想使用源代码 你能告诉我是否有一个源代码或一个灯光库来绘制像素字体吗 编辑:这是我的代码,我从M Oehm答案中更改了 int DrawChar(char c, uint8_t x, uint8_t y, int r, int g, int b, SDL_Surface *rectangle, SDL_Surface *e
int DrawChar(char c, uint8_t x, uint8_t y, int r, int g, int b, SDL_Surface *rectangle, SDL_Surface *ecran, SDL_Rect position)
{
uint8_t i,j;
// Convert the character to an index
c = c & 0x7F;
if (c < ' ') {
c = 0;
} else {
c -= ' ';
}
const uint8_t* chr = font[c];
int w = chr[0];
chr++;
for (j = 0; j < w; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (chr[j] & (1 << i)) {
draw_pixel(x+j, y+i, r, g, b, rectangle, ecran, position);
}
}
}
return w + CHAR_GAP;
}
void DrawString(const char* str, uint8_t x, uint8_t y, int r, int g, int b, SDL_Surface *rectangle, SDL_Surface *ecran, SDL_Rect position)
{
while (*str) {
x += DrawChar(*str++, x, y, r, g, b, rectangle, ecran, position);
}
}
结果是:字符大小仍然相同。
编辑:
解决方案就在这里
您必须对代码进行一些更改,因为它们是一些错误,但它可以工作。您可以轻松地将给定代码扩展为可变宽度字符。更改字体的定义:
const unsigned char font[96][10] = {
{3, 0x00, 0x00, 0x00}, //
{3, 0x00, 0x2f, 0x00}, // !
{4, 0x03, 0x00, 0x00, 0x03}, // "
...
};
第一个条目是字符的宽度。必须选择第二维度以容纳最宽的字符。在本例中,它可以是9像素宽
然后扩展DrawChar
函数,使用给定的宽度和alo返回drwing位置应前进的宽度,即字符的宽度加上一定的间距。(可以将间隙设置为参数,以便打印双倍行距的文本。)
DrawString
函数然后使用返回的宽度:
int DrawChar(char c, uint8 x, uint8 y, uint8 brightness)
{
uint8 i, j;
// Convert the character to an index
c = c & 0x7F;
if (c < ' ') {
c = 0;
} else {
c -= ' ';
}
const uint8* chr = font[c];
int w = chr[0];
chr++;
for (j = 0; j < w; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (chr[j] & (1 << i)) {
DrawPixel(x + j, y + i, brightness);
}
}
}
return w + CHAR_GAP;
}
void DrawString(const char* str, uint8 x, uint8 y, uint8 brightness)
{
while (*str) {
x += DrawChar(*str++, x, y, brightness);
}
}
int-DrawChar(字符c、uint8 x、uint8 y、uint8亮度)
{
uint8i,j;
//将字符转换为索引
c=c&0x7F;
如果(c<''){
c=0;
}否则{
c-=”;
}
const uint8*chr=font[c];
int w=chr[0];
chr++;
对于(j=0;j if(chr[j]&(1@M Oehm)谢谢你的回复。我尝试了你的代码,但不起作用。事实上,如果我更改了第一个条目(字符宽度)然后,它在字符旁边绘制更多像素,但不会随ITHMM的更改而更改。我已经测试了此代码,尽管没有使用完整的字母表。您是否复制了我的代码或只是进行了更改?然后请完全替换这两个子例程。很可能您遗漏了一些内容。请查看我的编辑。我复制了您的代码,但结果确实如此esn不起作用。请严肃点。您打印两个宽度相同的(!)字符,然后得出结论代码不起作用。代码起作用。句点。我添加了一个完整的工作示例。它将一个假位图打印到字符缓冲区。
int DrawChar(char c, uint8 x, uint8 y, uint8 brightness)
{
uint8 i, j;
// Convert the character to an index
c = c & 0x7F;
if (c < ' ') {
c = 0;
} else {
c -= ' ';
}
const uint8* chr = font[c];
int w = chr[0];
chr++;
for (j = 0; j < w; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (chr[j] & (1 << i)) {
DrawPixel(x + j, y + i, brightness);
}
}
}
return w + CHAR_GAP;
}
void DrawString(const char* str, uint8 x, uint8 y, uint8 brightness)
{
while (*str) {
x += DrawChar(*str++, x, y, brightness);
}
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef unsigned char uint8;
char screen[25][80];
#define CHAR_HEIGHT 8
#define CHAR_GAP 2
const unsigned char font[96][10] = {
[' ' - 32] = {1, 0x00},
['A' - 32] = {5, 0x3e, 0x09, 0x09, 0x09, 0x3e},
['B' - 32] = {5, 0x3f, 0x25, 0x25, 0x25, 0x19},
['D' - 32] = {5, 0x3f, 0x21, 0x21, 0x21, 0x1e},
['E' - 32] = {5, 0x3f, 0x25, 0x25, 0x25, 0x21},
['H' - 32] = {5, 0x3f, 0x04, 0x04, 0x04, 0x3f},
['I' - 32] = {1, 0x3f},
['L' - 32] = {4, 0x3f, 0x20, 0x20, 0x20},
['M' - 32] = {7, 0x3f, 0x02, 0x04, 0x18, 0x04, 0x02, 0x3f},
['O' - 32] = {5, 0x1e, 0x21, 0x21, 0x21, 0x1e},
['P' - 32] = {5, 0x3f, 0x09, 0x09, 0x09, 0x06},
['R' - 32] = {5, 0x3f, 0x09, 0x19, 0x19, 0x26},
['S' - 32] = {5, 0x22, 0x25, 0x25, 0x25, 0x19},
['W' - 32] = {7, 0x07, 0x38, 0x0c, 0x03, 0x0c, 0x38, 0x07},
};
void DrawPixel(int x, int y, uint8 c)
{
screen[y][x] = c;
}
int DrawChar(char c, uint8 x, uint8 y, uint8 brightness)
{
uint8 i, j;
// Convert the character to an index
c = c & 0x7F;
if (c < ' ') {
c = 0;
} else {
c -= ' ';
}
const uint8* chr = font[c];
int w = chr[0];
chr++;
for (j = 0; j < w; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (chr[j] & (1 << i)) {
DrawPixel(x + j, y + i, brightness);
}
}
}
return w + CHAR_GAP;
}
void DrawString(const char* str, uint8 x, uint8 y, uint8 brightness)
{
while (*str) {
x += DrawChar(*str++, x, y, brightness);
}
}
int main()
{
int i;
memset(screen, '.', sizeof(screen));
DrawString("HELLO WORLD", 2, 2, 'O');
DrawString("MISSISSIPPI", 8, 10, '#');
for (i = 0; i < 25; i++) printf("%.80s\n", screen[i]);
return 0;
}