Problems displaying a trangle

This is a discussion on Problems displaying a trangle within the Game Programming forums, part of the General Programming Boards category; I have a problem and can't display a trangle. I had followed the Managed D3D Tutorial and are programming with ...

  1. #1
    Registered User Grayson_Peddie's Avatar
    Join Date
    May 2002
    Posts
    96

    Problems displaying a trangle

    I have a problem and can't display a trangle. I had followed the Managed D3D Tutorial and are programming with C#. Here's the code and also the attachment:

    Code:
    // Project Name: D3DTutorial
    // D3DBlank.cs
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    using Direct3D=Microsoft.DirectX.Direct3D;
    
    public delegate void MessageDelegate(byte message); // Delegate for messages arriving via DirectPlay.
    public delegate void AudioDelegate(); // Delegate to handle audio playback.
    public delegate void PeerCloseCallback();           // This delegate will be called when the session terminated event is fired.
    
    public class GraphicsClass : GraphicsSample
    {
        private D3DXFont drawingFont = null;
        private Point destination = new Point(0, 0);
    
        public GraphicsClass()
        {            
            this.Text = "D3DTutorial";
            this.KeyDown += new KeyEventHandler(this.OnPrivateKeyDown);
            this.KeyUp += new KeyEventHandler(this.OnPrivateKeyUp);
            drawingFont = new D3DXFont( "Arial", System.Drawing.FontStyle.Bold );
        }
    
        /// <summary>
        /// Event Handler for windows messages
        /// </summary>
        private void OnPrivateKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                {
                    destination.X = 1;
                    break;
                }
                case Keys.Down:
                {
                    destination.X = -1;
                    break;
                }
                case Keys.Left:
                {
                    destination.Y = 1;
                    break;
                }
                case Keys.Right:
                {
                    destination.Y = -1;
                    break;
                }
            }
        }
        private void OnPrivateKeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.Down:
                    destination.X = 0;
                    break;
                case Keys.Left:
                case Keys.Right:
                {
                    destination.Y = 0;
                    break;
                }
            }
        }
    
        private VertexBuffer vertices;
    
        protected bool InitializeGraphics() {
            PresentParameters pres = new PresentParameters();
            pres.Windowed = true;
            pres.SwapEffect = SwapEffect.Discard;
    
            device = new Device(0,
                DeviceType.Hardware, this,
                CreateFlags.HardwareVertexProcessing, pres);
        
            vertices = CreateVertexBuffer(device);
    
            return true;
        }
    
        protected VertexBuffer CreateVertexBuffer(Device device) {
            device.VertexFormat = CustomVertex.TransformedColored.Format;
    
            CustomVertex.TransformedColored[] verts =
                new CustomVertex.TransformedColored[3];
    
            verts[0] = new CustomVertex.TransformedColored(
                this.Width/2, this.Height/4, 0.5f, 1.0f, Color.Red.ToArgb());
    
            verts[1] = new CustomVertex.TransformedColored(
                this.Width*3/4, this.Height*3/4, 0.5f, 1.0f, Color.Green.ToArgb());
    
            verts[2] = new CustomVertex.TransformedColored(
                this.Width/4, this.Height*3/4, 0.5f, 1.0f, Color.Blue.ToArgb());
    
            VertexBuffer vbuf = new VertexBuffer(
                typeof(CustomVertex.TransformedColored),
                verts.Length,
                device,
                0,
                CustomVertex.TransformedColored.Format,
                Pool.Default);
    
            GraphicsStream gs = vbuf.Lock(0, 0, 0);
            gs.Write(verts);
            vbuf.Unlock();
    
            return vbuf;
        }
    
        /// <summary>
        /// Called once per frame, the call is the entry point for 3d rendering. This 
        /// function sets up render states, clears the viewport, and renders the scene.
        /// </summary>
        protected override void Render()
        {
    
            //Clear the backbuffer to a Blue color 
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            //Begin the scene
            device.BeginScene();
            drawingFont.DrawText(5, 5, Color.White.ToArgb(), "X: " + destination.X + " Y: " + destination.Y);
    
            //
            // TODO: Insert application rendering code here.
            //
            device.SetStreamSource(0, vertices, 0);
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
    
            device.EndScene();
            device.Present();
        }
    
        protected void DisposeGraphics() {
            device.Dispose();
        }
    
        /// <summary>
        /// Initialize scene objects.
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            drawingFont.InitializeDeviceObjects(device);
    
            //
            // TODO: Insert application device initialization code here.
            //
        }
    
        /// <summary>
        /// Called when a device needs to be restored.
        /// </summary>
        protected override void RestoreDeviceObjects(System.Object sender, System.EventArgs e)
        {
            //
            // TODO: Insert application device restoration code here.
            //
        }
    }
    I am reading the tutorial and entered the code as is in the Managed DirectX Tutorial using C# I saw the picture of the triangle that looks like this:

    http://staff.develop.com/candera/images/screenshot.jpg

    However, I don't get the picture of the trangle when I compile it using Visual C# .Net. I do notice that DirectX 9 Wizard had created a code and when I compile it, and build it, I saw an application with blue screen, but I changed it to black and I like it better.

    Help would be appreciated.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with displaying highscore (snprintf)
    By Turbolego in forum C Programming
    Replies: 3
    Last Post: 11-29-2008, 10:07 AM
  2. Problems Displaying the Information
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 10-14-2005, 01:50 AM
  3. Problems with displaying class variables
    By Terroram in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2004, 07:21 PM
  4. Problems displaying a char
    By jase_dukerider in forum C Programming
    Replies: 4
    Last Post: 11-04-2003, 07:31 AM
  5. Problems displaying content of a text file
    By melissa in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 06:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21