简体   繁体   中英

How can i fix Index Out Of Range Exception of a char?

First the project it self i didn't do it i took it from github:

spazzarama/Direct3DHook

The problem the exception happen when i try to use any directx11 game. The exception happen in the DXHookD3D11.cs

#region Draw overlay (after screenshot so we don't capture overlay as well)
                if (this.Config.ShowOverlay)
                {
                    // Initialise Overlay Engine
                    if (_swapChainPointer != swapChain.NativePointer || _overlayEngine == null)
                    {
                        if (_overlayEngine != null)
                            _overlayEngine.Dispose();

                        _overlayEngine = new DX11.DXOverlayEngine();
                        _overlayEngine.Overlays.Add(new Capture.Hook.Common.Overlay
                        {
                            Elements =
                            {
                                //new Capture.Hook.Common.TextElement(new System.Drawing.Font("Times New Roman", 22)) { Text = "Test", Location = new System.Drawing.Point(200, 200), Color = System.Drawing.Color.Yellow, AntiAliased = false},
                                new Capture.Hook.Common.FramesPerSecond(new System.Drawing.Font("Arial", 16)) { Location = new System.Drawing.Point(5,5), Color = System.Drawing.Color.Red, AntiAliased = true }
                            }
                        });
                        _overlayEngine.Initialise(swapChain);

                        _swapChainPointer = swapChain.NativePointer;
                    }
                    // Draw Overlay(s)
                    else if (_overlayEngine != null)
                    {
                        foreach (var overlay in _overlayEngine.Overlays)
                            overlay.Frame();
                        _overlayEngine.Draw();
                    }
                }
                #endregion

Then when it's trying to Draw() i used a breakpoint it's going to the class DXOverlayEngine.cs to this line:

_spriteEngine.DrawString(textElement.Location.X, textElement.Location.Y, textElement.Text, textElement.Color.R, textElement.Color.G, textElement.Color.B, textElement.Color.A, font);

Then it's going to the DXSprite.cs to this part:

public void DrawString(int X, int Y, string text, int R, int G, int B, int A, DXFont F)
        {
            Color4 blendFactor = new Color4(1.0f);
            Color4 backupBlendFactor;
            int backupMask;
            var backupBlendState = _deviceContext.OutputMerger.GetBlendState(out backupBlendFactor, out backupMask);
            _deviceContext.OutputMerger.SetBlendState(_transparentBS, blendFactor);

            BeginBatch(F.GetFontSheetSRV());


            int length = text.Length;

            int posX = X;
            int posY = Y;

            Color4 color;
            Vector4 Vec = new Vector4(R > 0 ? (float)(R / 255.0f) : 0.0f, G > 0 ? (float)(G / 255.0f) : 0.0f, B > 0 ? (float)(B / 255.0f) : 0.0f, A > 0 ? (float)(A / 255.0f) : 0.0f);
            color = new Color4(Vec);

            for (int i = 0; i < length; ++i)
            {
                char character = text[i];

                if (character == ' ')
                    posX += F.GetSpaceWidth();
                else if (character == '\n')
                {
                    posX = X;
                    posY += F.GetCharHeight();
                }
                else
                {
                    Rectangle charRect = F.GetCharRect(character);

And last it's doing the line F.GetCharRect(character) in the DXFont.cs class:

public Rectangle GetCharRect(char c)
        {
            Debug.Assert(_initialized);

            return _charRects[c - StartChar];
        }

I used a breakpoint on the method GetCharRect and for example in this case the variable c value is: 163'£ and the value in the variable StartChar is 33 '!'

Then it's showing the exception on the line:

return _charRects[c - StartChar];

I tried to ask the owner about this problem and so far all he told me is:

"Sounds like it is trying to draw a character that it hasn't prepared..."

I tried to narrow the code here as much as i can but it's all connect each other.

Looking in DXFont.cs it looks like it only prepares the characters with ASCII Codes between 33 and 127

     const char StartChar = (char)33; 
     const char EndChar = (char)127; 
     const uint NumChars = EndChar - StartChar; 

The _charRects are set up during BuildFontSheetBitmap based on this range. You need to expand the range to include all the characters you want to render

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM