Skip to content

Examples

Guillaume DEREX edited this page Oct 24, 2021 · 1 revision

Intro

This repository comes with a few examples to learn about the wrapper and also check if it compiles.

You can run an example with the build command zig build run-example_name, which will run the example code in src/examples/example_name.zig. Ex: zig build run-green_circle.

Green circle

green_circle.zig imitates the example provided on the SFML tutorial for compiling an SFML project. The equivalent C++ code is:

#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;
}

This example is just meant to show you how to create a window, handle some events and draw on it, but also to test it everything compiles well. Please note that it just imitates the code shown above in order to show how similarly the wrapper works ; it doesn't really show some good practices (like setting a framerate limit) or cool stuff (like switches for the event polling).

If you are confused about why some things are a bit different, please refer to [TODO].

SFML Example

sfml_example.zig imitates the code present in the main page of the [https://www.sfml-dev.org/documentation/2.5.1/](SFML documentation). It demonstrates creating a window, displaying a sprite with a texture, some text, and playing music. Like the previous example, it only imitates the c++ code.

The resources to run that example are provided in the repository.

Heat haze

heat_haze.zig is a reproduction of an example in the SFML github wiki showing a heat effect shader, by binary1248.

The example is a good way to remember how to use shaders in your project, set uniforms, etc.

The resources to run that example are provided in the repository.

Clone this wiki locally