我的字体渲染测试中有以下代码:
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。这正是我需要的,事实上,像我这样改变音高是我的字母是否歪斜的决定性因素。
我的问题是:这是定义明确、安全且良好的做法吗?有没有我不知道的功能可以帮助我?有没有更安全的选择?
最佳答案
你写错了image->pitch。它可能在某些情况下有效,但在其他情况下会导致显示损坏或段错误。这完全取决于那 block 内存的来源。
我认为你把索引计算成pixels[]是错误的,应该是:
像素[i * image->pitch + j] = ...
关于c++ - 如果表面被锁定,访问 SDL_Surface 的俯仰数据成员是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102893/