dmuratshin 9 years ago
parent
commit
c68fcbbe66

BIN
examples/HelloFreeType/data/images/button.png


+ 50 - 1
examples/HelloFreeType/src/example.cpp

@@ -9,6 +9,11 @@ using namespace oxygine;
 //It is important on mobile devices with limited memory and you would load/unload them
 Resources gameResources;
 
+enum TextMode
+{
+    tm_no_shadow,
+    tm_shadow
+};
 
 class MainActor: public Actor
 {
@@ -61,13 +66,17 @@ public:
         TextStyle style;
 #if OXYGINE_VERSION > 3
         style.font = gameResources.getResFont("main");
-        style.fontSize = 40;
+        style.fontSize = 80;
 #else
         style.font = gameResources.getResFont("main")->getFont(0, 40);
 #endif
         style.color = Color::Crimson;
         style.vAlign = TextStyle::VALIGN_MIDDLE;
         style.hAlign = TextStyle::HALIGN_CENTER;
+        style.baselineScale = 0.7f;
+
+        //apply our custom option
+        style.options = tm_shadow;
 
         text->setStyle(style);
         text->setText("Hello\n World!");
@@ -135,11 +144,51 @@ typedef oxygine::intrusive_ptr<MainActor> spMainActor;
 
 void example_preinit() {}
 
+void myShadowsFilter(ResFontFT::postProcessData& data)
+{
+    Image& destIm = *data.dest;
+    ImageData& src = *data.src;
+    const glyphOptions& opt = data.opt;
+
+    if (opt == tm_no_shadow)
+    {
+        //if shadows disabled just copy from src to dest with premultiply
+        destIm.init(src.w, src.h, TF_R8G8B8A8);
+        ImageData rc = destIm.lock();
+        operations::blitPremultiply(src, rc);
+        return;
+    }
+
+    //alpha premultiply
+    operations::premultiply(src);
+
+    const int xoffset = 4;
+    const int yoffset = 3;
+
+    //initialize destination Image with increased size
+    destIm.init(src.w + xoffset, src.h + yoffset, TF_R8G8B8A8);
+    //clear it
+    destIm.fill_zero();
+
+
+    ImageData rc = destIm.lock(Rect(xoffset, yoffset, src.w, src.h));
+    //copy black image as shadow
+    operations::blitColored(src, rc, Color(0, 0, 0, 255));
+
+    //copy original image
+    operations::op_blend_one_invSrcAlpha op;
+    ImageData rc2 = destIm.lock(Rect(0, 0, src.w, src.h));
+    operations::applyOperation(op, src, rc2);
+}
+
 //called from entry_point.cpp
 void example_init()
 {
     ResFontFT::initLibrary();
 
+    //use it for adding shadows
+    ResFontFT::setGlyphPostProcessor(myShadowsFilter);
+
     //load xml file with resources definition
     gameResources.loadXML("res.xml");
 

+ 12 - 4
src/ResFontFT.cpp

@@ -105,16 +105,19 @@ uint32_t decodeSymbol(int sym)
 
 namespace oxygine
 {
-    void ftGenDefault(ImageData& src, MemoryTexture& dest, int code, const glyphOptions& opt)
+    void ftGenDefault(ResFontFT::postProcessData& data)
     {
+        Image& dest = *data.dest;
+        const ImageData& src = *data.src;
+
         dest.init(src.w, src.h, TF_R8G8B8A8);
         ImageData rc = dest.lock();
         operations::blitPremultiply(src, rc);
     }
 
-    static ResFontFT::ftGenHook _ftGen = ftGenDefault;
+    static ResFontFT::postProcessHook _ftGen = ftGenDefault;
 
-    void ResFontFT::setGenHook(ftGenHook f)
+    void ResFontFT::setGlyphPostProcessor(postProcessHook f)
     {
         _ftGen = f;
     }
@@ -172,7 +175,12 @@ namespace oxygine
             if (src.w && src.h)
             {
 #if OXYGINE_VERSION > 4
-                _ftGen(src, mt, code, opt);
+                ResFontFT::postProcessData gd;
+                gd.src = &src;
+                gd.dest = &mt;
+                gd.code = code;
+                gd.opt = opt;
+                _ftGen(gd);
 #else
                 _ftGen(src, mt, code, 0);
 #endif

+ 17 - 2
src/ResFontFT.h

@@ -13,6 +13,7 @@ namespace oxygine
 {
     class CreateResourceContext;
     class FontFT;
+    class Image;
     typedef unsigned int glyphOptions;
 
     class ResFontFT : public ResFont
@@ -21,9 +22,23 @@ namespace oxygine
         static void initLibrary();
         static void freeLibrary();
 
-        typedef void(*ftGenHook)(ImageData& src, class Image& dest, int code, const glyphOptions& opt);
+        struct postProcessData
+        {
+            //you could modify src pixels too
+            ImageData* src;
 
-        static void setGenHook(ftGenHook);
+            //where you should write result
+            Image* dest;
+
+            //your value passed to TextStyle/TextField
+            glyphOptions opt;
+
+            //glyph code
+            int code;
+        };
+        typedef void(*postProcessHook)(postProcessData&);
+
+        static void setGlyphPostProcessor(postProcessHook);
 
         ResFontFT();
         ~ResFontFT();