From e4f27b1966bf00ae5695dbc132e398ffd1548a42 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 11 Mar 2014 21:48:54 +0400 Subject: [PATCH] text drawing is working ok under OpenGL --- src/dlangui/graphics/gldrawbuf.d | 97 +++++++++++++++++-------- src/dlangui/graphics/glsupport.d | 30 -------- src/dlangui/platforms/common/platform.d | 2 + 3 files changed, 67 insertions(+), 62 deletions(-) diff --git a/src/dlangui/graphics/gldrawbuf.d b/src/dlangui/graphics/gldrawbuf.d index bb8e1ffe..2326c292 100644 --- a/src/dlangui/graphics/gldrawbuf.d +++ b/src/dlangui/graphics/gldrawbuf.d @@ -59,6 +59,16 @@ class GLDrawBuf : DrawBuf { /// draw 8bit alpha image - usually font glyph using specified color (clipping is applied) override void drawGlyph(int x, int y, Glyph * glyph, uint color) { assert(_scene !is null); + Rect dstrect = Rect(x,y, x + glyph.blackBoxX, y + glyph.blackBoxY); + Rect srcrect = Rect(0, 0, glyph.blackBoxX, glyph.blackBoxY); + //Log.v("GLDrawBuf.frawFragment dst=", dstrect, " src=", srcrect); + if (applyClipping(dstrect, srcrect)) { + GLGlyphCacheItem item = glGlyphCache.get(glyph.id); + if (item is null) + item = glGlyphCache.set(glyph); + // TODO: clipping + _scene.add(new GlyphSceneItem(glyph.id, dstrect, srcrect, color, null)); + } } /// draw source buffer rectangle contents to destination buffer override void drawFragment(int x, int y, DrawBuf src, Rect srcrect) { @@ -113,18 +123,6 @@ class Scene { } } -class SolidRectSceneItem : SceneItem { - Rect _rc; - uint _color; - this(Rect rc, uint color) { - _rc = rc; - _color = color; - } - override void draw() { - drawSolidFillRect(_rc, _color, _color, _color, _color); - } -} - private __gshared int activeSceneCount = 0; bool hasActiveScene() { return activeSceneCount > 0; @@ -147,15 +145,23 @@ void onObjectDestroyedCallback(uint pobject) { glImageCache.onCachedObjectDeleted(pobject); } +/// object deletion listener callback function type +void onGlyphDestroyedCallback(uint pobject) { + glGlyphCache.onCachedObjectDeleted(pobject); +} private __gshared GLImageCache glImageCache; +private __gshared GLGlyphCache glGlyphCache; + shared static this() { glImageCache = new GLImageCache(); + glGlyphCache = new GLGlyphCache(); } void LVGLClearImageCache() { glImageCache.clear(); + glGlyphCache.clear(); } private class GLImageCacheItem { @@ -549,10 +555,10 @@ public: _map.clear(); } /// draw cached item - void drawItem(uint objectId, Rect dstrc, Rect srcrc, uint color, int options, Rect * clip, int rotationAngle) { + void drawItem(uint objectId, Rect dstrc, Rect srcrc, uint color, Rect * clip) { if (objectId in _map) { GLGlyphCacheItem item = _map[objectId]; - item.page.drawItem(item, dstrc, srcrc, color, options, clip, rotationAngle); + item.page.drawItem(item, dstrc, srcrc, color, clip); } } /// handle cached object deletion, mark as deleted @@ -684,7 +690,7 @@ public: _needUpdateTexture = true; return cacheItem; } - void drawItem(GLGlyphCacheItem item, Rect dstrc, Rect srcrc, uint color, uint options, Rect * clip, int rotationAngle) { + void drawItem(GLGlyphCacheItem item, Rect dstrc, Rect srcrc, uint color, Rect * clip) { //CRLog::trace("drawing item at %d,%d %dx%d <= %d,%d %dx%d ", x, y, dx, dy, srcx, srcy, srcdx, srcdy); if (_needUpdateTexture) updateTexture(); @@ -693,13 +699,6 @@ public: Log.e("Invalid texture ", _textureId); return; } - //rotationAngle = 0; - int rx = dstrc.middlex; - int ry = dstrc.middley; - if (rotationAngle) { - //rotationAngle = 0; - //setRotation(rx, ry, rotationAngle); - } // convert coordinates to cached texture srcrc.offset(item._rc.left, item._rc.top); if (clip) { @@ -721,16 +720,7 @@ public: dstrc.bottom -= clip.bottom; } if (!dstrc.empty) - drawColorAndTextureRect(_textureId, _tdx, _tdy, srcrc, dstrc, color, srcrc.width() != dstrc.width() || srcrc.height() != dstrc.height()); - //drawColorAndTextureRect(vertices, texcoords, color, _textureId); - - if (rotationAngle) { - // unset rotation - setRotation(rx, ry, 0); - // glMatrixMode(GL_PROJECTION); - // glPopMatrix(); - // checkError("pop matrix"); - } + drawColorAndTextureRect(_textureId, _tdx, _tdy, srcrc, dstrc, color, false); } } @@ -740,3 +730,46 @@ public: updateTexture(); } }; + + + + + + +class SolidRectSceneItem : SceneItem { + Rect _rc; + uint _color; + this(Rect rc, uint color) { + _rc = rc; + _color = color; + } + override void draw() { + drawSolidFillRect(_rc, _color, _color, _color, _color); + } +} + +private class GlyphSceneItem : SceneItem { + uint objectId; + Rect dstrc; + Rect srcrc; + uint color; + Rect * clip; +public: + override void draw() { + if (glGlyphCache) + glGlyphCache.drawItem(objectId, dstrc, srcrc, color, clip); + } + this(uint _objectId, Rect _dstrc, Rect _srcrc, uint _color, Rect * _clip) + { + objectId = _objectId; + dstrc = _dstrc; + srcrc = _srcrc; + color = _color; + clip = _clip; + } + ~this() { + } +}; + + + diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index e4d51efa..7dc18354 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -317,40 +317,10 @@ void QMatrix4x4_ortho(float left, float right, float bottom, float top, float ne qtmatrix[y * 4 + x] = m[y][x]; } -/* -void myGlOrtho(float left, float right, float bottom, float top, - float zNearPlane, float zFarPlane) -{ - float r_l = 1.0f / (right - left); - float t_b = 1.0f / (top - bottom); - float f_n = 1.0f / (zFarPlane - zNearPlane); - - foreach(ref float item; matrix) - item = 0; - //memset(m, 0, sizeof(m)); - matrix[0] = 2.0f * r_l; - matrix[5] = 2.0f * t_b; - matrix[10] = -2.0f * f_n; - matrix[12] = (-(right + left)) * r_l; - matrix[13] = (-(top + bottom)) * t_b; - matrix[14] = (-(zFarPlane + zNearPlane)) * f_n; -} -*/ - void setOrthoProjection(int dx, int dy) { - //myGlOrtho(0, dx, 0, dy, -1.0f, 5.0f); bufferDx = dx; bufferDy = dy; - //myGlOrtho(0, dx, 0, dy, -0.1f, 5.0f); - - //Log.d("Ortho ", dx, "x", dy); - - //m = mat4.orthographic(0, dx, 0, dy, 0.5f, 50.0f); - //myGlOrtho(0, dx, 0, dy, 0.5f, 50.0f); QMatrix4x4_ortho(0, dx, 0, dy, 0.5f, 50.0f); - //Log.d("Matrix: ", m); - //Log.d("myMatrix: ", matrix); - //Log.d("qtMatrix: ", qtmatrix); glViewport(0, 0, dx, dy); checkError("glViewport"); } diff --git a/src/dlangui/platforms/common/platform.d b/src/dlangui/platforms/common/platform.d index eae604e3..41b8dcb4 100644 --- a/src/dlangui/platforms/common/platform.d +++ b/src/dlangui/platforms/common/platform.d @@ -3,6 +3,7 @@ module dlangui.platforms.common.platform; import dlangui.widgets.widget; import dlangui.graphics.drawbuf; import std.file; +private import dlangui.graphics.gldrawbuf; public class Window { int _dx; @@ -56,6 +57,7 @@ private __gshared bool _OPENGL_ENABLED = false; /// call on app initialization if OpenGL support is detected void setOpenglEnabled() { _OPENGL_ENABLED = true; + glyphDestroyCallback = &onGlyphDestroyedCallback; } version (Windows) {