c++ - 如果表面被锁定,访问 SDL_Surface 的俯仰数据成员是否安全?

标签 c++ sdl

我的字体渲染测试中有以下代码:

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/

相关文章:

c++ - c++移动二维数组的构造函数(语法逻辑不清楚):

c++ - extern C 返回类对象

c++ - 观察给定的精度,快速将 double 转换为字符串

c++ - 我使用的是什么 SDL 和 OpenGL 版本和实现

java - c++中是否有类似java中Desktop类的类? (特定于 Windows)

c++ - 非常快速的 Globals.h 问题(包括在内,一些项目仍然未定义)

lisp - 在 Racket 和racket-sdl 中初始化 SDL 矩形

javascript - 使用 asm.js/emscripten/SDL 时如何获取所有关键状态?

c++ - 如何在qt中的QTableWidget中获取comboBox项后的行号

windows - 对 `SDL_main' 的 undefined reference