Archive

Posts Tagged ‘Simple and Fast Multimedia Library’

SFML 2.0 Release Candidate

Hi all! Sorry then new posts still haven’t come through. I am working on it, but things are still very very hectic. Anyway, some good news in the mean time! SFML 2.0 Release Candidate is out! So it’s just going through a last phase of testing and minor changes before release. That means two main things for us:

  • prebuilt binaries on the official site (I think I’ll still provide them until the final release is out)
  • few (if any) public interface changes, so no more problems with tutorials becoming outdated due to new function names, etc.

If you want to try it, head over to the SFML downloads page and get it right away.

Sign up now for new SFML Binaries!

Later on today, I’ll be sending out some updated SFML binaries for Windows. Not only that, there’s going to be a brand new script for installing SFML on Unix (in particular, Ubuntu/Linux Mint, but I’m sure a little modification will have it running elsewhere). If you want to be the first to get hold of all this, remember to subscribe by email if you haven’t yet. Otherwise, they’ll be added to the downloads page in a few days.

Competition Time!

A reader suggested that I host a little competition during the leadup to Skyrim. Now, this isn’t actually a game I intend to be playing, but I’m told it’s a big thing… Anyway, it’s coming out on the 11th, so I’m going to have a little competition. Let’s see who can make the best game prototype by then! If you’re interested in competing, and indeed if you have any suggestions for changes to the competition, please do comment below.

It’s not a very long time, so I don’t expect much. Basically, what you should aim for is planning the game mechanics, the structure and – if relevant – the story. Then make a simple working prototype. Don’t worry and menus and options screens and what have you. Just focus on loading a sample level the character can run around in, or whatever is the equivalent prototype for the game you have in mind. Don’t worry too much about art, either. Just do something basic and functional ;) “Programmers’ art”, as it is called.

Unfortunately I won’t be able to compete this time. I’m very busy and won’t have to time to put anything together in 11 days. Come Christmas I intend to have a more drawn out competition, in which I shall definitely compete! So if you’re too busy, don’t feel bad and just running through some ideas for the Christmas competition.

Now, as for rules…

  1. The game must use the SFML library
  2. It can be written in any language SFML supports, though C/C++ is preferable
  3. You may use other low level libraries (such as Boost, RapidXml, etc.), but make sure they are cross platform. And use your discretion – don’t use a library which does everything for you. If you’re unsure about a library, just ask!
  4. There is no prize as such, other than a mention on the site. I can link to your site if you want, provided it appears relevant (at my sole discretion). And also I might get a few quotes from the winner by email about his/her project to share on the site.
  5. You have until midnight GMT on 11th November 2011.
  6. To submit your entry, post a comment on this post, with a brief description of your project, and a link to it. Either host on a file sharing site such as MegaUpload, or host the source on Github or Sourceforge if you know how. Provide compiled binaries if you want, but make sure to provide the source as well – Linux users like me may have difficulty playing a game compiled for Windows!
  7. Judging will be by public vote, but adjustments to results may be performed if deemed necessary (at my sole discretion).
  8. No negotiation will be entered into regarding judging.
  9. If for any reason the competition needs to be cancelled I reserve the right to do so at my sole discretion
  10. You retain the rights to your creation, but by submitting your entry you authorise me to use your game, its story and design, and its source files, media and other data for the purposes of publicising the competition, and also afterwards for the purpose of discussion in other posts.
Good luck!

Update

Hi there. Haven’t posted in a while, and I just wanted to let you all know I’m still here. It’s been really busy and will unfortunately stay that way as I’m back at school from next week. Nonetheless, I’ll try and write some more tutorials soon, and record more videos as well. I also know I owe you updated versions of the SFML 2.0 binaries – I’ll try and do that as soon as possible too.

Anyway, the point is, bear with me and hopefully things will pick up again soon. In the mean time, I’ve still got enough free time to answer any small queries, so if you have any questions or suggestions, email me, or use the Q & A page. And, of course, if you want to be the first no know when I start making posts and videos again soon, then subscribe to the blog here, and to my channel on YouTube. Oh yes, and do share any tutorial ideas you have here.

Until next time.
Xander

Building SFML 2.0 with Make (GCC/Ubuntu)

16.08.2011 15 comments
GNU Compiler Collection logo. Source/Author: from http://gcc.gnu.org/img/gccegg.svg, with tweaks.

Image via Wikipedia: The GNU Compiler Collection

I’ve now demonstrated how to build the SFML 2.0 binaries in an IDE and also with a build utility (better). However, all my tutorials have undoubtedly been Windows centric – I discussed Microsoft Visual C++ and MinGW, after all. Well now it’s time to make that right – in this article I’m going to explain how to build the latest versions of the SFML 2.0 binaries for Ubuntu, using GCC.

Other Unix Operating Systems

With the addition of the post, I’ve covered Windows – both MinGW and MSVC – and also GCC on Ubuntu, Linux Mint, Debian or an Ubuntu variant such as Xubuntu. But what about the rest of you? What if you’re on another Unix operating system? Well, the fact is that you should still have access to GCC. This means you should be able to invoke the actual build operation with the same commands. The other software we need – CMake – should also be available for most platforms. Thus the core elements of what I demonstrate here for GCC with Ubuntu should be applicable to GCC with other Unix platforms.

The thing that you will have to bear in mind, however, is that the other terminal commands I use (such as apt-get to install software) may not be available on your system. You may have a different terminal, and different package manager which is invoked via a different command and syntax. If you want to follow this tutorial, you’ll have to translate those commands into ones appropriate for your OS. Just remember that the invocation of cmake and make should be the same in most circumstances.

The Video

Read on the for textual tutorial. I suggest that even those of you who prefer videos read this one, as we’ll be working primarily with the terminal: something which is undoubtedly easier to talk about in writing. Read more…

SFML 2.0 Prebuilt Binaries for Download

13.08.2011 3 comments

As you know, I’ve now started to provide prebuilt SFML 2.0 binaries, downloadable from Google docs. Previously you had to subscribe to the blog by email to get them. However, one or two of you weren’t too keen on this, so I’ve decided to place a link here.

If you’ve already subscribed by email to get them, don’t feel short changed. You’ll still come out on top. I intend to regularly update the binaries, and I’ll send email subscribers an email notification with the new link as soon as the files are online. This page will be updated as well, but I shan’t be able to do it as frequently. Also, of course, you won’t be reminded to download a new version if you’re not subscribed by email.

In fact, I’ll take this opportunity to remind you: to get prompt notification of updated SFML 2.0 binaries, including builds for different platforms, then subscribe to the blog by email if you haven’t already. Just enter your email address on the right and click ‘Sign me up’; if you’re logged in to WordPress, you probably won’t need to enter the email address.

However, if you really don’t want email notifications and prefer to subscribe via WordPress or using the RSS feed then fear not. You can download the binaries on my new downloads page – you just might get updates a little less frequently.

Disclaimer: All the files provided in these downloads are AS-IS. I cannot guarantee that they are suitable for purpose, and nor can I be held accountable or responsible for any damages caused by them, even if I was advised of such. Note also that SFML is developed by Laurent Gomila. I do not intend to imply by this service that I am in any way affiliated with him. I compile the binaries from his source and provide them to you merely as a convenience.

Prebuilt SFML 2.0 Binaries

04.08.2011 4 comments

I know some of you wanted some prebuilt SFML 2.0 binaries. All you have to do to get them is subscribe to my blog by email on the right (where it says ‘Sign Me Up’). Just enter your email address (don’t worry: it will be safe with WordPress) and I’ll email you a download link for a 7 Zip archive containing binaries for MinGW, Visual C++ Express 2008 and Visual C++ Express 2010.

I know it’s a pain to subscribe, but I’m only asking you to do it so I can get a good idea of the number of people interested in this service. And don’t just unsubscribe after – the SFML source changes frequently and I shall be sending subsequent emails every week or so with links to more up to date builds.

What will my email address be used for?

All I’ll be using it for is sending these emails and a few others regarding downloads, pages and maybe eventually about one or two SFML books I hope to write. if you unsubscribe, I’ll try my best to remove you from the mailing list within a week. Your email address will also be used by WordPress to send automated notifications when I post on my blog. That way you get to read my posts right away!

So go ahead and get those binaries now. Subscribe!

If you are already subscribed, and do not want to receive this email, just mail me at sfmlcoder@gmail.com to let me know!

SFML Graphics – Fonts and Text

01.08.2011 12 comments

In this tutorial, I’m going to tell you about another SFML graphics feature – text. By the end, you should be happy loading fonts from files, and using them to display text to the screen. We’ll also briefly discuss the manipulation of text as a graphics object. As we go along, I’ll make some analogies to SFML’s image handling features, to ease the learning process.

The Video

Working with text in SFML

Let’s get straight to it. Open up our Minimal Application project (you know – the one which just created an empty window and cleared it to a constant colour). If you don’t have the source code for this application at hand, you can find it with the corresponding tutorial.

Getting Started

So first, let’s just go ahead and display our first piece of text. Add the following line before the start of your main loop.

sf::Text Text("SFML Coder");

This constructs a text object which contains the text ‘SFML Coder’. Now we just need to display it. Add the following code after the call to sf::Window::Clear(sf::Color&), but before sf::Window::Display(void).

Window.Draw(Text);

And that’s it. Run your app and you should get some text!

But that’s not really it, is it? ;) Sure, we displayed some text, but we should be able to do a whole lot more. Maybe you want to change the font face or size. Maybe you want to scale the text, or apply a colour to it. Maybe you want to apply other transformations to it, such as rotation and translation. Well you can do all these things, and we’ll see how next.

Custom Fonts

Loading a Font

Remember when we worked with images, SFML had two classes: the lightweight and easily copiable sf::Sprite and the resource heavy, slow-to-load sf::Image? Well it’s the same with text. sf::Font does the heavy lifting – loading ‘glyphs’ from font files into memory, while sf::Text worries about drawing things to the screen, without managing the actual pixel data.

Note that this means it’s not advisable to load fonts on the fly and copying of fonts should be avoided where possible. On the other hand, sf::Text can be both copied freely and created mid frame without performance issues.

So. Let’s see how to load a font now. Add this code before you enter the main loop. We’ll create an object of type sf::Font and load a font into it, from the file font.ttf. You’ll need to provide the file font.ttf in the project directory to run your application. Just grab something from your system fonts folder, copy and paste it, and rename it.

sf::Font Font;
if (!Font.LoadFromFile("font.ttf"))
  return 1;

sf::Font‘s default constructor doesn’t do much. The real work happens in sf::Font::LoadFromFile. This function is similar to sf::Image::LoadFromFile: they both return false on failure and true on success, and they both take a single string filename to specify the file from which to load. On failure, we return 1 to end our program signifying failure to the thing that started it (probably the operating system).

Text: The Full Constructor

In fact, sf::Text has a few more parameters in its constructor than we used last time. We passed just one: a constant reference to a string. Now we’ll add an additional two!

sf::Text Text2("SFML Coder", Font, 40U);

The second argument we pass is a reference to a font object. Note that this argument defaults to sf::Font::GetDefaultFont() which returns a reference to a default, inbuilt font which SFML has (Arial). The third argument is an unsigned integer to specify the size of the font. 30 is used by default. (Note the trailing U marks the integral literal 20 as unsigned).

Now replace the line to draw the previous font with this:

Window.Draw(Text2);

And you’ll see that the text is now smaller, and also in whatever font you provided in the directory.

Controlling Text Attributes after Construction

So an object of type sf::Text has a size, a font and a textual string associated with it, which could be set during construction. Such an object also has a style (bold, italic, underlined, normal, or a combination thereof). This cannot be set by the constructor, but it can be accessed afterwards via get/set methods. Indeed the other three things (the text string, the size and the font) can also be manipulated using get/set functions:

  • to access the font (face): sf::Text::GetFont() and sf::Text::SetFont(const sf::Font&)
  • to access the font size: sf::Text::GetCharacterSize() and sf::Text::SetCharacterSize(unsigned)
  • to access the text string: sf::Text::GetString() and sf::Text::SetString(const sf::string&)
  • to access the style: sf::Text::GetStyle() and sf::Text::SetStyle(unsigned long)
Note that all the text styles are located within the enumeration sf::Text::Style. For example, let’s create a third sf::Text, using it’s default constructor and then setting it’s properties using set methods.
sf::Text Text3;
Text3.SetFont(Font);
Text3.SetCharacterSize(20U);
Text3.SetString("http://www.youtube.com/sfmlcoder/");
Text3.SetStyle(sf::Text::Bold | sf::Text::Underlined);

Manipulating Text

Now sf::Text is derived from sf::Drawable, just like sf::Sprite. That means that all the manipulations we could perform on a sprite (translation, rotation, scaling and applying colours) can equally be applied to a text object. You can get and set its position using sf::Drawable::GetPosition() and sf::Drawable::SetPosition(const sf::Vector2f&) respectively. Similarly, there are functions for rotation and scaling, as well as relative transformations. Then you can use sf::Drawable::SetColor(const sf::Color&) to apply a colour to the text.

I shan’t say too much more on this subject, as I provided some examples in the Images and Sprites tutorial, and you can also read more here. Just remember, of course, that all these manipulations are applied to the sf::Text object and not the sf::Font object.

Here’s a brief example of possible manipulations applied to our last sf::Text object.

</p>

<pre>Text3.Move(400.0f, 300.0f);
Text3.Scale(2.0f, 1.8f);
Text3.SetColor(sf::Color(255, 0, 0));</pre>
<p style="text-align: justify;">

Complete Source Code

Below is the complete source code for this tutorial.

#include <SFML/Graphics.hpp>

int main()
{
	sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "SFMLCoder Tutorial - Text and Fonts");

    sf::Text Text("SFML Coder");

    sf::Font Font;
    if (!Font.LoadFromFile("font.ttf"))
        return 1;
    sf::Text Text2("http://sfmlcoder.wordpress.com/", Font, 40U);

    sf::Text Text3;
    Text3.SetFont(Font);
    Text3.SetCharacterSize(20U);
    Text3.SetString("http://www.youtube.com/sfmlcoder/");
    Text.SetStyle(sf::Text::Bold | sf::Text::Underlined);

    Text3.Move(400.0f, 300.0f);
    Text3.Scale(2.0f, 1.8f);
    Text3.SetColor(sf::Color(255, 0, 0));

	while (Window.IsOpened())
	{
		sf::Event Event;
		while (Window.PollEvent(Event))
		{
			switch (Event.Type)
			{
			case sf::Event::Closed:
				Window.Close();
				break;
			default:
				break;
			}
		}

		Window.Clear();
		Window.Draw(Text); // replace with Text2 or Text3 to display them instead
		Window.Display();
	}

	return 0;
}

Migrating to SFML 2.0

Many people have difficulty migrating from SFML 1.6 to 2.0. The fact is there are quite a few public interface changes and there are as yet no SFML 2.0 tutorials. I suggest you still use the 1.6 tutorials if you want to be guided through the learning process, and then make up the gap using either or both of the following

  • the SFML 2.0 documentation (note that the documentation on the site is also slightly out of date, but you can create your own using the source and Doxygen)
  • this thread delineating the changes in SFML 2.0
Hopefully, I can soon show you how to create the documentation yourself, using Doxygen.

Videos on the Way

02.07.2011 1 comment

Sorry that I’m getting further and further behind on the videos. I have to do the ones for NMake and MinGW, redo the first SFML project ones and also record ones for Video Modes and Windowing and (soon!) Text and Fonts. In the mean time, enjoy the textual tutorials, and I’ll get the videos to you as soon as I can.

The problem is that my video editor will only export 1080p videos in AVC H264 format, which results in >1GB files which take all day to upload. Therefore I’m looking to do the final render in Expression Encoder instead, which will mean the quality is better (fewer compression passes) and also the file size is smaller.

Until then
-Xander

Follow

Get every new post delivered to your Inbox.

Join 72 other followers