📜 ⬆️ ⬇️

1.1 SFML and Visual Studio



Translator: This article is the first in the translation cycle of the official SFML library guide. This series of articles aims to provide people who do not know the original language the opportunity to get acquainted with this library. SFML is a simple and cross-platform multimedia library. SFML provides a simple interface for developing games and other multimedia applications. The original article can be found here . Let's start.

The SFML library provides a simple interface for various components of your computer to facilitate the development of games and multimedia applications. It consists of five modules: system, window, graphics, audio and network.
')
Using SFML, your application can be compiled and run on the most common platforms: Windows, Linux, Mac OS X (support for Android and IOS is planned).

Precompiled SDKs for your OS are available on the download page .

SFML officially supports C and .NET. Also, thanks to its active community, it is available in many other languages, such as Java, Ruby, Python, Go, etc.

Table of contents:
0.1 Introduction

1. Getting Started

  1. SFML and Visual Studio
  2. SFML and Code :: Blocks (MinGW)
  3. SFML and Linux
  4. SFML and Xcode (Mac OS X)
  5. Compiling SFML with CMake

2. System Module

  1. Processing time
  2. Streams
  3. Work with custom data streams

3. Window Module

  1. Opening and managing windows
  2. Event handling
  3. Work with the keyboard, mouse and joysticks
  4. Using OpenGL

4. Graphics Module

  1. Drawing 2D objects
  2. Sprites and textures
  3. Text and Fonts
  4. Forms
  5. Designing your own objects using arrays of vertices
  6. Position, Rotation, Scale: Transform Objects
  7. Adding special effects with shaders
  8. 2D camera control and view

5. Audio module

  1. Playing sounds and music
  2. Audio recording
  3. Custom audio streams
  4. Spitalization: sounds in 3D

6. Network module

  1. Socket communication
  2. Use and Expansion Packages
  3. Web requests using HTTP
  4. File Transfer Using FTP


Introduction


This article is the first one you should read if you are using the Visual Studio development environment (Visual C ++ compiler). It will tell you how to set up your project.

SFML installation


First you need to download the SFML SDK from the download page .

The package you downloaded must match your version of Visual C ++. For example, a library compiled using VC ++ 10 (Visual Studio 2010) will not be compatible with VC ++ 12 (Visual Studio 2013). If you do not find an SFML package compiled for your version of Visual C ++ on the download page, you will have to build SFML yourself .

Next you have to unpack the archive with SFML in any directory convenient for you. Copying header files and libraries into your Visual Studio installation is not recommended. It is better to keep the libraries in a separate place, especially if you intend to use several versions of one library or several compilers.

Creating and configuring an SFML project


The first thing you need to do is choose the type of project you are creating: you must select the “Win32 application”. The wizard will offer you several options for setting up the project: select “Console application” if you need a console, or “Windows application” otherwise. Select “Empty project” if you don’t need the automatically generated code.

Create a file main.cpp and add it to the project. This will apply the C ++ settings (otherwise, Visual Studio will not know which language we will use for this project). The contents of the main.cpp file will be shown below.

Now you need to tell the compiler where to look for header files (files with the .hpp extension) and the linker, where to look for SFML libraries (files with the .lib extension).

Add the following to the project properties:


These paths are the same for Debug and Release configurations, so you can set them globally for your project ("All configurations").



The next step is to build your application with the SFML libraries (files with the .lib extension). SFML consists of five modules (system, window, graphics, network and audio) and libraries for each of them. Libraries must be added to the project properties in Linker »Input» Additional Dependencies. Add the SFML libraries that you need, for example, “sfml-graphics.lib”, “sfml-window.lib” and “sfml-system.lib”.

It is important to specify the libraries corresponding to the configuration: “sfml-xxx-d.lib” for Debug and “sfml-xxx.lib” for Release, otherwise errors may occur.

The settings above allow you to build your project with a dynamic version of SFML, which requires DLL files. If you want to directly integrate SFML into your executable file, rather than using the build with a dynamic library, you must build a static version of the library. SFML static libraries have the suffix "-s": "sfml-xxx-sd.lib" for Debug configuration and "sfml-xxx-s.lib" for Release.

You also need to define the SFML_STATIC macro in the preprocessor options of your project.



Starting with SFML 2.2, with a static layout, you also need to build all SFML dependencies. This means that if, for example, you build sfml-window-s.lib or sfml-window-sd.lib, you also have to build opengl32.lib, winmm.lib and gdi32.lib. Some of these libraries may already be listed in the “Inherited values” section, but adding them should not cause any problems.

The table below shows the dependencies for each module, add -d if you want to build the SFML Debug library:
ModuleDependencies
sfml-graphics-s.lib
  • sfml-window-s.lib
  • sfml-system-s.lib
  • opengl32.lib
  • freetype.lib
  • jpeg.lib
sfml-window-s.lib
  • sfml-system-s.lib
  • opengl32.lib
  • winmm.lib
  • gdi32.lib
sfml-audio-s.lib
  • sfml-system-s.lib
  • openal32.lib
  • flac.lib
  • vorbisenc.lib
  • vorbisfile.lib
  • vorbis.lib
  • ogg.lib
sfml-network-s.lib
  • sfml-system-s.lib
  • ws2_32.lib
sfml-system-s.lib
  • winmm.lib

From the table, you may have noticed that some SFML modules may depend on others, for example, sfml-graphics-s.lib depends on sfml-window-s.lib and sfml-system-s.lib. If you produce the static layout of the SFML library, be sure that all dependencies from the dependency chain have been satisfied. If one of the dependencies is missing, you will get a linker error.

If you're a little confused, don't worry, it’s completely normal for a beginner to be overloaded with all this static binding information. If you don’t get it right the first time, you can try again with all the above in mind. If you still have problems with static binding, you can try searching for a solution in the FAQ section or on the forum .

If you do not know the difference between dynamic (also called common) and static libraries and what type of libraries to use, you can find more information on the Internet. There are many good articles / blogs / posts on this topic.

Your project is ready, let's write some code to check that everything works correctly. Place the following code in the main.cpp file:

 #include <SFML/Graphics.hpp> int main() { sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!"); sf::CircleShape shape(100.f); shape.setFillColor(sf::Color::Green); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(); window.draw(shape); window.display(); } return 0; } 

If you chose the “Windows application” option when creating the project, then the entry point to your program should be the “WinMain” function instead of the “main” one. Since this is Windows-specific, your code will not compile on Linux or Mac OS X. SFML provides a way to save the standard “main” entry point if you build your project with the sfml-main module (“sfml-main-d. lib "for Debug," sfml-main.lib "for Release) in the same way you put sfml-graphics, sfml-window and sfml-system.

Now compile the project, and if you linked your program with a dynamic version of SFML, do not forget to copy the files with the extension .DLL (they are located in <SFML installation path> / bin ) to the directory where your executable file is located . Run the program, and if everything was done correctly, you should see this:



If you use the sfml-audio module (regardless of its static or dynamic version), you should also copy the external dll OpenAL32.dll. This file can also be found in the <install-SFML-path> / bin directory .

Next article: SFML and Code :: Blocks (MinGW) .

Source: https://habr.com/ru/post/278977/


All Articles