I saw that the SFML library also had updated bindings for the.
So SFML 2.0 recently went out of RC and is now the official stable version. As I found out about the news I decided to try it out, and since I just installed Visual Studio 2012 I also wanted to see if SFML would work on such an IDE. Special thanks to LA and Tyson for pointing out some details in the comments! ? You can check it out over at their website) It’s perfect for 2D games and other multimedia applications. Txt.( For those of you unfamiliar, SFML is one of the best alternative libraries to SDL and provides high-level access to graphics, sound, input and networking. Txt.setStyle(sf::Text::Bold | sf::Text::Underlined) // make text bold and draw line under it (ttf short for true type fonts)ĭrawing text to screen is as easy as image sf::Font class method loadFromFile loads font to memory from hard disc and sf::Text class utilize this font to draw text on screen sf::Text provides some method like set text, postion, scale, rotation, style and color etcĪssert(font.loadFromFile("Walk-Around-the-Block.ttf")) You can change size of sprite by changing its scale, scale (1,1) means no scale i.e sprite is in its original condition.ĬtScale(0.5f, 0.5f) // size down sprite 50%ĬtScale(-1.0f, 1.0f) // no change in scale but flip it around x-axisĬtScale(1.5f, 1.5f) // scale up by 50%ĭownload ttf font from fonts2u website which looks you cool. Pink doted rectangle sprite has no rotation, yellow Rect sprite has +90 rotaion and sky Rect has -45 degress rotation.Īnother example of Rotation but with pivot at centre of sprite Value passed to rotate is in degree +ve value rotates the sprite clockwise and -ve value rotates it counter clockwise. Pivot is origin of sprite or the point around which sprite rotates by default it is on top left corner you can set pivot any where you want with setOrigin function.ĬtOrigin(tex.getSize().x / 2, tex.getSize().y / 2) // pivot at centerĬtOrigin(tex.getSize().x, 0) // pivot at top right Here is screenshot of three images at position (0,0 ), (250,200), (550,0) from left to right SetPosition function sets the position of sprite at particular coordinate which you pass as parameter. This function is very useful used todo sprite frame animation, Output is like this
Other way is you load full image and ask Sprite class hey set Texture like this.ĬtTextureRect(sf::IntRect(34, 12, 176, 171)) One way to-do is you can only load that part of image in which you are interested, here 2nd parameter sf::IntRect is new it says hey only load that Rect. Lets say you want to draw only head of cartoon you can achieve this in two ways Now its time to check some properties of Texture and Sprite Class Draw part of Sprite
Run this code and you will get result like this, Cartoon on screen at 0,0 position on window. Sf::Event Class captures all events taking place on window like keydown, keyup, mouseclicks, window resize, close or minimize, Text Enter etc, I will cover this in next post.
If image fails to load you got this kind of message by assert. The advantage of asset is you don’t have to check manually if texture is loaded or not.It saves your time and code looks neat , the expression is false), the program gets terminated with message on console window. If the argument expression of this macro is equal to zero (i.e. SFML sports file formats like JPEG, PNG and some other, if your are using JPEG image than write jpg after dot like cartoon.jpg.Ī: assert is a macro contained by #include or #include. The purpose of sf::Texture Class is to load image and hold it, sf::Sprite Class utilize this texture and draws this on RenderWindow, this class also contains some handy function like rotation, scale, set origin and change sprite color etc we will see them after short time.Īt line # 8 & 9 declare sf::Texture and sf::Sprite variableĪt line # 11 loadFromFile function of Texture class loads image from hard-drive to main memory parameter to this function is string that is path to image with file name. In SFML top left corner of window is 0,0. Sf::RenderWindow window(sf::VideoMode(800, 600), "Pong") Texture stores image and Sprite draws it.ĭownload this image and put this in project folder Drawing image is very simple all you need sf::Texture & sf::Sprite class objects.