Jump to content
  • Advertisement
Sign in to follow this  
Sandra-24

Why doesn't this work?

This topic is 4775 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

I made my hello quad code, but it doesn't draw anything but garbage. Can someone help me figure out why? I'm sure it's something very simple and stupid, I'm not advanced enough to screw up badly ;) Tips and criticism welcome. -Sandra
[source lang=c#]
public class GamePanel : Form
{
private Device device;
private VertexBuffer vertices;
private Texture texture;
protected PresentParameters presentParams = new PresentParameters();

public GamePanel()
{
SetStyle(ControlStyles.Opaque,true); // don't paint background

Initialize();
device.VertexFormat = CustomVertex.PositionTextured.Format;
texture = new Texture(device, new Bitmap(Image.FromFile(@"M:\Images\5.jpg")), Usage.Dynamic, Pool.Default);
}

public static void Main()
{
using(GamePanel game = new GamePanel())
{
game.Show();
game.Render();
Application.Run(game);
}
}

protected override void OnPaint(PaintEventArgs e)
{
// Since we don't use/need a render loop, just render when we need to paint the window
Render(); 
}

private void Initialize()
{
presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.DeviceWindow = this;
presentParams.PresentationInterval = PresentInterval.One;
presentParams.SwapEffect = SwapEffect.Discard;

// store our default adapter
int adapterOrdinal = Manager.Adapters.Default.Adapter;
// get our device capabilities so we can check
Caps caps = Manager.GetDeviceCaps(adapterOrdinal, DeviceType.Hardware);
CreateFlags createFlags; 

if(caps.DeviceCaps.SupportsHardwareTransformAndLight)
createFlags = CreateFlags.HardwareVertexProcessing;
else
createFlags = CreateFlags.SoftwareVertexProcessing; 

if(caps.DeviceCaps.SupportsPureDevice)
createFlags |= CreateFlags.PureDevice;

// create our device
device = new Device(adapterOrdinal, DeviceType.Hardware, this.Handle, createFlags, presentParams);

SetupDevice();
SetupCamera();
SetupVertices();
}

private void SetupDevice()
{
device.RenderState.CullMode = Cull.None;
device.RenderState.Lighting = false;
}

private void SetupCamera()
{
device.Transform.Projection = Matrix.OrthoLH(Width, Height, 0.0f, 1.0f);
device.Transform.World = Matrix.Identity;
device.Transform.View = Matrix.Identity;
}

private void SetupVertices()
{
vertices = new VertexBuffer(typeof(CustomVertex.PositionTextured),
4, device, 0, CustomVertex.PositionTextured.Format, Pool.Default);

CustomVertex.PositionTextured [] quad = (CustomVertex.PositionTextured[]) vertices.Lock(0, 0);

// Bottom Left
quad[0] = new CustomVertex.PositionTextured(-1, -1, 0, 0,1);
// Top Left
quad[1] = new CustomVertex.PositionTextured(-1, 1, 0, 0,0);
// Bottom Right
quad[2] = new CustomVertex.PositionTextured(1, -1, 0, 1,1);
// Top Right
quad[3] = new CustomVertex.PositionTextured(1, 1, 0, 1,0);

vertices.Unlock();
}

protected void Render()
{
device.BeginScene();

// Draw the quads
device.SetTexture(0, texture);

device.SetStreamSource(0, vertices, 0);
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

device.EndScene();
device.Present();
}
} // end class

Share this post


Link to post
Share on other sites
Advertisement
I don't know much about C# or managed DirectX, but I think you're forgetting to clear the back buffer and depth buffer...

If that doesn't help/is not the problem then can you explain what kind of garbage are you getting?

Share this post


Link to post
Share on other sites
Like Kamikaze15 said, you're missing Clear() from the beginning of the frame. Also, you're not setting correct FVF. Insert this line before device.SetStreamSource():

device.FVF = CustomVertex.PositionTextured.Format;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!