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.
Related
I have some homegrown code to draw a circle in SDL2 (It's slow). Each time I want to update the window, I have to call SDL_RenderClear() and redraw the same circle. Is there any way I can avoid clearing the circle but still clear the rest of the screen to allow me to draw everything else.
The circle has to be drawn over the top of everything else.
https://imgur.com/a/0qAgd1Y
I could draw the circle each time over the rest of the image, but that takes about 500ms.
I am making some classes to help create a GUI using SFML. I would like to be able to display only a part of a sf::Text. Something like setTextureRect in the sf::Sprite class.
The only solution I found was to draw the text to a sf::RenderTexture and then I assign its texture to a sprite. I can then draw this sprite on the window.
//The class contains :
sf::RenderTexture buffer;
//When the class is created :
buffer.create(window.getSize().x,window.getSize().y);
//To draw on the screen :
window.clear();
buffer.clear();
buffer.draw(text);
buffer.display();
sprite.setTexture(buffer.getTexture());
window.draw(sprite);
window.display();
This allows me to use sf::Sprite::setTextureRect, but it is very slow. I measured that it is about 100 times slower than a direct draw. I tried creating the buffer to the size of the text but it had almost no impact on the performance.
So my question is, is there a way to display only a part of a sf::Text (or sf::Shape) that would be more efficient?
You can use sf::View to determine what is drawn and what not.
Remember that the rectangle that is drawn, with respect to the window, use the range 0.f - 1.f.
The tutorials can help.
As you saw, there's no method to do this in sf::Text.
http://www.sfml-dev.org/documentation/2.3.1-fr/classsf_1_1Text.php
However, if as you said, you want to do this: http://i.imgur.com/FsvnP.png
You can compute the size of the sf::Text and cut it before it exceeds the size of the frame (and eventually replace the last characters).
For example, "Person 2554548747848874874" will be "Person 25545..".
It's smooth and a lot of list are done this way. (You have an example with the Windows task explorer)
As DarkPhantom said, there's also sf::View, but you'll have to try to see if it's reliable to do a lot of setView().
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.)
So, i'm a complete stranger to SDL and i found this nice code online:
http://gamedevgeek.com/tutorials/animating-sprites-with-sdl/
I was just wondering how to make it so that when i press space a shape gets placed infront of me? For instance, im just walking around and when i press space a rectangle or another bmp is places in front of me.
Sorry for not being explicit in what i want, i just dont know how to explain it.
You need:
Another Surface to draw (blit), made from the rectangle.bmp. This would use the same method as is used for the grass (or the player if you wanted animation).
Knowledge of where "in front" is: up, down, left or right. Look at the code and see what variables change when you press one of the arrow keys. (Hint: don't use rcSprite.) In a larger game, you would want to define a new variable for the direction that the player is facing, and then use that for both the sprite animation and for placing the rectangle, as it would make the code easier to understand.
Some new code in HandleEvent, which does something if the key is SDLK_SPACE.
Then calculate a position to place the rectangle (say, the player's position with 50 added to "x" if they were facing right), and draw the rectangle in the same way as the grass.
In general, look at what code has already been written, and Google for stuff you don't know (e.g. the name of SDLK_SPACE).
Good luck
I was wondering if there is a way to draw a gl::texture file with out having to use the gl::draw command every loop. Is there a way I can draw it once, and then not worry about it.
Drawing the image on every loop of draw() is slowing down my application, so I'd like to only draw things once on the screen and then update them if need be.
Quoting from Cinder's tutorials:
"When you create a new Cinder project, you will notice there are a few functions declared for you. Every Cinder project is made up of three main functions. You initialize your variables in the setup() method which is called once when your program begins. You make changes to those variables in the update() method. And finally, you draw() content in your program window. Update and draw are the heartbeat of any Cinder project. UpdateSetup, then update and draw, update and draw, update and draw, on and on until you quit the application."
There's a way though to draw objects permanently in OpenGL and concequently in Cinder but I wouldn't recommend it. Is to disable gl::clear() in your draw function. You can't though selectively delete any unneeded object. You will have to render your scene from scratch. Think of OpenGL's frame-buffer more of a canvas. Everytime you call gl::clear() you take the brush and you paint your canvas black or what ever color you specify with gl::clear(). After that the frame-buffer is "tabula rasa" you have to draw everything you want to display from scratch. If you don't state any gl::clear() command when you draw a new object is like your canvas stays intact and you draw your object on top of the already drawn.