Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

stryker115

DirectX & C# Newbie - Please Help!

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

Hi all, I'm currently trying to start a simple 2D game, using the examples from Tom Miller's DX9 book. He shows how to use both DirectDraw and Direct3D for sprite rendering - and I've been using DirectDraw so far. However, should I just work with Direct3D from the get go? Secondly, my code below will not render anything, no matter what I do. It should be displaying some text and a ball, but it doesn't display anything! I can't for the life of me figure it out, so any help would be appreciated.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
//using Microsoft.DirectX.DirectInput;

using Microsoft.DirectX.DirectSound;
using DirectDraw = Microsoft.DirectX.DirectDraw;

namespace SwiftStrike
{
	/// <summary>

	/// Summary description for Form1.

	/// </summary>

	public class mainForm : System.Windows.Forms.Form
	{
		private DirectDraw.Device device = null;

		public int screenWidth = 800;
		public int screenHeight = 600;

		private Surface primary = null;
		private Surface secondary = null;
		private Surface text = null;

		Ball ball = null;
		Paddle paddle = null;
		

		/// <summary>

		/// Required designer variable.

		/// </summary>

		private System.ComponentModel.Container components = null;

		public mainForm()
		{
			//

			// Required for Windows Form Designer support

			//

			InitializeComponent();

			this.Size = new Size(800,600);
			this.Text = "SwiftStrike";

			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
		}

		/// <summary>

		/// Clean up any resources being used.

		/// </summary>

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>

		/// Required method for Designer support - do not modify

		/// the contents of this method with the code editor.

		/// </summary>

		private void InitializeComponent()
		{
			// 

			// Form1

			// 

			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Name = "Form1";
			this.Text = "Form1";

		}
		#endregion

		/// <summary>

		/// The main entry point for the application.

		/// </summary>

		[STAThread]
		static void Main() 
		{
			using (mainForm form = new mainForm())
			{
				form.Show();
				form.InitializeGraphics();
				Application.Run(form);
			}
		}

		private void InitializeGraphics()
		{
			SurfaceDescription desc = new SurfaceDescription();
			device = new DirectDraw.Device();

		#if DEBUG
			// In debug mode, use windowed level

			device.SetCooperativeLevel(this, 
				CooperativeLevelFlags.Normal);
		#else
			// In release mode, use fullscreen...

			device.SetCooperativeLevel(this, 
				CooperativeLevelFlags.FullscreenExclusive);

			// ...and 640x480x16 resolution with 85Hz frequency

			device.SetDisplayMode(640, 480, 16, 85, true);
		#endif

			/*device.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive);
			device.SetDisplayMode(screenWidth, screenHeight, 16, 0, false);*/

			desc.SurfaceCaps.PrimarySurface = true;

		#if !DEBUG
			// In release mode, we enable flipping, set the complex

			// flag and tell the surface that we will use one back

			// buffer

			desc.SurfaceCaps.Flip = true;
			desc.SurfaceCaps.Complex = true;
			desc.BackBufferCount = 1;
		#endif
			
			/*desc.SurfaceCaps.Flip = true;
			desc.SurfaceCaps.Complex = true;
			desc.BackBufferCount = 1;*/

			primary = new Surface(desc, device);
			desc.Clear();

		#if DEBUG
			// In debug mode, we simply copy the primary surfaces

			// dimensions and create a offscreenplain secondary

			// surface

			desc.Width = primary.SurfaceDescription.Width;
			desc.Height = primary.SurfaceDescription.Height;
			desc.SurfaceCaps.OffScreenPlain = true;
			secondary = new Surface(desc, device);
		#else
			// In release mode, we set the backbuffer flag to true

			// and retrieve a backbuffer surface from the primary

			// surface

			desc.SurfaceCaps.BackBuffer = true;
			secondary = primary.GetAttachedSurface(desc.SurfaceCaps);
		#endif

			/*SurfaceCaps caps = new SurfaceCaps();
			caps.BackBuffer = true;
			secondary = primary.GetAttachedSurface(caps);*/

			Clipper graphicsClipper = new Clipper(device);
			graphicsClipper.Window = this;

			// Attach clipper to the surface

			primary.Clipper = graphicsClipper;

			/*desc.Clear();
			desc.Height = 30;
			desc.Width = 150;
			desc.SurfaceCaps.OffScreenPlain = true;

			text = new Surface(desc, device);
			text.ForeColor = Color.White;
			text.DrawText(0, 0, "SwiftStrike Testing Mode", true);*/

			ball = new Ball(device);
			paddle = new Paddle(device);

		}

		protected override void OnKeyUp(KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
				this.Close();

			base.OnKeyUp(e);
		}

		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			Microsoft.DirectX.DirectXException.IgnoreExceptions();
			//secondary.ColorFill(System.Drawing.Color.Black);


			Draw(secondary, ball.getSurface(), ball.getSize());

			secondary.ForeColor = Color.White;
			secondary.DrawText(0, 0, "SwiftStrike Testing Mode", true);

		#if DEBUG
			primary.Draw(secondary, DrawFlags.DoNotWait);
		#else
			primary.Flip(surfaceSecondary, FlipFlags.Wait);
		#endif
			
			this.Invalidate();
		}

		public static void Draw(Surface backBuffer, Surface drawSurface, Rectangle surfaceSize)
		{
			backBuffer.DrawFast(30, 30, drawSurface, surfaceSize, DrawFastFlags.DoNotWait | DrawFastFlags.SourceColorKey);
		}
	}
}

using System;
using System.Drawing;

using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
//using Microsoft.DirectX.DirectInput;

using Microsoft.DirectX.DirectSound;
using DirectDraw = Microsoft.DirectX.DirectDraw;

namespace SwiftStrike
{
	/// <summary>

	/// Summary description for Ball.

	/// </summary>

	public class Ball
	{
		private Surface ballSurface = null;
		Rectangle ballSize;

		public Ball(DirectDraw.Device device)
		{
			SurfaceDescription desc = new SurfaceDescription();
			desc.SurfaceCaps.OffScreenPlain = true;
			ballSurface = new Surface(@"..\..\ball.bmp", desc, device);
			ColorKey ck = new ColorKey();
			ballSurface.SetColorKey(ColorKeyFlags.SourceDraw, ck);
			ballSize = new Rectangle(0, 0, ballSurface.SurfaceDescription.Height, ballSurface.SurfaceDescription.Width);
		}

		public Surface getSurface()
		{
			return ballSurface;
		}

		public Rectangle getSize()
		{
			return ballSize;
		}
	}
}
  
[edited by - stryker115 on June 10, 2004 7:00:16 PM] [For really large chunks of code like this one, please use [ source ] tags instead of [ code ] ones. Thanks - Superpig] [edited by - Superpig on June 10, 2004 11:29:30 PM]

Share this post


Link to post
Share on other sites
Advertisement
Im having the same problem but with the Sprite class in D3D...i have a properly setup sprite.draw() function ()..i feed it all the proper things..
I put it between my sprite.begin(Spriteflags.Billboard) and sprite.end...
Ived tried playing with the transforms..the sprites position..everything..and all i get is a blank screen.


Be interested if you figure out what your doing wrong as it maybe what im doing wrong...

Share this post


Link to post
Share on other sites

  • 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!