-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyView.m
More file actions
127 lines (97 loc) · 2.94 KB
/
myView.m
File metadata and controls
127 lines (97 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// myView.m
// OpenGLScreenSnapshot
//
// Created by Brian Moore on 9/14/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "myView.h"
#import <CommonCrypto/CommonDigest.h>
@implementation MyView
-(id) init
{
self = [super init];
myReader = NULL;
captureX = 480;
captureY = 260;
return self;
}
- (void)drawRect:(NSRect)needsDisplayInRect {
int fx, fy;
int i,j;
fx = fy = 0;
if(myReader != NULL) {
CGImageRef imageRef = [myReader createRGBImageFromBufferData];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
NSColor * red = [NSColor redColor];
NSColor * green = [NSColor greenColor];
// fill target rect
NSRect rect1 = NSMakeRect ( captureX,captureY,320,320 );
[red set];
NSFrameRectWithWidth ( rect1, 1 );
[green set];
//static char colors[8] = {'X','R','B','P','G','Y','O','W'};
NSArray* colorArray = [NSArray arrayWithObjects:
[NSColor grayColor], [NSColor redColor],
[NSColor blueColor], [NSColor purpleColor],
[NSColor greenColor], [NSColor yellowColor], [NSColor orangeColor],
[NSColor grayColor], nil];
int idx=0;
//printf("*******************************************\n");
for(i = captureY + 20; i < captureY + 320; i += 40) {
for(j = captureX + 20; j < captureX + 320; j += 40, idx++) {
rect1 = NSMakeRect(j-17,i-17,34,34);
[[colorArray objectAtIndex:blocks[idx].color] set];
NSFrameRectWithWidth ( rect1, 3 );
rect1 = NSMakeRect(j-15,i-15,30,30);
[[NSColor colorWithDeviceRed:blocks[idx].r
green:blocks[idx].g
blue:blocks[idx].b
alpha:1.0] set];
NSFrameRectWithWidth ( rect1, 3 );
if (blocks[idx].isMultiplier) {
[[NSColor whiteColor] set];
NSFrameRectWithWidth ( rect1, 1 );
}
if (blocks[idx].isSpecial) {
[[NSColor blackColor] set];
NSFrameRectWithWidth ( rect1, 1 );
}
}
}
CFRelease(imageRef);
free(rawData);
}
[self setNeedsDisplay:YES];
}
- (void)setReader:(NSObject *)reader {
myReader = reader;
}
- (void)setCaptureX:(int)x y:(int)y {
//NSLog(@"Capturing at x=%d, y=%d",x,y);
captureX = x;
captureY = y;
}
- (void)setBoard:(Block[][9])board {
int i,j;
int idx=0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++,idx++) {
blocks[idx] = board[i][j];
}
}
return;
}
@end