Advertisement Jump to content
Sign in to follow this  

Why doesn't this work?

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

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())

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

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; 

createFlags = CreateFlags.HardwareVertexProcessing;
createFlags = CreateFlags.SoftwareVertexProcessing; 

createFlags |= CreateFlags.PureDevice;

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


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);


protected void Render()

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

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

} // end class

Share this post

Link to post
Share on other sites
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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!