
My little using()-trick to ease the burden on the programmer..
Posted Saturday, 1 November, 2008 - 20:34 by objarni inMy GL-drawing code got completely scrambled today because I forgot to put an GL.End() after my GL.Vertex2f-code.
So, I thought, can't I do something to relieve me of the burden of matching GL.Begin-End pairs all the time..?
This is what I came up with, thought I'd share it for anyone else to use in case you also forget those evil GL.End()-lines every now and then!
Update: After Kamujins complaint about allocating objects every frame, I changed the class to this which allocates only one object per application run:
class GLDraw : IDisposable { private GLDraw() { } private static GLDraw drawer = new GLDraw(); public static GLDraw Begin(BeginMode mode) { GL.Begin(mode); return drawer; } public void Dispose() { GL.End(); } } .... using (GLDraw.Begin(BeginMode.Quads)) { GL.Vertex2(0, 0); GL.Vertex2(size, 0); GL.Vertex2(size, size); GL.Vertex2(0, size); }
Happy coding,


Comments
Re: little using()-trick
Interesting idea with the using statement, just for completeness this should be mentioned as an option aswell:
Note the curly braces.
Re: My little using()-trick to ease the burden on the..
True :)
But the using trick does make sense if you want to maintain some basic guarantees when exceptions can be thrown (not likely in this example, but think of buffer mapping/unmapping).
Re: My little using()-trick...
Hi, I've came about the same idea about a month ago. I did it with structs and generics to avoid boxing...
It's pretty convenient to bind textures and shaders. BTW, is there a way to push/pop shaders?
I'm attaching my version. Warning: ugly hacks!
ps. OpenTK rules!
Re: My little using()-trick to ease the burden on the...
This is a very Interesting approach, especially the IBindable and implicit / explicit use interfaces. Thanks for sharing!
One snag is that structs are autoboxed when cast to an interface (IDisposable in this case), which means they approach does generate garbage. This may or may not have an effect in real use, but it's something to be aware of.
Have you considered using an object pool of Binders instead? If you are especially worried about GC performance, this could completely eliminate allocations.
Re: My little using()-trick to ease the burden on the programmer
For structs there's the IBindableExplicitUse interface. It does not make any heap allocations. For instance you can do semething like this:
For static state usage like glBegin/glEnd and glPush/glPos the above GLDraw class pattern is probably most optimal, but the IBindable pattern has an advantage because the Binder struct can contain the state object to be cleaned up and/or the old one to be restored.
Now I'm going to sleep... ^c^ ...work... *c*
Re: My little using()-trick to ease the burden on the ...
I just re-read your code and the generic Binder struct manages to avoid the cast-to-interface entirely. That's some awesome work, kudos!
Please ignore my previous post, I understood your code wrongly.
Re: My little using()-trick to ease the burden on the programmer
The IBinder interface is a really good idea. I've used the using pattern for a long time, but never thought of combining it with extension methods.
I think I found an improvement: instead of the the explicit versions, you can just use a generic extension method and do it implicitly.
Re: My little using()-trick to ease the burden on the programmer
I didn't know it was possible, thanks, works like a charm :)
If you want to "use" matrices without modifying OpenTK's code...
usage: