Sign in to follow this  

Fading the screen

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I fade the screen to or from black with Managed Direct3D 9. I am relatively new to Direct3D (and C#, most of my previous programming was Java and C++) so example code would be appreciated.

Share this post


Link to post
Share on other sites
Greetings!

I was wondering how do they manage that. I never implemented it, but I believe you create a QUAD with 4 RHW (2D vertices) and change the ALPHA value in time. At the beggining it is transparent and it fades to white where it becomes opaque i believe.

I there is a better way, let me know!

Share this post


Link to post
Share on other sites
Here's the code I wrote, but it doesn't appear to work for me. If there's something wrong with it or if it needs me to set some renderstates beforehand or something, please tell me.

using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace foo
{
public enum FadeType
{
FadeIn,
FadeOut
}

public class Fader
{
private static VertexBuffer Verts;

private FadeType Type;
private int EndTime;
private int StartTime;

public Fader(FadeType type, int duration)
{
Type = type;
StartTime = Environment.TickCount;
EndTime = StartTime + duration;
}

public static void Initialize()
{
Verts = new VertexBuffer(typeof(CustomVertex.Transformed), 4, Graphics.Dev, Usage.WriteOnly, CustomVertex.Transformed.Format, Pool.Default);
Verts.Created += new EventHandler(OnVertexBufferCreated);
OnVertexBufferCreated(Verts, null);
}

private static void OnVertexBufferCreated(object sender, EventArgs e)
{
CustomVertex.Transformed[] v = new CustomVertex.Transformed[4];
v[0].X = 0;
v[0].Y = 0;
v[0].Z = 0.5f;
v[0].Rhw = 1;
v[1].X = 799;
v[1].Y = 0;
v[1].Z = 0.5f;
v[1].Rhw = 1;
v[2].X = 0;
v[2].Y = 599;
v[2].Z = 0.5f;
v[2].Rhw = 1;
v[3].X = 799;
v[3].Y = 599;
v[3].Z = 0.5f;
v[3].Rhw = 1;
GraphicsStream gs = Verts.Lock(0, 0, 0);
gs.Write(v);
Verts.Unlock();
}

public static void Terminate()
{
Verts.Dispose();
}

public bool Finished
{
get
{
if(Environment.TickCount > EndTime)
return true;
else
return false;
}
}

public void Draw()
{
if(Finished)
return;
Material m = new Material();
if(Type == FadeType.FadeIn)
m.DiffuseColor = new ColorValue(0.0f, 0.0f, 0.0f, 1.0f - ((float)Environment.TickCount - (float)StartTime) / ((float)EndTime - (float)StartTime));
else
m.DiffuseColor = new ColorValue(0.0f, 0.0f, 0.0f, ((float)Environment.TickCount - (float)StartTime) / ((float)EndTime - (float)StartTime));
Graphics.Dev.Material = m;
Graphics.Dev.SetStreamSource(0, Verts, 0);
Graphics.Dev.VertexFormat = CustomVertex.Transformed.Format;
Graphics.Dev.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
}
}
}

Share this post


Link to post
Share on other sites
There are many options for doing this. In one of my projects I just faded the lighting down to black. This is obviously not a reasonable solution in many situations, but the point is that if you can't get one method to work, ponder on it and find another.

Share this post


Link to post
Share on other sites

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this