Jump to content

[SFML / C++] Game from scratch guide

as96

post-49433-0-93237600-1444323880.png

 

Hello, LTT I've been wanting to do something like this for a while now, initially through videos but I'm too shy to do that and my upload speed won't allow me to do that so... here I am, hopefully it will help me improve my writing as well as help you guys get started with SFML.

 

First of all I'm gonna answer the question that it's gonna come up for sure: "Why would I even want to read your crappy guide instead of the official SFML one?", the answer is simple, this will have more stuff and you can actually follow along and eventually create a working game.

 

A good C++ knowledge is suggested and I'm not gonna teach you the language since it would be impossible, but I'm gonna cover some part of language if I think it's necessary, for example for the "union" which is pretty unknown to most people.

 

I'm a human being like you, so I can make mistakes in that case don't hesitate to let me know and I'll fix them.

 

This is an index of what I'm gonna do:

 

- Basics of SFML: part 1 - part2 - part3
- Graphics: part1 - part2 - part3
- Input
- Audio
- Network
- Box2D integration (Physics)
- Asio integration (Network)
- Making a game
 
I'll write those parts as posts of this discussion and link them in that list. If you have any request feel free to ask. And if you want to contribute by sharing your knowledge of SFML you can simply write a post here, I'll add a link to your post in this first one, I'm probably gonna make a different index to avoid confusion.
 
Now let's come to the painful part... the art... prepare your eyes for some awful sprites, if you want to help you can give me some of your sprites, I'm gonna use everything you guys send as long as it's not against the CoC, so no explicit or offensive image.
If your eyes start bleeding after seeing my drawing it's not my fault.
 
Users' contributions:
Link to comment
Share on other sites

Link to post
Share on other sites

Where to download

 

In this guide I will use Visual Studio Community 2013, but there is a new version of it so you can download the 2015, you need an account but don't worry it's completely free to use.

You can download Visual Studio from here: https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx

You can download SFML from here: http://www.sfml-dev.org/download/sfml/2.3.2/ (Download the 32bit version)

Link to comment
Share on other sites

Link to post
Share on other sites

thanks dude its like you came down from heaven, i will try this later so keep it up mate

Link to comment
Share on other sites

Link to post
Share on other sites

I'm on board for this quide, I made some low-res linus and luke sprites from the wan show intro and their animations. I can send you those later today, just send me a PM or @ me in this thread.

 

I'd also be cool if you knew how to incorporate SFML without an IDE if you know how to do that.

Edited by prolemur
Link to comment
Share on other sites

Link to post
Share on other sites

Configuring SFML when using CodeBlocks

Download an appropriate SFML version from the download link : http://www.sfml-dev.org/download/sfml/2.3.2/ .

There are multiple variants of gcc for Windows, which are incompatible with each other (different exception management, threading model, etc.). Make sure you select the package which corresponds to the version that you use. If you are using Codeblocks, then you probably are using MinGW , which should have GCC 4.8.1. If that's the case, then download the SFML GCC 4.8.1 TDM (SJLJ) - 32-bit version. You don't need the 64 bit version. Likewise, if you are using Visual Studio, then you probably have the Visual C++ compiler , and you need to get the version which suits your version of Visual Studio.

 

Then, extract it to a convenient location.

 

Now, it's time to set up our project. Create an empty project. Then, we need to tell the compiler where to look for the SFML headers and tell the linker where to look for the SFML libraries.

Right click on your project, go to build proprieties. While selecting global build proprieties, go to search directories , then compiler. Add a path to the SFML headers , usually PATH\SFML\include . Then click the Linker tab, and add a path to the libraries ( PATH\SFML\lib )

start-cb-paths.png

Then you need to link the SFML libraries. Select Release build options , then linker settings. SFML is made of 5 modules , everyone having a library of their own : system, window, graphics, network and audio . Add them as this :

sfml-graphics
sfml-window
sfml-system

start-cb-link-libs.png

The order does matter, since libraries have dependencies (for example, system needs to be loaded before graphics) .

Other options for linking the libraries are :

sfml-xxx-d

for debugging.

sfml-xxx-s

for static linking.

What does that mean? If you link to the dynamic version of SFML , then you need to have the libraries in the same dir as the executable for it to run. The static version will integrate those into the exe itself. Also, if you are using static SFML , then you need to go to compiler settings, #defines , and add the preprocessor option

SFML_STATIC

start-cb-static.png

 

Then, you just need to copy the dlls from /sfml/bin to the directory in which you have your project or cpp file.

ybv308j.png

 

@as96 I hope you don't mind my contribution to this thread :D . I really like CodeBlocks honestly, and it's good for people to have more options

Also, I found SFML to be really well built, and the official tutorials are really good as well.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

I'd also be cool if you knew how to incorporate SFML without an IDE if you know how to do that.

It's not that hard. It's just compiler (and linker) flags.

Something like this should work with g++, I think :

g++ SFML.cpp -o test -I D:\SFML\include -L D:\SFML\lib -lsfml-graphics -lsfml-window -lsfml-system

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm on board for this quide, I made some low-res linus and luke sprites from the wan show intro and their animations. I can send you those later today, just send me a PM or @ me in this thread.

 

I'd also be cool if you knew how to incorporate SFML without an IDE if you know how to do that.

Thank you for the sprites, since I'd like to have every resource available could you post a link in this discussion?

 

Sorry but I won't show any installation process besides the VS one, since if I do that I would have to cover also every other IDE.

 

 - text wall-

@as96 I hope you don't mind my contribution to this thread :D . I really like CodeBlocks honestly, and it's good for people to have more options

Thank you, no I don't mind, as I said every contribution is accepted and encouraged.

Link to comment
Share on other sites

Link to post
Share on other sites

Setup Visual Studio

 

The first thing that you need to do is create a new project:

post-49433-0-04182200-1444330724.png

 

Inside the wizard you'll get a few options that you should set like this:

post-49433-0-96921700-1444330765.png

 

Now you need to enter the project properties:

http://i.imgur.com/d0xAbis.gifv

 

And now we need to set a few things, don't worry it's easy, let's start by telling visual studio where are the include and lib folders of SFML:

(C/C++ -> General -> Additional Include Directories)

post-49433-0-28414000-1444330966_thumb.p

(Linker -> General -> Additional Library Directories)

post-49433-0-00367700-1444330977_thumb.p

 

Then we need to set the dependecies, this is divided in two steps, one for the debug and one for the release, if you want you can skip the release since we won't be using this for a while.

Set the "Configuration:" to Debug and then go to Linker -> Input -> Additional Dependencies:

post-49433-0-82001800-1444331153_thumb.p

add these dependencies

sfml-graphics-d.libsfml-window-d.libsfml-audio-d.libsfml-system-d.libsfml-network-d.lib

and repeat the same steps for the release configuration:

post-49433-0-74564300-1444331320_thumb.p

sfml-graphics.libsfml-window.libsfml-audio.libsfml-system.libsfml-network.lib

Now you need to start the debugging, it will throw an error but don't worry, by doing this we've generated a folder called "Debug" inside our project directory, go ahead and copy the dll files from the SFML folder inside "Debug":

post-49433-0-52984900-1444331464_thumb.p

 

and we are almost ready to go, we just need to create the main function and include the necessary headers at this point we just need iostream and SFML/Graphics, so let's create a new file called main.cpp:

press CTRL + SHIFT + A and fill it like this:

post-49433-0-74755100-1444331583_thumb.p

 

copy paste this code (don't worry about what it means, I'll explain everything in the next post):

#include <iostream>#include <SFML/Graphics.hpp>int main(){	sf::RenderWindow window(sf::VideoMode(1280, 720), "LTT SFML Project");	window.setVerticalSyncEnabled(true);	while (window.isOpen())	{		sf::Event event;		while (window.pollEvent(event))		{			if (event.type == sf::Event::Closed)				window.close();		}		window.clear();		window.display();	}	return 0;}

and if everything is done correctly you should get something like this:

post-49433-0-31067500-1444331636_thumb.p

post-49433-0-36292500-1444330873_thumb.g

Link to comment
Share on other sites

Link to post
Share on other sites

Basics of SFML (Part 1 of 3)

 

We finally got to the part where we actually talk about SFML and see some code.

 

SFML is based on OpenGL and if you want you can actually use OpenGL code but I won’t cover that since it’s not something that I’m comfortable doing, now why am I saying this to you? It’s simple because we are about to analyze the code that I wrote in the setup post (in case you skipped that part I’ll write it down here) and one type that is often mistaken by beginners is in that code.

#include <iostream>#include <SFML/Graphics.hpp>int main(){	sf::RenderWindow window(sf::VideoMode(1280, 720), "LTT SFML Project");	window.setVerticalSyncEnabled(true);	while (window.isOpen())	{		sf::Event event;		while (window.pollEvent(event))		{			if (event.type == sf::Event::Closed)				window.close();		}		window.clear();		window.display();	}	return 0;}

#include <SFML/Graphics.hpp> well this shouldn’t require any explanation we just included the Graphics header, but you should know that SFML is incredibly well organized and if you need let’s say just the RenderWindow you can simply ignore all the other “junk” that’s inside the Graphics.hpp and simply include RenderWindow.hpp instead of the whole graphics module and that won’t cause any issue.

 

Now let’s enter the main and start with the actual code, the first line that we see in the code I wrote earlier is this:

 

sf::RenderWindow window(sf::VideoMode(1280,720), “LTT SFML Project”);

 

and now… remember the thing I said about that type that’s often mistaken by beginners? I was talking about the RenderWindow type since in SFML there are two types of windows available one being RenderWindow and the other is Window, this is an important thing to know since Window is ONLY for OpenGL rendering and not for SFML, so what usually happens is that users ends up trying to call methods that do not exist inside the Window class, like the draw() method and as you can guess that’s a pretty important one.

 

Aaaaaand back to that line, as you can see the RenderWindow constructors takes a few arguments, the first is VideoMode and it’s simply the resolution of the window, the second argument is the window title, and there are also other two arguments that you don’t see in my code, and those are the style and the context settings, but I’ll talk in more detail about those two when I get to the part where we will make a window manager class, and trust me that’s gonna be useful.

 

So the line after that calls the method “setVerticalSyncEnabled()” from the window object that we just created, that is as you can imagine used to enable or disable the vSync but there is also a “setFrameRateLimit()” method available although it’s not precise and should be avoided unless necessary and you should not use both methods since you will most likely run in awful performance issues if you do that.

	while (window.isOpen())	{		sf::Event event;		while (window.pollEvent(event))		{			if (event.type == sf::Event::Closed)				window.close();		}		window.clear();		window.display();	}

Now we got to what is commonly known as “game loop” which is the heart of our code, inside the while statement we can see that we are checking to see if the window is open using the method “isOpen()”, we are then creating a new variable of type “Event” called event (My imagination is so good right? what an original name), the sf::Event is an union which is a pretty unknown C++ structure that allows one portion of memory to be used for different types and this is important to know since only one member will be valid at a time, that valid member can be will match the “type” field of our event variable so we can make a check using a switch or a simple if, in our case we used an if statement since we just needed to check if our window received a close request, and if that was true we called the “close()” method to close the window, and obviously at the next iteration we will exit the game loop, now, to fill our event variable with data we’ll use “pollEvent(event)”.

 

We are now left with two other lines, one being a call to the “clear()” and the other to the “display()” method call, one cleans the window and the other displays what we’ve drawn, now you may be wondering “if that’s the case… will we have flashes between frames?” and the answer is no since as I said SFML is based on OpenGL and that API uses different buffers, one that’s being displayed and the other that’s gonna be displayed on the next frame, so the SFML’s “display()” method is basically used to flip those buffers.

 

Next part: click here

Edited by as96
Link to comment
Share on other sites

Link to post
Share on other sites

I like the step-by-step format and am following along with ease (haven't actually done the steps but read them and will come back when I have more time), keep the default font, remove random white space like extra line-breaks, and first curly braces on same line as method definition :)

 

http://puu.sh/kDA2r/da59d23fce.7z luke and linus files, they're currently bmp. They each have 13 frames to make 2 steps and are super tiny so they'll need to be scaled up by the game

Link to comment
Share on other sites

Link to post
Share on other sites

I like the step-by-step format and am following along with ease (haven't actually done the steps but read them and will come back when I have more time), keep the default font, remove random white space like extra line-breaks, and first curly braces on same line as method definition :)

 

http://puu.sh/kDA2r/da59d23fce.7z luke and linus files, they're currently bmp. They each have 13 frames to make 2 steps and are super tiny so they'll need to be scaled up by the game

The formatting and spaces are a due to the fact that I wrote it on notepad, I'll use a decent text editor next time.

Thank you for the suggestions and the sprites :)

Link to comment
Share on other sites

Link to post
Share on other sites

Basics of SFML (Part 2 of 3)

 

In this second part of the basics of SFML we'll take a look at vectors, rects, transforms, colors an see how to take care of the time with clocks.

 

Let's start with vectors, what are these weird things you ask? Just points for example let's say that Linus ended up in a 2D space, we can identify his position with two simple numbers (that togheter make a point), so we would be using a two dimensional vector that in SFML is called “Vector2”

 

post-49433-0-16135600-1444386149_thumb.p

 

Now, in SFML there are two versions of Vector, one being the two dimensional one that we  saw above and the other is a three dimensional vector, that in this guide we'll probably never use.

You can have sf::Vectors of different numeric types since it uses a template but there are available a couple of useful typedefs like the float vector called “Vector2f” and integer vector which is incredibly named “Vector2i” (these are also available in the 3d version), with these data structure we can represent anything that requires two or three numerical types, for example position, velocity, direction, size, etc.

 

Transforms, these are related to the vectors that we've just seen, in particular a transform is basically a 3x3 matrix that is used to store position, rotation and scale, I don't think this requires any more explanation at the moment since we'll see how to work with them in code in another part of this guide.

 

We are now left with two things to explain, colors and clocks so... I'm gonna start with the colors, yes I could have put this inside the graphics part of the guide but since it's so simple and pretty similar to vectors I decided to put them here. In SFML the type “Color” is used to express an RGBA value (Red, Green, Blue, Alpha), if you haven't worked with graphics alpha is basically the transparency, these values are Uint8 which is an integer value that goes from 0 to 255.

 

The last thing that we'll see in this part is how to keep track of time with clocks, and this is extremely important when we get to the part where we are gonna write the movements and physics since you definitely don't want to tie those things to the framerate, they need to be fixed steps and to do that we use clocks

 

post-49433-0-36225000-1444386178_thumb.p

 

A clock has two methods (if we don't count the constructor) the “restart()” and “getElapsedTime()” method, both of them return a value of type “Time” that has three methods that we can use to get the actual values, yes I said three, since it can return the time as seconds, milliseconds and microseconds with “asSeconds()”, “asMilliseconds()”, “asMicroseconds()” and it also contains a static attribute called “Zero” which doesn't require any explanation since the name tells everything.

 

Previous part: click here

Next part: click here

 

PS: Thanks to @prolemur for the LMG sprites

Link to comment
Share on other sites

Link to post
Share on other sites

Basics of SFML (Part 3 of 3)
 
In this part we are going back to our IDE and start implementing what we've seen in the previous post, but first I'm gonna post our code once again to make sure we are on the same page:
#include <iostream>#include <SFML/Graphics.hpp>int main(){	sf::RenderWindow window(sf::VideoMode(1280, 720), "LTT SFML Project");	window.setVerticalSyncEnabled(true);	while (window.isOpen())	{		sf::Event event;		while (window.pollEvent(event))		{			if (event.type == sf::Event::Closed)				window.close();		}		window.clear();		window.display();	}	return 0;}
 
I'm now going to load a texture and make a sprite, don't worry too much about the code I'll explain everything in detail when we get to the Graphic “chapter”, right now I just want to use vectors and clocks to demonstrate what I wrote in part 2 of the basics of SFML.
 
First of all I'm creating a resource folder inside my project just to make my paths shorter but you can ignore this step if you want.
 
 
then move the sprites that I want to use in that folder, and since I want 12 sprites I will also create a subfolder called “LinusSprites” that will contain every Linus sprite contained in the archive that @prolemur uploaded. But since at the moment I've yet to create a resource holder I'm going to use only 3 of those (otherwise the code would be awfully unorganized) to create a simple animation.
 
I will create a class called Linus, this is NOT going to be the definitive class that we will use in the actual game, but I want to keep my code clean so... well I'm making a class.
 
class Linus{public:	Linus(std::string& t1, std::string& t2, std::string& t3)	{		mTextures[0].loadFromFile(t1);	// I won't bother with error checking at this point		mTextures[1].loadFromFile(t2);		mTextures[2].loadFromFile(t3);		mClock.restart();		mSprite.setTexture(mTextures[0]);		mFrameCounter = 0;	}	void Update(sf::RenderWindow* window)	{		if (mClock.getElapsedTime().asSeconds() >= 0.5f)		{			mSprite.setTexture(mTextures[mFrameCounter]);			if (mFrameCounter == 2)				mFrameCounter = 0;			else				mFrameCounter++;			mClock.restart();		}		window->draw(mSprite);	}private:	sf::Texture mTextures[3];	sf::Sprite mSprite;	sf::Clock mClock;	int mFrameCounter;};
 
and in our main function we will create an object of Linus and call his Update method inside the game loop:
 
int main(){	std::string t1Path = "Resources/LinusSprites/linus1.bmp", t2Path = "Resources/LinusSprites/linus2.bmp", t3Path = "Resources/LinusSprites/linus3.bmp";	Linus linus(t1Path, t2Path, t3Path);	sf::RenderWindow window(sf::VideoMode(1280, 720), "LTT SFML Project");	window.setVerticalSyncEnabled(true);	while (window.isOpen())	{		sf::Event event;		while (window.pollEvent(event))		{			if (event.type == sf::Event::Closed)				window.close();		}		window.clear();		linus.Update(&window);		window.display();	}	return 0;}
 
and the result should be this:
 
 
PS: make sure to include the string header.
 
Now that I've demonstrated how clock works let's take a look at vectors, to do that I'm gonna make a few modifications to Linus' Update method:
 
	void Update(sf::RenderWindow* window)	{		if (mClock.getElapsedTime().asSeconds() >= 0.5f)		{			mSprite.setTexture(mTextures[mFrameCounter]);			if (mFrameCounter == 2)				mFrameCounter = 0;			else				mFrameCounter++;			mSprite.setPosition(sf::Vector2f(mSprite.getPosition().x, mSprite.getPosition().y) + sf::Vector2f(5.0f, 5.0f));			mClock.restart();		}		window->draw(mSprite);	}
the result should be this:
 
 
sure this is nothing special but I want to take a look at what I did to change the position:
mSprite.setPosition(sf::Vector2f(mSprite.getPosition().x, mSprite.getPosition().y) + sf::Vector2f(5.0f, 5.0f));
as you can see I performed an addition with vectors, yes it's that simple, SFML's vector have operator overloading for every basic operation, so you can also subtract, multiply and divide vectors.
 
The complete code is this in case you just want to copy paste:
 
#include <iostream>#include <string>#include <SFML/Graphics.hpp>class Linus{public:	Linus(std::string& t1, std::string& t2, std::string& t3)	{		mTextures[0].loadFromFile(t1);	// I won't bother with error checking at this point		mTextures[1].loadFromFile(t2);		mTextures[2].loadFromFile(t3);		mClock.restart();		mSprite.setTexture(mTextures[0]);		mFrameCounter = 0;	}	void Update(sf::RenderWindow* window)	{		if (mClock.getElapsedTime().asSeconds() >= 0.5f)		{			mSprite.setTexture(mTextures[mFrameCounter]);			if (mFrameCounter == 2)				mFrameCounter = 0;			else				mFrameCounter++;			mSprite.setPosition(sf::Vector2f(mSprite.getPosition().x, mSprite.getPosition().y) + sf::Vector2f(5.0f, 5.0f));			mClock.restart();		}		window->draw(mSprite);	}private:	sf::Texture mTextures[3];	sf::Sprite mSprite;	sf::Clock mClock;	int mFrameCounter;};int main(){	std::string t1Path = "Resources/LinusSprites/linus1.bmp", t2Path = "Resources/LinusSprites/linus2.bmp", t3Path = "Resources/LinusSprites/linus3.bmp";	Linus linus(t1Path, t2Path, t3Path);	sf::RenderWindow window(sf::VideoMode(1280, 720), "LTT SFML Project");	window.setVerticalSyncEnabled(true);	while (window.isOpen())	{		sf::Event event;		while (window.pollEvent(event))		{			if (event.type == sf::Event::Closed)				window.close();		}		window.clear();		linus.Update(&window);		window.display();	}	return 0;}

Previous part: click here

Link to comment
Share on other sites

Link to post
Share on other sites

I just started this tutorial series http://www.learnopengl.com/#!Getting-started/OpenGL I replaced their GLFW with SDL2

 

I will follow yours too if you will have any 3D math tutorials and OOP design thats what i lack the most. 

Sorry I will not cover 3D, but if you are looking for some OOP design you can go ahead and click that follow button since you'll see quite a lot of those, in the meanwhile you can take a look at this: http://gameprogrammingpatterns.com/contents.html

Link to comment
Share on other sites

Link to post
Share on other sites

Ok guys the next step is the graphic "chapter" that's gonna be incredibly long, the first part will probably be about the texture holder.

Link to comment
Share on other sites

Link to post
Share on other sites

Graphics (Part 1 of ???) - Textures and Sprites

 

What are sprites and textures? These two things are often confused, but the concept is simple, the texture is just an image nothing more nothing less, you can't move them, you can't interact with them in game, they are just images loaded in memory. While sprites are actual entities in our game, they can be interacted with but to be shown they need some kind of graphics on them, and that's when we load the texture that we've created inside our sprite object. So if you want a sprite you'll also need a texture, one texture can be used for multiple sprites and you shouldn't load the same image more than one time since it would just be a waste of memory. Now that I've explained what are sprites and textures we can see how they work in SFML, since as I said a sprite needs a texture we need to create that first, an sf::Texture has two constructors and empty one which is the default and a copy constructor so we can't actually load the image inside the constructor, that said the process is simple and short we just need two lines (more if you want to handle errors), there are three ways to load a texture, from file, from stream, from memory and from image the first three are pretty self explanatory but the last may need some more information since it could be confused, the load from image method uses a class called sf::Image which is used to manipulate an image data but be careful an sf::Image is stored on system memory not on video memory so it's a bit slower to draw but it's gonna be faster to manipulate, anyway I don't think we are gonna use that method in this guide, what we are going to use for sure though is the load from file method, let's see how it works:



sf::Texture texture;
if (!texture.loadFromFile("Resources/LinusSprites/linus1.bmp"))
{
// in case of error do something
}


the first line is just the declaration of a variable of type sf::Texture, in the second line we enter an if statement since we want to check if our image was loaded into the texture variable correctly, since as of now we don't actually handle any error situation we can ignore it but eventually we'll want to throw an exception. 

An sf::Texture offers also two useful functions, the smoothing and the repeating. Smoothing is used to smooth the image when it's scaled up but it makes it a little bit blurry although it's still better than a Minecraft stile isn't it? While repeating basically as the name says repeats the texture when used in a sprite set to show a rectangle larger than the actual texture.

 

Now we can finally create a sprite this is even simpler than the texture since we don't need to do any error checking:



sf::Sprite sprite;
sprite.setTexture(texture);


just like with the texture one we declare a variable this time of type sf::Sprite then set the texture in the second line, if needed we can also use just a part of that texture:



sprite.setTextureRect(sf::IntRect(0, 0, 16, 16));


A sprite can be moved, rotated, scaled and modified in color, we'll see how to do these things in code in a later part of this guide since we have a few things that we need to see before we can actually start making our game.

 

Next part: to be released

Link to comment
Share on other sites

Link to post
Share on other sites

Ok since I don't want to get things messy the game itself is gonna be done after all the other chapters, now since I have no idea what people want to see I'm gonna add a poll with a few genres but if you have something else in mind just post it and I will add it to the poll.

Link to comment
Share on other sites

Link to post
Share on other sites

You'd also be able to use

++mFrameCounter %= mTextures.length;

in replace of

if (mFrameCounter == 2)          mFrameCounter = 0;else	mFrameCounter++;

I think... as of right now that 2 is a magical number btw, I know it is suppose to be the max animation frame but could be clearer.

Link to comment
Share on other sites

Link to post
Share on other sites

You'd also be able to use

++mFrameCounter %= mTextures.length;

in replace of

if (mFrameCounter == 2)          mFrameCounter = 0;else	mFrameCounter++;

I think... as of right now that 2 is a magical number btw, I know it is suppose to be the max animation frame but could be clearer.

Yes, that 2 was hardcoded but don't worry that's gonna change, it was more to show how clocks work in SFML.

Anyway thank you for the suggestion :) .

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys I just wanted to let you know that I'm not dead, but I'm gonna continue this guide on this weekend due to lack of time.

Link to comment
Share on other sites

Link to post
Share on other sites

Graphics (part 2 of ???) - Text

 

Sorry for the big time gap from the last part but I really didn't have enough time, now I'll try to post at least once a week and to get us started again let's talk about text on SFML, so we can write something on screen.

 

To print a string on screen we need two things an sf::Font and an sf::Text both classes should be pretty self explanatory so I won't say what's a font, but what's important to know that SFML does NOT have a pre-loaded font so you can't just create a variable of type Text and draw it, to do that you need to load a font and with load a font I actually mean load from file since you can't use the system fonts as all the other resource related classes the Font class has a “loadFromFile”, “loadFromMemory” and “loadFromStream” all three functions return a boolean so you can check if your font was loaded correctly and in case handle the error.

 

Once you've loaded the font you can make a text using the sf::Text class, this class is also extremly simple all you need to do is set the font with “setFont” and set the string that you want to print on screen with “setString” as parameter the “setString” method actually take a custom string class (sf::String) instead of the std::string class.

 

The Text class also provides methods for setting the style (bold, underlined... etc.), color and size.

 



text.setFont(font);
text.setString("Hello world");
text.setCharacterSize(24);
text.setColor(sf::Color::Red);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);


Link to comment
Share on other sites

Link to post
Share on other sites

Would it be possible to add a section on setting up SFML with Cmake?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×