( Alt + Enter) a new window pops up which looks like thisĬlick Configuration: Select All Configuration after that Select VC++ Directories under Configuration properties as show in figure above, click Include Directories, click little button which appears on right side after that click Edit.Ī new window pop ups now click on yellow folder icon a new line appears under Neath it click Now go to project properties by right clicking project in solution explorer or by pressing Right click on Source Files folder and add new item or for short cut press ( Ctrl + Shift + A) a new window opens select c++ file and name it main.cpp this file serves as entry point for our application. Txt.setColor(sf::Color::Green) // set color to greenĭownload SFML latest version Visual C++ 12 (2013) – 32-bit from their offical website and unzip it in any directory which you like.Īfter that open visual studio 2013, create new project select Visual c++ under Templates and select empty project and name it Pong or what ever you like, press OK. 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
#Visual studio sfml texture load image full#
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
#Visual studio sfml texture load image code#
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.