multiple character generators.

This commit is contained in:
Kelvin Sherlock 2018-02-28 10:22:20 -05:00
parent 5b8c8fec17
commit b766854f80
2 changed files with 36 additions and 8 deletions

View File

@ -8,6 +8,12 @@
#import <Cocoa/Cocoa.h>
enum {
CGApple80,
CGApple40,
CGVT52,
CGVT100
};
@interface CharacterGenerator : NSObject
{
@ -17,6 +23,9 @@
}
+(CharacterGenerator *)generator;
+(CharacterGenerator *)generatorForCharacterSet: (unsigned)characterSet;
@property (nonatomic, readonly) NSSize characterSize;

View File

@ -12,6 +12,7 @@
@interface CharacterGenerator ()
-(void)loadImageNamed: (NSString *)imageName;
-(id)initWithImageNamed: (NSString *)imageName;
@end
@implementation CharacterGenerator
@ -38,27 +39,45 @@ static CGImageRef PNGImage(NSString *path)
}
#endif
+(id)generator
{
static CharacterGenerator *singleton = nil;
+(CharacterGenerator *)generatorForCharacterSet: (unsigned)characterSet {
static CharacterGenerator *singletons[4] = {};
static NSString *names[] = {
@"a2-charset-80",
@"a2-charset-40",
@"vt52-charset",
@"vt100-charset",
};
constexpr unsigned MaxCharSet = sizeof(names) / sizeof(names[0]);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[CharacterGenerator alloc] init];
for (unsigned i = 0; i < MaxCharSet; ++i)
singletons[i] = [[CharacterGenerator alloc] initWithImageNamed: names[i]];
});
return singleton;
if (characterSet >= MaxCharSet) return nil;
return singletons[characterSet];
}
-(id)init
+(id)generator
{
return [self generatorForCharacterSet: CGApple80];
}
-(id)initWithImageNamed: (NSString *)imageName {
if ((self = [super init]))
{
[self loadImageNamed: @"a2-charset-80"];
[self loadImageNamed: imageName];
}
return self;
}
/*
* This loads the image then split it up into 256 images.
*