Skip to content

Embed Texture

Maverick Peppers edited this page Dec 26, 2018 · 2 revisions

Sometimes your segue effects need extra sample textures.

If you're an advanced programmer, you can create a super-class of the ActivityController that has access to your media and simply share those resources with the segue in the constructor.

But if you want to share your segue with the public, shipping an extra file is not ideal. Solution: We can embed textures in the header file.

This is purely optional and if you want to make sharing your segues as easy as possible, is great practice.


In this tutorial, we will use our own checkerboard pattern.

One, download GIMP.

Two, open your source texture with GIMP.

Three, scale your texture down percent-wise as small as possible. If downsizing degrades the quality of the effect too much, you can skip this step.

Four, in GIMP, click FILE > EXPORT AS > Choose .c source code

Five, and the most important, CHECK RGBA/RGB. This will save our image in 32bit RGB that SFML uses.

Six, UNCHECK Glib types (guint8).

Seven, save. Open the file contents. You'll see a struct you can copy and paste into your segue like this:

    /* exported from GIMP */
    static const struct {
      unsigned int 	 width;
      unsigned int 	 height;
      unsigned int 	 bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
      unsigned char	 pixel_data[8 * 6 * 4 + 1];
    } checkerboard_raw_32bit_rgba = {
      8, 6, 4,
      "LKL\377\\\\]\377\276\276\276\377qqq\377dcd\377UWU\377\010\010\010\377---\377"
      "*)*\377\202\202\202\377uuu\377\016\016\016\377YXY\377\322\322\322\377>=>\377"
      "\060\060\060\377\206\206\207\377\037\037\037\377kjk\377\346\346\346\377hgh\377"
      "BAB\377ffg\377ZZZ\377EFE\377nnn\377VVW\377SSS\377|||\377\067\067\067\377\220"
      "\220\220\377\034\034\033\377\377\377\377\377ddd\377a`a\377IHI\377:;;\377\000\000"
      "\000\377xxy\377\021\022\021\377OPO\377&&&\377\247\247\247\377?>?\377\061\061\061"
      "\377###\377\025\025\025\377HHH\377",
    };

    // Create a raw image buffer with our data exported from GIMP
    sf::Image buffer;
    buffer.create(checkerboard_raw_32bit_rgba.width, checkerboard_raw_32bit_rgba.height, checkerboard_raw_32bit_rgba.pixel_data);

    // Load an sf::Texture with the image data
    pattern.loadFromImage(buffer);

    // Set the texture as one of our shader's uniform
    shader.loadFromMemory(MY_CHECKERBOARD_FRAG_SHADER, sf::Shader::Fragment);
    shader.setUniform("texture", sf::Shader::CurrentTexture);
    shader.setUniform("pattern", pattern);
Clone this wiki locally