Blender  V2.59
KX_FontObject.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: KX_FontObject.cpp 35171 2011-02-25 13:35:59Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00034 #include "KX_FontObject.h"
00035 #include "DNA_curve_types.h"
00036 #include "KX_Scene.h"
00037 #include "KX_PythonInit.h"
00038 #include "BLI_math.h"
00039 
00040 extern "C" {
00041 #include "BLF_api.h"
00042 }
00043 
00044 #define BGE_FONT_RES 100
00045 
00046 KX_FontObject::KX_FontObject(   void* sgReplicationInfo,
00047                                                                 SG_Callbacks callbacks,
00048                                                                 RAS_IRenderTools* rendertools,
00049                                                                 Object *ob):
00050         KX_GameObject(sgReplicationInfo, callbacks),
00051         m_object(ob),
00052         m_dpi(72),
00053         m_resolution(1.f),
00054         m_rendertools(rendertools)
00055 {
00056         Curve *text = static_cast<Curve *> (ob->data);
00057         m_text = text->str;
00058         m_fsize = text->fsize;
00059 
00060         /* FO_BUILTIN_NAME != "default" */
00061         /* I hope at some point Blender (2.5x) can have a single font   */
00062         /* with unicode support for ui and OB_FONT                      */
00063         /* once we have packed working we can load the FO_BUILTIN_NAME font     */
00064         const char* filepath = text->vfont->name;
00065         if (strcmp(FO_BUILTIN_NAME, filepath) == 0)
00066                 filepath = "default";
00067 
00068         /* XXX - if it's packed it will not work. waiting for bdiego (Diego) fix for that. */
00069         m_fontid = BLF_load(filepath);
00070         if (m_fontid == -1)
00071                 m_fontid = BLF_load("default");
00072 
00073         /* initialize the color with the object color and store it in the KX_Object class
00074            This is a workaround waiting for the fix:
00075            [#25487] BGE: Object Color only works when it has a keyed frame */
00076         copy_v4_v4(m_color, (const float*) ob->col);
00077         this->SetObjectColor((const MT_Vector4&) m_color);
00078 }
00079 
00080 KX_FontObject::~KX_FontObject()
00081 {
00082         //remove font from the scene list
00083         //it's handled in KX_Scene::NewRemoveObject
00084 }
00085 
00086 CValue* KX_FontObject::GetReplica() {
00087         KX_FontObject* replica = new KX_FontObject(*this);
00088         replica->ProcessReplica();
00089         return replica;
00090 }
00091 
00092 void KX_FontObject::ProcessReplica()
00093 {
00094         KX_GameObject::ProcessReplica();
00095         KX_GetActiveScene()->AddFont(this);
00096 }
00097 
00098 void KX_FontObject::DrawText()
00099 {
00100         /* only draws the text if visible */
00101         if(this->GetVisible() == 0) return;
00102 
00103         /* update the animated color */
00104         this->GetObjectColor().getValue(m_color);
00105 
00106         /* XXX 2DO - handle multiple lines */
00107         /* HARDCODED MULTIPLICATION FACTOR - this will affect the render resolution directly */
00108         float RES = BGE_FONT_RES * m_resolution;
00109 
00110         float size = m_fsize * m_object->size[0] * RES;
00111         float aspect = 1.f / (m_object->size[0] * RES);
00112 
00113         m_rendertools->RenderText3D(m_fontid, m_text, int(size), m_dpi, m_color, this->GetOpenGLMatrix(), aspect);
00114 }
00115 
00116 #ifdef WITH_PYTHON
00117 
00118 /* ------------------------------------------------------------------------- */
00119 /* Python Integration Hooks                                                                      */
00120 /* ------------------------------------------------------------------------- */
00121 
00122 PyTypeObject KX_FontObject::Type = {
00123         PyVarObject_HEAD_INIT(NULL, 0)
00124         "KX_FontObject",
00125         sizeof(PyObjectPlus_Proxy),
00126         0,
00127         py_base_dealloc,
00128         0,
00129         0,
00130         0,
00131         0,
00132         py_base_repr,
00133         0,
00134         &KX_GameObject::Sequence,
00135         &KX_GameObject::Mapping,
00136         0,0,0,
00137         NULL,
00138         NULL,
00139         0,
00140         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00141         0,0,0,0,0,0,0,
00142         Methods,
00143         0,
00144         0,
00145         &KX_GameObject::Type,
00146         0,0,0,0,0,0,
00147         py_base_new
00148 };
00149 
00150 PyMethodDef KX_FontObject::Methods[] = {
00151         {NULL,NULL} //Sentinel
00152 };
00153 
00154 PyAttributeDef KX_FontObject::Attributes[] = {
00155         KX_PYATTRIBUTE_STRING_RW("text", 0, 280, false, KX_FontObject, m_text), //arbitrary limit. 280 = 140 unicode chars in unicode
00156         KX_PYATTRIBUTE_FLOAT_RW("size", 0.0001f, 10000.0f, KX_FontObject, m_fsize),
00157         KX_PYATTRIBUTE_FLOAT_RW("resolution", 0.0001f, 10000.0f, KX_FontObject, m_resolution),
00158         /* KX_PYATTRIBUTE_INT_RW("dpi", 0, 10000, false, KX_FontObject, m_dpi), */// no real need for expose this I think
00159         { NULL }        //Sentinel
00160 };
00161 
00162 #endif // WITH_PYTHON