Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1452 chris 1
#region License
2
//
3
// The Open Toolkit Library License
4
//
5
// Copyright (c) 2006 - 2009 the Open Toolkit library.
6
//
7
// Permission is hereby granted, free of charge, to any person obtaining a copy
8
// of this software and associated documentation files (the "Software"), to deal
9
// in the Software without restriction, including without limitation the rights to 
10
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
// the Software, and to permit persons to whom the Software is furnished to do
12
// so, subject to the following conditions:
13
//
14
// The above copyright notice and this permission notice shall be included in all
15
// copies or substantial portions of the Software.
16
//
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
// OTHER DEALINGS IN THE SOFTWARE.
25
//
26
#endregion
27
 
28
using System;
29
using System.Collections.Generic;
30
using System.Text;
31
using System.Diagnostics;
32
 
33
namespace OpenTK.Graphics
34
{
35
    // Used in debug-mode only, for automatic OpenGL error-checking.
36
    //
37
    // Works like this: an instance is created before each OpenGL function is called.
38
    // The constructor resets the OpenGL error state. Once the native function returns,
39
    // the error state is checked again, raising the relevant exceptions.
40
    //
41
    // A using-region is used to ensure Dispose() is called.
42
    //
43
    // Make sure that no error checking is added to the GetError function,
44
    // as that would cause infinite recursion!
45
    [Obsolete]
46
    struct ErrorHelper : IDisposable
47
    {
48
        #region Fields
49
 
50
        static readonly object SyncRoot = new object();
51
        static readonly Dictionary<GraphicsContext, List<ErrorCode>> ContextErrors =
52
            new Dictionary<GraphicsContext, List<ErrorCode>>();
53
        readonly GraphicsContext Context;
54
 
55
        #endregion
56
 
57
        #region Constructors
58
 
59
        public ErrorHelper(IGraphicsContext context)
60
        {
61
            if (context == null)
62
                throw new GraphicsContextMissingException();
63
 
64
            Context = (GraphicsContext)context;
65
            lock (SyncRoot)
66
            {
67
                if (!ContextErrors.ContainsKey(Context))
68
                    ContextErrors.Add(Context, new List<ErrorCode>());
69
            }
70
            ResetErrors();
71
        }
72
 
73
        #endregion
74
 
75
        #region Public Members
76
 
77
        // Retrieve all OpenGL errors to clear the error list.
78
        // See http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/geterror.html
79
        [Conditional("DEBUG")]
80
        internal void ResetErrors()
81
        {
82
            if (Context.ErrorChecking)
83
            {
84
                while (GL.GetError() != ErrorCode.NoError)
85
                { }
86
            }
87
        }
88
 
89
        // Retrieve all OpenGL errors and throw an exception if anything other than NoError is returned.
90
        [Conditional("DEBUG")]
91
        internal void CheckErrors()
92
        {
93
            if (Context.ErrorChecking)
94
            {
95
                List<ErrorCode> error_list = ContextErrors[Context];
96
                error_list.Clear();
97
                ErrorCode error;
98
                do
99
                {
100
                    error = GL.GetError();
101
                    error_list.Add(error);
102
                } while (error != ErrorCode.NoError);
103
 
104
                if (error_list.Count != 1)
105
                {
106
                    StringBuilder sb = new StringBuilder();
107
                    foreach (ErrorCode e in error_list)
108
                    {
109
                        if (e != ErrorCode.NoError)
110
                        {
111
                            sb.Append(e.ToString());
112
                            sb.Append(", ");
113
                        }
114
                        else
115
                            break;
116
                    }
117
                    sb.Remove(sb.Length - 2, 2); // Remove the last comma
118
 
119
                    throw new GraphicsErrorException(sb.ToString());
120
                }
121
            }
122
        }
123
 
124
        #endregion
125
 
126
        #region IDisposable Members
127
 
128
        public void Dispose()
129
        {
130
            CheckErrors();
131
        }
132
 
133
        #endregion
134
    }
135
}