XNA Creators Club Online
Page 1 of 1 (13 items)
Sort Posts: Previous Next

N00by question (creating triangle)

Last post 08-23-2008 11:04 AM by aka Sova. 12 replies.
  • 08-19-2008 8:52 AM

    N00by question (creating triangle)

    Hi guys, i'm lamer in programming with DirectX, but actually have to make project of 3D game, so i came here to ask your help :)

     I got 2 books about programming in .NET with DirectX, but actually no one of them helped me, cuz the code is written wrong (may be they wrote it for c# 2005). So now i learn the basics from internet. I got stuck on the second tutorial of creating a 2D triangle. I took the code from here and here , combined together and i didnt get errors, but after i run the program, it tells me about the error in one line, that i cant really understand why there is a mistake. As i said, i dont know where the mistake is, so i put here all the code. Sorry for inconveniece.
     namespace Project1
    {
        public class CreateDevice: Form
        {
            private Device device = null;
            VertexBuffer vertexBuffer;

            public void OnCreateDevice(object sender, EventArgs e)
            {
                CustomVertex.TransformedColored[ verts = new CustomVertex.TransformedColored[3];
                Device dev = (Device)sender;

                // Now create the vertex buffer
                vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored), 3, dev, 0,
                                                CustomVertex.TransformedColored.Format, Pool.Default);

                GraphicsStream stm = vertexBuffer.Lock(0, 0, LockFlags.None);            

                verts[0].X = 150; verts[0].Y = 50; verts[0].Z = 0.5f; verts[0].Rhw = 1;
                verts[0].Color = System.Drawing.Color.Aqua.ToArgb();

                verts[1].X = 250; verts[1].Y = 250; verts[1].Z = 0.5f; verts[1].Rhw = 1;
                verts[1].Color = System.Drawing.Color.Brown.ToArgb();

                verts[2].X = 50; verts[2].Y = 250; verts[2].Z = 0.5f; verts[2].Rhw = 1;
                verts[2].Color = System.Drawing.Color.LightPink.ToArgb();

                stm.Write(verts);
                vertexBuffer.Unlock();          

            }


            public CreateDevice()
            {
                this.ClientSize = new System.Drawing.Size(400,300);
                this.Text = "D3D Tutorial 01: CreateDevice";            
            }

            public bool InitializeGraphics()
            {
            try
            {
               // Now  setup our D3D stuff
               PresentParameters presentParams = new PresentParameters();
               presentParams.Windowed=true;
               presentParams.SwapEffect = SwapEffect.Discard;
               device = new Device(0, DeviceType.Hardware, this,
                           CreateFlags.SoftwareVertexProcessing, presentParams);
               return true;
            }
           catch (DirectXException)
            {
             return false;
            }
            }

            static void Main()
            {

                using (CreateDevice frm = new CreateDevice())
                {
                if (!frm.InitializeGraphics()) // Initialize Direct3D
                {
                    MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");
                    return;
                }
                frm.Show();
                while(frm.Created)
                {
                    frm.Render();
                    Application.DoEvents();
                }
                }
            }

            public void Render()
                {
                if (device == null)
                    return;

                device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
                device.BeginScene();    

                device.SetStreamSource(0, vertexBuffer, 0);
                device.VertexFormat = CustomVertex.TransformedColored.Format;
                device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);   - Here the error is

                device.EndScene();
                device.Present();
                }
        }

    }

  • 08-19-2008 2:13 PM In reply to

    Re: N00by question (creating triangle)

    What is the error it gives you?

    EDIT:  Does your OnCreateDevice handler ever get called? 
    Matt Pettineo | DirectX/XNA MVP


    Ride into The Danger Zone
  • 08-19-2008 2:22 PM In reply to

    Re: N00by question (creating triangle)

    The last value in SetStreamSource cannot be 0, you have to tell it how big a vertex actually is, so that should actually be CustomVertex.TransformedColored.StrideSize.

    Another error you are having is in this line

    CustomVertex.TransformedColored[ verts = new CustomVertex.TransformedColored[3];

    which should be

    CustomVertex.TransformedColored verts = new CustomVertex.TransformedColored[3];

  • 08-20-2008 10:23 AM In reply to

    Re: N00by question (creating triangle)

    MJP:
    What is the error it gives you?

    EDIT:  Does your OnCreateDevice handler ever get called? 

    The error is

     Unhandled Exception: Error in the application.
    -2005530516 (D3DERR_INVALIDCALL)
       at Microsoft.DirectX.Direct3D.Device.DrawPr
    pe, Int32 startVertex, Int32 primitiveCount)

     hm...i dont really think it gets called. Where do i call it? In static void Main?

  • 08-20-2008 10:24 AM In reply to

    Re: N00by question (creating triangle)

    NightCreature:
    The last value in SetStreamSource cannot be 0, you have to tell it how big a vertex actually is, so that should actually be CustomVertex.TransformedColored.StrideSize.

    Another error you are having is in this line

    CustomVertex.TransformedColored[ verts = new CustomVertex.TransformedColored[3];

    which should be

    CustomVertex.TransformedColored verts = new CustomVertex.TransformedColored[3];

     I fixed the first one, but it still doesnt work :(

    The second one is just a problem of copy-paste.

  • 08-20-2008 10:34 AM In reply to

    Re: N00by question (creating triangle)

    Does anybody has here ICQ or skype?
  • 08-20-2008 3:51 PM In reply to

    Re: N00by question (creating triangle)

    Unhandled Exception: Error in the application.
    -2005530516 (D3DERR_INVALIDCALL)
       at Microsoft.DirectX.Direct3D.Device.DrawPr
    pe, Int32 startVertex, Int32 primitiveCount)

    This error means that on the point where you call drawPrimitive is not allowed to be called, are you sure you are creating a valid device and storing that in the class. I am not a managed DX coder I use the unmanaged version and it's a wee bit different in how to create a device. So I can't help you in that respect.

     

    As for the [ I figured that was a copy-paste issue it was just to point it out if it werent one.
  • 08-21-2008 8:38 AM In reply to

    Re: N00by question (creating triangle)

    NightCreature:
    Unhandled Exception: Error in the application.
    -2005530516 (D3DERR_INVALIDCALL)
       at Microsoft.DirectX.Direct3D.Device.DrawPr
    pe, Int32 startVertex, Int32 primitiveCount)

    This error means that on the point where you call drawPrimitive is not allowed to be called, are you sure you are creating a valid device and storing that in the class. I am not a managed DX coder I use the unmanaged version and it's a wee bit different in how to create a device. So I can't help you in that respect..

     I'm not sure it's valid device, i'm not sure all i've done is right :)) Is unmanaged version more comfortable? What are the differences?

     

     

     

  • 08-21-2008 9:13 AM In reply to

    Re: N00by question (creating triangle)

    I can't see where OnCreateDevice() gets called. Call it right after you create the device, also make sure you subscribe it to the device's event handler so it gets called whenever the device is reset.

    If you're comfortable with C++ then that's the way to go. Managed DX is dead now, C++ killed it off years ago. If you want to stick with C# then try SlimDX or XNA games studio.

  • 08-21-2008 1:07 PM In reply to

    Re: N00by question (creating triangle)

    Unmanaged is more difficult, for one it's not using C# but C++, which means more pointers and stuff. Device creation is a lot different if this is your first time doing DX stuff i would stick to unmanaged or even go XNA.
  • 08-22-2008 2:29 PM In reply to

    Re: N00by question (creating triangle)

    Bad Habit:

    I can't see where OnCreateDevice() gets called. Call it right after you create the device, also make sure you subscribe it to the device's event handler so it gets called whenever the device is reset.

    If you're comfortable with C++ then that's the way to go. Managed DX is dead now, C++ killed it off years ago. If you want to stick with C# then try SlimDX or XNA games studio.

     How do i call it? I put it right after i created device, here:

            try
            {
               // Now  setup our D3D stuff
               PresentParameters presentParams = new PresentParameters();
               presentParams.Windowed=true;
               presentParams.SwapEffect = SwapEffect.Discard;
               device = new Device(0, DeviceType.Hardware, this,
                           CreateFlags.SoftwareVertexProcessing, presentParams);
               OnCreateDevice();
               return true;
            }

     but what arguments do i tell him?

    So you advice me to learn C++ if i wanna use direct3D? I'm usual to c# and dont wanna swap to another language :( Do you have some online conversation accounts, like icq or skype?

      NightCreature, XNA is like DirectX? Is it compatible with c#? More comfortable? here is what i found. Did anyone heard about this program? Is it really free? :)) I hardly belive

     

     

     

  • 08-22-2008 3:12 PM In reply to

    Re: N00by question (creating triangle)

    Call it like this: OnCreateDevice(device, null);

    My advice to you is:
    1) download XNA Game Studio 2.0
    2) install it
    3) enjoy yourself by going through the tutorials

    What's wrong with you! XNA has a thriving co