Jump to content

  • Log In with Google      Sign In   
  • Create Account

cephalo

Member Since 04 Mar 2011
Offline Last Active Oct 26 2015 07:40 AM

Posts I've Made

In Topic: Organizing Game Logic

22 September 2015 - 08:46 PM

Thanks for the replies. I'm fairly decent at OOP, but for most of my programming life I've always dealt with graphics and system oriented stuff. I haven't done much actual 'this is what the game part of the game needs to do' programming.

 

Game states are something I forgot about. Really, map generation happens in it's own game state. Separating the states into different classes is a great start. There will be subdivisions of labor beyond that, but those will probably be somewhat fluid. When you're in uncharted territory, sometimes things that seem separate end up being intricately related, and things that seem related end up needing to be forcibly separated.


In Topic: Need some dynamic buffer advice

18 August 2015 - 01:04 PM

Thanks for the push in the right direction. I was foundering between option 1 and 2 in my setup, which was kinda painful and hopeless.

 

I'm intrigued by 'buffers go in a memory pool'. What is that exactly, a giant buffer that is maintained manually somehow? How do I put an instance buffer(misc data used by the shaders) into a memory pool and then use it in my shader?


In Topic: Stereoscopic 3D + SharpDX + WPF possible?

02 June 2015 - 01:55 PM

 I did some experimentation mixing WPF with SharpDX and I found that it wasn't usable. For one thing, you can't use a swap chain, so any drawing that is done is visible irrespective of when your actual frame is finished rendering, so the result is a series of ugly, half finished frames. Only if your 3D scene is very simple will it work, and that of course is mostly machine dependent as to how simple it needs to be. MS was going to make WPF d3d compatible but they gave up, saying that WPF was just not designed for it and making it work was just too complex.

 

EDIT: The fix for all this stuff is Windows 8 and WinRT. :)


In Topic: SharpDX Questions

24 April 2015 - 11:45 AM

I have 3 questions about how to do things in SharpDX:

 

1 - How can I render to a rectangle that's only part of a window?

2 - How can I render orthogonally, like if I want to display 2D images on certain parts of the window?

3 - How do I draw text; do I use DirectWrite, and does anyone have an example?

 Yeah, 1 and 3 are potentially problematic as things are. 2 is as Josh said, you just need the right view matrix, no big deal.

 

As for thing 1, if you mean rendering to part of a window while other things go on the other parts of said window, there is no nice way to do it unless you go Windows 8 only.

 

As for item 2, Text rendering for Windows 7 requires some difficult solutions. I have an example to show, but it requires you to use the D3D 11_2 version of SharpDX, which luckily does work on Windows 7. For reasons I don't understand, you have to open your solution file, and in the first property tag add the line 

 

<SharpDXDirectXVersion>DirectX11_2</SharpDXDirectXVersion>

 

If you do this, you might also have to use a specific version of the shader compiler for your other things, I have d3dcompiler_47.dll being copied to my output directory or else it won't be found. (I don't know why exactly)

 

Here is my crappy code for this, it's full of outdated comments and other nonsense, use at your own risk! The TextRenderer class draws text to my main screen.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX;
using SharpDX.Direct3D;
using SharpDX.D3DCompiler;
//using SharpDX.DirectWrite;
//using SharpDX.DXGI;
//using SharpDX.Direct3D11;
//using SharpDX.Direct3D10;
using System.Runtime.InteropServices;
using d2 = SharpDX.Direct2D1;
using d3d = SharpDX.Direct3D11;
using dxgi = SharpDX.DXGI;
//using wic = SharpDX.WIC;
using dw = SharpDX.DirectWrite;

namespace TextWriterDX11
{
    public static class TextTextureRenderer
    {

        public static void DrawTextToTexture(IntPtr dx11DevicePtr, d3d.Texture2D texture, string text, Color4 color)
        {
            d3d.Device1 dx11Device;
            dxgi.Device dxgiDevice;
            d2.Device d2dDevice;
            dxgi.Surface surface;
            dw.TextFormat textFormat;
            d2.DeviceContext dwRenderTarget;
            d2.SolidColorBrush brushText;

            d3d.Device device110 = new SharpDX.Direct3D11.Device(dx11DevicePtr);
            dx11Device = device110.QueryInterface<d3d.Device1>();

            // DirectX DXGI 1.1 factory
            SharpDX.DXGI.Factory1 factory1 = new SharpDX.DXGI.Factory1();

            // The 1st graphics adapter
            dxgi.Adapter1 adapter1 = factory1.GetAdapter1(0);

            FeatureLevel fl = dx11Device.FeatureLevel;
            var factory2D = new d2.Factory1();

            dxgiDevice = dx11Device.QueryInterface<dxgi.Device>();
            d2dDevice = new d2.Device(dxgiDevice);

            surface = texture.QueryInterface<dxgi.Surface>();

            d2.CreationProperties cp = new d2.CreationProperties
            {
                DebugLevel = d2.DebugLevel.Information,
                Options = d2.DeviceContextOptions.None,
                ThreadingMode = d2.ThreadingMode.SingleThreaded
            };
            dwRenderTarget = new d2.DeviceContext(surface, cp);

            //           srv = new d3d.ShaderResourceView(dx11Device, textureD3D11);
            // Direct Write factory
            SharpDX.DirectWrite.Factory dwFactory = new SharpDX.DirectWrite.Factory(
                SharpDX.DirectWrite.FactoryType.Isolated
            );

            // The textFormat we will use to draw text with
            textFormat = new SharpDX.DirectWrite.TextFormat(
                dwFactory,
                "Code2000",
                SharpDX.DirectWrite.FontWeight.Normal,
                SharpDX.DirectWrite.FontStyle.Normal,
                SharpDX.DirectWrite.FontStretch.Normal,
                160
            );

            textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center;
            textFormat.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Center;


            // Brush used to DrawText
            brushText = new SharpDX.Direct2D1.SolidColorBrush(
                dwRenderTarget,
                color
            );

            dwRenderTarget.BeginDraw();
            dwRenderTarget.DrawText(text, textFormat, new RectangleF(0, 0, dwRenderTarget.Size.Width, dwRenderTarget.Size.Height), brushText);
            dwRenderTarget.EndDraw();

            adapter1.Dispose();

            dxgiDevice.Dispose();
            d2dDevice.Dispose();
            textFormat.Dispose();
            surface.Dispose();

            dwRenderTarget.Dispose();
            brushText.Dispose();

            factory1.Dispose();
            factory2D.Dispose();
            dwFactory.Dispose();
        }

    }
    public class TextRenderer
    {
        d3d.Device1 dx11Device;
        d3d.Texture2D textureD3D11;
        dw.TextFormat textFormat;
        d2.DeviceContext dwRenderTarget;
        d2.SolidColorBrush brushText;
        d3d.BlendState BlendState_Transparent;
        d3d.InputLayout layoutText;
        d3d.Buffer vertexBufferText;
        d3d.VertexShader textVertexShader;
        d3d.PixelShader textPixelShader;
        d3d.SamplerState samLinear;
        dxgi.Surface surface;
        d3d.ShaderResourceView srv;
        dxgi.Device dxgiDevice;
        d2.Device d2dDevice;
        SharpDX.DirectWrite.Factory dwFactory;
        d2.Factory1 factory2D;
        string debugString;
        string text;
        Color4 textColor;
        public string Text { get { return text; } set { textChanged = true; text = value; } }
        public Color4 TextColor { get { return textColor; } set { colorChanged = true; textColor = value; } }
        bool textChanged;
        bool colorChanged;

        public TextRenderer()
        {
            TextColor = new Color4(0.5f, 0, 0.5f, 1);
        }
        public void Initialize(IntPtr dx11DevicePtr, dxgi.Surface backBuffer)
        {
            d3d.Device device110 = new SharpDX.Direct3D11.Device(dx11DevicePtr);
            this.dx11Device = device110.QueryInterface<d3d.Device1>();

            // DirectX DXGI 1.1 factory
            SharpDX.DXGI.Factory1 factory1 = new SharpDX.DXGI.Factory1();

            // The 1st graphics adapter
            dxgi.Adapter1 adapter1 = factory1.GetAdapter1(0);

            FeatureLevel fl = dx11Device.FeatureLevel;
            factory2D = new d2.Factory1();

            dxgiDevice = dx11Device.QueryInterface<dxgi.Device>();
            d2dDevice = new d2.Device(dxgiDevice);

            //surface = swapChain.GetBackBuffer<dxgi.Surface>(0);
            surface = backBuffer;

            d2.CreationProperties cp = new d2.CreationProperties
            {
                DebugLevel = d2.DebugLevel.Information,
                Options = d2.DeviceContextOptions.None,
                ThreadingMode = d2.ThreadingMode.SingleThreaded
            };
            dwRenderTarget = new d2.DeviceContext(surface,cp);

 //           srv = new d3d.ShaderResourceView(dx11Device, textureD3D11);
            // Direct Write factory
            dwFactory = new SharpDX.DirectWrite.Factory(
                SharpDX.DirectWrite.FactoryType.Isolated
            );

            // The textFormat we will use to draw text with
            textFormat = new SharpDX.DirectWrite.TextFormat(
                dwFactory,
                "Calibri",        
                SharpDX.DirectWrite.FontWeight.Normal,
                SharpDX.DirectWrite.FontStyle.Normal,
                SharpDX.DirectWrite.FontStretch.Normal,
                18
            );

            textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Justified;
            textFormat.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near;


            // Brush used to DrawText
            brushText = new SharpDX.Direct2D1.SolidColorBrush(
                dwRenderTarget,
                TextColor
            );

            Text = adapter1.Description1.Description;
            debugString = Text;
            textChanged = false;
            colorChanged = false;

            adapter1.Dispose();
            //sharedResource.Dispose();
            factory1.Dispose();
        }
        public void Update()
        {
            // Brush used to DrawText
            if (colorChanged)
            {
                brushText.Dispose();
                brushText = new SharpDX.Direct2D1.SolidColorBrush(
                    dwRenderTarget,
                    TextColor
                );
                colorChanged = false;
            }
            if (textChanged)
            {
                debugString = Text;
                textChanged = false;
            }

        }

        public void Render()
        {
            dwRenderTarget.BeginDraw();
            dwRenderTarget.DrawText(debugString, textFormat, new RectangleF(0, 0, dwRenderTarget.Size.Width, dwRenderTarget.Size.Height), brushText);
            dwRenderTarget.EndDraw();

        }
        public void Dispose()
        {
            dxgiDevice.Dispose();
            d2dDevice.Dispose();
            surface.Dispose();

            dwRenderTarget.Dispose();
            brushText.Dispose();
            factory2D.Dispose();
            dwFactory.Dispose();
        }
    }
}


In Topic: Time to decide on a UI Framework (SharpDX)

20 April 2015 - 02:48 PM

Ok, I finally got my scene to render with WPF using the SharpDX.WPF library, and the results are not usable. It might work if your 3D scene is very simple, but you aren't using a swap chain, and the result when your framerate is too slow is that you get a random splattering of screens is various stages of completion.

 

It looks like mixing DirectX with XAML is a Windows 8+ only thing.

 

I don't know what to do.


PARTNERS