-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDrawState.m
149 lines (120 loc) · 3.11 KB
/
DrawState.m
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// DrawState.m
// Evo1
//
// Created by Tim Hinderliter on 4/30/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "DrawState.h"
@implementation DrawState
- (DrawState*) init {
if (self = [super init]) {
self->rotate = 0.0f;
self->scale.x = self->scale.y = 1.0f;
self->translate.x = self->translate.y = 0.0f;
self->color[0] = self->color[1] = self->color[2] = self->color[3] = 1;
}
return self;
}
- (void) dealloc {
[super dealloc];
}
@synthesize translate;
//@synthesize color;
- (GLfloat*) color {
return self->color;
}
- (void) setColor:(GLfloat*)col {
self->color[0] = col[0];
self->color[1] = col[1];
self->color[2] = col[2];
self->color[3] = col[3];
}
- (CGPoint) transformedPoint:(CGPoint)point {
CGPoint res;
res.x = cosf(rotate) * scale.x * point.x;
res.y = sinf(rotate) * scale.x * point.x;
res.x += sinf(rotate) * scale.y * point.y;
res.y += cosf(rotate) * scale.y * point.y;
return res;
}
- (CGPoint) translate:(CGPoint)offset {
CGPoint transformed = [self transformedPoint:offset];
self->translate.x += transformed.x;
self->translate.y += transformed.y;
return self->translate;
}
- (CGPoint) scale:(CGPoint)toScale {
self->scale.x *= toScale.x;
self->scale.y *= toScale.y;
return self->scale;
}
- (GLfloat) rotate:(GLfloat)toRotate {
self->rotate += toRotate;
return self->rotate;
}
- (NSString*) description {
return [NSString stringWithFormat:@"%@ (scale %f, %f) (rotate %f) (translate %f,%f)",
[self class],
scale.x, scale.y,
rotate,
translate.x, translate.y];
}
@end
@implementation MachineState
@synthesize stack;
- (MachineState*) init:(int)inNumRegisters {
if (self = [super init]) {
self->stack = [[NSMutableArray new] retain];
self->numRegisters = inNumRegisters;
self->registers = (GLfloat*) calloc(sizeof(GLfloat) * numRegisters, 0);
}
return self;
}
- (void) dealloc {
[self->stack dealloc];
free(self->registers);
[super dealloc];
}
- (GLfloat) getReg:(int)regIdx {
//NSLog(@"get %d: %.2f\n", regIdx, registers[regIdx]);
return registers[regIdx];
}
- (GLfloat) setReg:(int)regIdx withValue:(GLfloat)value {
//NSLog(@"SET %d: %.2f = %.2f\n", regIdx, registers[regIdx], value);
registers[regIdx] = value;
return value;
}
- (GLfloat) push:(GLfloat)number {
[stack addObject:[NSNumber numberWithFloat:number]];
return number;
}
- (GLfloat) pop {
GLfloat res = 0;
if ([stack count] > 0) {
res = [self peek:0];
[stack removeLastObject];
}
return res;
}
- (GLfloat) peek:(int)index {
GLfloat res = 0;
if ([stack count] > index) {
NSNumber * numLast = [stack objectAtIndex:([stack count] - index - 1)];
res = [numLast floatValue];
}
return res;
}
- (void) clone:(int)num {
for (int i = 0; i < num; i ++) {
// each time we add another, 'num' means a more recent entry onto
// the stack.
[self push:[self peek:num]];
}
}
- (NSString*) description {
return [NSString stringWithFormat:@"%@ (stack size %d) :: ",
[self class],
[stack count], stack];
}
@end