public void End()
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.GetType().ToString());
            }

            GraphicsContext.Assert();

            int current_matrix;

            GL.GetInteger(GetPName.MatrixMode, out current_matrix);

            viewport = viewport_stack.Pop();
            GL.Viewport(viewport.X, viewport.Y, viewport.Width, viewport.Height);

            GL.MatrixMode(MatrixMode.Texture);
            matrix = texture_stack.Pop();
            GL.LoadMatrix(ref matrix);

            GL.MatrixMode(MatrixMode.Modelview);
            matrix = modelview_stack.Pop();
            GL.LoadMatrix(ref matrix);

            GL.MatrixMode(MatrixMode.Projection);
            matrix = projection_stack.Pop();
            GL.LoadMatrix(ref matrix);

            GL.MatrixMode((MatrixMode)current_matrix);
        }
Exemple #2
0
        public static void End()
        {
            GraphicsContext.Assert();

            int current_matrix;

            GL.GetInteger(GetPName.MatrixMode, out current_matrix);

            viewport = viewport_stack.Pop();
            GL.Viewport(viewport.X, viewport.Y, viewport.Width, viewport.Height);

            GL.MatrixMode(MatrixMode.Texture);
            matrix = texture_stack.Pop();
            GL.LoadMatrix(ref matrix);

            GL.MatrixMode(MatrixMode.Modelview);
            matrix = modelview_stack.Pop();
            GL.LoadMatrix(ref matrix);

            GL.MatrixMode(MatrixMode.Projection);
            matrix = projection_stack.Pop();
            GL.LoadMatrix(ref matrix);

            GL.MatrixMode((MatrixMode)current_matrix);
        }
Exemple #3
0
        /// <summary>
        /// Checks for errors that might have occurred during video processing. You should avoid calling this method due to performance reasons.
        /// Only use it on suspect.
        /// </summary>
        /// <param name="silent">If true, errors aren't logged.</param>
        /// <returns>True, if an error occurred, false if not.</returns>
        public static bool CheckOpenGLErrors(bool silent = false, [CallerMemberName] string callerInfoMember = null, [CallerFilePath] string callerInfoFile = null, [CallerLineNumber] int callerInfoLine = -1)
        {
            // Accessing OpenGL functionality requires context. Don't get confused by AccessViolationExceptions, fail better instead.
            GraphicsContext.Assert();

            ErrorCode error;
            bool      found = false;

            while ((error = GL.GetError()) != ErrorCode.NoError)
            {
                if (!silent)
                {
                    Log.Core.WriteError(
                        "Internal OpenGL error, code {0} at {1} in {2}, line {3}.",
                        error,
                        callerInfoMember,
                        callerInfoFile,
                        callerInfoLine);
                }
                found = true;
            }
            if (found && !silent && System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
            }
            return(found);
        }
        public static void UpdateCurrentViewport()
        {
            GraphicsContext.Assert();
            Viewport viewport = new Viewport();

            GL.GetInteger(GetPName.Viewport, out viewport.X);
            currentViewport = viewport;
        }
        /// <summary>
        /// Update the current viewport
        /// </summary>
        public static void UpdateCurrentViewport()
        {
            GraphicsContext.Assert();
            int[] viewportInts = new int[4];

            GL.GetInteger(GetPName.Viewport, viewportInts);
            _currentViewport = new Viewport(viewportInts[0], viewportInts[1], viewportInts[2], viewportInts[3]);
        }
        public static void UpdateCurrentViewport()
        {
            GraphicsContext.Assert();
            int[] viewportInts = new int[4];

            //TODO Test that this function returns the expected values -> tested, 96% sure it works.
            GL.GetInteger(GetPName.Viewport, viewportInts);
            _currentViewport = new Viewport(viewportInts[0], viewportInts[1], viewportInts[2], viewportInts[3]);
        }
Exemple #7
0
 public static void UpdateCurrentViewportFromHardware()
 {
     lock (cachedViewportStack) {
         GraphicsContext.Assert();
         Viewport viewport = new Viewport();
         GL.GetInteger(GetPName.Viewport, out viewport.X);
         cachedViewportStack.Pop();
         cachedViewportStack.Push(viewport);
     }
 }
Exemple #8
0
        private void GameLoop()
        {
            this.Log("Create graphics context");

            context = new GraphicsContext(graphicsMode, window.WindowInfo, 4, 0, GraphicsContextFlags.Debug);

            context.MakeCurrent(window.WindowInfo);

            GraphicsContext.Assert();

            this.Log($"VSync: {Configuration.VSync}");
            VSync = Configuration.VSync;

            this.Log("Loading OpenGL entry points");
            context.LoadAll();

            batch = new SpriteBatch(this);

            this.Log("Setup OpenGL");
            SetupOpenGL();

            this.Log("Setup OpenAL");
            SetupOpenAL();
            LoadAssetProviders();

            Load();

            window.Visible = true;

            CalculateTimings();

            Time.Start();

            this.Log("Enter game loop");
            while (!Closing)
            {
                if (context.IsDisposed)
                {
                    this.Log("Context not available");
                    break;
                }

                updating = true;

                InternUpdate();
                Render(batch);

                updating = false;
            }
            this.Log("Exited game loop");

            Time.Stop();
        }
        public static void End()
        {
            GraphicsContext.Assert();

            GL.MatrixMode(MatrixMode.Modelview);
            GL.PopMatrix(); //pop modelview

            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix(); //pop projection

            GL.MatrixMode(MatrixMode.Modelview);
        }
        public static void Begin()
        {
            GraphicsContext.Assert();

            Viewport currentVp = (Viewport)CurrentViewport;

            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix(); //push projection matrix
            GL.LoadIdentity();
            GL.Ortho(currentVp.X, currentVp.Width, currentVp.Height, currentVp.Y, -1.0, 1.0);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.PushMatrix();  //push modelview matrix
            GL.LoadIdentity();
        }
Exemple #11
0
        public static void LogOpenGLSpecs()
        {
            // Accessing OpenGL functionality requires context. Don't get confused by AccessViolationExceptions, fail better instead.
            GraphicsContext.Assert();

            string versionString = null;

            try
            {
                CheckOpenGLErrors();
                versionString = GL.GetString(StringName.Version);
                Log.Core.Write(
                    "OpenGL Version: {0}" + Environment.NewLine +
                    "Vendor: {1}" + Environment.NewLine +
                    "Renderer: {2}" + Environment.NewLine +
                    "Shader Version: {3}",
                    versionString,
                    GL.GetString(StringName.Vendor),
                    GL.GetString(StringName.Renderer),
                    GL.GetString(StringName.ShadingLanguageVersion));
                CheckOpenGLErrors();
            }
            catch (Exception e)
            {
                Log.Core.WriteWarning("Can't determine OpenGL specs, because an error occurred: {0}", Log.Exception(e));
            }

            // Parse the OpenGL version string in order to determine if it's sufficient
            if (versionString != null)
            {
                string[] token = versionString.Split(' ');
                for (int i = 0; i < token.Length; i++)
                {
                    Version version;
                    if (Version.TryParse(token[i], out version))
                    {
                        if (version.Major < MinOpenGLVersion.Major || (version.Major == MinOpenGLVersion.Major && version.Minor < MinOpenGLVersion.Minor))
                        {
                            Log.Core.WriteWarning(
                                "The detected OpenGL version {0} appears to be lower than the required minimum. Version {1} or higher is required to run Duality applications.",
                                version,
                                MinOpenGLVersion);
                        }
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Retrieves all capability values from the currently active graphics context.
        /// </summary>
        public void RetrieveFromAPI()
        {
            // Reset all values to default / undefined so we'll end up with only the values we successfully retrieved
            this.Reset();

            // Accessing OpenGL functionality requires context. Don't get confused by AccessViolationExceptions, fail better instead.
            GraphicsContext.Assert();

            // Retrieve raw values from OpenGL
            try
            {
                GraphicsBackend.CheckOpenGLErrors();
                this.glVersionString   = GL.GetString(StringName.Version);
                this.glVendor          = GL.GetString(StringName.Vendor);
                this.glRenderer        = GL.GetString(StringName.Renderer);
                this.glslVersionString = GL.GetString(StringName.ShadingLanguageVersion);
                GraphicsBackend.CheckOpenGLErrors();
            }
            catch (Exception e)
            {
                Logs.Core.WriteWarning(
                    "Unable to determine OpenGL base specs: {0}",
                    LogFormat.Exception(e));
            }

            // Retrieve extended / in-depth specs from OpenGL
            try
            {
                GraphicsBackend.CheckOpenGLErrors();
                GL.GetInteger(GetPName.MaxTextureSize, out this.maxTextureSize);
                GL.GetInteger(GetPName.MaxTextureImageUnits, out this.maxTextureBindings);
                GL.GetInteger(GetPName.MaxRenderbufferSize, out this.maxRenderTargetSize);
                GraphicsBackend.CheckOpenGLErrors();
            }
            catch (Exception e)
            {
                Logs.Core.WriteWarning(
                    "Unable to determine OpenGL extended specs: {0}",
                    LogFormat.Exception(e));
            }

            // Parse raw values such as version strings for higher level info
            this.TryParseVersionString(this.glVersionString, out this.glVersion);
            this.TryParseVersionString(this.glslVersionString, out this.glslVersion);

            // Translate some of the GL-specific attributes to the generalized base class ones
            this.apiVersion = this.glslVersion;
        }
Exemple #13
0
        public static void End()
        {
            GraphicsContext.Assert();

            GL.MatrixMode(MatrixMode.Modelview);
            GL.PopMatrix(); //pop modelview

            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix(); //pop projection

            GL.MatrixMode(MatrixMode.Modelview);

            GL.Disable(EnableCap.Texture2D);
            GL.Disable(EnableCap.Blend);
            Begun = false;
        }
Exemple #14
0
 public static VSyncMode getVSync()
 {
     EnsureUndisposed();
     GraphicsContext.Assert();
     if (getContext().SwapInterval < 0)
     {
         return(VSyncMode.Adaptive);
     }
     else if (getContext().SwapInterval == 0)
     {
         return(VSyncMode.Off);
     }
     else
     {
         return(VSyncMode.On);
     }
 }
Exemple #15
0
        public static void setVSync(VSyncMode v)
        {
            EnsureUndisposed();
            GraphicsContext.Assert();
            switch (v)
            {
            case VSyncMode.On:
                getContext().SwapInterval = 1;
                break;

            case VSyncMode.Off:
                getContext().SwapInterval = 0;
                break;

            case VSyncMode.Adaptive:
                getContext().SwapInterval = -1;
                break;
            }
        }
        public void Begin()
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.GetType().ToString());
            }

            GraphicsContext.Assert();

            // Save the state of everything we are going to modify:
            // the current matrix mode, viewport state and the projection, modelview and texture matrices.
            // All these will be restored in the TextPrinter.End() method.
            int current_matrix;

            GL.GetInteger(GetPName.MatrixMode, out current_matrix);

            GL.GetInteger(GetPName.Viewport, out viewport.X);
            viewport_stack.Push(viewport);

            GL.GetFloat(GetPName.ProjectionMatrix, out matrix.Row0.X);
            projection_stack.Push(matrix);
            GL.GetFloat(GetPName.ModelviewMatrix, out matrix.Row0.X);
            modelview_stack.Push(matrix);
            GL.GetFloat(GetPName.TextureMatrix, out matrix.Row0.X);
            texture_stack.Push(matrix);

            // Prepare to draw text. We want pixel perfect precision, so we setup a 2D mode,
            // with size equal to the window (in pixels).
            // While we could also render text in 3D mode, it would be very hard to get
            // pixel-perfect precision.
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(viewport.X, viewport.Width, viewport.Height, viewport.Y, -1.0, 1.0);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();

            GL.MatrixMode(MatrixMode.Texture);
            GL.LoadIdentity();

            GL.MatrixMode((MatrixMode)current_matrix);
        }
Exemple #17
0
        /// <summary>
        /// Enter the gameloop.
        /// </summary>
        void EnterGameloop()
        {
            // Create graphics context
            this.Log("Creating graphics context");
            context = new GraphicsContext(
                mode: graphicsMode,
                window: window.WindowInfo,
                major: 4,
                minor: 0,
                flags: GraphicsContextFlags.ForwardCompatible
                );

            // Make the created context the current context
            context.MakeCurrent(window.WindowInfo);

            // Throw if context is not available
            GraphicsContext.Assert();

            // Set vsync mode
            SetupVsync();

            // Load OpenGL entry points
            this.Log("Loading OpenGL entry points");
            context.LoadAll();

            // Initialize the sprite batch
            SpriteBatch = new SpriteBatch(this);

            // Bind the UI
            UI.Bind(this);

            // Initialize the game
            this.Log("Initializing game");
            PreInitialize();
            Initialize();

            // Set target framerate
            // Use 60hz if framerate is not set
            var framerate = Configuration.TargetFramerate > 0 ? Configuration.TargetFramerate : 60;

            // Prepare timing variables
            PrepareTiming(framerate);

            // Present the window to the user
            window.Visible = true;

            // Wait till the window is visible
            while (!windowVisible && !window.Focused && !window.Visible)
            {
            }

            // Enter the actual game loop
            while (!exit)
            {
                // Set updating to true
                updating = true;

                // Invoke waiting actions
                for (var i = 0; i < ContextActions.Count; i++)
                {
                    Action action;
                    if (ContextActions.TryDequeue(out action))
                    {
                        try {
                            action();
                        } catch (Exception e) {
                            this.Throw(e.Message);
                        }
                    }
                }

                // Set the paused variable to true
                // if the game should be paused and continue
                if (pause)
                {
                    paused = true;
                    continue;
                }

                // Set the paused variable to false
                paused = false;

                // Break out of the loop if the context is not available.
                if (context.IsDisposed)
                {
                    this.Log("Context not available");
                    this.Log("Leaving gameloop");
                    break;
                }

                // Update current time
                currentTime = DateTime.UtcNow;

                // Update
                InternalUpdate(currentTime);

                // Draw
                Draw(gameTime);

                // Set updating to false
                updating = false;
            }
        }