Annotation of OpenXM/src/cfep/MyOpenGLCommand.m, Revision 1.5
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.5 ! takayama 84: }else if ([s compare: @"glDisable"] == NSOrderedSame) {
! 85: opCode = CFEPglDisable; fargc = 0; iargc = 1; endGroup = YES;
! 86:
! 87: }else if ([s compare: @"glEnable"] == NSOrderedSame) {
! 88: opCode = CFEPglEnable; fargc = 0; iargc = 1; endGroup = YES;
! 89:
1.1 takayama 90: }else if ([s compare: @"glEnd"] == NSOrderedSame) {
91: opCode = CFEPglEnd; fargc = 0; iargc = 0; endGroup = YES;
92:
1.3 takayama 93: }else if ([s compare: @"glFlush"] == NSOrderedSame) {
94: opCode = CFEPglFlush; fargc = 0; iargc = 0; endGroup = YES;
1.5 ! takayama 95:
! 96: }else if ([s compare: @"glLineStipple"] == NSOrderedSame) {
! 97: opCode = CFEPglLineStipple; fargc = 0; iargc = 2; endGroup = YES;
! 98:
! 99: }else if ([s compare: @"glLineWidth"] == NSOrderedSame) {
! 100: opCode = CFEPglLineWidth; fargc = 1; iargc = 0; endGroup = YES;
! 101:
! 102: }else if ([s compare: @"glNormal3f"] == NSOrderedSame) {
! 103: opCode = CFEPglNormal3f; fargc = 3; iargc = 0; endGroup = YES;
1.3 takayama 104:
105: }else if ([s compare: @"glPointSize"] == NSOrderedSame) {
106: opCode = CFEPglPointSize; fargc = 1; iargc = 0; endGroup = NO;
107:
1.1 takayama 108: }else if ([s compare: @"glRectf"] == NSOrderedSame) {
109: opCode = CFEPglRectf; fargc = 4; iargc = 0; endGroup = YES;
110:
111: }else if ([s compare: @"glVertex3f"] == NSOrderedSame) {
112: opCode = CFEPglVertex3f; fargc = 3; iargc = 0; endGroup = NO;
113:
114: }else if ([s compare: @"glib_line"] == NSOrderedSame) {
115: opCode = CFEPglib_line; fargc = 4; iargc = 1; endGroup = YES;
116:
117: }else if ([s compare: @"glib_putpixel"] == NSOrderedSame) {
118: opCode = CFEPglib_putpixel; fargc = 2; iargc = 1; endGroup = YES;
119:
120: }else if ([s compare: @"glib_flush"] == NSOrderedSame) {
121: opCode = CFEPglib_flush; fargc = 0; iargc = 0; endGroup = YES;
122:
123: }else if ([s compare: @"glib3_bounding_box"] == NSOrderedSame) {
124: opCode = CFEPglib3_bounding_box; fargc = 1; iargc = 0; endGroup = YES;
125:
126: }else if ([s compare: @"glib3_ray_init"] == NSOrderedSame) {
127: opCode = CFEPglib3_ray_init; fargc = 0; iargc = 0; endGroup = YES;
128:
129: }else if ([s compare: @"glib3_ray_reshape"] == NSOrderedSame) {
130: opCode = CFEPglib3_ray_reshape; fargc = 0; iargc = 0; endGroup = YES;
131:
132: }else if ([s compare: @"glib3_ray"] == NSOrderedSame) {
133: opCode = CFEPglib3_ray; fargc = 0; iargc = 0; endGroup = YES;
134: [sender output: @"Ray by David Bucciarelli. (MesaDemo, GPL)"];
135:
136: }else if ([s compare: @"glib3_std_scene0"] == NSOrderedSame) {
137: opCode = CFEPglib3_std_scene0; fargc = 0; iargc = 0; endGroup = YES;
138: [sender output: @"glib3_std_scene0(0,0,2) using glFrustum"];
139:
140: }else if ([s compare: @"glib3_icosahedron"] == NSOrderedSame) {
141: opCode = CFEPglib3_icosahedron; fargc = 0; iargc = 0; endGroup = YES;
142: [sender output: @"Icosahedron"];
143:
144: }else {
145: NSLog(@"Undefined command=<%@>\n",s);
146: errmsg = [NSString stringWithFormat: @"Undefined command=<%@>",s];
147: [sender output: errmsg];
148: return nil;
149: }
150: // Format glxxxpfqi fargc=p, iargc=q. Example: glxxx4f1i, 0.1,0.2,0.3,0.4,34
1.3 takayama 151: command = s;
152: [command retain]; // bug. How to release?
1.1 takayama 153: if (fargc > 0) {
154: for (i=1; i< min(n,fargc+1); i++) {
155: x = [a objectAtIndex: i];
156: if ([x isMemberOfClass: [MyFloat class]] == YES) f = [x getFValue];
157: else {
158: NSLog(@"float is exected.\n");
159: f = 0.0;
160: }
161: if (i-1 < 4) f4[i-1] = f;
162: }
163: }
164: if (iargc > 0) {
165: for (i=1+fargc; i< min(n,fargc+iargc+1); i++) {
166: x = [a objectAtIndex: i];
167: if ([x isMemberOfClass: [MyInt class]] == YES) ii = [x getIValue];
168: else {
169: ii = 0;
170: NSLog(@"int is exected.\n");
171: }
172: if (i-1-fargc < 4) i4[i-1-fargc] = ii;
173: }
174: }
175: return self;
176: }
177:
178: -(int) getOpCode { return opCode; }
179: -(double *)getF4 { return f4; }
180: -(int *) getI4 { return i4; }
181: -(NSArray *)getArgv { return argv; }
182:
183: @end
184:
185: /* Code to test MyFloat put in compile:
186: MyFloat *x;
187: NSLog(@"member test.\n"
188: x = [MyFloat allocWith: 1.0];
189: if ([x isMemberOfClass: [MyFloat class]] == YES) NSLog(@"[x class] is MyFloat\n");
190: if ([x isMemberOfClass: [MyInt class]] == YES) NSLog(@"[x class] is MyInt\n");
191: */
192: @implementation MyFloat
193: +(MyFloat *)allocWith: (float) f {
194: MyFloat *ff;
195: ff=[MyFloat alloc];
196: [ff setFValue: f];
197: return ff;
198: }
199: -(void) setFValue: (float) f { fValue = f; }
200: -(float) getFValue { return fValue; }
201: -(NSString *)description { return [NSString stringWithFormat: @"(float)%f",fValue]; }
202: @end
203:
204: @implementation MyInt
205: +(MyInt *)allocWith: (int) i {
206: MyInt *ii;
207: ii=[MyInt alloc];
208: [ii setIValue: i];
209: return ii;
210: }
211: -(void) setIValue: (int) i { iValue = i; }
212: -(int) getIValue { return iValue; }
213: -(NSString *)description { return [NSString stringWithFormat: @"(int)%d",iValue]; }
214: @end
215:
216: #define SMAX 0x1000 //4096*16
217: @implementation MyUtil
218: static int debugMyUtil = 0;
219: +(void) setDebugMyUtil {
220: if (debugMyUtil) debugMyUtil = 0;
221: else debugMyUtil = 1;
222: }
223: +(NSMutableArray *) arrayOfStringFrom: (NSString *) args {
224: int n,i,k,type;
225: unichar c;
226: unichar s[SMAX];
227: NSMutableArray *a; // for debug
228: NSMutableArray *aa;
229: NSString *ss;
230: int status,level;
231: status = -1; level = 0; type = 0;
232: n = [args length];
233: if (n >= SMAX-1) {
234: NSLog(@"Too big string for stringToArrayOfString.\n");
235: return nil;
236: }
237: a = [NSMutableArray arrayWithCapacity: 1];
238: aa = [NSMutableArray arrayWithCapacity: 1];
239: for (i=0, k=0; i<n; i++) {
240: c = [args characterAtIndex: i];
241: if (level == 0) {
242: switch (status) { // See my note on 2006-02-20.
243: case -1:
244: if ( c == '[') { k=0; status=0;}
245: break;
246: case 0:
247: if ( c == '[') {
248: k = 0; s[k] = c; k++; status = 1; type = 1; // type 1 : list
249: } else if (c == ',') {
250: } else if (c == '(') {
251: k=0; status = 21; type=0; //ex. (int)
252: } else if ((('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {
253: k=0; status = 2; type=3; s[k] = c; k++; // type -1 : string.
254: } else if (c > ' ') {
255: k=0; status = 2; type=0; s[k] = c; k++; // type 0 : float
256: } else if (c == ']') status = -1;
257: break;
258: case 2:
259: if ((c == ',') || (c == ']')) {
260: if (c == ',') status = 0; else status = -1;
261: if (k > 0) ss = [NSString stringWithCharacters: s length: k];
262: else ss = @"";
263: [a addObject: ss];
264: if (type == 2) [aa addObject: [MyInt allocWith: [ss intValue]]];
265: else if (type == 0) [aa addObject: [MyFloat allocWith: [ss floatValue]]];
266: else [aa addObject: ss];
267: }else{
268: s[k] = c; k++;
269: }
270: break;
271: case 1:
272: if (c == '[') {
273: s[k] = c; k++;
274: level++;
275: }else if (c == ']') {
276: s[k] = ']'; k++;
277: if (k > 0) ss = [NSString stringWithCharacters: s length: k];
278: else ss = @"";
279: [a addObject: ss];
280: [aa addObject: [MyUtil arrayOfStringFrom: ss]];
281: status = 0;
282: }else {
283: s[k] = c; k++;
284: }
285: break;
286: case 21:
287: if (c == ')') {
288: if ((s[0] == 'i') && (s[1] == 'n') && (s[2] == 't')) type=2;
289: else type = 0;
290: status = 2; k = 0; // starting to read data.
291: }else if (c > ' ') {
292: s[k] = c; k++;
293: }
294: break;
295: }
296: }else if (level > 0) {
297: s[k] = c; k++;
298: if (c == '[') level++;
299: else if (c == ']') level--;
300: }
301: }
302: if (debugMyUtil) {
303: NSLog(@"a=%@\n",a);
304: NSLog(@"aa=%@\n",aa);
305: }
306: return aa;
307: }
308:
309: /*
310: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[]"]);
311: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,[]]"]);
312: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,2,[3,4],5]"]);
313: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @" [1, 2,3, 4,5]"]);
314: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[[1],2,[[3,[4]]],5]"]); // There is a bug. Output is 1,2,...
315: */
316:
1.2 takayama 317: +(NSString *)pruneThings: (NSString *)ss {
318: int n,i, start,end;
319: unichar c;
320: unichar s[SMAX];
321: NSString *ans;
322: n = [ss length];
323: if (n >= SMAX-1) {
324: NSLog(@"Too big string for pruneThings.\n");
325: return nil;
326: }
327: start = 0; end = n-1;
328: for (i=0; i<n; i++) {
329: c = [ss characterAtIndex: i];
330: start = i;
331: if (c > 0x20) break;
332: }
333: for (i=n-1; i>=0; i--) {
334: c = [ss characterAtIndex: i];
335: end = i;
336: if (c > 0x20) break;
337: }
338: if (start > end) return nil;
339: for (i=0; i<= end-start ; i++) {
340: s[i] = [ss characterAtIndex: (start+i)];
341: s[i+1] =0;
342: }
343: ans = [NSString stringWithCharacters: s length: (end-start+1)];
344: return ans;
345: }
346:
347: +(id)attributedStringWithPath: (NSString *)path {
348: NSFileWrapper *theWrapper;
349: NSTextAttachment *theAttachment;
350: NSAttributedString *aStr;
351: theWrapper = [[NSFileWrapper alloc] initWithPath: path];
352: if (!theWrapper) NSLog(@"theWrapper is nil. Path=[%@]\n",path);
353: if (!theWrapper) return nil;
354: theAttachment = [[NSTextAttachment alloc] initWithFileWrapper:theWrapper];
355: aStr = [NSAttributedString attributedStringWithAttachment:theAttachment];
356: return aStr; // How should I do autorelease?
357: }
358:
359:
360: @end
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>