// // CursesView.m // IPan // // Created by Michael Heyeck on 6/17/10. // Copyright 2010 Fair Oaks Labs, Inc.. All rights reserved. // #import "CursesView.h" @implementation CursesView - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { if (self.frame.size.width != 320) { // This class assumes that it's 320 points wide [self release]; return nil; } font = [[UIFont fontWithName:@"CourierNewPSMT" size:16] retain]; w = 33; h = floor(self.frame.size.height/[font pointSize]); screen = (char*) malloc(w*h+1); } return self; } - (id)initWithCoder:(NSCoder*)aDecoder { if (self = [super initWithCoder:aDecoder]) { if (self.frame.size.width != 320) { // This class assumes that it's 320 points wide [self release]; return nil; } font = [[UIFont fontWithName:@"CourierNewPSMT" size:16] retain]; w = 33; h = floor(self.frame.size.height/[font pointSize]); screen = (char*) malloc(w*h+1); } return self; } - (void)drawRect:(CGRect)rect { // What to render, and how screen[w*h] = '\0'; NSString* s = [NSString stringWithCString:screen encoding:NSASCIIStringEncoding]; // Dimensions CGSize size = self.frame.size; // Get a pointer to context CGContextRef context = UIGraphicsGetCurrentContext(); // Flop text rightside-up CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1, -1)); // Clear background CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); // Render text (Setup) NSUInteger ch = [font pointSize]; CGContextSetTextDrawingMode(context, kCGTextFillStroke); CGContextSetLineWidth(context, 1.0); CGContextSelectFont(context, [[font fontName] cStringUsingEncoding:NSASCIIStringEncoding], ch, kCGEncodingMacRoman); [[UIColor greenColor] set]; // Draw text NSString* ss; for (NSUInteger c = 0, n = w, l = [s length], row = ch; c < l; c = n, n += w, row += ch) { ss = [s substringWithRange:NSMakeRange(c, (n= w) { r += 1; c = 0; } } } p.x = c; p.y = r; } - (void)dealloc { [font release]; if (screen) free(screen); [super dealloc]; } @end