奇怪的SDL_Surface->格式->字节每像素值
Weird SDL_Surface->format->BytesPerPixel value
所以我正在使用SDL_image在我的OpenGL应用程序中加载高度图并创建地形。
这就是我初始化SDL_image的方式:
int flags = IMG_INIT_PNG;
int initted = IMG_Init(flags);
if((initted & flags) != flags) {
printf("IMG_Init: Failed to init required jpg and png support!n");
printf("IMG_Init: %sn", IMG_GetError());
return;
}
Load(filename);
。这是我的加载函数:
void Load(string filename) {
img = IMG_Load(filename.c_str());
if(!img) {
printf("IMG_Load: %sn", IMG_GetError());
return;
}
printf("IMG_Load: %sn", IMG_GetError());
xsize = img->w;
ysize = img->h;
SDL_LockSurface(img);
imgData = (Uint32*)img->pixels;
SDL_UnlockSurface(img);
}
然后,在我准备顶点缓冲区的 Terrain 课程中,我使用此方法读取像素值:
Uint32 getPixel(int x, int y) {
SDL_LockSurface(img);
int bpp = img->format->BytesPerPixel;
//cout << "bpp " << bpp << "n";
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)img->pixels + y * img->pitch + x * bpp;
SDL_UnlockSurface(img);
switch(bpp) {
case 1:
return *p;
break;
case 2:
return *(Uint16 *)p;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
break;
case 4:
return *(Uint32 *)p;
break;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
。事实证明,每次我运行程序时img->format->BytesPerPixel
都会返回一个随机值......什么鬼?有人知道吗?这应该只返回 1、2、3 或 4。
好吧,我只是很愚蠢...但是,如果有人像我一样遇到问题:我包含错误的SDL_image版本...... #include <SDL/SDL_image.h>
而不是#include <SDL2/SDL_image.h>
.现在一切都按预期工作:)
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 在UNIX系统中使用DIR查找文件的字节大小
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- std::当在256字节边界上写入整数时,流的奇怪行为
- 当比特(而不是字节)的顺序至关重要时的持久性
- EASTL矢量<向量<int>>连续的
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 如何在文件中查找字节序列
- luaL_dofile在已知良好的字节码上失败,可以使用未编译的版本
- 字节到位运算符重载C++
- 在java中读取c++字节的位字段
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- 字节真的是最小可寻址单元吗
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 如何将原始字节附加到 std::vector?