00001 #region License
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #endregion
00027
00028 using System;
00029 using System.Collections.Generic;
00030 using System.Text;
00031 using System.Diagnostics;
00032
00033 namespace OpenTK.Graphics.OpenGL
00034 {
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 struct ErrorHelper : IDisposable
00046 {
00047 #region Fields
00048
00049 static readonly object SyncRoot = new object();
00050 static readonly Dictionary<GraphicsContext, List<ErrorCode>> ContextErrors =
00051 new Dictionary<GraphicsContext, List<ErrorCode>>();
00052 readonly GraphicsContext Context;
00053
00054 #endregion
00055
00056 #region Constructors
00057
00058 public ErrorHelper(IGraphicsContext context)
00059 {
00060 if (context == null)
00061 throw new GraphicsContextMissingException();
00062
00063 Context = (GraphicsContext)context;
00064 lock (SyncRoot)
00065 {
00066 if (!ContextErrors.ContainsKey(Context))
00067 ContextErrors.Add(Context, new List<ErrorCode>());
00068 }
00069 ResetErrors();
00070 }
00071
00072 #endregion
00073
00074 #region Public Members
00075
00076
00077
00078 [Conditional("DEBUG")]
00079 internal void ResetErrors()
00080 {
00081 if (Context.ErrorChecking)
00082 {
00083 while (GL.GetError() != ErrorCode.NoError)
00084 { }
00085 }
00086 }
00087
00088
00089 [Conditional("DEBUG")]
00090 internal void CheckErrors()
00091 {
00092 if (Context.ErrorChecking)
00093 {
00094 List<ErrorCode> error_list = ContextErrors[Context];
00095 error_list.Clear();
00096 ErrorCode error;
00097 do
00098 {
00099 error = GL.GetError();
00100 error_list.Add(error);
00101 } while (error != ErrorCode.NoError);
00102
00103 if (error_list.Count != 1)
00104 {
00105 StringBuilder sb = new StringBuilder();
00106 foreach (ErrorCode e in error_list)
00107 {
00108 if (e != ErrorCode.NoError)
00109 {
00110 sb.Append(e.ToString());
00111 sb.Append(", ");
00112 }
00113 else
00114 break;
00115 }
00116 sb.Remove(sb.Length - 2, 2);
00117
00118 throw new GraphicsErrorException(sb.ToString());
00119 }
00120 }
00121 }
00122
00123 #endregion
00124
00125 #region IDisposable Members
00126
00127 public void Dispose()
00128 {
00129 CheckErrors();
00130 }
00131
00132 #endregion
00133 }
00134 }