MPMA Framework 0.4
TextWriter.h
Go to the documentation of this file.
00001 
00002 //Luke Lenhart, 2010
00003 //See /docs/License.txt for details on how this code may be used.
00004 
00005 #pragma once
00006 
00007 #include "../base/Setup.h"
00008 
00009 #ifdef MPMA_COMPILE_GFX
00010 
00011 #include "../base/Types.h"
00012 #include "../base/Vary.h"
00013 #include "../gfxsetup/GL.h"
00014 #include "../gfxsetup/Extensions.h"
00015 #include "Texture.h"
00016 #include "Vertex.h"
00017 #include <string>
00018 
00019 #ifdef GFX_USES_IMAGEMAGICK
00020 
00021 namespace
00022 {
00023     class FontCacheEntry;
00024 }
00025 
00026 namespace GFX
00027 {
00029     enum FontType
00030     {
00031         VARIABLE_SERIF   =0x0000, 
00032         VARIABLE_SANSERIF=0x0001, 
00033         FIXED_SERIF      =0x0100, 
00034         //FIXED_SANSERIF   =0x0101
00035     };
00036 
00038     struct TextColor
00039     {
00040         uint8 Red; 
00041         uint8 Green; 
00042         uint8 Blue; 
00043         uint8 Opacity; 
00044 
00046         TextColor(): Red(255), Green(255), Blue(255), Opacity(255) {}
00048         TextColor(uint8 red, uint8 green, uint8 blue, uint8 opacity=255): Red(red), Green(green), Blue(blue), Opacity(opacity) {}
00049     };
00050 
00052     class EncodedText
00053     {
00054     public:
00056         inline EncodedText() {}
00058         inline EncodedText(const std::string &plainText)
00059         {
00060             AppendPlainText(plainText);
00061         }
00062 
00064         inline void Clear()
00065         {
00066             data.clear();
00067         }
00068 
00070         void AppendColorChange(uint8 red, uint8 green, uint8 blue, uint8 opacity=255);
00072         inline void AppendColorChange(const TextColor &color)
00073         {
00074             AppendColorChange(color.Red, color.Green, color.Blue, color.Opacity);
00075         }
00077         void AppendPlainText(const std::string &text);
00078 
00080         EncodedText& operator<<(const char *sstring);
00082         EncodedText& operator<<(const std::string &sstring);
00084         EncodedText& operator<<(const MPMA::Vary &vstring);
00086         EncodedText& operator<<(const TextColor &color);
00088         EncodedText& operator<<(const EncodedText &et);
00089     private:
00090         std::vector<uint8> data;
00091 
00092         friend class TextWriter;
00093     };
00094 
00099     class TextWriter
00100     {
00101     public:
00103         static void ClearFontCache();
00104 
00105         // -- Step 1: Convert the text to an internal image.
00106 
00108         EncodedText Text;
00109 
00111         nuint FontSize;
00112 
00114         FontType TextFont;
00115 
00117         nuint TextWidth;
00118 
00120         bool CreateTextImage();
00121 
00123         inline nuint GetTextHeight() const { return textHeight; }
00124 
00126         inline nuint GetTextWidth() const { return sourceTextWidth; }
00127 
00128         // -- Step 2: Convert the internal image to a texture that can be used for rendering.
00129 
00131         nuint ViewWindowHeight;
00132 
00134         nuint ViewWindowScroll;
00135 
00137         inline nuint GetMaxViewWindowScroll() const
00138         {
00139             nsint actualViewHeight=(nsint)GetActualViewHeight();
00140             nsint extraHeightPixels=textHeight-actualViewHeight;
00141             if (extraHeightPixels<0)
00142                 extraHeightPixels=0;
00143             return (nuint)extraHeightPixels;
00144         }
00145 
00147         bool DebugDrawBorder;
00148 
00150         bool CreateTexture();
00151 
00153         inline nuint GetTextureWidth();
00155         inline nuint GetTextureHeight();
00156 
00157         // -- Step 3: Render the texture using these properties.
00158 
00160         inline Texture2D& GetTexture() { return texture; }
00161 
00163         inline float GetTexCoordUMin() const { return renderUMin; }
00165         inline float GetTexCoordUMax() const { return renderUMax; }
00167         inline float GetTexCoordVMin() const { return renderVMin; }
00169         inline float GetTexCoordVMax() const { return renderVMax; }
00170 
00172         inline nuint GetRenderWidth() const { return renderWidth; }
00174         inline nuint GetRenderHeight() const { return renderHeight; }
00175 
00177         void RenderTexture(float leftX, float topY);
00178 
00179         // --
00180 
00181         //
00182         TextWriter(); 
00183         TextWriter(const TextWriter &other); 
00184         TextWriter(TextWriter &&other); 
00185 
00186     private:
00187         void Init();
00188         void CopyValues(const TextWriter &other, bool copyMovableValues);
00189 
00190         std::vector<uint32> textPixels;
00191         nuint textHeight;
00192         nuint sourceTextWidth;
00193 
00194         struct CharacterRenderListEntry
00195         {
00196             nuint CharacterIndex;
00197             nuint X;
00198             nuint Y;
00199             uint32 Color;
00200         };
00201         nuint ComputeWrappedCharacterRendering(const FontCacheEntry &font, std::vector<CharacterRenderListEntry> &outRenderList);
00202         void ComputeUnboundedCharacterRendering(const FontCacheEntry &font, std::vector<CharacterRenderListEntry> &outRenderList, nuint &outTextWidth, nuint &outTextHeight);
00203 
00204         nuint textureWidth, textureHeight;
00205 
00206         Texture2D texture;
00207         VertexBuffer vb;
00208         InterleavedVertexFormat vbFormat;
00209         void InitGL();
00210         void SetVB(float leftX, float bottomY);
00211 
00212         float renderUMin, renderUMax;
00213         float renderVMin, renderVMax;
00214         nuint renderWidth, renderHeight;
00215 
00216         inline nuint GetActualViewHeight() const
00217         {
00218             nuint actualViewHeight=ViewWindowHeight;
00219 
00220             if (actualViewHeight==0)
00221                 actualViewHeight=GetTextHeight();
00222 
00223             if (actualViewHeight>GetTextHeight())
00224                 actualViewHeight=GetTextHeight();
00225 
00226             return actualViewHeight;
00227         }
00228 
00229         std::vector<uint32> texturePixels; //temporary cache during CreateTexture, doesn't need persisted
00230 
00231         bool createTextureEverCalled;
00232         bool createTextImageEverCalled;
00233     };
00234 }
00235 
00236 #endif //#ifdef GFX_USES_IMAGEMAGICK
00237 
00238 #endif //#ifdef MPMA_COMPILE_GFX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends