Create SIO2font by name from iPhone font
4 posters
Create SIO2font by name from iPhone font
If you want to quickly create a SIO2font without a bitmap, you can use the function I hacked this morning with a little inspiration from Apple's Texture2D class. Just call it with the name of the font:
To find out which fonts are available on your device and how they look, you can download the free app "Fonts" from the App Store.
Here is the header file FontUtilities.h:
Here is the implementation FontUtilities.mm:
- Code:
SIO2font* newFont = createSIO2Font("Marker Felt");
To find out which fonts are available on your device and how they look, you can download the free app "Fonts" from the App Store.
Here is the header file FontUtilities.h:
- Code:
#import "sio2/sio2.h"
SIO2font* createSIO2Font( const char* fontName = "Marker Felt",
const unsigned int cellSize = 32,
const unsigned int fontSize = 20,
const float spacing = 10.0f);
Here is the implementation FontUtilities.mm:
- Code:
#import "FontUtilities.h"
SIO2font* createSIO2Font(const char* fontName,
const unsigned int cellSize,
const unsigned int fontSize,
const float spacing)
{
const unsigned int width = cellSize * 16;
const unsigned int height = width;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void* data = calloc(height*4, width);
CGContextRef context = CGBitmapContextCreate(data, width, height, 8,
width*4, colorSpace,
kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextSetGrayFillColor(context, 1.0, 1.0);
CGContextTranslateCTM(context, 0.0, height);
CGContextScaleCTM(context, 1.0, -1.0);
UIGraphicsPushContext(context);
UIFont* font = [UIFont fontWithName: [NSString stringWithCString: fontName] size: fontSize];
assert(font != nil);
char buf[2] = { 0, 0 };
for (int x = 0; x < 16; ++x) {
for (int y =0; y < 16; ++y) {
buf[0] = (char) (y*16+x);
NSString* character = [[NSString alloc] initWithCString: buf
encoding:NSISOLatin1StringEncoding];
CGSize stringSize = [character sizeWithFont: font];
CGPoint point = { x * cellSize + (cellSize/2.0f - stringSize.width/2.0f), y * cellSize };
[character drawAtPoint: point
forWidth: 2*cellSize
withFont: font
fontSize: fontSize
lineBreakMode: UILineBreakModeWordWrap
baselineAdjustment: UIBaselineAdjustmentAlignBaselines];
[character release];
}
}
UIGraphicsPopContext();
SIO2image *image = sio2ImageInit("fontImage");
image->width = width;
image->height = height;
image->bits = 4;
unsigned int size = image->width * image->height * image->bits;
image->tex = ( unsigned char * ) malloc( size );
memcpy(image->tex, data, size);
free(data);
sio2ImageGenId( image, NULL, 0.0f );
SIO2font* sio2Font = sio2FontInit("newFont");
SIO2material* fontMaterial = sio2MaterialInit( "fontMaterial" );
fontMaterial->diffuse->x = 1.0f;
fontMaterial->diffuse->y = 1.0f;
fontMaterial->diffuse->z = 1.0f;
fontMaterial->diffuse->w = 1.0f;
fontMaterial->blend = SIO2_MATERIAL_COLOR;
fontMaterial->_SIO2image[ 0 ] = image;
sio2FontCreate( sio2Font,
fontMaterial,
16,
0,
cellSize,
spacing );
CGContextRelease(context);
return sio2Font;
}
Faikus- Posts : 23
Join date : 2009-05-26
Location : Berlin
Re: Create SIO2font by name from iPhone font
What do you mean? Are you talking about not all fonts being on both the iPhone and the iPod Touch? I think using fonts from lists like http://www.alexcurylo.com/blog/2008/10/05/snippet-available-uifonts/ is safe.
Faikus- Posts : 23
Join date : 2009-05-26
Location : Berlin
Re: Create SIO2font by name from iPhone font
ROm very likely meant that some of the fonts you have on the iPhone would not be there on other platforms such as Android, etc - and some of the calls in the code are Cocoa / Objective-C specific ones, not OGL ones, or not a pure 'C' implementation.
If you use a true OGL font bitmap approach, it would be more portable as OGL is "usually" quite portable across platforms.
My take is that what you provided is great nonetheless - more choices is good and am sure will help lots of folks who don't want to go with a bitmap approach ;-)
Cheers and thanks for providing this, Faikus.
If you use a true OGL font bitmap approach, it would be more portable as OGL is "usually" quite portable across platforms.
My take is that what you provided is great nonetheless - more choices is good and am sure will help lots of folks who don't want to go with a bitmap approach ;-)
Cheers and thanks for providing this, Faikus.
Francescu- Posts : 136
Join date : 2009-03-18
Re: Create SIO2font by name from iPhone font
YES - thanks for the post
BTW The accelerometer is also NOT "platform independent", doesn't mean we shouldn't develop for it!
BTW The accelerometer is also NOT "platform independent", doesn't mean we shouldn't develop for it!
cybergreg- Posts : 21
Join date : 2009-07-21
Location : SoCal, USA
Re: Create SIO2font by name from iPhone font
Well if you take a look at SIO2, everything is independent... even for the accelerometer its just a callback... there's no platform specific code whatsoever in SIO2...
Re: Create SIO2font by name from iPhone font
Yup, it is just meant to be a small utility function for iPhones/iPod Touches. It will be interesting to see Sio2 on other platforms. Are there any plans to port it to Android or Palm Pre? It would be cool if our games ran on those with minimal porting effort, but I don't know how easy it is to adapt a framework written in C to them. Android allows native C code but at the moment only with lots of caveats I think and Palm Pre can only run Javascript.
Faikus- Posts : 23
Join date : 2009-05-26
Location : Berlin
Similar topics
» Create primitives
» Programatically Creating Objects
» How to Create .sio2 file???
» Best approach to create a 2D boardgame with 3D game board
» Bitmap Font Generator
» Programatically Creating Objects
» How to Create .sio2 file???
» Best approach to create a 2D boardgame with 3D game board
» Bitmap Font Generator
Permissions in this forum:
You cannot reply to topics in this forum