• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
blacken

Form Flashing when Painting

2 posts in this topic

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.

Cheers
[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]
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
Nevermind, I'm good.

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0