Click here to Skip to main content
13,191,877 members (33,192 online)
Click here to Skip to main content

Stats

58.9K views
2.9K downloads
27 bookmarked
Posted 9 Nov 2010

FreeType on OpenGL ES (iPhone)

, 24 Nov 2010
Faster, smarter and better looking fonts rendered with OpenGL ES
OpenGLFont
.DS_Store
__MACOSX
OpenGLFont
._.DS_Store
arial.ttf
Classes
.DS_Store
Classes
._.DS_Store
EAGLView.mm
._EAGLView.mm
freetype-2.4.3
.DS_Store
freetype-2.4.3
._.DS_Store
.gitignore
include
.DS_Store
include
._.DS_Store
freetype
.DS_Store
freetype
._.DS_Store
config
config
internal
services
src
.DS_Store
src
._.DS_Store
OpenGLFontAppDelegate.mm
main.m
MainWindow.xib
OpenGLFont-Info.plist
._OpenGLFont-Info.plist
OpenGLFont.xcodeproj
lefty.mode1v3
lefty.pbxuser
project.pbxproj
OpenGLFont_Prefix.pch
Shaders
Shader.fsh
Shader.vsh
times.ttf
.DS_Store
arial.ttf
.DS_Store
EAGLView.mm
.DS_Store
.gitignore
.DS_Store
.DS_Store
.DS_Store
OpenGLFontAppDelegate.mm
main.m
MainWindow.xib
OpenGLFont-Info.plist
lefty.mode1v3
lefty.pbxuser
project.pbxproj
Shader.fsh
Shader.vsh
times.ttf
#include "PreCompile.h"
#include "TreeNode.h"
#include "FTBitmapChar.h"

Allocator<TreeNode> TreeNode::m_allocator;

TreeNode::TreeNode()
{
	m_pLeaf1 = NULL;
	m_pLeaf2 = NULL;
}

TreeNode::TreeNode(int x, int y, int width, int height)
{
	m_x = x;
	m_y = y;
	m_width = width;
	m_height = height;
	m_pLeaf1 = NULL;
	m_pLeaf2 = NULL;
}

void TreeNode::Set(int x, int y, int width, int height)
{
	m_x = x;
	m_y = y;
	m_width = width;
	m_height = height;
	m_pLeaf1 = NULL;
	m_pLeaf2 = NULL;
}

TreeNode::~TreeNode()
{
}

void TreeNode::CreateBranches(FTBitmapChar* pFTBitmapChar)
{
	int dx = m_width - pFTBitmapChar->GetWidth();
	int dy = m_height - pFTBitmapChar->GetHeight();
	// we split to give one very small leaf and one very big one
	// because it allows more efficent use of space
	// if you don't do this, the bottom right corner never gets used
	if (dx < dy)	
	{
		//	split so the top is cut in half and the rest is one big rect below
		m_pLeaf1 = m_allocator.New();
		m_pLeaf1->Set(m_x + pFTBitmapChar->GetWidth(), m_y, 
				m_width - pFTBitmapChar->GetWidth(), 
				pFTBitmapChar->GetHeight());
		m_pLeaf2 = m_allocator.New();
		m_pLeaf2->Set(m_x, m_y + pFTBitmapChar->GetHeight(), 
				m_width, m_height - pFTBitmapChar->GetHeight());
	}
	else
	{
		//	m_pLeaf1 = left (cut in half)
		m_pLeaf1 = m_allocator.New();
		m_pLeaf1->Set(m_x, m_y + pFTBitmapChar->GetHeight(), 
				pFTBitmapChar->GetWidth(), m_height - pFTBitmapChar->GetHeight());
		// m_pLeaf2 = right (not cut)
		m_pLeaf2 = m_allocator.New();
		m_pLeaf2->Set(m_x + pFTBitmapChar->GetWidth(), m_y, 
				m_width - pFTBitmapChar->GetWidth(), m_height);
	}
}

bool TreeNode::Add(FTBitmapChar* pFTBitmapChar)
{
	if (pFTBitmapChar->IsEmpty()) return true;
	if (IsEmpty())
	{	
		if (Fits(pFTBitmapChar))
		{
			CreateBranches(pFTBitmapChar);
			pFTBitmapChar->SetXY(m_x, m_y);
			return true;
		}
		return false;
	}
	if (m_pLeaf1->Add(pFTBitmapChar)) 
	{
		return true;
	}
	if (m_pLeaf2->Add(pFTBitmapChar)) 
	{
		return true;
	}
	return false;		
}

bool TreeNode::Fits(FTBitmapChar* pFTBitmapChar)
{
	return pFTBitmapChar->GetWidth() <= m_width && pFTBitmapChar->GetHeight() <= m_height;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ed welch
Software Developer Astronautz
Spain Spain
After working in the software industry for many years, I've started my own games company that specialises in strategy games for mobile platforms.

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.2 | Last Updated 24 Nov 2010
Article Copyright 2010 by ed welch
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid