banner



Is Smfl Hard To Learn

So in this very quick tutorial, permit's learn well-nigh the basics of the mighty OpenGL-powered SFML Graphics Library.

What is SFML?

But put SFML is a multimedia library for C++ with bindings available for other languages such every bit Python, Rust, etc. It does not simply let you utilize hardware-accelerated 2D Graphics with OpenGL but too has a variety of methods related to dissimilar types of media such as fonts, audio, etc. It stands for Unproblematic and Fast Multimedia Library. Well, how elementary is it? And so elementary that y'all could become a snake game running in less than fifteen minutes.
How fast is it?
It's so fast that y'all'd be running the application at several k frames per second.

Where is SFML used?

SFML is used heavily in making games and fifty-fifty game engines. And even though SFML doesn't support 3D rendering all the same it is used for context cosmos. Since OpenGL can't create a window, 3D graphics programmers employ SFML (at least most of the time) for the task.

Setting upward Environment

Earlier we program in SFML we must set the environment! Hopefully, it'southward not a much of a headache especially not if y'all are in Linux! Debian Linux users (and any other flavour of Linux with apt/apt-get) can install SFML with the following command:-

sudo apt-get install libsfml-dev        

Windows users can download SFML from this link and follow the guide to install information technology.

Getting started with SFML

Here'south the most bones SFML awarding y'all'll ever find:

#include <SFML/Graphics.hpp>

int principal()

{

sf::Window window(

sf::VideoMode(640, 480),

"Hello World" );

return 0;

}

Now to compile the program – since SFML is a dynamically linked library 1 needs to link the program to the library. This step depends on what component of SFML one is using. Since we are using Graphics.hpp we say:-

thou++ main.cpp -lsfml-graphics -lsfml-window -lsfml-system

Assuming the source filename you lot used main.cpp! The footstep will be dissimilar if someone uses Visual Studio. You can refer here to see how to compile a SFML project with Visual Studio.

Now once you lot run the program you may notice that the window is created but information technology vanishes automatically. This is because our commuter office master creates a window and in one case it is created it moves to the second statement which is return 0; and hence exits the program! So what nosotros demand is a basic loop that will go along running as long every bit the user doesn't close the window.

So allow'south refactor our lawmaking and this time understand line-by-line what is really going on:

#include <SFML/Graphics.hpp>

int master()

{

sf::Window window(

sf::VideoMode(640, 480),

"Hello World" );

while (window.isOpen()) {

sf::Upshot event;

while (window.pollEvent(result))

if (effect.blazon ==

sf::Event::Closed)

window.close();

}

return 0;

}

Before running the program permit'due south understand what's going on!
Explanation

  1. The first argument creates a window object using the Window constructor. In this, nosotros pass the videomode and title of the window. And by the mode, VideoMode is just a type that defines the dimensions of the window (and bits per pixel just nosotros don't demand that now). And besides note that we are using 'sf' namespace as all the SFML classes and Methods are defined under this grade.
  2. In the 2nd statement we have some new logic. We are basically running a loop as long as the window is open. Turns out the Window grade provides a method isOpen which returns whether or not is a window open up. Now inside of this loop we check for events. Nosotros create an event object which and so we subsequently pass in window.pollEvent. What pollEvent method basically does is it checks if the the event queue is not empty. Information technology updates outcome with the adjacent consequence in the queue and then in the issue loop nosotros check if the event was not sf::Event::Closed which is just a cryptic mode of asking SFML did the user X..ed the window? By default nothing happens when the user hits the shut button, so we override this behaviour by closing the window.

Note that endmost the window doesn't terminate the program! Just since our window is now closed the isOpen method will now render fake and we volition render out of what's called – the chief loop of the program!

At present if you run the program you become a weird looking window (as shown beneath).

Sure you can close it but information technology looks weird, the previous contents of the screen are mapped to it and look super weird. This is happening because we are not clearing the window!!

In the adjacent example, nosotros are going to do just that (clearing the window) and also draw shapes to the screen? Sounds fun! Allow's get started-

Rendering Uncomplicated Shapes with SFML

SFML is not all about window-creation (even though many people employ it only for that). Information technology tin can be used to render hardware-accelerated graphics (including some basic likewise every bit complex shapes). In this case, we'll constrain ourselves to describe a basic shape (a circumvolve):-

#include <SFML/Graphics.hpp>

int main()

{

sf::RenderWindow window(

sf::VideoMode(640, 480),

"Hello World" );

sf::CircleShape shape(200);

while (window.isOpen())

{

sf::Event upshot;

while (

window.pollEvent(consequence))

if (consequence.type ==

sf::Event::Closed)

window.close();

window.clear();

window.draw(shape);

window.display();

}

return 0;

}

This would produce the following output:-

Caption:

  1. Note: RenderWindow has been used instead of Window.
    This is particularly because we are rendering something in the window. The Window form is really meant for OpenGL programmers or anyone who simply wants to create and draw a window! If we have to render something on the window we have to use RenderWindow class. The fundamental difference between the two is simply this that 1 is meant for simply creating a blank window for context-cosmos and other is specifically for second rendering which you tin't use exterior of SFML.
  2. Moving on from RenderWindow, in line 06 we create a circle shape and pass in a radius of 100. Now exactly after the consequence loop nosotros clear the contents of the window. This is important otherwise the circumvolve is drawn in the previous frame (previous iteration) would still be there and if we accept a moving circumvolve it'd accept the effect of creating a lot of circles! And so we clear the screen to erase the stuff that was drawn in the previous frame and in the fresh new screen we draw our circle shape! Note that since we didn't set the position of the circle it'd be defaulted to (0, 0). After drawing the shape nosotros take to display the contents of the window. Why this is important is because the shape is first drawn not on the window merely on a blank empty sheet, and while SFML is drawing on this empty canvas it shows the contents of the previous iteration. And when the procedure of drawing on the canvas is completed, it flips the windows contents with the newly created canvas. This entire process is called double buffering if you are curious.

Advantages of SFML

The advantages of SFML over other multimedia libraries are:-

  1. SFML is free and open up-source (unlike DirectX which is airtight source or Metal which is proprietary)
  2. SFML is cross-platform It tin run in Windows, Linux/Unix, Mac and experiments are already going on to go far portable with Android and iOS (Compared to other graphic libraries such as Windows API, etc which only support a single platform)
  3. SFML is super-fast Your application will about of the time run at several thousand frames per second compared to some graphic APIs which fail to attain even 500fps!
  4. SFML is multi-language In this article we used C++ merely yous could accept created the same thing using Python or fifty-fifty GoLang!! That is to say that SFML provides official bindings for several mainstream languages. Virtually Graphic APIs hardly provide more than 1 language-bounden whereas SFML provides bindings for over a dozen of dissimilar programming languages!!

Disadvantages of SFML

There are also some disadvantages of SFML that come along the way:-

  1. SFML cannot do 3D! SFML is strictly written to provided depression-level hardware-accelerated 2D graphics rendering. For 3D you'll perhaps take to use it in combination with OpenGL
  2. No firm-support for Android and iOS! Even though experiments are going on yet currently there is no such house support for Android, iOS or other mobile platforms

Further Reading

Now I'd end this article. I promise you learned a thing or to. Here are some books that yous can read to larn more than in SFML

  • SFML Blueprints
  • Mastering SFML Game Development

    Of course the original sfml wiki is charm, you tin can refer there besides. Also the people at the forums are very helpful and will gladly help you if you lot have whatever doubt. I wish you skillful luck in your journey of learning SFML.

    You tin can also read more nigh the dynamic libraries (and in what aspect practice they differ from their static counterparts) in this post

    References

    https://www.sfml-dev.org/tutorials/two.0/start-linux.php
    https://www.sfml-dev.org/tutorials/2.0/window-window.php
    https://world wide web.sfml-dev.org/tutorials/ii.0/graphics-shape.php


  • Is Smfl Hard To Learn,

    Source: https://www.geeksforgeeks.org/sfml-graphics-library-quick-tutorial/

    Posted by: ortegaandutimmose.blogspot.com

    0 Response to "Is Smfl Hard To Learn"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel