- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码抛出访问错误。这是代码:
SDL_Color *getPixelColor(SDL_Surface *loadingSurface, int x, int y) {
SDL_Color getColor = {0,0,0};
SDL_Texture *readingTexture = nullptr;
readingTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, loadingSurface->w, loadingSurface->h);
SDL_SetTextureBlendMode(readingTexture, SDL_BLENDMODE_BLEND);
void * vPixels;
SDL_LockTexture(readingTexture, &loadingSurface->clip_rect, &vPixels, &loadingSurface->pitch);
memcpy(vPixels, loadingSurface->pixels, loadingSurface->w * loadingSurface->h);
Uint32 *aPixels = (Uint32*)vPixels;
SDL_UnlockTexture(readingTexture);
//Get pixel at location.
Uint32 getPixel = aPixels[y * loadingSurface->w + x];
Uint8 *colors = (Uint8*)getPixel;
//cout << colors[0] << " " << colors[1] << " " << colors[2] << endl;
getColor.r = colors[0];
getColor.g = colors[1];
getColor.b = colors[2];
SDL_Color *color = &getColor;
return color;
}
抛出异常:读取访问冲突。颜色是 0xFF00。
我随时尝试访问颜色。 SDL文档中描述的方法都返回相同的错误,互联网上没有关于 LazyFoo 的信息(此方法基于它,但不起作用。)
感谢您的帮助。
编辑:
我通过重写代码并忽略我用来理解 SDL 某些部分的一些错误源来修复我的代码。这是工作代码:
Uint32 getPixel(SDL_Surface *loadingSurface, int x, int y) {
Uint32 *pixels = (Uint32*)loadingSurface->pixels;
return pixels[(y * loadingSurface->pitch / 4) + x]; // I noticed that each y selection was off by 4 pixels in the y so I divided by 4. Why this is the case remains a mystery.
}
为什么我需要除以 4 是个谜,我花了一段时间才弄明白。有什么想法吗?
SDL_Color getPixelColor(SDL_Surface *loadingSurface, int x, int y) {
SDL_Color getColor = {0,0,0};
SDL_PixelFormat *format;
Uint32 pixel, index;
Uint8 red, green, blue, alpha;
format = loadingSurface->format;
SDL_LockSurface(loadingSurface);
pixel = getPixel(loadingSurface, x, y);
SDL_UnlockSurface(loadingSurface);
index = pixel & format->Rmask;
index = index >> format->Rshift;
index = index << format->Rloss;
red = (Uint8)index;
index = pixel & format->Gmask;
index = index >> format->Gshift;
index = index << format->Gloss;
green = (Uint8)index;
index = pixel & format->Bmask;
index = index >> format->Bshift;
index = index << format->Bloss;
blue = (Uint8)index;
index = pixel & format->Amask;
index = index >> format->Ashift;
index = index << format->Aloss;
alpha = (Uint8)index;
getColor.r = red;
getColor.g = green;
getColor.b = blue;
getColor.a = alpha;
return getColor;
}
最佳答案
这一行:
Uint8 *colors = (Uint8*)getPixel;
据我从您的代码中得知,getPixel
是纹理中位置 (x,y) 处的 32 位 RGBA 像素颜色值。然后出于某种原因将该值转换为 Uint8*
。
所以现在 colors
包含一个无意义的地址,等于像素颜色值,而不是您可能想要的 getPixel
地址。因此,当您执行 colors[0]
并尝试通过此“地址”读取内存时,您会遇到访问冲突。
您的意思是在转换之前先获取 getPixel
的地址,以便访问各个颜色 channel 值吗?
Uint8 *colors = (Uint8*)&getPixel; // Note the &
与您的确切问题无关,但也与您的代码有关:您正在返回一个指向局部变量的指针:
SDL_Color getColor = {0,0,0};
// etc.
SDL_Color *color = &getColor;
return color;
getColor
将在函数返回时超出作用域,函数的调用者将得到一个悬空指针。
您应该重写您的函数以按值返回 SDL_Color
:只需 return getColor;
并将返回类型更改为 SDL_Color
。
关于c++ - 从 SDL_Texture 访问像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47106312/
我有以下函数可以从一个更大的纹理中提取一个 64x64 像素的纹理: SDL_Texture* cropTexture (SDL_Texture* src, int x, int y) { S
我一直在使用 SDL 开发一个相当大的应用程序,最近发现了一个奇怪的错误;每当一个窗口关闭时,另一个窗口上的所有内容都会呈现完全黑色。当我绘制红色线条时,它会变成黑色,而当我绘制 SDL_Textur
我在使用 SDL_Texture 时遇到了问题 角色扮演教程.cpp #include "stdafx.h" int main(int argc, char *argv[]) { bool q
如何更改 SDL_Texture 的不透明度?而且我不知道如何在我的函数中应用不透明度数。 我的代码 void drawTexture(SDL_Texture *img, int x, int
我想为我的小游戏做插值。有 2 个 SDL_Textures,当我点击第一个然后再点击第二个时,它们的位置被交换,但没有移动动画。那么如何为 SDL_Textures 制作动画位置交换? void M
一天前,我安装了一个 SDL2 库。它还没有在 Debian Wheezy 中,所以我使用了 configure, make, make install 命令。 毕竟,当我尝试使用 SDL_Textu
我的代码抛出访问错误。这是代码: SDL_Color *getPixelColor(SDL_Surface *loadingSurface, int x, int y) { SDL_Color
我正在编写一个基于 Tile 的游戏的原型(prototype)来学习 SDL。我的 map 只是一个 257x257 的 Tiles 阵列,每个 Tile 是 1x1 到 60x60 像素(不同的缩
我试图在两个单独的 SDL_Renderer* 上使用相同的 SDL_Texture*,但显然 SDL_Texture 似乎“绑定(bind)”到 SDL_Renderer*。 有没有办法让第二个渲染
给定一个由 unsigned char[] 表示的单 channel 位图(alpha 从 0 到 255),从中制作 SDL_Texture 的方法有哪些?位图没有额外的数据。 回答 auto* s
我在寻找有关如何检索 SDL_Texture 上像素的特定颜色的解决方案时遇到了一些问题...更具体一点:我正在尝试计算给定纹理中使用的平均颜色量。稍后我想将红色像素的数量除以像素总数。对于这个任务,
我正在使用 C++ 和 SDL 制作游戏,该游戏是太空入侵者类型的游戏。一切都很顺利,直到我在 while 循环中添加了背景图像,这是背景的渲染和初始化代码: SZ_Background.cpp: v
我有一个 sprite 类,它使用 SDL_Texture 而不是 SDL_Surface 来代替性能。该类看起来像这样: class Sprite { public: ...
我唯一看到的是 RenderDrawColor。 “circle.png”也在正确的文件夹中(main.cpp 所在的文件夹)。 #include #include int main(int ar
我正在尝试使用 SDL 播放视频。为此,我使用 opencv 加载视频并获取帧。然后我只需要将这些帧转换为 SDL_Texture*,然后我就可以在屏幕上绘制它们了。 那是我的问题,我正在将其转换为
我从在 gcc 中编译我的代码转向了 Visual Studio 2017 提供的编译器。 每次我尝试运行该应用程序时,我都会收到以下错误: 这是我认为错误来自的文件: Texture.h #incl
我一直在努力了解 SDL 的基础知识,但我被看似简单的东西难倒了。 SDL_MapRGB() 需要 const SDL_PixelFormat*,我使用 SDL_PixelFormatEnum 在我的
SDL_Texture *t0; SDL_Texture *t1; SDL_Texture *t[] = {t0,t1}; 它编译得很好,但如果我尝试渲染它(当然,考虑到 t0 和 t1 已经定义
我正在尝试制作一个简单的游戏,当我尝试渲染我的 SDL_Texture 时,我遇到了一个莫名其妙的错误。我已正确设置所有内容,我能够使用 SDL_RenderClear 成功清除屏幕,并且我的纹理不为
我是一名优秀的程序员,十分优秀!