From 0cff3908feb4a382ba464b5c4fd9282b6ea67a8b Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Wed, 12 Mar 2014 10:15:28 +0400 Subject: [PATCH] fix problem with font colors under OpenGL --- src/dlangui/graphics/gldrawbuf.d | 53 ++++++++++++++++++-------- src/dlangui/graphics/glsupport.d | 5 +-- src/dlangui/platforms/windows/winapp.d | 16 +++----- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/dlangui/graphics/gldrawbuf.d b/src/dlangui/graphics/gldrawbuf.d index 6d6f9b2a..3aa5d913 100644 --- a/src/dlangui/graphics/gldrawbuf.d +++ b/src/dlangui/graphics/gldrawbuf.d @@ -67,7 +67,7 @@ class GLDrawBuf : DrawBuf { 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); + Log.v("GLDrawBuf.drawGlyph dst=", dstrect, " src=", srcrect, " color=", color); if (applyClipping(dstrect, srcrect)) { if (!glGlyphCache.get(glyph.id)) glGlyphCache.put(glyph); @@ -248,13 +248,15 @@ private class GLImageCache { } } - void invertAlpha(GLImageCacheItem item) { + void convertPixelFormat(GLImageCacheItem item) { Rect rc = item._rc; - for (int y = rc.top; y < rc.bottom; y++) { + for (int y = rc.top - 1; y <= rc.bottom; y++) { uint * row = _drawbuf.scanLine(y); - for (int x = rc.left; x < rc.right; x++) { + for (int x = rc.left - 1; x <= rc.right; x++) { uint cl = row[x]; + // invert A cl ^= 0xFF000000; + // swap R and B uint r = (cl & 0x00FF0000) >> 16; uint b = (cl & 0x000000FF) << 16; row[x] = (cl & 0xFF00FF00) | r | b; @@ -304,7 +306,7 @@ private class GLImageCache { return null; buf.onDestroyCallback = &onObjectDestroyedCallback; _drawbuf.drawImage(cacheItem._rc.left, cacheItem._rc.top, buf); - invertAlpha(cacheItem); + convertPixelFormat(cacheItem); _needUpdateTexture = true; return cacheItem; } @@ -507,8 +509,8 @@ private class TextureSceneItem : SceneItem { }; - - +/// by some reason ALPHA texture does not work as expected +private immutable USE_RGBA_TEXTURE_FOR_GLYPHS = true; private class GLGlyphCache { @@ -555,6 +557,9 @@ private class GLGlyphCache { } } + static if (USE_RGBA_TEXTURE_FOR_GLYPHS) { + uint[] _rgbaBuffer; + } void updateTexture() { if (_drawbuf is null) return; // no draw buffer!!! @@ -566,10 +571,22 @@ private class GLGlyphCache { } //CRLog::debug("updateTexture - setting image %dx%d", _drawbuf.width, _drawbuf.height); ubyte * pixels = _drawbuf.scanLine(0); - if (!setTextureImageAlpha(_textureId, _drawbuf.width, _drawbuf.height, pixels)) { - deleteTexture(_textureId); - _textureId = 0; - return; + static if (USE_RGBA_TEXTURE_FOR_GLYPHS) { + int len = _drawbuf.width * _drawbuf.height; + _rgbaBuffer.length = len; + for (int i = 0; i < len; i++) + _rgbaBuffer[i] = ((cast(uint)pixels[i]) << 24) | 0x00FFFFFF; + if (!setTextureImage(_textureId, _drawbuf.width, _drawbuf.height, cast(ubyte*)_rgbaBuffer.ptr)) { + deleteTexture(_textureId); + _textureId = 0; + return; + } + } else { + if (!setTextureImageAlpha(_textureId, _drawbuf.width, _drawbuf.height, pixels)) { + deleteTexture(_textureId); + _textureId = 0; + return; + } } _needUpdateTexture = false; if (_closed) { @@ -650,8 +667,10 @@ private class GLGlyphCache { dstrc.top += clip.top; dstrc.bottom -= clip.bottom; } - if (!dstrc.empty) + if (!dstrc.empty) { + Log.d("drawing glyph with color ", color); drawColorAndTextureRect(_textureId, _tdx, _tdy, srcrc, dstrc, color, false); + } } } @@ -659,6 +678,9 @@ private class GLGlyphCache { _closed = true; if (_needUpdateTexture) updateTexture(); + static if (USE_RGBA_TEXTURE_FOR_GLYPHS) { + _rgbaBuffer = null; + } } } @@ -723,10 +745,9 @@ private class GLGlyphCache { } /// draw cached item 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, clip); - } + GLGlyphCacheItem * item = objectId in _map; + if (item) + item.page.drawItem(*item, dstrc, srcrc, color, clip); } /// handle cached object deletion, mark as deleted void onCachedObjectDeleted(uint objectId) { diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 7dc18354..fdbb38ce 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -63,7 +63,6 @@ void drawSolidFillRect(Rect rc, uint color1, uint color2, uint color3, uint colo _solidFillProgram.execute(vertices, colors); } else Log.e("No program"); - //drawSolidFillRect(vertices, colors); } void drawColorAndTextureRect(uint textureId, int tdx, int tdy, Rect srcrc, Rect dstrc, uint color, bool linear) { @@ -72,9 +71,6 @@ void drawColorAndTextureRect(uint textureId, int tdx, int tdy, Rect srcrc, Rect } void drawColorAndTextureRect(uint textureId, int tdx, int tdy, int srcx, int srcy, int srcdx, int srcdy, int xx, int yy, int dx, int dy, uint color, bool linear) { - //if (crconfig.getTextureFormat() == TEXTURE_ALPHA) { - // color = 0x000000; - //} float colors[6*4]; LVGLFillColor(color, colors.ptr, 6); float dstx0 = cast(float)xx; @@ -466,6 +462,7 @@ class SolidFillProgram : GLProgram { glDisable(GL_CULL_FACE); checkError("glDisable(GL_CULL_FACE)"); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); bind(); //glUniformMatrix4fv(matrixLocation, 1, false, m.value_ptr); diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index 6880e92c..f90fa998 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -12,6 +12,7 @@ import std.file; import dlangui.platforms.common.platform; import dlangui.platforms.windows.win32fonts; import dlangui.platforms.windows.win32drawbuf; +import dlangui.widgets.styles; import dlangui.graphics.drawbuf; import dlangui.graphics.images; import dlangui.graphics.fonts; @@ -250,21 +251,16 @@ class Win32Window : Window { GLDrawBuf buf = new GLDrawBuf(_dx, _dy, false); buf.beforeDrawing(); if (false) { + // for testing for render buf.fillRect(Rect(100, 100, 200, 200), 0x704020); buf.fillRect(Rect(40, 70, 100, 120), 0x000000); buf.fillRect(Rect(80, 80, 150, 150), 0x80008000); // green - DrawableRef img = drawableCache.get("exit"); - if (!img.isNull) { - img.drawTo(buf, Rect(300, 100, 364, 164)); - img.drawTo(buf, Rect(400, 200, 528, 328)); - } - DrawableRef img2 = drawableCache.get("btn_default_pressed"); - if (!img2.isNull) { - img2.drawTo(buf, Rect(300, 200, 564, 264)); - img2.drawTo(buf, Rect(600, 200, 628, 328)); - } + drawableCache.get("exit").drawTo(buf, Rect(300, 100, 364, 164)); + drawableCache.get("btn_default_pressed").drawTo(buf, Rect(300, 200, 564, 264)); drawableCache.get("btn_default_normal").drawTo(buf, Rect(300, 0, 400, 50)); drawableCache.get("btn_default_selected").drawTo(buf, Rect(0, 0, 100, 50)); + FontRef fnt = currentTheme.font; + fnt.drawText(buf, 40, 40, "Some Text 1234567890 !@#$^*", 0x80FF0000); } else { onDraw(buf); }