Annotation of OpenXM/src/cfep/MyOpenGLCommand.m, Revision 1.4
1.1 takayama 1: //
2: // MyOpenGLCommand.m
3: // cfep
4: //
5: // Created by Nobuki Takayama on 06/02/18.
6: // Copyright 2006 OpenXM.org. All rights reserved.
7: //
8:
9: #import "MyOpenGLCommand.h"
10: #define min(a,b) (a>b?b:a)
11:
12:
13: @implementation MyOpenGLCommand
14: -(id)init {
15: int i;
16: command = nil;
17: opCode = -1;
18: endGroup = NO;
19: for (i=0; i<4; i++) f4[i] = 0.0;
20: for (i=0; i<4; i++) i4[i] = 0;
21: return self;
22: }
23: -(void) dealloc {
24: if (argv) [argv autorelease];
25: }
26: -(void)print {
27: int i;
1.3 takayama 28: NSLog(@"command=%@s, opCode=%d, [",command,opCode);
1.1 takayama 29: for (i=0; i<4; i++) NSLog(@"%f,",f4[i]);
30: NSLog(@"; ");
31: for (i=0; i<4; i++) NSLog(@"%d,",i4[i]);
32: NSLog(@"]\n");
33: }
1.3 takayama 34: -(NSString *)toString {
35: return [NSString stringWithFormat: @"%@[%d], %f, %f, %f, %f; %d, %d, %d, %d",command,opCode,f4[0],f4[1],f4[2],f4[3],i4[0],i4[1],i4[2],i4[3]];
36: }
1.1 takayama 37: +(MyOpenGLCommand *)allocAndCompile: (NSString *)cmd by: (id) sender {
38: MyOpenGLCommand *c;
39: c=[[MyOpenGLCommand alloc] init];
40: [c autorelease];
41: return [c compile: cmd by: sender];
42: }
43: -(BOOL) isEndGroup {
44: return endGroup;
45: }
46: -(MyOpenGLCommand *) compile: (NSString *)cmd by: (id) sender {
47: // parse cmd and set opCode and f4[]. opCode starts with CFEPgl.
48: // Interpreter code is in MyOpenGLView.m with the name execute:
49: NSArray *a;
50: NSString *s;
51: NSString *errmsg;
52: id x;
53: int fargc, iargc;
54: int i,n,ii;
55: double f;
56:
57: a = [MyUtil arrayOfStringFrom: cmd]; //"[glColor4f,1.0,1.0,0.0,1.0]" -> array of [NSString, MyFloat, MyFloat,..]
58: [a retain]; argv = a;
59: n = [a count];
60: fargc = 0; iargc = 0;
61: // for (i=0; i<n; i++) { NSLog(@"%@,",[a objectAtIndex: i]); }
62: if (n == 0) return nil;
63: x = [a objectAtIndex: 0];
64: if ([x isKindOfClass: [NSString class]] == YES) s = x; // isMemberOfClass does not work. ??
65: else {
66: NSLog(@"NSString is exected.\n");
67: s = @"";
68: }
69: if ([s compare: @"glBegin"] == NSOrderedSame) {
70: opCode = CFEPglBegin; fargc = 0; iargc = 1; endGroup = NO;
71:
72: }else if ([s compare: @"glColor4f"] == NSOrderedSame) {
73: opCode = CFEPglColor4f; fargc = 4; iargc = 0; endGroup = NO;
74:
1.3 takayama 75: }else if ([s compare: @"glClear"] == NSOrderedSame) {
76: opCode = CFEPglClear; fargc = 0; iargc = 1; endGroup = YES;
77:
78: }else if ([s compare: @"glClearColor"] == NSOrderedSame) {
79: opCode = CFEPglClearColor; fargc = 4; iargc = 0; endGroup = YES;
80:
81: }else if ([s compare: @"glClearDepth"] == NSOrderedSame) {
82: opCode = CFEPglClearDepth; fargc = 1; iargc = 0; endGroup = YES;
83:
1.1 takayama 84: }else if ([s compare: @"glEnd"] == NSOrderedSame) {
85: opCode = CFEPglEnd; fargc = 0; iargc = 0; endGroup = YES;
86:
1.3 takayama 87: }else if ([s compare: @"glFlush"] == NSOrderedSame) {
88: opCode = CFEPglFlush; fargc = 0; iargc = 0; endGroup = YES;
89:
90: }else if ([s compare: @"glPointSize"] == NSOrderedSame) {
91: opCode = CFEPglPointSize; fargc = 1; iargc = 0; endGroup = NO;
92:
1.1 takayama 93: }else if ([s compare: @"glRectf"] == NSOrderedSame) {
94: opCode = CFEPglRectf; fargc = 4; iargc = 0; endGroup = YES;
95:
96: }else if ([s compare: @"glVertex3f"] == NSOrderedSame) {
97: opCode = CFEPglVertex3f; fargc = 3; iargc = 0; endGroup = NO;
98:
99: }else if ([s compare: @"glib_line"] == NSOrderedSame) {
100: opCode = CFEPglib_line; fargc = 4; iargc = 1; endGroup = YES;
101:
102: }else if ([s compare: @"glib_putpixel"] == NSOrderedSame) {
103: opCode = CFEPglib_putpixel; fargc = 2; iargc = 1; endGroup = YES;
104:
105: }else if ([s compare: @"glib_flush"] == NSOrderedSame) {
106: opCode = CFEPglib_flush; fargc = 0; iargc = 0; endGroup = YES;
107:
108: }else if ([s compare: @"glib3_bounding_box"] == NSOrderedSame) {
109: opCode = CFEPglib3_bounding_box; fargc = 1; iargc = 0; endGroup = YES;
110:
111: }else if ([s compare: @"glib3_ray_init"] == NSOrderedSame) {
112: opCode = CFEPglib3_ray_init; fargc = 0; iargc = 0; endGroup = YES;
113:
114: }else if ([s compare: @"glib3_ray_reshape"] == NSOrderedSame) {
115: opCode = CFEPglib3_ray_reshape; fargc = 0; iargc = 0; endGroup = YES;
116:
117: }else if ([s compare: @"glib3_ray"] == NSOrderedSame) {
118: opCode = CFEPglib3_ray; fargc = 0; iargc = 0; endGroup = YES;
119: [sender output: @"Ray by David Bucciarelli. (MesaDemo, GPL)"];
120:
121: }else if ([s compare: @"glib3_std_scene0"] == NSOrderedSame) {
122: opCode = CFEPglib3_std_scene0; fargc = 0; iargc = 0; endGroup = YES;
123: [sender output: @"glib3_std_scene0(0,0,2) using glFrustum"];
124:
125: }else if ([s compare: @"glib3_icosahedron"] == NSOrderedSame) {
126: opCode = CFEPglib3_icosahedron; fargc = 0; iargc = 0; endGroup = YES;
127: [sender output: @"Icosahedron"];
128:
129: }else {
130: NSLog(@"Undefined command=<%@>\n",s);
131: errmsg = [NSString stringWithFormat: @"Undefined command=<%@>",s];
132: [sender output: errmsg];
133: return nil;
134: }
135: // Format glxxxpfqi fargc=p, iargc=q. Example: glxxx4f1i, 0.1,0.2,0.3,0.4,34
1.3 takayama 136: command = s;
137: [command retain]; // bug. How to release?
1.1 takayama 138: if (fargc > 0) {
139: for (i=1; i< min(n,fargc+1); i++) {
140: x = [a objectAtIndex: i];
141: if ([x isMemberOfClass: [MyFloat class]] == YES) f = [x getFValue];
142: else {
143: NSLog(@"float is exected.\n");
144: f = 0.0;
145: }
146: if (i-1 < 4) f4[i-1] = f;
147: }
148: }
149: if (iargc > 0) {
150: for (i=1+fargc; i< min(n,fargc+iargc+1); i++) {
151: x = [a objectAtIndex: i];
152: if ([x isMemberOfClass: [MyInt class]] == YES) ii = [x getIValue];
153: else {
154: ii = 0;
155: NSLog(@"int is exected.\n");
156: }
157: if (i-1-fargc < 4) i4[i-1-fargc] = ii;
158: }
159: }
160: return self;
161: }
162:
163: -(int) getOpCode { return opCode; }
164: -(double *)getF4 { return f4; }
165: -(int *) getI4 { return i4; }
166: -(NSArray *)getArgv { return argv; }
167:
168: @end
169:
170: /* Code to test MyFloat put in compile:
171: MyFloat *x;
172: NSLog(@"member test.\n"
173: x = [MyFloat allocWith: 1.0];
174: if ([x isMemberOfClass: [MyFloat class]] == YES) NSLog(@"[x class] is MyFloat\n");
175: if ([x isMemberOfClass: [MyInt class]] == YES) NSLog(@"[x class] is MyInt\n");
176: */
177: @implementation MyFloat
178: +(MyFloat *)allocWith: (float) f {
179: MyFloat *ff;
180: ff=[MyFloat alloc];
181: [ff setFValue: f];
182: return ff;
183: }
184: -(void) setFValue: (float) f { fValue = f; }
185: -(float) getFValue { return fValue; }
186: -(NSString *)description { return [NSString stringWithFormat: @"(float)%f",fValue]; }
187: @end
188:
189: @implementation MyInt
190: +(MyInt *)allocWith: (int) i {
191: MyInt *ii;
192: ii=[MyInt alloc];
193: [ii setIValue: i];
194: return ii;
195: }
196: -(void) setIValue: (int) i { iValue = i; }
197: -(int) getIValue { return iValue; }
198: -(NSString *)description { return [NSString stringWithFormat: @"(int)%d",iValue]; }
199: @end
200:
201: #define SMAX 0x1000 //4096*16
202: @implementation MyUtil
203: static int debugMyUtil = 0;
204: +(void) setDebugMyUtil {
205: if (debugMyUtil) debugMyUtil = 0;
206: else debugMyUtil = 1;
207: }
208: +(NSMutableArray *) arrayOfStringFrom: (NSString *) args {
209: int n,i,k,type;
210: unichar c;
211: unichar s[SMAX];
212: NSMutableArray *a; // for debug
213: NSMutableArray *aa;
214: NSString *ss;
215: int status,level;
216: status = -1; level = 0; type = 0;
217: n = [args length];
218: if (n >= SMAX-1) {
219: NSLog(@"Too big string for stringToArrayOfString.\n");
220: return nil;
221: }
222: a = [NSMutableArray arrayWithCapacity: 1];
223: aa = [NSMutableArray arrayWithCapacity: 1];
224: for (i=0, k=0; i<n; i++) {
225: c = [args characterAtIndex: i];
226: if (level == 0) {
227: switch (status) { // See my note on 2006-02-20.
228: case -1:
229: if ( c == '[') { k=0; status=0;}
230: break;
231: case 0:
232: if ( c == '[') {
233: k = 0; s[k] = c; k++; status = 1; type = 1; // type 1 : list
234: } else if (c == ',') {
235: } else if (c == '(') {
236: k=0; status = 21; type=0; //ex. (int)
237: } else if ((('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {
238: k=0; status = 2; type=3; s[k] = c; k++; // type -1 : string.
239: } else if (c > ' ') {
240: k=0; status = 2; type=0; s[k] = c; k++; // type 0 : float
241: } else if (c == ']') status = -1;
242: break;
243: case 2:
244: if ((c == ',') || (c == ']')) {
245: if (c == ',') status = 0; else status = -1;
246: if (k > 0) ss = [NSString stringWithCharacters: s length: k];
247: else ss = @"";
248: [a addObject: ss];
249: if (type == 2) [aa addObject: [MyInt allocWith: [ss intValue]]];
250: else if (type == 0) [aa addObject: [MyFloat allocWith: [ss floatValue]]];
251: else [aa addObject: ss];
252: }else{
253: s[k] = c; k++;
254: }
255: break;
256: case 1:
257: if (c == '[') {
258: s[k] = c; k++;
259: level++;
260: }else if (c == ']') {
261: s[k] = ']'; k++;
262: if (k > 0) ss = [NSString stringWithCharacters: s length: k];
263: else ss = @"";
264: [a addObject: ss];
265: [aa addObject: [MyUtil arrayOfStringFrom: ss]];
266: status = 0;
267: }else {
268: s[k] = c; k++;
269: }
270: break;
271: case 21:
272: if (c == ')') {
273: if ((s[0] == 'i') && (s[1] == 'n') && (s[2] == 't')) type=2;
274: else type = 0;
275: status = 2; k = 0; // starting to read data.
276: }else if (c > ' ') {
277: s[k] = c; k++;
278: }
279: break;
280: }
281: }else if (level > 0) {
282: s[k] = c; k++;
283: if (c == '[') level++;
284: else if (c == ']') level--;
285: }
286: }
287: if (debugMyUtil) {
288: NSLog(@"a=%@\n",a);
289: NSLog(@"aa=%@\n",aa);
290: }
291: return aa;
292: }
293:
294: /*
295: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[]"]);
296: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,[]]"]);
297: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,2,[3,4],5]"]);
298: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @" [1, 2,3, 4,5]"]);
299: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[[1],2,[[3,[4]]],5]"]); // There is a bug. Output is 1,2,...
300: */
301:
1.2 takayama 302: +(NSString *)pruneThings: (NSString *)ss {
303: int n,i, start,end;
304: unichar c;
305: unichar s[SMAX];
306: NSString *ans;
307: n = [ss length];
308: if (n >= SMAX-1) {
309: NSLog(@"Too big string for pruneThings.\n");
310: return nil;
311: }
312: start = 0; end = n-1;
313: for (i=0; i<n; i++) {
314: c = [ss characterAtIndex: i];
315: start = i;
316: if (c > 0x20) break;
317: }
318: for (i=n-1; i>=0; i--) {
319: c = [ss characterAtIndex: i];
320: end = i;
321: if (c > 0x20) break;
322: }
323: if (start > end) return nil;
324: for (i=0; i<= end-start ; i++) {
325: s[i] = [ss characterAtIndex: (start+i)];
326: s[i+1] =0;
327: }
328: ans = [NSString stringWithCharacters: s length: (end-start+1)];
329: return ans;
330: }
331:
332: +(id)attributedStringWithPath: (NSString *)path {
333: NSFileWrapper *theWrapper;
334: NSTextAttachment *theAttachment;
335: NSAttributedString *aStr;
336: theWrapper = [[NSFileWrapper alloc] initWithPath: path];
337: if (!theWrapper) NSLog(@"theWrapper is nil. Path=[%@]\n",path);
338: if (!theWrapper) return nil;
339: theAttachment = [[NSTextAttachment alloc] initWithFileWrapper:theWrapper];
340: aStr = [NSAttributedString attributedStringWithAttachment:theAttachment];
341: return aStr; // How should I do autorelease?
342: }
343:
344:
345: @end
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>