Exemple #1
0
        public void Detach()
        {
            if (mRenderingContext != IntPtr.Zero)
            {
                try
                {
                    using (ScopedLock lLock = ScopedLock.Lock(mRenderingContext))
                    {
                        if (Wgl.wglGetCurrentContext() == mRenderingContext)
                        {
                            if (!Wgl.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero))
                            {
                                throw new Exception("Could not release rendering context");
                            }
                        }

                        lock (mRenderingContexts)
                        {
                            mRenderingContexts.Remove(mRenderingContext);
                            if (!Wgl.wglDeleteContext(mRenderingContext))
                            {
                                throw new Exception("Could not delete rendering context");
                            }
                        }
                    }
                }
                finally
                {
                    try
                    {
                        if (mSurface != null)
                        {
                            mSurface.Resize -= Surface_Resize;
                            mSurface.ReleaseDeviceContext -= Surface_ReleaseDeviceContext;
                            mSurface.Paint -= Surface_Paint;
                        }
                    }
                    finally
                    {
                        mSurface          = null;
                        mRenderingContext = IntPtr.Zero;
                    }
                }
            }
        }
Exemple #2
0
        private static int LoadTexture(Bitmap xiTexture)
        {
            int lTextureId;

            lock (mRenderingContexts)
            {
                if (mRenderingContexts.Count == 0)
                {
                    throw new Exception("No rendering contexts exist");
                }

                Rectangle  rectangle = new Rectangle(0, 0, xiTexture.Width, xiTexture.Height);
                BitmapData lData     = xiTexture.LockBits(
                    new Rectangle(0, 0, xiTexture.Width, xiTexture.Height),
                    ImageLockMode.ReadOnly,
                    PixelFormat.Format24bppRgb);

                using (ScopedLock lLock = ScopedLock.Lock(mRenderingContexts[0]))
                {
                    Gl.glGenTextures(1, out lTextureId);

                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, lTextureId);
                    Gl.glTexImage2D(
                        Gl.GL_TEXTURE_2D,
                        0,
                        Gl.GL_RGB8,
                        xiTexture.Width,
                        xiTexture.Height,
                        0,
                        Gl.GL_BGR,
                        Gl.GL_UNSIGNED_BYTE,
                        lData.Scan0);

                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                }

                xiTexture.UnlockBits(lData);
            }
            return(lTextureId);
        }
Exemple #3
0
        protected ScopedLock LockContext()
        {
            ScopedLock lLock = ScopedLock.Lock(mRenderingContext);

            try
            {
                if (Wgl.wglGetCurrentContext() != mRenderingContext)
                {
                    if (!Wgl.wglMakeCurrent(mSurface.DeviceContext, mRenderingContext))
                    {
                        throw new Exception("Could not set the rendering context");
                    }
                }
            }
            catch
            {
                lLock.Dispose();
                throw;
            }

            return(lLock);
        }