gpt4 book ai didi

c++ - SDL_FillRect 改变值

转载 作者:行者123 更新时间:2023-11-30 02:57:56 25 4
gpt4 key购买 nike

我试图通过使用 SDL_FillRect() 和以下代码来覆盖 SDL_Surface * 中的小矩形:

        int display(SDL_Surface * screen, Uint16 tile_size){
if (!screen)
return 1;
std::cout << x << " " << y << std::endl;
SDL_Rect pos = {(Sint16) (x * tile_size), (Sint16) ((y - 2) * tile_size), tile_size, tile_size};
std::cout << pos.x << " " << pos.y << std::endl;
for(uint8_t Y = 0; Y < 4; Y++){
pos.x = x * tile_size;
for(uint8_t X = 0; X < 4; X++){
// bit mask check to see which bits should be displayed.
// not relevant to question
// if (shape[orientation][Y] & (1U << (3 - X))){
std::cout << pos.x << " " << pos.y << " -> ";
SDL_FillRect(screen, &pos, color);
std::cout << pos.x << " " << pos.y << std::endl;
// }
pos.x += tile_size;
}
pos.y += tile_size;
}
std::cout << std::endl << std::endl;
return 0;
}

x 的范围从 0 到 9,y 的范围从 0 到 21,tile_size 很小(现在是 25)。

运行此代码时,输​​出如下:

3 0                             // x y
75 -50 // x*tile_size y*tile_size
75 -50 -> 75 0 // what in the world?
100 0 -> 100 0
125 0 -> 125 0
150 0 -> 150 0
75 25 -> 75 25
100 25 -> 100 25
125 25 -> 125 25
150 25 -> 150 25
75 50 -> 75 50
100 50 -> 100 50
125 50 -> 125 50
150 50 -> 150 50
75 75 -> 75 75
100 75 -> 100 75
125 75 -> 125 75
150 75 -> 150 75

第三行(第一个显示的矩形)以某种方式从 -50 移动到 0。它打乱了我的显示计算。怎么了?我是否遗漏了一些明显的东西?

最佳答案

我在浏览文档时看到了这个:“如果在目标上设置了一个剪辑矩形(通过 SDL_SetClipRect 设置),那么这个函数将根据剪辑矩形的交集进行剪辑dstrect 矩形和 dstrect 矩形将被修改以表示实际填充的区域。”

这表明存在导致问题的剪辑矩形。

该剪辑矩形是包含在您的 SDL_Surface 中的一个属性,可能从 (0,0) 开始。这将使任何负数在调用 SDL_FillRect() 后变为 0。

您可以通过传递对 dstrect 拷贝的引用来解决此问题。

来源:http://www.libsdl.org/docs/html/sdlfillrect.html

关于c++ - SDL_FillRect 改变值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com