Skip to content

Starfield

Hapaxia edited this page Oct 4, 2016 · 7 revisions

Introduction

A class to draw a simple parallax starfield.

Starfield creates a multitude of random pixel stars within a specified sized rectangle. The "view" of these stars can be "moved" by any 2D vector. The stars, which have different alphas depending on the depth/distance, are moved at a speed relative to this distance.
The starfield can be moved infinitely and the stars will continuously (but randomly) wrap. Note that this means as soon as the stars leave the field, they won't arrive back in the same formation.

Usage

Declaration

sw::Starfield starfield;
creates a starfield that has no size, the default amount of stars (100) and the default colour of light grey

sw::Starfield starfield(sf::Vector2f size);
creates a starfield that has the specified size (sf::Vector2f) with the default amount of stars (100) and the default colour of light grey

sw::Starfield starfield(sf::Vector2f size, unsigned int numberOfStars);
creates a starfield that has the specified size (sf::Vector2f) and the specified number of stars with the default colour of light grey

sw::Starfield starfield(sf::Vector2f size, unsigned int numberOfStars, sf::Color color);
creates a starfield that has the specified size (sf::Vector2f), the specified number of stars and specified colour (sf::Color)

Drawing

This class inherits from sf::Drawable so it is drawn in the same way as all SFML drawables:
window.draw(starfield);
where window is an sf::RenderWindow.

Transformations

This class inherits from sf::Transformable so it has all the usual SFML transformations available.

Manipulation

Set-up

  • regenerate(sf::Vector2f size, unsigned int numberOfStars)
    regenerates the stars with a new size and number of stars. Some of the parameters can be specified while leaving off others. A new colour cannot be specified in a regeneration as the colour can be set separately without the need to regenerate. The combinations allowed are:
    () (no parameters)
    (size)
    (size, numberOfStars)
    (numberOfStars)

Colour

  • setColor(sf::Color color)
    modifies the colour of all of the stars in the field to this colour. Note that the alpha channel is used for depth/speed so the alpha of the passed colour (sf::Color) is ignored.

Movement

  • move(sf::Vector2f)
    takes an sf::Vector2f and moves the stars in the direction of this vector. The "closest"/"brightest" stars are moved by this exact vector while others are moved less to give the impression of depth/distance

Simple Example

#include <SFML/Graphics.hpp>
#include <SelbaWard/Starfield.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(480, 270), "Starfield simple example");
    sw::Starfield starfield(sf::Vector2f(window.getSize()), 300u); // size of the window and 300 stars
    sf::Clock clock;
    const float speed{ 300.f }; // speed of movement
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        const float frameTime{ clock.restart().asSeconds() };
        starfield.move({ -speed * frameTime, 0.f }); // move stars left
        window.clear();
        window.draw(starfield);
        window.display();
    }
    return EXIT_SUCCESS;
}

The code above displays something similar to:
Simple Example
The example animates (scrolls with parallax) the starfield, which cannot be seen in the screenshot.

(Starfield v1.1)

Clone this wiki locally