Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SFML Game Development.pdf
Скачиваний:
209
Добавлен:
28.03.2016
Размер:
4.19 Mб
Скачать

Making a Game Tick

Through the words in this book, we will attempt to convey the best knowledge we possibly can. We aim to teach techniques that we learned along the years, techniques that we would like to have been told about in our early days of game development. We now write this book to save your time, by showing you directly the solution to common problems, and why things work the way they do.

Alongside our attempt to teach the basics and a little beyond game development, we will especially focus on the Simple and Fast Multimedia Library (SFML). Every part of this book will be about developing a game and leveraging the advantages that SFML is able to provide us. To spice things up a little, and since we use the C++ programming language, we will try our best to use the language in a modern way, applying more recent language features, techniques, and programming styles, in a fully object-oriented approach. C++ is a great tool and it always has been, however, it is a good thing if we grow with it and adapt to the possibilities it has to offer in the present day.

This chapter introduces the SFML library and shows you its capabilities by creating a small application. We are going to address the basic concepts relevant to game development, namely; rendering, game loops, and code organization. Furthermore,

the first part of our game code developed, will serve as a basis for the coming chapters.

Introducing SFML

Before we start developing a game, we would like to tell you a little bit about the library we will use throughout the book. SFML is an object-oriented C++ framework. As can be guessed by the name, its philosophy consists of having a

simple, user-friendly application programming interface (API), and allowing for both high performances and fast development.

www.it-ebooks.info

Making a Game Tick

SFML is a multimedia library, meaning that it provides a layer between you and the hardware. It is split into five modules:

System: This is a core module upon which all other modules are built. It provides two-dimensional and three-dimensional vector classes, clocks, threads, and Unicode strings, among other things.

Window: The Window module makes it possible to create application windows, and to collect user input, such as mouse movement or key presses.

Graphics: This module provides all functionalities that are related to twodimensional rendering, such as images, texts, shapes, and colors.

Audio: SFML also offers a module to work with sound. When you want to load a music theme and play it on the computer's loudspeakers, this is the module you have to look for.

Network: Another medium SFML covers is the network, a more and more important part of our interconnected world. This module allows you to send data over LAN or the Internet; it also lets you work with protocols, such as HTTP or FTP.

If you don't need all the modules, it is possible to use only a part of SFML. We will cover every module in SFML, but of course we are not able to use every single class. We recommend having a look at the SFML documentation, which is available at www.sfml-dev.org/documentation.php. The documentation explains every class and function in a detailed manner, and is an invaluable tool when developing a game using SFML.

SFML is open source, which means that you have access to its complete source code. Usually, the implementations aren't relevant to the user, but if you are interested in how something was solved, don't hesitate to skim through SFML's code.

The library uses the zlib/libpng license, which is extremely permissive. You can use SFML in both open and closed source projects, both free and commercial.

Downloading and installation

There are two possibilities when using SFML: download the pre-built libraries, or recompile them yourself. The first option is simpler, but you have to wait for major versions (2.0, 2.1, and so on) to be released. If you want to use the latest development sources, you can download the current Git revision. The configuration software

CMake is used to prepare the sources for compilation with a compiler of your choice. For example, CMake creates Visual Studio solutions or g++ Makefiles. The recompilation process is explained in detail in the SFML tutorials, which can be

found at www.sfml-dev.org/tutorials.php.

[ 8 ]

www.it-ebooks.info

Chapter 1

As mentioned, SFML is split into five modules. There are five headers to include a complete module (and its dependencies). To include the whole Audio module, you can write:

#include <SFML/Audio.hpp>

On the other hand, if you need a specific header file, you can find it in the directory of the corresponding module:

#include <SFML/Audio/Sound.hpp>

Each module is compiled to a separate library, which makes it possible to use only the modules you need. SFML can be built for release or debug mode, and it can be linked statically or dynamically. The resulting libraries are named according to the scheme sfml-module[-s][-d]. The -s postfix is required if you link statically; the -d postfix specifies debug mode. For example, to link the Graphics module statically in release mode, you have to specify the library sfml-graphics-s in your linker options. Depending on your compiler, a file extension (such as .lib) might be necessary. Keep in mind that some modules depend on others; therefore, you have to link the dependencies too. For example, Graphics depends on Window, which depends on System; therefore, you should link the three (in this order).

An important point to note is that if you link SFML statically, you have to define the macro SFML_STATIC in your projects, so that the linker knows what functions to resolve.

In case you do not know how linking a library works for a specific compiler, please refer to the online tutorials. They explain how to install everything correctly, and are always up-to-date.

A minimal example

Before you go deeper into the book and SFML itself, let's take a look at a minimal application example to show how an application that uses this library looks like, its general flow of execution, and some basic functionality.

#include <SFML/Graphics.hpp>

int main()

{

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

sf::CircleShape shape; shape.setRadius(40.f); shape.setPosition(100.f, 100.f);

[ 9 ]

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]