C++ SFML 1.6 Sprite Position with Mouse - c++

When using SFML 1.6, I have run into a small problem, that I know there is an easy solution, but I currently cannot think of it/ haven't been able to find the answer via research.
I know that I have to transform the sprite to a global position using TransformToGlobal(someVector), but I don't know where to put it.
Here is an example of my code that I am using which does not work because it isn't in the global position.
if(sprite.GetSubRect().Contains(mouseX, mouseY))
sprite.SetImage(someImage);
else
sprite.SetImage(someOtherImage);
I tried adding sprite.TransformToGlobal(sprite.GetPosition()); before it, but it did not work as well.

You could try using the transformed mouse position and then checking if the sprites contains the mouse:
sf::Vector2f mousePos = App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
if(sprite.GetSubRect().Contains(mousePos.x, mousePos.y))
sprite.SetImage(someImage);
else
sprite.SetImage(someOtherImage);

Related

how can i draw a sprite by left clicking?

i've tried to make a project, but i can't draw a sprite as i want. I mean that everything works when i just draw a sprite, but it stop working when i am trying to draw the sprite by clicking left mouse button. There's code i tried:
if(zdarzenie.type == Event::MouseButtonPressed && zdarzenie.mouseButton.button == Mouse::Left)
{
pocisk.setPosition(10, 10);
oknoAplikacji.draw(pocisk);
}
Btw, I am writing in Polish as if it would change something.
And yes, i have everything good besides that.
(and i am using 2.4.1 version of SFML)
I don't know what you are doing now because you didn't provide enough of your code and I actually don't understand your if statement but, it can just be :
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sprite.setPosition(sf::Mouse::getPosition());
renderTarget.draw(sprite);
}
By the way I strongly suggest that you do not use the draw function here but organize your code to have a draw method called in a render loop, because depending on where your code is in your program, the sprite could be drawn for only one frame and then erased since it's not updated.
From what I understand in your code in Polish, you have the right code to do what you want, but the problem comes from the fact that you draw the sprite only once.
The draw method is called every frame and it will erase everything on screen and then redraw them. Doing it only once, like in your code, will only draw it a single time then delete it the very next frame.
At that point multiple solution can be used. If its a GameObject clicking can activate the object to then draw it or a simple bool could be used has a switch in your draw to make it appear.

How do you make a clickable sprite in SFML?

I've been looking through the SFML documentation for making clickable sprites, but so far I haven't found anything.
Do you guys think you could help me out?
There is nothing like sf::ClickableSprite in SFML so far, and probably there will never be. (Current list of classes in SFML)
However, you can obtain this behavior with the sf::Sprite object and the events. The idea is simple - as soon as you get the sf::Mouse::isButtonPressed(sf::Mouse::Left) event, check if the mouse is in the sprite. If it is, perform the action. You can perform another action (maybe undo) when button is released.
There is sf::Sprite::getGlobalBounds() function which returns you the position and the dimensions of the sprite. There's also sf::Mouse::getPosition() function, which returns the current position of the mouse. You can use sprite.getGlobalBounds().contains(mousePos) to check whether the mouse is in the sprite.
If you're using views, you'll need to add the view's position to sf::Mouse::getPosition(window), since it gets the mouse position relative to window coordinates.
(thanks to Chaosed0 for additional notes.)

Setting mouse position?

Well I'm working on a small program and I'm trying to set the position of the window's mouse to it's center every time it moves. Thing is I have no idea how to make this work. I had the following code:
auto point = new Point(300.0, 200.0);
//auto ends up being Windows::Foundation::Point;
But I could still move the mouse freely all over the screen...
So then I searched the internet a bit and used:
Windows::UI::Input::PointerPoint point = Point(300.0, 200.0);//1
//and//
auto point = Windows::UI::Xaml::PointHelper::FromCoordinates(300.0, 200.0);//2
And example one wouldn't compile. Example 2 did compile but I could still move the mouse acrros the whole screen.
Don't know if it's important but I'm coding in C++, using DirectX for graphics and XAML for Text. (I'm also using the base class as C++/CX to work with XAML).
Try using SetCursorPos(), it should work on Windows.

cocos2d GL_SUBTRUCT problem when using CCRenderTexture

My game needs to implement night vision effect. I'm using a CCRenderTexture as a mask, and I plan to draw the visible areas onto the mask by using [rangeSprite visit];
However, it need GL_SUBTRUCT mode. I searched cocos2d's codes, but found nowhere the macro is used. Does it mean cocos2d do not support this?
Have you solved this? I have the same problem, and am approaching it currently by using a blendmode like this:
[light setBlendFunc:ccBlendFuncMake(GL_DST_ALPHA, GL_ZERO)];
...whereas light is a sprite I paint onto my CCRenderTexture, which gets laid over the screen.

how to keep a GUI on-screen without moving or resizing it according to camera movement

Hey i'm trying to make a GUI lib with SFML, and everything's done except one problem: Making the Interface stay still even when the camera moves or zooms in.
This would be easy to fix if zooming wasn't possible, but zooming out means scaling the contents of the Interface up, which causes it's text/images to become blurry.
Does anyone have a way to get around this issue? (preferably only using SFML, but if i must i can add OpenGL stuff....)
Stop using the same camera and perspective matrices for your GUI as you do for your scene.
In the case of SFML apply a different view, then restore the old one.