📜 ⬆️ ⬇️

GLGDI + or “switch from GDI + to OpenTK”

I think many have had to deal with the problem that GDI + slows down, and slows down, because it is not accelerated by iron.
So I once wrote a level editor

and I realized that GDI + is no longer enough for me, I need to move on to something accelerated, and in minimal time. A little googling, I decided to opt for the OpenTK library.

OpenTK (Open Toolkit) is a low-level library that is a wrapper for OpenGL, OpenGL ES, OpenCL and OpenAL. This library is designed for games, scientific applications, as well as for any other applications that require 3D-graphics, audio and computational functionality. OpenTK is a cross-platform library and works on both Mono and .NET.

Why the choice fell on OpenTK? First, OpenTK provides the GLControl class (which is a direct descendant of UserControl), which allows you to simplify the transition to this library. Secondly, the bundle includes the TextPrinter class, which makes it quite easy to display text on the screen. Thirdly, during googling, I found a small archive “Engine.zip” with a set of classes for 2D rendering, OpenGL initialization and other amenities.

At first, I planned to describe which pitfalls I encountered when switching to OpenTK, but, recalling that the issue of switching from GDI + to something fast is quite popular, I decided to write a small library that mimics GDI + and put it in open access. I called it GLGDI + . Remember that this is a lightweight library and contains only the most necessary minimum and does not attempt to completely copy the GDI + API. It contains the classes GLGraphics, GLImage and GLMultiImage. GLGraphics contains the following methods:

Some moments

I will still describe some points that I had to face when using OpenTK. There are good tutorials on the OpenTK site, which I, of course, have not read, I’d read them, and there would be fewer problems. But, that was, that was:
  1. The first thing I encountered was that the control did not redraw at all at startup, it was decided simply, you need to remember to call the SwapBuffers method at the end of OnPaint, GLControl already has the built-in DoubleBuffer functionality, you only need to switch the buffer
  2. The next moment I stumbled over was black rectangles instead of images. It was solved by calling image.SetBlending () before rendering, now all images are drawn with blending in the library, so there should be no problems with it
  3. When I transferred the second control to OpenTK rails, it turned out that the first control stopped performing its functions after that, it was decided to call the MakeCurrent method on OnPaint before drawing the elements
  4. TextPrinter (the basis for DrawString) sometimes falls when displaying text, there's nothing you can do about it - it's buggy and it falls on certain combinations of drawing quality, font and font size, I had to choose the font and size
  5. You also need to keep track of what control was loaded and only after that work with OpenTK / GLGraphics, and also check that the control is not in the form designer mode
  6. When resizing controls you need to remember to call GLGraphics.Resize (Width, Height)


The library is on GoogleCode , under the BSD license. If anyone needs - use on health. To understand how to work with the library - look at the source code of the sample that comes with it, it is very simple:

All the “moments” described above are taken into account.
I would not recommend using this library for products aimed at a mass user - it is too young for this, but in utilities it is quite suitable for use.
At the moment I have no plans for the development of the library - I have enough of its functionality. But bugfixes and patches are welcome :)

Judging by what I read on the OpenTK forum, TextPrinter (used for text output) will be excluded from the library in the future, but perhaps in a separate project. So his fate is still foggy. Now he is in the Deprecated state, therefore he gives warnings when compiling the library.
Now the map editor is much faster and does not slow down anything, which is what you want!

UPD: forgot to provide a link to the site OpenTK
UPD2: fixed a bug with the paths in the source code of the sample (both in the archive and in SVN)

Source: https://habr.com/ru/post/104152/

All Articles