Jump to content
  • Advertisement
Sign in to follow this  
Specchum

Managed directX engine design query

This topic is 4833 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 seem to be working forever on my directX engine rather than actually writing some game based on it. My excuse is that the engine isn't ready so the game will have to wait. :) Anyhow, I've now decided to port my C++ engine to C# (as an excercise in procrastination so that I don't have to begin on a game!) but this immediately poses some fresh problems (no surprises there then): First off, do I derive the core engine from a System.Windows.Form so that it is a self-contained window ready unit (you know with all the members and the ability to over-ride methods like Paint and Reset) or do I make it completely independent unit that ONLY sets up the directX stuff (which is how my c++ code is setup - it has a seperate class to handle window creation and a seperate class that takes care of directX setup)? In the latter case, my windows app will create the form and pass in the handle so that the directX renderer can be associated with it. I rather like the former approach since the engine care will take care of most of the things including window creation. But the latter will allow me finer control (which window/control/form to render to for example). Anyone have any suggestions on this? Or is there an even better way of going about this?

Share this post


Link to post
Share on other sites
Advertisement
Right now my game starts up as the actual form, and then the form creates all of the engines (Graphics, Input, Music), and then loops calling another module for everything else. It doesn't actually do anything except pump input and update messages into the game, accepting calls to play music, and handling device resets. It is actually steadily shrinking.

I like this way just because most of my codebase is divorced from the form completely. Its just launching point into my own engine.

Share this post


Link to post
Share on other sites
Hi there Specchum,
You have to remember a couple of points regarding designing engines. Keep the rendering code seperate from the other code. It's an elegant solution to keep your initialization code in your constructor of your window. Also try not to wrap the rendering code in a bulky manner but try and batch/create new methods like RenderEntities(). That renders the entities.

Here is an example of what I mean.

[source lang = "c#"]
//Window.cs
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Engine
{
class Window : Form
{
#region Members
private PresentParameters presentParams;
private Device device;
#endregion

#region Methods
public Window()
{
Width = 800;
Height = 600;
Text = "Tutorial 1 - Creating the Device";

AdapterInformation adapterInfo = Manager.Adapters[0]; //Helper class to gain access to information about the display adapter
presentParams = new PresentParameters();
presentParams.BackBufferCount = 1; //Number of backbuffers to create
presentParams.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format; //The current format of the display device
presentParams.BackBufferHeight = this.Height;
presentParams.BackBufferWidth = this.Width;
presentParams.SwapEffect = SwapEffect.Discard; //How the backbuffer and the current display will be swapped
presentParams.AutoDepthStencilFormat = DepthFormat.D24S8; //24 bits for the depth and 8 bits for the stencil
presentParams.EnableAutoDepthStencil = true; //Let direct3d handle the depth buffers for the application
presentParams.Windowed = true;
device = new Device(0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, presentParams);

device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, 1.0f, 1.0f, 1000.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -100.0f),
new Vector3(0.0f, 0.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f));
}
public void BeginRender(Color bgColor)
{
if(device != null)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, bgColor, 1.0f, 0);
device.BeginScene();
}
}
public void FinishRender()
{
if(device != null)
{
device.EndScene();
device.Present();
}
}
public void DisposeDevice()
{
if(device != null)
{
device.Dispose();
}
}
#endregion
}
}



and the Core
[source lang = "c#"]
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Engine
{
public class Core
{
#region Members
private Window window;
#endregion

#region Methods
public Core()
{
window = new Window();
}
private void Update()
{
}
private void Render()
{
window.BeginRender(Color.Black);
window.FinishRender();
}
public void Run()
{
window.Show();
while(window.Created)
{
Update();
Render();
Application.DoEvents();
}
window.DisposeDevice();
}
#endregion
}
}



and then the implementation
[source lang = "c#"]
using System;

namespace Engine
{
public class Game
{
public static void Main()
{
Core core = new Core();
core.Run();
}
}
}



I have tried to keep the rendering code seperate from the update code where you would do matrix transformations etc...

Share this post


Link to post
Share on other sites
Thanks to both of you for your inputs! I was thinking something along the lines of what Armadon had suggested and now it would appear to be a good solution. Can't wait to go home and start coding! Cheers!

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.

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!