FORUM CLOSED, PLEASE REGISTER AT FORUM.SIO2INTERACTIVE.COM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Create SIO2font by name from iPhone font

4 posters

Go down

Create SIO2font by name from iPhone font Empty Create SIO2font by name from iPhone font

Post  Faikus Wed Aug 12, 2009 6:17 am

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:

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);
Note that it uses default arguments, so you might have to modify it if you use C and not C++.

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

Back to top Go down

Create SIO2font by name from iPhone font Empty Re: Create SIO2font by name from iPhone font

Post  sio2interactive Wed Aug 12, 2009 6:23 am

Convenient but not platform independent Wink
sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 44
Location : Shanghai

http://sio2interactive.com

Back to top Go down

Create SIO2font by name from iPhone font Empty Re: Create SIO2font by name from iPhone font

Post  Faikus Wed Aug 12, 2009 11:48 am

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

Back to top Go down

Create SIO2font by name from iPhone font Empty Re: Create SIO2font by name from iPhone font

Post  Francescu Wed Aug 12, 2009 12:04 pm

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.

Francescu

Posts : 136
Join date : 2009-03-18

Back to top Go down

Create SIO2font by name from iPhone font Empty Re: Create SIO2font by name from iPhone font

Post  cybergreg Wed Aug 12, 2009 3:05 pm

YES - thanks for the post

BTW The accelerometer is also NOT "platform independent", doesn't mean we shouldn't develop for it! Very Happy
cybergreg
cybergreg

Posts : 21
Join date : 2009-07-21
Location : SoCal, USA

Back to top Go down

Create SIO2font by name from iPhone font Empty Re: Create SIO2font by name from iPhone font

Post  sio2interactive Wed Aug 12, 2009 3:54 pm

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...
sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 44
Location : Shanghai

http://sio2interactive.com

Back to top Go down

Create SIO2font by name from iPhone font Empty Re: Create SIO2font by name from iPhone font

Post  Faikus Thu Aug 13, 2009 10:26 am

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

Back to top Go down

Create SIO2font by name from iPhone font Empty Re: Create SIO2font by name from iPhone font

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum