internal void Draw(D3DState state) { foreach (Layer layer in layers) { layer.Draw(state); } }
public void DoCreate(Control pc) { state = new D3DState(); control = pc; pc.SizeChanged += new EventHandler(PSI_DirectFrame_SizeChanged); //paused = false; if (!InitializeGraphics()) { MessageBox.Show("Error while initializing Direct3D"); } ThreadStart ts = new ThreadStart(RunThread); Thread runthread = new Thread(ts); runthread.Start(); paused = false; }
public bool InitializeGraphics() { try { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard; state = new D3DState(); state.graphics = new Direct3D.Device(0, DeviceType.Hardware, control, Direct3D.CreateFlags.SoftwareVertexProcessing, presentParams); //The previous code creates a new graphics device. It’s not going to make a whole lot of //sense to you right now, but don’t worry that much about it. Just know that it works, and //that I’ll get to it in much more detail in Chapter 7. //The next part of the code sets up the event handlers (which are delegates): state.graphics.DeviceLost += new EventHandler(this.InvalidateDeviceObjects); state.graphics.DeviceReset += new EventHandler(this.RestoreDeviceObjects); state.graphics.Disposing += new EventHandler(this.DeleteDeviceObjects); state.graphics.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing); /* 2d feature for turning off back culling on badly chosen flat rectangels */ state.graphics.RenderState.CullMode = Direct3D.Cull.None; // yes I want this. state.graphics.RenderState.AlphaBlendEnable = true; //The first three events handle whenever a device is lost (say, if the user switches to another //window), the device is reset for any reason, or the device is disposed of. These first three //events are solid events; when they happen, you have to handle them. No ifs, ands, or buts //about it—the operating system is telling you something happened and your event handler //has to take care of the situation. //The final event handles when the graphics device is resized, which is a special kind of //event because it can be cancelled. A cancelable event is an event that your program can //decide to reject. For example, if the user says he’s going to resize your game window, your //program will get the event, but you can tell the operating system, “Nope, it’s not gonna //happen!” and the event won’t complete. This behavior is used mostly to prevent forms //from closing before the user saves his data in windows applications. //The final part of the code returns true for a successful initialization, or catches any //DirectXExceptions that may have been thrown and returns false: return(true); } catch (DirectXException) { return(false); } }
// cells are setup as x, y index of col, row, and 1, 1 wide,high, and a total rows and cols they are of. // okay so this computes from that, public void SetupCell(D3DState state, float x, float y, float width, float height, float rows, float cols) { x_ofs = (int)x; y_ofs = (int)y; this.rows = (int)rows; this.cols = (int)cols; if (state == null) { return; } cell_verts = new Direct3D.VertexBuffer( typeof(Direct3D.CustomVertex.PositionTextured), 4, state.graphics, 0, Direct3D.CustomVertex.PositionTextured.Format, Direct3D.Pool.Default); Direct3D.CustomVertex.PositionTextured[] verts = (Direct3D.CustomVertex.PositionTextured[])cell_verts.Lock(0, 0); verts[0].X = -1.0f; verts[0].Y = -1.0f; verts[0].Z = 0.0f; verts[0].Tu = (x * width) / cols; verts[0].Tv = (y * height) / rows; verts[1].X = -1.0f; verts[1].Y = 1.0f; verts[1].Z = 0.0f; verts[1].Tu = ((x + 1) * width) / cols; verts[1].Tv = (y * height) / rows; verts[2].X = 1.0f; verts[2].Y = -1.0f; verts[2].Z = 0.0f; verts[2].Tu = (x * width) / cols; verts[2].Tv = ((y + 1) * height) / rows; verts[3].X = 1.0f; verts[3].Y = 1.0f; verts[3].Z = 0.0f; verts[3].Tu = ((x + 1) * width) / cols; verts[3].Tv = ((y + 1) * height) / rows; cell_verts.Unlock(); }
void BoardControl_Render(D3DState state) { board.Draw(state); }