Jump to content
  • Advertisement
Sign in to follow this  

[.net] My sprite flickers ( changed title )

This topic is 4882 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 just started with managed direct x and I got a problem that I seem to be unable to solve. The program is supposed to render a D3D Sprite on a green background but the sprite doesn't render on screen. Form
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication4
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			this.Text = "Hjärter";
			this.Size = new Size( 640, 480 );
			this.BackColor = Color.Green;
			this.StartPosition = FormStartPosition.CenterScreen;
			this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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(632, 453);
			this.Name = "Form1";
			this.Text = "Hjärter";
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			using ( Form1 Form = new Form1() )
			{
				Form.Initialize();
				Form.Show();
				Application.Run(Form);
			}
		}

		public void Initialize()
		{
			GameClass.Instance.Initialize( this );
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			GameClass.Instance.Frame();
			
			this.Invalidate();
		}
	}
}

/// <summary>
	/// Summary description for GameClass.
	/// </summary>
	public sealed class GameClass
	{
		private VertexBuffer vb = null;

		public static readonly GameClass Instance = new GameClass();
		public GameClass(){}
		
		CSprite s;

		private System.Windows.Forms.Control _targetControl;
		public System.Windows.Forms.Control targetControl
		{
			get{ return _targetControl; }
		}

		private D3DDev _renderer;
		public D3DDev renderer
		{
			get{ return _renderer; }
		}

		public void Initialize( System.Windows.Forms.Control targetControl )
		{
			_targetControl = targetControl;
				
			_renderer = new D3DDev( _targetControl );
			
			s = new CSprite( "test.bmp", _renderer.Device );
			s.Position( 50, 50 );
				
		}

		public void Frame()
		{

			Render();
		}

		public void Render()
		{
			_renderer.BeginScene();
			
			s.Draw( _renderer.sprite );

			_renderer.EndScene();
		}

	}

public class D3DDev
	{
		public System.Windows.Forms.Control _targetControl;
		private Device _device;
		private Sprite _sprite;
		private CSprite card;

		public Sprite sprite
		{
			get { return _sprite; }
		}

		public Device Device
		{
			get { return _device; }
		}

		public D3DDev( System.Windows.Forms.Control  targetControl )
		{
			_targetControl = targetControl;

			PresentParameters pp = new PresentParameters();
			pp.SwapEffect = SwapEffect.Discard;

			pp.Windowed = true;

			_device = new Device( 0, DeviceType.Hardware, _targetControl, CreateFlags.SoftwareVertexProcessing, pp );
			
			_sprite = new Sprite( _device );
			

			
		}

		public void OnDeviceReset( object sender, EventArgs e )
		{
			
		}

		public void BeginScene()
		{
			_device.Clear(ClearFlags.Target, System.Drawing.Color.Green, 1.0f, 0);
			_device.BeginScene();
			_sprite.Begin(SpriteFlags.None);
			
		}
		public void EndScene()
		{
			_sprite.End();
			_device.EndScene();
_device.Present();
		}		
	}

public class CSprite
	{
		static readonly Vector3 Center = new Vector3(0,0,0);
		private Texture spriteTexture;
		private Rectangle rect;

		private Vector3 position;
		
		public CSprite( string filename, Device dev )
		{
			position = new Vector3( 0, 0, 1 );	
			spriteTexture  = TextureLoader.FromFile( dev, filename );
			
			using ( Surface s = spriteTexture.GetSurfaceLevel(0) )
			{
				SurfaceDescription desc = s.Description;
				rect = new Rectangle( 0, 0, desc.Width, desc.Height );
			}


		}

		public void Position( int x, int y )
		{
			position.X = x;
			position.Y = y;
		}

		public void Draw( Sprite sprite )
		{
			sprite.Draw( spriteTexture, rect, Center, position, Color.White );
		}
	}





I get no errors so I am suspecting that there is something else that I need to do, but what? [Edited by - kappa on January 6, 2005 4:32:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
Okey I "fixed" it, but the answere got me really confused.

To make the sprite render I had to add another device.Present(); after the current one, that made my sprite show as it should have.

[edit]
I didn't fix it, it just makes my sprite flicker horribly, should I draw it to a backbuffer first? Kickstart said I didn't need one.

if so how?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!