Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDalai Felinto <dfelinto@gmail.com>2010-12-16 13:25:41 +0300
committerDalai Felinto <dfelinto@gmail.com>2010-12-16 13:25:41 +0300
commit0890b80ed9343ac9b86b40f612d5a994745d85d7 (patch)
tree8bdcdcfa1666c47d0651a93bc935af2301e02a77 /source/gameengine/Ketsji/KX_FontObject.cpp
parented4e7271f163b1d98feee7b956c0229cc47dad92 (diff)
Patch:[#25163] BGE support for Blender Font objects - unicode support
Problem/Bug: ------------ There were no way to have proper unicode characters (e.g. Japanese) in Blender Game Engine. Now we can :) You can see a sample here: http://blog.mikepan.com/multi-language-support-in-blender/ Functionality Explanation: -------------------------- This patch converts the Blender Font Objects to a new BGE type: KX_FontObject This object inherits KX_GameObject.cpp and has the following properties: - text (the text of the object) - size (taken from the Blender object, usually is 1.0) - resolution (1.0 by default, maybe not really needed, but at least for debugging/the time being it's nice to have) The way we deal with linked objects is different than Blender. In Blender the text and size are a property of the Text databock. Therefore linked objects necessarily share the same text (and size, although the size of the object datablock affects that too). In BGE they are stored and accessed per object. Without that it would be problematic to have addObject adding texts that don't share the same data. Known problems/limitations/ToDo: -------------------------------- 1) support for packed font and the <builtin> 2) figure why some fonts are displayed in a different size in 3DView/BGE (BLF) 3) investigate some glitches I see some times 4) support for multiline 5) support for more Blender Font Object options (text aligment, text boxes, ...) [1] Diego (bdiego) evantually will help on that. For the time being we are using the "default" (ui) font to replace the <builtin>. [2] but not all of them. I need to cross check who is calculating the size/dpi in/correctly - Blender or BLF. (e.g. fonts that work well - MS Gothic) [3] I think this may be related to the resolution we are drawing the font [4] It can't/will not be handled inside BFL. So the way I see it is to implement a mini text library/api that works as a middlelayer between the drawing step and BLF. So instead of: BLF_draw(fontid, (char *)text, strlen(text)); We would do: MAGIC_ROUTINE_IM_NOT_BLF_draw(fontir, (char *)text, styleflag, width, height); [5] don't hold your breath ... but if someone wants to have fun in the holidays the (4) and (5) are part of the same problem. Code Explanation: ----------------- The patch should be simple to read. They are three may parts: 1) BL_BlenderDataConversion.cpp:: converts the OB_FONT object into a KX_FontObject.cpp and store it in the KX_Scene->m_fonts 2) KetsjiEngine.cpp::RenderFonts:: loop through the texts and call their internal drawing routine. 3) KX_FontObject.cpp:: a) constructor: load the font of the object, and store other values. b) DrawText: calculate the aspect for the given size (sounds hacky but this is how blf works) and call the render routine in RenderTools 4) KX_BlenderGL.cpp (called from rendertools) ::BL_print_game_line:: Draws the text. Using the BLF API *) In order to handle visibility of the object added with AddObject I'm adding to the m_scene.m_fonts list only the Fonts in a visible layer - unlike Cameras and Lamps where all the objects are added. Acknowledgements: ---------------- Thanks Benoit for the review and adjustment suggestions. Thanks Diego for the BFL expertise, patches and support (Latin community ftw) Thanks my boss for letting me do part of this patch during work time. Good thing we are starting a project in a partnership with a Japanese Foundation and eventual will need unicode in BGE :) for more details on that - www.nereusprogram.org - let's call it the main sponsor of this "bug feature" ;)
Diffstat (limited to 'source/gameengine/Ketsji/KX_FontObject.cpp')
-rwxr-xr-xsource/gameengine/Ketsji/KX_FontObject.cpp147
1 files changed, 147 insertions, 0 deletions
diff --git a/source/gameengine/Ketsji/KX_FontObject.cpp b/source/gameengine/Ketsji/KX_FontObject.cpp
new file mode 100755
index 00000000000..2bd41b8fe1a
--- /dev/null
+++ b/source/gameengine/Ketsji/KX_FontObject.cpp
@@ -0,0 +1,147 @@
+/**
+ * $Id$
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#include "KX_FontObject.h"
+#include "DNA_curve_types.h"
+#include "KX_Scene.h"
+#include "KX_PythonInit.h"
+
+extern "C" {
+#include "BLF_api.h"
+}
+
+#define BGE_FONT_RES 100
+
+KX_FontObject::KX_FontObject( void* sgReplicationInfo,
+ SG_Callbacks callbacks,
+ RAS_IRenderTools* rendertools,
+ Object *ob):
+ KX_GameObject(sgReplicationInfo, callbacks),
+ m_rendertools(rendertools),
+ m_object(ob),
+ m_dpi(72),
+ m_resolution(1.f),
+ m_color(ob->col) /* initial color - non-animatable */
+{
+ Curve *text = static_cast<Curve *> (ob->data);
+ m_text = text->str;
+ m_fsize = text->fsize;
+
+ /* <builtin> != "default" */
+ /* I hope at some point Blender (2.5x) can have a single font */
+ /* with unicode support for ui and OB_FONT */
+ /* once we have packed working we can load the <builtin> font */
+ const char* filepath = text->vfont->name;
+ if (strcmp("<builtin>", filepath) == 0)
+ filepath = "default";
+
+ /* XXX - if it's packed it will not work. waiting for bdiego (Diego) fix for that. */
+ m_fontid = BLF_load(filepath);
+ if (m_fontid == -1)
+ m_fontid = BLF_load("default");
+}
+
+KX_FontObject::~KX_FontObject()
+{
+ //remove font from the scene list
+ //it's handled in KX_Scene::NewRemoveObject
+}
+
+CValue* KX_FontObject::GetReplica() {
+ KX_FontObject* replica = new KX_FontObject(*this);
+ replica->ProcessReplica();
+ return replica;
+}
+
+void KX_FontObject::ProcessReplica()
+{
+ KX_GameObject::ProcessReplica();
+ KX_GetActiveScene()->AddFont(this);
+}
+
+void KX_FontObject::DrawText()
+{
+ /* only draws the text if visible */
+ if(this->GetVisible() == 0) return;
+
+ /* XXX 2DO - handle multiple lines
+ /* HARDCODED MULTIPLICATION FACTOR - this will affect the render resolution directly */
+ float RES = BGE_FONT_RES * m_resolution;
+
+ float size = m_fsize * m_object->size[0] * RES;
+ float aspect = 1.f / (m_object->size[0] * RES);
+ m_rendertools->RenderText3D(m_fontid, m_text, int(size), m_dpi, m_color, this->GetOpenGLMatrix(), aspect);
+}
+
+#ifdef WITH_PYTHON
+
+/* ------------------------------------------------------------------------- */
+/* Python Integration Hooks */
+/* ------------------------------------------------------------------------- */
+
+PyTypeObject KX_FontObject::Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "KX_FontObject",
+ sizeof(PyObjectPlus_Proxy),
+ 0,
+ py_base_dealloc,
+ 0,
+ 0,
+ 0,
+ 0,
+ py_base_repr,
+ 0,
+ &KX_GameObject::Sequence,
+ &KX_GameObject::Mapping,
+ 0,0,0,
+ NULL,
+ NULL,
+ 0,
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ 0,0,0,0,0,0,0,
+ Methods,
+ 0,
+ 0,
+ &KX_GameObject::Type,
+ 0,0,0,0,0,0,
+ py_base_new
+};
+
+PyMethodDef KX_FontObject::Methods[] = {
+ {NULL,NULL} //Sentinel
+};
+
+PyAttributeDef KX_FontObject::Attributes[] = {
+ KX_PYATTRIBUTE_STRING_RW("text", 0, 100, false, KX_FontObject, m_text),
+ KX_PYATTRIBUTE_FLOAT_RW("size", 0.0001f, 10000.0f, KX_FontObject, m_fsize),
+ KX_PYATTRIBUTE_FLOAT_RW("resolution", 0.0001f, 10000.0f, KX_FontObject, m_resolution),
+ /* KX_PYATTRIBUTE_INT_RW("dpi", 0, 10000, false, KX_FontObject, m_dpi), */// no real need for expose this I think
+ { NULL } //Sentinel
+};
+
+#endif // WITH_PYTHON