Exemplo n.º 1
0
 internal TokenContext()
 {
     ws_           = new WhiteSpaceState(this);
     ps_           = new PunctState(this);
     as_           = new AlphaState(this);
     s1_s_         = new SpecialSingleCharState(this);
     s2_s_         = new SpecialCharPairState(this);
     cs_s_         = new CommentSingleState(this);
     cm_s_         = new CommentMultiState(this);
     str_s_        = new QuotedStrState(this);
     currentState_ = ws_;
 }
        internal TokenContext()
        {
            whiteSpaceState_  = new WhiteSpaceState(this);
            puncState_        = new PuncState(this);
            alphaState_       = new AlphaState(this);
            specialPuncState_ = new SpecialPuncState(this);
            dQuoteState_      = new DoubleQuoteState(this);
            sQuoteState_      = new SingleQuoteState(this);
            cCommentState_    = new CCommentState(this);
            cppCommentState_  = new CppCommentState(this);


            // more states here
            currentState_ = whiteSpaceState_;
        }
        public async Task AlphaStateTest(ApplicationState alphaState)
        {
            context.AppState.State = ApplicationState.Running;

            var clientConnection = new AuditorWebSocketConnection(context, new FakeAuditorConnectionInfo(new FakeWebSocket()));

            var envelope = new AlphaState
            {
                State = alphaState
            }.CreateEnvelope().Sign(TestEnvironment.AlphaKeyPair);

            using var writer = new XdrBufferWriter();
            var inMessage = envelope.ToIncomingMessage(writer);
            var isHandled = await context.MessageHandlers.HandleMessage(clientConnection, inMessage);

            Assert.IsTrue(isHandled);
        }
        public async Task AlphaStateTest(ApplicationState alphaState)
        {
            Global.AppState.State = ApplicationState.Running;

            var clientConnection = new AuditorWebSocketConnection(new FakeWebSocket(), null);

            var envelope = new AlphaState
            {
                State = alphaState
            }.CreateEnvelope();

            envelope.Sign(TestEnvironment.AlphaKeyPair);

            var isHandled = await MessageHandlers <AuditorWebSocketConnection> .HandleMessage(clientConnection, envelope);

            Assert.IsTrue(isHandled);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Populate the _alphaState field if it is still AlphaState.NotKnownYet
        /// </summary>
        private void TryDetectAlpha()
        {
            Debug.Assert(_alphaState == AlphaState.NotKnownYet);
            if (State != TextureState.WinFormsImageCreated && State != TextureState.GlTextureCreated)
            {
                return;
            }
            lock (_lock)
            {
                // in order to LockBits(), we need to create a Bitmap. In case the given Image
                // *is* already a Bitmap however, we can directly re-use it.
                try {
                    Bitmap textureBitmap = null;
                    if (_image is Bitmap)
                    {
                        textureBitmap = (Bitmap)_image;
                    }
                    else
                    {
                        // replace the image by the Bitmap - Upload() needs it anyway
                        var old = _image;
                        _image = textureBitmap = new Bitmap(_image);
                        old.Dispose();
                    }

                    var textureData = textureBitmap.LockBits(
                        new Rectangle(0, 0, textureBitmap.Width, textureBitmap.Height),
                        ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format32bppArgb
                        );

                    _alphaState = LookForAlphaBits(textureData) ? AlphaState.HasAlpha : AlphaState.Opaque;
                    textureBitmap.UnlockBits(textureData);
                }
                catch
                { }
            }
        }
Exemplo n.º 6
0
 public static void InsertAlphaState(bool lineStrip, bool alphaTest, DrawBlendType blend, Texture texture, byte[] buffer, int length, int count)
 {
     AlphaState state = null;
     if ((m_AlphaStateCount > 0) && ((m_AlphaStateCount - 1) < m_AlphaStates.Count))
     {
         state = (AlphaState) m_AlphaStates[m_AlphaStateCount - 1];
     }
     bool flag = state == null;
     if (!flag)
     {
         flag = (((state.m_LineStrip != lineStrip) || (state.m_AlphaTest != alphaTest)) || (state.m_BlendType != blend)) || (state.m_Texture != texture);
     }
     if (flag)
     {
         if (m_AlphaStateCount < m_AlphaStates.Count)
         {
             state = (AlphaState) m_AlphaStates[m_AlphaStateCount];
         }
         else
         {
             state = new AlphaState {
                 m_TextureVB = new TextureVB()
             };
             m_AlphaStates.Add(state);
         }
         state.m_LineStrip = lineStrip;
         state.m_AlphaTest = alphaTest;
         state.m_BlendType = blend;
         state.m_Texture = texture;
         state.m_TextureVB.m_Count = 0;
         state.m_TextureVB.m_Frame = -1;
         state.m_TextureVB.m_Stream.Seek(0L, SeekOrigin.Begin);
         m_AlphaStateCount++;
     }
     state.m_TextureVB.m_Count += count;
     state.m_TextureVB.m_Frame = m_ActFrames;
     state.m_TextureVB.m_Stream.Write(buffer, 0, length);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Upload the texture to video RAM.
        ///
        /// This requires that State == TextureState.WinFormsImageCreated, i.e. the
        /// RAM texture image must be ready for use and the Gl object may not have
        /// been created yet.
        ///
        /// Must be called on a thread that is allowed to use Gl APIs.
        /// </summary>
        public void Upload()
        {
            Debug.Assert(State == TextureState.WinFormsImageCreated);

            // this may be required if ReleaseUpload() has been called before
            if (_gl != 0)
            {
                GL.DeleteTexture(_gl);
                _gl = 0;
            }

            lock (_lock) { // this is a long CS, but at this time we don't expect concurrent action.
                // http://www.opentk.com/node/259
                Bitmap textureBitmap       = null;
                var    shouldDisposeBitmap = false;

                // in order to LockBits(), we need to create a Bitmap. In case the given Image
                // *is* already a Bitmap however, we can directly re-use it.
                try {
                    if (_image is Bitmap)
                    {
                        textureBitmap = (Bitmap)_image;
                    }
                    else
                    {
                        textureBitmap       = new Bitmap(_image);
                        shouldDisposeBitmap = true;
                    }

                    GL.GetError();

                    // apply texture resolution bias? (i.e. low quality textures)
                    if (GraphicsSettings.Default.TexQualityBias > 0)
                    {
                        var b = ApplyResolutionBias(textureBitmap, GraphicsSettings.Default.TexQualityBias);
                        if (shouldDisposeBitmap)
                        {
                            textureBitmap.Dispose();
                        }
                        textureBitmap       = b;
                        shouldDisposeBitmap = true;
                    }

                    var textureData = textureBitmap.LockBits(
                        new Rectangle(0, 0, textureBitmap.Width, textureBitmap.Height),
                        ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format32bppArgb
                        );


                    // determine alpha pixels if this has not been done before
                    if (_alphaState == AlphaState.NotKnownYet)
                    {
                        _alphaState = LookForAlphaBits(textureData) ? AlphaState.HasAlpha : AlphaState.Opaque;
                    }

                    int tex;
                    GL.GenTextures(1, out tex);

                    GL.ActiveTexture(TextureUnit.Texture0);
                    GL.BindTexture(TextureTarget.Texture2D, tex);

                    ConfigureFilters();

                    // upload
                    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Four,
                                  textureBitmap.Width,
                                  textureBitmap.Height,
                                  0,
                                  PixelFormat.Bgra,
                                  PixelType.UnsignedByte,
                                  textureData.Scan0);

                    textureBitmap.UnlockBits(textureData);

                    // set final state only if the Gl texture object has been filled successfully
                    if (GL.GetError() == ErrorCode.NoError)
                    {
                        _gl   = tex;
                        State = TextureState.GlTextureCreated;
                    }
                }
                finally {
                    if (shouldDisposeBitmap)
                    {
                        textureBitmap.Dispose();
                    }
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Populate the _alphaState field if it is still AlphaState.NotKnownYet
        /// </summary>
        private void TryDetectAlpha()
        {
            Debug.Assert(_alphaState == AlphaState.NotKnownYet);
            if (State != TextureState.WinFormsImageCreated && State != TextureState.GlTextureCreated)
            {
                return;
            }
            lock(_lock)
            {               
                // in order to LockBits(), we need to create a Bitmap. In case the given Image
                // *is* already a Bitmap however, we can directly re-use it.
                try {
                    Bitmap textureBitmap = null;
                    if (_image is Bitmap)
                    {
                        textureBitmap = (Bitmap)_image;
                    }
                    else
                    {
                        // replace the image by the Bitmap - Upload() needs it anyway
                        var old = _image;
                        _image = textureBitmap = new Bitmap(_image);
                        old.Dispose();
                    }

                    var textureData = textureBitmap.LockBits(
                        new Rectangle(0, 0, textureBitmap.Width, textureBitmap.Height),
                            ImageLockMode.ReadOnly,
                            System.Drawing.Imaging.PixelFormat.Format32bppArgb
                        );
                
                   _alphaState = LookForAlphaBits(textureData) ? AlphaState.HasAlpha : AlphaState.Opaque;
                   textureBitmap.UnlockBits(textureData);
                }
                catch
                { }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Upload the texture to video RAM.
        /// 
        /// This requires that State == TextureState.WinFormsImageCreated, i.e. the
        /// RAM texture image must be ready for use and the Gl object may not have 
        /// been created yet.
        /// 
        /// Must be called on a thread that is allowed to use Gl APIs.
        /// </summary>
        public void Upload()
        {
            Debug.Assert(State == TextureState.WinFormsImageCreated);

            // this may be required if ReleaseUpload() has been called before
            if (_gl != 0)
            {
                GL.DeleteTexture(_gl);
                _gl = 0;
            }

            lock (_lock) { // this is a long CS, but at this time we don't expect concurrent action.
                // http://www.opentk.com/node/259
                Bitmap textureBitmap = null;
                var shouldDisposeBitmap = false;
               
                // in order to LockBits(), we need to create a Bitmap. In case the given Image
                // *is* already a Bitmap however, we can directly re-use it.
                try {
                    if (_image is Bitmap)
                    {
                        textureBitmap = (Bitmap)_image;
                    }
                    else
                    {
                        textureBitmap = new Bitmap(_image);
                        shouldDisposeBitmap = true;
                    }

                    GL.GetError();

                    // apply texture resolution bias? (i.e. low quality textures)
                    if(GraphicsSettings.Default.TexQualityBias > 0)
                    {
                        var b = ApplyResolutionBias(textureBitmap, GraphicsSettings.Default.TexQualityBias);
                        if(shouldDisposeBitmap)
                        {
                            textureBitmap.Dispose();
                        }
                        textureBitmap = b;
                        shouldDisposeBitmap = true;
                    }

                    var textureData = textureBitmap.LockBits(
                        new Rectangle(0, 0, textureBitmap.Width, textureBitmap.Height),
                            ImageLockMode.ReadOnly,
                            System.Drawing.Imaging.PixelFormat.Format32bppArgb
                        );


                    // determine alpha pixels if this has not been done before
                    if (_alphaState == AlphaState.NotKnownYet)
                    {
                        _alphaState = LookForAlphaBits(textureData) ? AlphaState.HasAlpha : AlphaState.Opaque;
                    }

                    int tex;
                    GL.GenTextures(1, out tex);

                    GL.Arb.ActiveTexture(TextureUnit.Texture0);
                    GL.BindTexture(TextureTarget.Texture2D, tex);

                    ConfigureFilters();                    

                    // upload
                    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Four,
                                  textureBitmap.Width,
                                  textureBitmap.Height,
                                  0,
                                  PixelFormat.Bgra,
                                  PixelType.UnsignedByte,
                                  textureData.Scan0);

                    textureBitmap.UnlockBits(textureData);

                    // set final state only if the Gl texture object has been filled successfully
                    if (GL.GetError() == ErrorCode.NoError)
                    {
                        _gl = tex;
                        State = TextureState.GlTextureCreated;
                    }
                }       
                finally {
                    if (shouldDisposeBitmap)
                    {
                        textureBitmap.Dispose();
                    }
                }
            }
        }
Exemplo n.º 10
0
 void updateAlphaState(AlphaState state)
 {
     currentAlphaState = state;
 }