Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon
14. September 2015

 

In this Closer Look At we are going to be taking a closer look at the SFML framework.  The Closer Look At series is a combination of an introduction, overview and getting started tutorial that is designed to help you quickly decide if a game engine/framework/library is right for you.  SFML stands for Simple and Fast Multimedia Library.  It’s a cross platform, open source, C++ based collection of libraries handling 2D graphics, input, audio and networking.

sfmlBanner

 

An HD video of this guide is available here or embedded below.

 

Meet SFML

 

SFML itself is not technically a game engine, it contains no concept of a scene graph, there are no tools, no level editor, etc.  Instead it provides the base technical building blocks your build your game or game engine on top of, including platform agnostic system classes, input, audio, networking and 2D graphics.  The library itself is very modular in nature and you only use what you need.  In terms of scope and functionality it is very similar to LWJGL, Allegro or SDL. 

 

SFML is written in C++ and the source is hosted in this Github repository.  It uses the cross platform CMake build system enabling you to create project files for your platform and IDE of choice.  There are language bindings available for many other languages including:

  • C
  • .NET (C#, VB.Net, F#, Managed C++, etc.)
  • D
  • Java
  • Python
  • Ruby
  • Rust

The full selection of language bindings is available here.  Note however, on the C and .NET bindings are officially supported, the rest are developed or contributed by the community and may be incomplete or lag behind the current release.

 

SFML is composed of several modules, available in static and dynamic forms and compiled in debug or release format.  The modules are:

  • system handles system level tasks in a cross platform manner, items such as threading, timers and strings
  • window native window handling, as well as input handling (polled and event driven mouse, keyboard, touch and gamepads)
  • graphics 2D graphics, sprites, textures, shapes, etc.  built over OpenGL
  • audio audio playback and recording as well as positional audio
  • network TCP and UDP based networking as well as FTP and HTTP implementations

 

Your application or game has to always include the system module as all the other modules depend on it, but all others can be used independently.  For example, if you wanted to use SFML for gamepad support, you could simply link system and window.  It is actually quite common to use SFML in place of libraries like GLut for handling window creation and input handling in a OpenGL 3D application, ignoring the graphics, audio and networking functionality available.  It should be made clear SFML is not and never will be a 3D library, although it can be used in conjunction with 3D libraries and is built over OpenGL.

 

SFML has traditionally been for desktop operating systems only, which I believe hindered it’s adoption to a degree.  However as of version 2.2, there has been support for Android and iOS targets as well.

 

Developing with SFML

 

Since SFML is a code only solution, I suppose it makes sense to jump right in with the coding experience eh?  Installation is a breeze, assuming your compiler is supported out of the box and you have experience setting up a C++ project.  Configuring the linker, working directory, etc.  are all huge potential tripping points for new developers.  I walk through the entire process for Visual Studio 2013 in the video if you need help.  If your compiler isn’t supported out of the box, you will have to build it from scratch using CMake.  I actually documented this process in the past, on both Windows and MacOS with CLion if you want more information.

 

Ideally though, you will just use the precompiled binaries available here(2.3.1 link).  Simply select the combination that is right for you.  If in doubt and running on Windows, select the 32bit version.

image

 

Simply extract the libraries and configure your IDE or makefile to point to the include and lib files, while the required DLLs are in the bin directory and need to be copied into your application folder or somewhere on your system’s path.  This process is beyond the scope of this guide, so if you want more details be sure to watch the video.    There are however getting started tutorials for Windows, Code Blocks, Linux, XCode/Mac and using CMake.  The Windows tutorial is solid, although has a few problems that could trip up new users.  ( No mention of requiring the DLLs for a dynamic build, while defining the process for static linking, but not actually setting up the appropriate libraries ).  Frankly if you are new and are going to have trouble with SFML, this is probably the point that it will happen.  It’s not really SFML’s fault, the downside of using a language with a compile/link process from the 1970s.

 

Now let’s an extremely simple SFML application.

#include "SFML/Graphics.hpp"

int main(int argc, char** argv){
  sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Hello Cruel World");

  while (renderWindow.isOpen()){
    sf::Event event;

    while (renderWindow.pollEvent(event)){
      if (event.type == sf::Event::EventType::Closed)
        {
          renderWindow.close();
        }
      }
      renderWindow.clear(sf::Color::White);
      renderWindow.display();
    }
  }
}

 

This is about the simplest SFML application you can create and for good reason, it does almost exactly nothing!  When you run it, it simply creates a main window 640x480 in size, like so;

image

 

It does however show you the very basics of a typical SFML game loop.  The application starts up, creates a RenderWindow object, then loops forever or until that window is closed.  Each pass through the loop it checks to see all the events that have occurred since that last pass through the loop.  Finally it clears the screen and renders the results.  If you’ve ever seen a game loop before, this should look immediately familiar.

 

Now let’s change things up a bit so it actually does something useful.  I think it’s about time for a Hello World example!

#include <iostream>
#include "SFML/Graphics.hpp"


int main(int argc, char** argv){
  sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Hello Cruel World");

  // Create a font object and load it from file relative
  sf::Font font;
  if (!font.loadFromFile("calibri.ttf")){
    return 42; // Robust error handling!
  }
  
  //Create Hello World text objecct using our font and size 128pt
  sf::Text text("Hello World", font, 128);

  //Set the text color to red 
  text.setColor(sf::Color::Red);

  //Get the text object's physical dimensions and use them to center the text to 
  our render window
  //By default things are drawn relative to their top left corner and can be 
  changed by calling setOrigin()
  sf::FloatRect bounds(text.getLocalBounds());
  text.setPosition(renderWindow.getSize().x/2 - (bounds.left + bounds.width/2), 
          renderWindow.getSize().y/2 - (bounds.top + bounds.height/2));

  while (renderWindow.isOpen()){
    sf::Event event;

    while (renderWindow.pollEvent(event)){
      if (event.type == sf::Event::EventType::Closed)
      {
        renderWindow.close();
      }
    } 
    renderWindow.clear(sf::Color::White);
    //Draw our text object to the window
    renderWindow.draw(text);
    renderWindow.display();

  }
}

 

In this example we draw the text Hello World centered to our screen in red.

image

 

The code is fairly extensively commented but gives you a better idea of how things work in SFML.  One thing to be aware of, I had to copy the ttf into the working directory.  If the font fails to load, it most likely isn't in the correct place.  By default the origin (0,0) is the top left corner, both of the window and of the object, but can be changed if you prefer.

 

This example shows two key concepts in SFML, resource loading and Drawables.  The Font resource is loaded using it’s method loadFromFile(), although other options exist for loading.  These calls are expensive and the resulting object is heavy, meaning you should be careful in where you do it and how long you keep the resource around.  The sf::Text object on the other hand is rather light weight, using far less resources.  It is possible to share a single sf::Font with multiple sf::Text objects. 

 

The other thing of note here is the sf::Text class which derives from two key classes, sf::Drawable and sf::Transformable.  The one class gives the Text object the ability to be drawn to the RenderWindow, while the Transformable class gives it spatial properties, enabling the option to be transformed and positioned.  These two classes are inherited by several key objects in SFML including sf::Text, sf::Shape and sf::Sprite.

 

Speaking of Sprite’s, let take a look at loading and displaying graphics as well as handling input in our next example:

#include "SFML/Graphics.hpp"


int main(int argc, char** argv){
  sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Hello Cruel World");
  sf::Texture texture;
  if (!texture.loadFromFile("GFSLogo.png")){
    renderWindow.close();
  }

  sf::Sprite sprite(texture);
  sprite.setPosition(0, 0);

  while (renderWindow.isOpen()){
    sf::Event event;

    while (renderWindow.pollEvent(event)){
      if (event.type == sf::Event::EventType::Closed)
      {
        renderWindow.close();
      }

      if (event.type == sf::Event::EventType::KeyPressed)
      {
        // Event driven input handling
        if (event.key.code == sf::Keyboard::Left)
          sprite.move(-1, 0); //Relative transform
        if (event.key.code == sf::Keyboard::Right)
          sprite.move(1, 0);
      }
    } 
      // Polled input handling -- mouse coordinates are in screen space, not 
      window space
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        sprite.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(
                           renderWindow)));//Absolute transform

      renderWindow.clear(sf::Color::White);
      renderWindow.draw(sprite);
      renderWindow.display();

  }
}

 

When you run this example you see your image on screen, like so:

image

 

You can move the image left and right one pixel at a time using the left and right arrow keys.  If you hold down the spacebar the image will move along with the mouse cursor.

 

As you can tell, the functionality between using sf::Text and sf::Sprite are very similar and follow a similar pattern.  The Resource itself ( the Font in the earlier example, the Texture in this example ) are heavy weight objects and loaded at start up.  The instance though, in this case the Sprite, is a much lighter weight object that uses the resource.  Due to the common parent’s, the code looks virtually identical between the two objects.

 

Here you also see the two ways you can handle input in SFML.  Can handle it in an event driven manner, responding to Input events inside your game loop.  You can also poll the device directly to see what it’s status is.  Both are comparable ways to work, simply pick the one that fits your work style the best or mix and match as need.

 

This example also shows how Transformable’s can be positioned in multiple ways.  In response to the arrow keys, we move our sprite relative to it’s previous position using the move() method.  Later we set the position directly using the setPosition() method.  There are several other options available including matrix transforms.

 

Our final example is going to show how to use a GLSL vertex and fragment shader with SFML.

 

Here are the shaders:

shader.frag

uniform sampler2D texture;
uniform float opacity;

void main()
{
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);

    gl_FragColor = pixel * vec4(1.0,1.0,1.0,opacity); 
}

shader.vert

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

    gl_FrontColor = gl_Color;
}

And finally our C++ code:

#include <SFML/Graphics.hpp>

int main(int argc, char** argv){

  sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Hello Cruel World");
  
  sf::Texture texture;
  if (!texture.loadFromFile("GFSLogo.png"))
    return 42;

  sf::Sprite sprite(texture);
  sprite.setPosition(0, 0);

  sf::Shader shader;
  if (!shader.loadFromFile("shader.vert", "shader.frag"))
    return 42;
  
  float opacity = 1.0f;

  shader.setParameter("texture", sf::Shader::CurrentTexture);
  shader.setParameter("opacity", opacity);

  sf::Event event;

  sf::Clock clock;

  while (renderWindow.isOpen()){
    while (renderWindow.pollEvent(event)){
      if (event.type == sf::Event::EventType::Closed)
        renderWindow.close();

      if (event.type == sf::Event::EventType::KeyPressed){
        if (event.key.code == sf::Keyboard::Left)
          sprite.move(-1, 0);
        if (event.key.code == sf::Keyboard::Right)
          sprite.move(1, 0);
      }
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
      sprite.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(
                         renderWindow)));
    }

    if (clock.getElapsedTime().asSeconds() > 0.1){
      opacity -= 0.05;
      if (opacity < 0.0)
        opacity = 1.0;

      clock.restart();
      shader.setParameter("opacity", opacity);
    }

    renderWindow.clear(sf::Color::White);
    renderWindow.draw(sprite, &shader);
    renderWindow.display();
  }
}

 

When run:

sfml

 

Shaders are actually beyond the scope of this document, this does however illustrates it's fairly simple to load, pass parameters too and apply a shader in SFML.  There is slightly more details in the video version if you are interested.  Also for more information on GLSL check here.  One thing to be aware of however, shader support on iOS and Android is currently confined to OpenGL ES, which uses the fixed function pipeline, meaning no shader support.  I believe this is being addressed in future versions.

 

Functionality and 3rd Party Libraries

 

These are relatively minor examples, but they all give you a pretty good idea of what the coding experience is like and the consistency you should expect.  For the most part, SFML is a straight forward library that becomes intuitive very quickly.  In addition to what we have covered here, SFML also provides support for:

  • Sprite/Sprite Sheets
  • 2D Geometrics such as Circles and Rectangles
  • Render to texture
  • Vertex and Pixel Shaders
  • Joystick, Keyboard, Mouse, Touch and Sensor Events
  • UDP/TCP Networking
  • HTTP and FTP implementations
  • Audio Playback and Recording
  • Spatial Audio
  • Music Streaming
  • Threading and Mutex support in cross platform manner
  • Window creation and handling
  • Cross platform string with Unicode support
  • Math functionality including Matrix support
  • And More

 

SFML is focused on providing the underpinnings of a 2D game in a cross platform manner.  It however is not a full game engine, although there exist several community libraries build over top of SFML to provide game engine like functionality. 

Some of these third party libraries include (but are by no means limited to):

 

Thor 

Extends SFML adding animation, advanced math, more complex shapes, particle systems, timers and more.

 

sfeMovie

Built over FFMPeg, it adds the ability to play movies.

 

tmxLoader

A tmx (tiled) loader.  Other tilemap libraries exist.

 

Let There Be Light

Dynamic lighting library.

 

Documentation and Community

 

One very strong area for SFML is the documentation.  The reference materials are exceptional, and for many developers are all you will need 95% of the time.  Rarely is detail glossed over and often there are good examples illustrating the usage.  Additionally there are clickable inheritance diagrams making navigating familial hierarchies a breeze.

There are also a comprehensive set of tutorials that are kept current with each new release.  Quality is a bit more variable than the reference material, but for the most part the materials are quite good.  For a beginner starting out they will find the occasion hole to fall in.

The community is active on both the English and French forums.  The active community is somewhat small, and can be a bit… harsh in their response to repetitive posts, but for the most part questions go answered fairly quickly.  SFML is popular among the indie and amateur communities, so finding support in other forums such as gamedev.net, reddit or stackoverflow is also an option.

 

Books

 

SFML has been around for a number of years and as a result a couple of books on the subject are available:

sfmlblueprintbooksfmlessentialsbookssfmlgamedevbook

 

There is another title, SFML Game Development by Example coming in November.  SFML Game Development is the only title I have read and it is a very good book.

 

Conclusion

 

If you are looking for a lower level 2D multimedia library, especially if you want to work with C++, SFML is a great choice.  It is very similar to SDL in what it provides, although I personally find the SFML API to be much more to my taste.  The code is very clean, consistent and well documented.  It was however refactored for SFML 2.0, breaking backward compatibility with many older examples and tutorial series. 

 

Android and iOS support are finally available but are in the early stages so may not be quite as solid or stable as desktop platforms.  New releases used to come at a snails pace, so if you aren’t comfortable building from source and depend on critical bug fixes, this can certainly be an issue.  Recently however there have been great improvements in this regard and a nightly build option is now available.

 

Just be realistic in what you expect from SFML.  It is not a turn key game engine, nor is it a 3D (or 2.5D)  library.  If you are looking for either thing, look elsewhere.  It is however a very solid, well documented and well supported cross platform media layer for building a 2D game or game engine on top of. 

 

The Video

Programming


4. September 2015

 

While on vacation I have been looking at the process of extending the Godot Game engine using C++.  Due to the vacation, this isn’t a proper tutorial, I may do one later on when I have access to video, etc…  There is pretty solid documentation of the process in this post on the Godot wiki beyond that however there is a huge lack of information.  So the following is mostly trial and error.

 

I worked entirely in Visual Studio for this example and am working from the most recent source from Github.  If you have trouble working in Visual Studio with Godot, be sure to check out this post.

 

We are going to have to create several files in the modules folder, but don’t worry many can be copy/pasted from an existing module.  I created a new module called modTest inside the modules folder, and the contents end up looking like:

 

image

 

Many I coped directly from gridmap, but I will show the entire contents below.

 

config.py

def can_build(platform):
  return True
  
  
def configure(env):
  pass
  

 

SCSub

Import('env')

env.add_source_files(env.modules_sources,"*.cpp")


 

register_types.h

void register_modtest_types();
void unregister_modtest_types();

 

register_types.cpp

#include "register_types.h"
#include "object_type_db.h"
#include "modtest.h"
#include "modtest_editor.h"

#define TOOLS_ENABLED 1

void register_modtest_types() {
  ObjectTypeDB::register_type<ModTest>();

#ifdef TOOLS_ENABLED
  EditorPlugins::add_by_type<modtest_editor_plugin>();
#endif
}



void unregister_modtest_types() {


}

 

modtest.h

#ifndef MODTEST_H
#define MODTEST_H

#include "scene/2d/node_2d.h"

class ModTest : public Node2D {
  OBJ_TYPE(ModTest, Node2D);
};

#endif

 

modtest.cpp

#include "modtest.h"

 

modtest_editor.h

#pragma once

#include "tools/editor/editor_plugin.h"
#include "tools/editor/editor_node.h"
#include "tools/editor/pane_drag.h"


class ModtestEditor : public VBoxContainer {
  OBJ_TYPE(ModtestEditor, VBoxContainer);

public:
  ModtestEditor(){}
  ModtestEditor(EditorNode * p_editor);


private:
  VBoxContainer * container;
  Label* label;
  EditorNode* _editor;
};




class modtest_editor_plugin : public EditorPlugin
{
  OBJ_TYPE(modtest_editor_plugin, EditorPlugin);

public:
  modtest_editor_plugin(EditorNode *p_editor);
  ~modtest_editor_plugin();

  virtual void make_visible(bool isVisible);
  virtual void edit(Object *p_node);
  virtual bool handles(Object *p_node) const;

private:
  EditorNode * _editor;
  ModtestEditor* modtestEditor;
};

 

modtest_editor.cpp

#include "modtest_editor.h"
#include "tools/editor/plugins/canvas_item_editor_plugin.h"
#include "tools/editor/editor_settings.h"

#include "scene/main/viewport.h"

#include <iostream>


ModtestEditor::ModtestEditor(EditorNode* p_editor){
  std::cout << "New Modtest Editor" << std::endl;

  this->set_size(Size2(600, 600));
  label = new Label();
  label->set_text("Hello World");
  this->add_child(label);

  //p_editor->add_child(this);
  p_editor->get_scene_root()->add_child(this);
  _editor = p_editor;
  
  this->hide();
}


// **************************** PLUGIN BEGIN ********************************************

modtest_editor_plugin::modtest_editor_plugin(EditorNode * p_editor)
{
  _editor = p_editor;
  modtestEditor = memnew(ModtestEditor(_editor));
  
  std::cout << "Editor" << std::endl;
  
}


modtest_editor_plugin::~modtest_editor_plugin()
{
}


void modtest_editor_plugin::make_visible(bool isVisible){
  std::cout << "Make visible" << std::endl;
  if (isVisible){
    std::cout << "Showing" << std::endl;
    modtestEditor->show();
  }
  else{
    std::cout << "Hiding" << std::endl;
    modtestEditor->hide();
  }
}

void modtest_editor_plugin::edit(Object *p_object) {
  std::cout << "Edit" << std::endl;
}

bool modtest_editor_plugin::handles(Object *p_object) const {

  return p_object->is_type("ModTest");
}


So that was a mountain of examples, but nothing particularly complicated.

 

The SCSub and config.py files simply tell the Scons build system how to build your module, both of them can be copied in unchanged from the gridmap module.  It’s only if your module doesn’t build for every platform or you need to do something other than compile all the included cpp files that you need to alter either of these files.

 

Next up are the register_types cpp and h files, which registers your type with Godot.  ModTest is a simple Node2D extension type, it can be registered using ObjectTypeDB::register_type().  Additionally we have an editor we want to show when editing our new type, that is registered using EditorPlugins::add_by_type() .  The TOOLS_ENABLED means that you are building Godot with full tooling support, as opposed to building just a game (without the editor in the generated binary).

 

Our extension is completely pointless.  It’s inherited from Node2D and well, that’s about it.  Obviously you would add functionality as you go.  The simple fact it inherits from Node2D makes it available in Godot as a Node:

image

 

Next we register an editor and a plugin for our ModTest type.  The ModtestEditor class is going to be the editor displayed within Godot.  It’s a simple VBoxContainer.  In this case all we do is add a label with the text Hello World then add it to the editor using the passed in pointer to the EditorNode.  This is generally where the majority of logic will go.

 

Next up is the modtest_editor_plugin which inherits EditorPlugin.  This class is loaded with Godot by the call EditorPlugins::add_by_type<modtest_editor_plugin>().  The important call is modtest_editor_plugin::handles(), which attaches the editor to the type it edits.  In the constructor, we create an instance of our editor.  The make_visible() method is called each time the editor is needed, or no longer needed and toggles the visibility of our editor.

 

As you add or select a node of ModTest type, the editor will automatically be shown:

image

 

Obviously this is a barebones example.  As I implement a more detailed example I will share the results.

Programming


2. June 2015

 

So a lot has happened in the land of SFML since I created my SFML tutorial series and I decided to check out what’s new.  I happen to be on my Macbook, so Visual Studio wasn’t an option.  Time for a bit of a confession… I hate Xcode, especially for C++ development.  I just find it to be a downright unpleasant, disorganized, unintuitive mess and C++ is a third class citizen.  The alternatives such as Code::Blocks never really appealed to me, and Qt Creator always seems to make you jump through half a hundred extra steps.

 

Thankfully CLion, a cross platform C++ IDE from JetBrains, the makers of IntelliJ, was recently released.  So I decided to kill two birds with one stone…  check out what’s new with SFML and evaluate CLion all at the same time.  This involves setting up the development environment and probably the area where most new C++ game developers fall flat on their faces.

 

So, if you are interested in developing using SFML on Mac using CLion… this post is for you!  We are going to walk through installing SFML and configuring your very first project.

 

Installing SFML

First we need to download and install SFML.  There are two ways to go about this…  first you can head to SFML downloads page, download the Mac OS zip package, extra it, and copy the files to the locations specified in the readme file...

 

… or you can use homebrew.  This is what I did.  Assuming you have homebrew installed, at a terminal simply type:

brew update
brew install sfml


In theory this will download and install all the requisite parts of SFML.  In theory.  If it fails, go back to the manual process.

 

Creating a new project in CLion 

I’m going to assume at this point you’ve already got CLion installed.  If not, it’s available here and can be installed as a 30 day trial.

Once installed, fire up CLion, it’s time for us to create a new project.

 

 

Ss1

 

Select new Project

 

Ss2

 

Name however and wherever you wish, just be sure to remember the location.

 

SS3

In the Project panel, locate CMakeList.txt and double click it.  This file is basically your CMake based project file, which CLion uses for it’s project format, as does SFML.

 

CMakeList.txt will open in the editor, replace with the follow text:

 

cmake_minimum_required(VERSION 3.2)
project(SFMLDemo)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(SFMLDemo ${SOURCE_FILES})

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
find_package(SFML REQUIRED system window graphics network audio)
if (SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(SFMLDemo ${SFML_LIBRARIES})
endif()

 

If you’re going to screw up, this is the part you will screw up.  Let me point out the critical portions here.

 

Ss3 5

 

Make sure your project name is set correctly everywhere I boxed in red.  Also make sure your cmake_modules directory is set right...

 

Oh yeah, about that… make a directory called cmake_modules.  We need to copy this file (FindSFML.cmake) into that folder.  Be sure you save as Raw if downloading from Github and make sure the extension stays cmake, not cmake.txt as Mac OS is found of doing.  Or you could just create a new file called FindSFML.cmake in CLion and copy/paste the contents.  The choice is yours, but in the end your project should look like:

Ss5

 

 

Creating a Run Configuration

 

Now we are ready to run our application.  Select the menu Run->Edit Configuration...

 

Ss4

 

Be sure to set the working directory to you project folder ( or wherever you are going to put assets ), and chose your project as the executable, then hit Apply/OK.

 

You should now be able to run your application using the pulldown at the top of CLion, like so:

Ss6

Use the play icon to run, the bug icon to debug.

 

The default application should run and we should see:

Ss7

 

Now let’s try with some actual SFML code.  Replace the contents of main.cpp with the following:

#include <SFML/Graphics.hpp>

 

 

int main() {

 

    sf::RenderWindow window(sf::VideoMode(640,480,32),"Hello SFML");

 

    sf::Font font;

    font.loadFromFile("OpenSans-Bold.ttf");

 

    sf::Text text("Hello World",font,11);

    text.setCharacterSize(32);

    text.setPosition(window.getSize().x/2 - text.getGlobalBounds().width/2,

                     window.getSize().y/2 - text.getGlobalBounds().height/2);

 

 

    while(window.isOpen()){

 

        sf::Event event;

        while(window.pollEvent(event)) {

            if(event.type == sf::Event::Closed){

                window.close();

            }

 

            window.clear(sf::Color::Black);

            window.draw(text);

            window.display();

        }

    }

    return 0;

}

 

Finally we need the rtf file used in this example, I downloaded it from right here, and copied the file OpenSans-Bold.ttf to the root of our project directory. It is important this matches the run directory you specified earlier.  When run you should now see:

 

Ss8

 

 

There you go, a fully configured, really to run SFML project on MacOS, without an Xcode in sight!  There is one thing to be aware of though… right now the line:

find_package(SFML REQUIRED system window graphics network audio)

Is linking in every single SFML module, needed or not.


10. May 2015

 

 

Just noticed this on Twitter and it proved to be an interesting read.  JetBrains, the makers of IntelliJ, CLion, ReSharper and more, havebook sponsored this free book from O’Reilly Press.  It’s a 74 page book that combines a history lesson, modern introduction and Modern C++ overview all into one.  It’s free and a good read, what’s not to like?

 

 

 

Here is the Table of Contents:

 

1. The Nature of the Beast. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2. The Origin Story. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3. The Beast Wakes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4. The Beast Roars Back. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5. Digging Deep on Modern C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6. The Future of C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Bibliography. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

 

 

 

The book is available as a free PDF download directly at this link.  No other actions are required, simply download and read.

 

Even if you are just bored and want a bit of a history lesson and look to the future of C++, this is a good read.  Of course, if you are actually a C++ developer, it will be much more useful.

News


10. May 2015

 

SFML, the Simple and Fast Media Library, just release version 2.3.  SFML is a cross platform C++ based mostly 2D based game library built over top of OpenGL.  Although getting a bit long in the tooth, GameFromScratch created an SFML C++ game dev tutorial series if you are interested in learning more.

 

As to release 2.3, here are the announced changes:

 

SFML 2.3

General
  • Examples only link against sfml-main in release mode (#610, #766)
  • Replaced unsigned int with std::size_t for array indices and sizes (#739)
  • Fixed some issues with the Doxygen documentation (#750)
  • Added support for EditorConfig (#751)
  • Hide success message for CMake in quiet mode (#753)
  • Improved documentation for statuses with sf::Ftp (#763)
  • Moved stb_image into the extlibs directory (#795)
  • Changed the SOVERSION to major.minor (#812)
  • Fixed warnings about switch-statements (#863)
  • Added missing includes in the general headers (#851)
  • [Android] Updated toolchain file and dependencies (#791)
  • [Linux] Fixed missing pthread dependency (#794)
  • [OS X] Relaxed CMake installation rules regarding framework dependencies (#767)

 

Window
Features

 

Bugfixes
  • Fixed glXSwapIntervalSGI being broken for some driver implementations (#727, #779)
  • Fixed simultaneous context operations causing crashes on some AMD hardware (#778, #779)
  • Fixed joystick identification (#809, #811)
  • [iOS] Fixed various issues including stencil bits, device orientation and retina support (#748)
  • [iOS] Fixed inconsistency between sf::Touch::getPosition and touch events (#875)
  • [Linux] Fixed Alt+F4 not getting triggered in window mode (#274)
  • [Linux] Fixed Unix joystick stuff (#838)
  • [OS X] Fixed typo in JoystickImpl.cpp to prevent a crash (#762, #765)
  • [OS X] Fixed an issue in InputImpl::getSFOpenGLViewFromSFMLWindow (#782, #792)

 

Graphics
Features
  • Replaced GLEW with loader generated by glLoadGen (#779)
  • Added a new constructor to sf::Color that takes an sf::Uint32 (#722)
  • Updated stb_image to v2.02 (#777)
  • Updated FreeType to v2.5.5 (#799, #804)
  • Added checks for software OpenGL (#870)

 

Bugfixes
  • Fixed GL_ARB_compatibility not being detected (#859)
  • Fixed pixel format selection (#862)
  • Bumped back the OpenGL version requirement to 1.1 (#858)

 

Audio
Features
  • Dropped libsndfile and started using Vorbis, FLAC and OGG directly (#604, #757)
  • Added a FLAC file to the sound example (#815)

 

Bugfixes
  • Fixed access violation error in the destructor of sf::AudioDevice (#30, #602)
  • [OS X] Fixed threading issue with sf::SoundStream and OpenAL (#541, #831)

 

Network
Bugfixes
  • Fixed sf::TcpSocket not handling partial sends properly (#749, #796)

News


GFS On YouTube

See More Tutorials on DevGa.me!

Month List