Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!

We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

Form Flashing when Painting

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 blacken   Members   -  Reputation: 126


Posted 15 December 2012 - 01:18 AM

Hi Guys,

Just wondering if someone would be able to shed some light on an issue i am having.

I am drawing a simply triangle and getting it to rotate around its axis.

However, when i re paint the screen it flashes as it repaints.

I have attached the code if anyone could shed some light on this problem.

[source lang="csharp"]using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using SlimDX;using SlimDX.Direct3D9;using SlimDX.Windows;using System.Runtime.InteropServices;namespace WindowsFormsApplication1{ public partial class Form1 : Form { private Device device; CustomVertex.PositionColored[] vertices; public static RenderForm Window; // Window used for rendering. public static Device D3DDevice; // Direct3D device. public static VertexBuffer Vertices; // Vertex buffer object used to hold vertices. public static int time; // Used for rotation caculations. public static float angle; // Angle of rottaion. public Form1() { InitializeComponent(); // Initialize Direct3D } public void InitializeD3D() { // Create a new PresentParameters object and fill in the necessary fields. PresentParameters presentParams = new PresentParameters(); // Below are the required bare mininum, needed to initialize the D3D device. presentParams.BackBufferHeight = this.ClientRectangle.Height; // BackBufferHeight, set to the Window's height. presentParams.BackBufferWidth = this.ClientRectangle.Width; // BackBufferWidth, set to the Window's width. presentParams.DeviceWindowHandle = this.Handle; // DeviceWindowHandle, set to the Window's handle. // Create the device. D3DDevice = new Device(new Direct3D(), 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, presentParams); //D3DDevice = new Device(0, DeviceType.Hardware, Window.Handle, CreateFlags.HardwareVertexProcessing, presentParams); // Create the vertex buffer and fill with the triangle vertices. Vertices = new VertexBuffer(D3DDevice, 3 * Vertex.SizeBytes, Usage.WriteOnly, VertexFormat.None, Pool.Managed); DataStream stream = Vertices.Lock(0, 0, LockFlags.None); stream.WriteRange(BuildVertexData()); Vertices.Unlock(); // Turn off culling, so we see the front and back of the triangle D3DDevice.SetRenderState(RenderState.CullMode, Cull.None); // Turn off D3D lighting, since we are providing our own vertex colors D3DDevice.SetRenderState(RenderState.Lighting, false); } private static Vertex[] BuildVertexData() { Vertex[] vertexData = new Vertex[3]; vertexData[0].Position = new Vector3(-1.0f, -1.0f, 0.0f); vertexData[0].Color = Color.Red.ToArgb(); vertexData[1].Position = new Vector3(1.0f, -1.0f, 0.0f); vertexData[1].Color = Color.Blue.ToArgb(); vertexData[2].Position = new Vector3(0.0f, 1.0f, 0.0f); vertexData[2].Color = Color.Green.ToArgb(); return vertexData; } public void SetupMatrices() { // For our world matrix, we will just rotate the object about the y-axis. // Set up the rotation matrix to generate 1 full rotation (2*PI radians) // every 1000 ms. To avoid the loss of precision inherent in very high // floating point numbers, the system time is modulated by the rotation // period before conversion to a radian angle. Matrix world = Matrix.RotationY(angle); D3DDevice.SetTransform(TransformState.World, world); // Set up our view matrix. A view matrix can be defined given an eye point, // a point to lookat, and a direction for which way is up. Here, we set the // eye five units back along the z-axis and up three units, look at the // origin, and define "up" to be in the y-direction. Vector3 Eye = new Vector3(0.0f, 3.0f, -5.0f); Vector3 LookAt = new Vector3(0.0f, 0.0f, 0.0f); Vector3 Up = new Vector3(0.0f, 1.0f, 0.0f); Matrix view = Matrix.LookAtLH(Eye, LookAt, Up); D3DDevice.SetTransform(TransformState.View, view); // For the projection matrix, we set up a perspective transform (which // transforms geometry from 3D view space to 2D viewport space, with // a perspective divide making objects smaller in the distance). To build // a perpsective transform, we need the field of view (1/4 pi is common), // the aspect ratio, and the near and far clipping planes (which define at // what distances geometry should be no longer be rendered). float fov = (float)Math.PI / 4; float apsectRatio = (float)this.ClientRectangle.Width / this.ClientRectangle.Height; float nearPlane = 1.0f; float farPlane = 100.0f; Matrix projection = Matrix.PerspectiveFovLH(fov, apsectRatio, nearPlane, farPlane); D3DDevice.SetTransform(TransformState.Projection, projection); } private void Form1_Load(object sender, EventArgs e) { InitializeD3D(); } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); // Begin the scene. D3DDevice.BeginScene(); // Update the time and angle. time = Environment.TickCount % 1000; angle = (float)(time * (2.0f * Math.PI) / 1000.0f); // Setup the world, view and projection matrices. SetupMatrices(); // Render the vertex buffer. D3DDevice.SetStreamSource(0, Vertices, 0, Vertex.SizeBytes); D3DDevice.VertexFormat = Vertex.Format; D3DDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); // End the scene. D3DDevice.EndScene(); // Present the backbuffer contents to the screen. D3DDevice.Present(); this.Invalidate(); //this.Update(); } } class CustomVertex { //[StructLayout(LayoutKind.Sequential)] public struct TransformedColored { public Vector4 Position; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.PositionRhw; } //[StructLayout(LayoutKind.Sequential)] public struct PositionColored { public Vector3 Position; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position; } //[StructLayout(LayoutKind.Sequential)] public struct PositionNormalColored { public Vector3 Position; public Vector3 Normal; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.Normal; } } struct Vertex { public Vector3 Position; public int Color; public static int SizeBytes { get { return Marshal.SizeOf(typeof(Vertex)); } } public static VertexFormat Format { get { return VertexFormat.Position | VertexFormat.Diffuse; } } } }[/source]


#2 blacken   Members   -  Reputation: 126


Posted 16 December 2012 - 12:30 AM

Anyone out there help me out?

I believe that it has something to do with the speed at which the device clears the screen before painting the new one. If i put a breakpoint on the this.invalidate line and then hold f5 to just continue the program it works perfectly.

Looking at msdn and a few other things, the invalidate method has to wait for the messages or something to stop or you should make the messages or something stop, i personally have no idea, so i am hoping someone can help me. Once it get this problem solved i should be good.

Cheers guys

#3 blacken   Members   -  Reputation: 126


Posted 17 December 2012 - 01:02 AM

Nevermind, I'm good.

Turns out that refreshing the matrices every draw doesn't quite work appropriately.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.