我的字体渲染测试中有以下代码:
SDL_Surface* image = SDL_CreateRGBSurface(SDL_SWSURFACE,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
8,
0, 0, 0,
0);
if(!image)
{
throw std::runtime_error("Failed to generate 8 bit image");
}
//There's no better way to do this, right? Being it just sets up a basic grayscale palette
SDL_Color colors[256];
for(int i=0; i<256; ++i)
{
colors[i].r = i;
colors[i].g = i;
colors[i].b = i;
}
SDL_SetColors(image, colors, 0, 256);
SDL_LockSurface(image);
uint8_t* pixels = static_cast<uint8_t*>(image->pixels);
for(int i = 0; i<face->glyph->bitmap.rows; ++i)
{
for(int j = 0; j<face->glyph->bitmap.width; ++j)
{
pixels[i * face->glyph->bitmap.width + j] = face->glyph->bitmap.buffer[
i * face->glyph->bitmap.width + j];
}
}
###############################################################################
# Here's the spotlight #
# | #
# _________________/ #
# / #
###############################################################################
image->pitch = image->w;
SDL_UnlockSurface(image);
请注意,我将间距更改为宽度,有效地使每像素字节等于 1。这正是我所需要的,事实上,像我一样改变音高是我的信件是否歪曲的决定因素。
我的问题是:这是定义、安全和良好的做法吗?有没有我不知道的功能可以帮助我?有没有更安全的选择?