Fade in and out

Why would you need to fade in and out? Well, maybe because you want to create a smooth transition between two different scenes, display the credits, show the player dieing and so on. The good news is that the code inside this article is simple and doesn't use too much video memory (less than 3Kb). Don't use panels; they will need much more memory!

This is beginner's corner so the idea is simple: we create an entity, we place it in front of the camera and we set a variable alpha factor for it. Let's take a look at the code that creates the entity:

entity fader_ent
{
type = <fader.mdl>;
layer = 40;
alpha = 0;
pan = 90;
flags = transparent, visible;
view = camera;
x = 250; // 250 quants ahead of the view
y = 0;
z = 0;
}

I have used a model named fader.mdl and I have changed its pan angle to make sure that it covers the view. The entity is transparent and visible and it is placed 250 quants in front of the view. Now let's see the functions that create the fade in and fade out effects:

function fade_in()
{
while (fader_ent.alpha > 5)
{
fader_ent.alpha -= 5 * time;
wait (1);
}
fader_ent.alpha = 0;
}

function fade_out()
{
while (fader_ent.alpha < 95)
{
fader_ent.alpha += 5 * time;
wait (1);
}
fader_ent.alpha = 100;
}

on_i fade_in;
on_o fade_out;

The first function (fade_in) runs when we press the "i" key on the keyboard and it decreases fader_ent.alpha until its value is below 5, then sets it to zero (as if the entity wouldn't exist). Use this function when you have used fade_out, the screen is completely black and you want to return to the game. Function fade_out will darken the image, hiding the screen progressively; it increases fader_ent.alpha until its value becomes bigger than 95 and then sets is to 100 (completely dark). Start your level and press "o" to fade out and then press "i" to fade in. Add one of these lines of code to an action or function used in your game:

fade_in();

fade_out();

to fade in or out when you need to. Of course that you can change the color of the skin for the entity model to simulate blood, water, slime, etc. Take a look at the picture below to see an example that uses a fader model with a completely red skin.

Dust

You don't see too much dust in today's games, isn't it? All this computer power keeps the streets clean but maybe sometimes, when you are traveling in a desert, you could use some dust; if this is the case, read on.

The dust is created using particles that start at player's feet level and rise up, slowly fading away. Of course that you can add the same effect to your monsters or (even better) to your vehicles.

We begin with a starter function, a function that runs by itself at game start:

var pos1;
var pos2;
var start_pos;

starter init_dust()
{
while (player == null) {wait (1);}
while (1)
{
vec_set (pos1, player.pos);
wait (1);
vec_set (pos2, player.pos);
if (pos1 != pos2)
{
start_pos.x = player.x;
start_pos.y = player.y;
start_pos.z = player.z - 30;
effect (dust, 1, start_pos, normal);
}
}
}

The code waits for the player to be created and then we store the current player position in a var named pos1. We wait for a frame and then we store player's position again, this time in a var named pos2. There are two possibilities:
a) The player hasn't moved during the last frame. If this is the case, nothing happens;
b) The player has moved during the last frame. If this is true, pos1 and pos2 hold different positions so we set start_pos to a position close to player's feet and we start to emit particles using the function named dust:

function dust()
{
temp.x = (random(2) - 1) * 0.3;
temp.y = (random(2) - 1) * 0.3;
temp.z = (random(1) + 1) * 0.2;
vec_add (my.vel_x, temp);
my.alpha = 10 + random(5);
my.bmap = dust_pcx;
my.size = random(5) + 10;
my.flare = on;
my.move = on;
my.lifespan = 20;
my.function = fade_dust;
}

function fade_dust()
{
my.alpha -= 0.3 * time;
if (my.alpha < 0) {my.lifespan = 0;}
}

The dust has a random speed on x and y and a random but positive speed on z to make sure that the dust moves upwards. The bitmap used for the dust particles is dust_pcx and its transparency factor, as well as its size ranges from 10 to 15. The function that runs the particles (fade_dust) decreases their alpha until they are made invisible and then it removes them.

You can play with the size of the particle, its alpha factor, its speed and of course with the particle bitmap to create the dust that fits your needs. Oh, and press F7 several times to switch the camera to the side view if you want to be able to admire the dust.