如何使用SFML鼠标移动分别移动多个图像
How to move multiple images separately using SFML mouse movements?
下面是我的代码,它在窗口中显示两个图像。
现在通过移动鼠标,两个图像一起移动,但我应该如何分别访问它们?
int main()
{
// Let's setup a window
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML ViewTransformation");
// Let's create the background image here, where everything initializes.
sf::Texture BackgroundTexture;
sf::Sprite bd;
sf::Vector2u TextureSize; //Added to store texture size.
sf::Vector2u WindowSize; //Added to store window size.
if(!BackgroundTexture.loadFromFile("bg1.jpg"))
{
return -1;
}
else
{
TextureSize = BackgroundTexture.getSize(); //Get size of texture.
WindowSize = window.getSize(); //Get size of window.
float ScaleX = (float) WindowSize.x / TextureSize.x;
float ScaleY = (float) WindowSize.y / TextureSize.y; //Calculate scale.
bd.setTexture(BackgroundTexture);
bd.setScale(ScaleX, ScaleY); //Set scale.
}
// Create something simple to draw
sf::Texture texture;
texture.loadFromFile("background.jpg");
sf::Sprite background(texture);
sf::Vector2f oldPos;
bool moving = false;
sf::View view = window.getDefaultView();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == 0) {
moving = true;
oldPos = window.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y));
}
break;
case sf::Event::MouseButtonReleased:
if (event.mouseButton.button == 0) {
moving = false;
}
break;
case sf::Event::MouseMoved:
{
if (!moving)
break;
const sf::Vector2f newPos = window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y));
const sf::Vector2f deltaPos = oldPos - newPos;
view.setCenter(view.getCenter() + deltaPos);
window.setView(view);
oldPos = window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y));
break;
}
}
}
window.clear(sf::Color::White);
window.draw(bd);
window.draw(background);
window.display();
}
}
我也很乐意听到建议,在那里我可以找到具有适当见解和良好解释的示例。提前谢谢。
问题是你移动的是视图而不是精灵本身:
view.setCenter(view.getCenter() + deltaPos);
window.setView(view);
相反,您应该移动精灵。所以用这条线交换上面的两行,它应该可以工作
bd.setPosition(bd.getPosition() - deltaPos);
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何从具有移动语义的类对象中生成共享指针
- 如何将图像数据从搜索窗口中移动到本地存储器OpenCL
- 如何在移动滑块时串联加载和显示多个图像?
- 图像上的原子移动平均线
- 如何使用SFML鼠标移动分别移动多个图像
- OpenCV:相对于参考图像移动/对齐面图像(图像配准)
- SDL2:如何沿正方形路径移动图像
- 将我的阵列移动到 Mat 并使用打开的 CV 显示图像
- 如何在 SDL C++中将图像移动到点
- 基于图像的计数算法,用于对移动输送机上的物体进行计数
- 在OpenCV中移动图像内容
- opencvc++在阈值图像中查找移动
- 使用OpenCV工具从连续的图像差异中检测国际象棋的移动
- 移除移动物体,从多个图像中获取背景模型