using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.Generic;
using Microsoft.DirectX.Direct3D.CustomVertex;
class Program
{
Device device = null;
VertexBuffer vb = null;
public void Init()
{
//...
vb = new VertexBuffer(device, 4, Usage.WriteOnly, PositionColored.Format, Pool.Managed, vb_Created);
}
void vb_Created(object sender, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)sender;
PositionColored[] vertices = new PositionColored[4];
vertices[0] = new PositionColored(-1.0f, -1.0f, 5.0f, Color.Red.ToArgb()); // 0
vertices[1] = new PositionColored(-1.0f, 1.0f, 5.0f, Color.Blue.ToArgb()); // 1
vertices[2] = new PositionColored(1.0f, -1.0f, 5.0f, Color.Green.ToArgb()); // 2
vertices[3] = new PositionColored(1.0f, 1.0f, 5.0f, Color.Yellow.ToArgb()); // 3
GraphicsBuffer<PositionColored> gb = vb.Lock<PositionColored>(0, 4, LockFlags.None);
gb.Write(vertices[0]);
gb.Write(vertices[1]);
gb.Write(vertices[2]);
gb.Write(vertices[3]);
vb.Unlock();
}
}
VertexBuffer creation in December 2005 SDK (C#)
I'm having trouble with the last argument in the vertex buffer constructor. It's an "EventHandler" named "createdEvent", and when I pass my "vb_Created" method, I get an "InvalidCallException". Here's part of my code.
Earlier versions of the SDK didn't have the "createdEvent" argument, so I'm not sure how to use it and can't find any documentation for it. Does anyone know how to set up a vertex buffer correctly in C# with the December 2005 SDK?
You need to use:
As the last argument. If you start typing in new, you'll get an autocomplete thingy. Press Tab twice and it'll add a function for you :).
I actually was having problems with it, I couldn't figure out how to get the vertex buffer from the call. I ended up not using it and just adding the code right after creation :).
new EventHandler(vb_Created)
As the last argument. If you start typing in new, you'll get an autocomplete thingy. Press Tab twice and it'll add a function for you :).
I actually was having problems with it, I couldn't figure out how to get the vertex buffer from the call. I ended up not using it and just adding the code right after creation :).
It still gives me the same error. I replaced
vb = new VertexBuffer(device, 4, Usage.WriteOnly, PositionColored.Format, Pool.Managed, vb_Created);
with
vb = new VertexBuffer(device, 4, Usage.WriteOnly, PositionColored.Format, Pool.Managed, new EventHandler(vb_Created));
I also tried passing null for the event handler, but it gives the same error.
The double tabbing to auto create the code only works when I use "vb.Created +="
If you have a working example for the december sdk, maybe you can show it to me. If I could just create the vertex buffer without any events, I would. But it seems that this sdk forces me to use one.
vb = new VertexBuffer(device, 4, Usage.WriteOnly, PositionColored.Format, Pool.Managed, vb_Created);
with
vb = new VertexBuffer(device, 4, Usage.WriteOnly, PositionColored.Format, Pool.Managed, new EventHandler(vb_Created));
I also tried passing null for the event handler, but it gives the same error.
The double tabbing to auto create the code only works when I use "vb.Created +="
If you have a working example for the december sdk, maybe you can show it to me. If I could just create the vertex buffer without any events, I would. But it seems that this sdk forces me to use one.
Quote:Original post by Proudest
It still gives me the same error. I replaced
vb = new VertexBuffer(device, 4, Usage.WriteOnly, PositionColored.Format, Pool.Managed, vb_Created);
with
vb = new VertexBuffer(device, 4, Usage.WriteOnly, PositionColored.Format, Pool.Managed, new EventHandler(vb_Created));
I also tried passing null for the event handler, but it gives the same error.
Well, I'm no Einstein, but this sorta tells me you're doing something else wrong. I guess one of the other arguments is incorrect. I use null and it works fine.
The first possible thing I see is the size. Note this should be the size in bytes, not the size in vertices. You need to change that to 4 * sizeof(Positioned.Colored);
That's all I see right now, though I'm not sure if the WriteOnly flag is acceptable for a managed resource, though it probably is :).
Hope this helps.
Does the debug output say anything about the exception? See the Forum FAQ if you need help setting it up.
I see one problem, and that's that the second parameter to the VertexBuffer constructor needs to be the size of the buffer in bytes. Could be that it's complaining about the fact that even a single PositionColored structure takes more than 4 bytes to represent.
There is an overload that takes the Vertex Count, but it requires that you pass the Type of the vertex in. As others have said, for that version of new you should be passing the size in bytes, or (since you seem to be using the built in vertex formats anyway) use the overload that takes the type.
Quote:Original post by DrunkenHyena
There is an overload that takes the Vertex Count, but it requires that you pass the Type of the vertex in. As others have said, for that version of new you should be passing the size in bytes, or (since you seem to be using the built in vertex formats anyway) use the overload that takes the type.
The overload that takes a Type argument no longer exists in MDX for .NET 2.0, which seems to be what he's using. That functionality has been replaced with the inclusion of the generic Lock<> method. I should make sure to note that the number passed into the second parameter to the generic Lock call should remain the number of elements, although if you use the non-generic version it take the size in bytes.
You guys are right, the error occurs in the "sizeInBytes" argument. I tested it by changing the value to 16 and that line of code didn't give the error any more. Instead an error was produced when locking the vertex buffer with "numberElementsToLock" having a value of 4, because it was inconsistent with 16 bytes.
The problem is, I can't use "4 * sizeof(PositionColored)" because the compiler says "'...PositionColored' does not have a predefined size, therefore sizeof can only be used in an unsafe context".
The problem is, I can't use "4 * sizeof(PositionColored)" because the compiler says "'...PositionColored' does not have a predefined size, therefore sizeof can only be used in an unsafe context".
You can allow unsafe code by changing the appropriate setting in your project's properties (in VS2003 that's under configuration - build - set allow Unsafe code blocks to true).
Alternatively you can use System.Runtime.InteropServices.Marshal.SizeOf() to the same effect.
Alternatively you can use System.Runtime.InteropServices.Marshal.SizeOf() to the same effect.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement