[BACK]Return to MyOpenGLCommand.m CVS log [TXT][DIR] Up to [local] / OpenXM / src / cfep

Annotation of OpenXM/src/cfep/MyOpenGLCommand.m, Revision 1.3

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:
                    169: @end
                    170:
                    171: /* Code to test MyFloat put in compile:
                    172:    MyFloat *x;
                    173:    NSLog(@"member test.\n"
                    174:    x = [MyFloat allocWith: 1.0];
                    175:    if ([x isMemberOfClass: [MyFloat class]] == YES) NSLog(@"[x class] is MyFloat\n");
                    176:    if ([x isMemberOfClass: [MyInt class]] == YES) NSLog(@"[x class] is MyInt\n");
                    177: */
                    178: @implementation MyFloat
                    179: +(MyFloat *)allocWith: (float) f {
                    180:    MyFloat *ff;
                    181:    ff=[MyFloat alloc];
                    182:    [ff setFValue: f];
                    183:    return ff;
                    184: }
                    185: -(void) setFValue: (float) f { fValue = f; }
                    186: -(float) getFValue { return fValue; }
                    187: -(NSString *)description { return [NSString stringWithFormat: @"(float)%f",fValue]; }
                    188: @end
                    189:
                    190: @implementation MyInt
                    191: +(MyInt *)allocWith: (int) i {
                    192:   MyInt *ii;
                    193:   ii=[MyInt alloc];
                    194:   [ii setIValue: i];
                    195:   return ii;
                    196: }
                    197: -(void) setIValue: (int) i { iValue = i; }
                    198: -(int) getIValue { return iValue; }
                    199: -(NSString *)description { return [NSString stringWithFormat: @"(int)%d",iValue]; }
                    200: @end
                    201:
                    202: #define SMAX  0x1000   //4096*16
                    203: @implementation MyUtil
                    204: static int debugMyUtil = 0;
                    205: +(void) setDebugMyUtil {
                    206:   if (debugMyUtil) debugMyUtil = 0;
                    207:   else debugMyUtil = 1;
                    208: }
                    209: +(NSMutableArray *) arrayOfStringFrom: (NSString *) args {
                    210:   int n,i,k,type;
                    211:   unichar c;
                    212:   unichar s[SMAX];
                    213:   NSMutableArray *a; // for debug
                    214:   NSMutableArray *aa;
                    215:   NSString *ss;
                    216:   int status,level;
                    217:   status = -1; level = 0; type = 0;
                    218:   n = [args length];
                    219:   if (n >= SMAX-1) {
                    220:     NSLog(@"Too big string for stringToArrayOfString.\n");
                    221:     return nil;
                    222:   }
                    223:   a = [NSMutableArray arrayWithCapacity: 1];
                    224:   aa = [NSMutableArray arrayWithCapacity: 1];
                    225:   for (i=0, k=0; i<n; i++) {
                    226:     c = [args characterAtIndex: i];
                    227:        if (level == 0) {
                    228:                switch (status) {  // See my note on 2006-02-20.
                    229:                case -1:
                    230:                        if ( c == '[') { k=0; status=0;}
                    231:                        break;
                    232:                case 0:
                    233:                        if ( c == '[') {
                    234:                                k = 0; s[k] = c; k++; status = 1; type = 1; // type 1 : list
                    235:                        } else if (c == ',') {
                    236:                        } else if (c == '(') {
                    237:                           k=0; status = 21; type=0;  //ex. (int)
                    238:                    } else if ((('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {
                    239:                           k=0; status = 2; type=3; s[k] = c; k++;      // type -1 : string.
                    240:                        } else if (c > ' ') {
                    241:                                k=0; status = 2; type=0; s[k] = c; k++;     // type 0 : float
                    242:                        } else if (c == ']') status = -1;
                    243:                        break;
                    244:                case 2:
                    245:                        if ((c == ',') || (c == ']')) {
                    246:                           if (c == ',') status = 0; else status = -1;
                    247:                if (k > 0) ss = [NSString stringWithCharacters: s length: k];
                    248:                           else ss = @"";
                    249:                           [a addObject: ss];
                    250:                           if (type == 2) [aa addObject: [MyInt allocWith: [ss intValue]]];
                    251:                           else if (type == 0) [aa addObject: [MyFloat allocWith: [ss floatValue]]];
                    252:                           else [aa addObject: ss];
                    253:                        }else{
                    254:                                s[k] = c; k++;
                    255:                        }
                    256:                        break;
                    257:                case 1:
                    258:                        if (c == '[') {
                    259:                           s[k] = c; k++;
                    260:                           level++;
                    261:                        }else if (c == ']') {
                    262:                           s[k] = ']'; k++;
                    263:                           if (k > 0) ss = [NSString stringWithCharacters: s length: k];
                    264:                           else ss = @"";
                    265:                           [a addObject: ss];
                    266:                           [aa addObject: [MyUtil arrayOfStringFrom: ss]];
                    267:                           status = 0;
                    268:                        }else {
                    269:                           s[k] = c; k++;
                    270:                        }
                    271:                        break;
                    272:                case 21:
                    273:             if (c == ')') {
                    274:                          if ((s[0] == 'i') && (s[1] == 'n') && (s[2] == 't')) type=2;
                    275:                          else type = 0;
                    276:                          status = 2; k = 0; // starting to read data.
                    277:                        }else if (c > ' ') {
                    278:                          s[k] = c; k++;
                    279:                        }
                    280:                        break;
                    281:                }
                    282:        }else if (level > 0) {
                    283:            s[k] = c; k++;
                    284:                if (c == '[') level++;
                    285:                else if (c == ']') level--;
                    286:        }
                    287:   }
                    288:   if (debugMyUtil) {
                    289:     NSLog(@"a=%@\n",a);
                    290:     NSLog(@"aa=%@\n",aa);
                    291:   }
                    292:   return aa;
                    293: }
                    294:
                    295: /*
                    296:    NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[]"]);
                    297:    NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,[]]"]);
                    298:    NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,2,[3,4],5]"]);
                    299:    NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @" [1, 2,3, 4,5]"]);
                    300:    NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[[1],2,[[3,[4]]],5]"]); // There is a bug. Output is 1,2,...
                    301: */
                    302:
1.2       takayama  303: +(NSString *)pruneThings: (NSString *)ss {
                    304:   int n,i, start,end;
                    305:   unichar c;
                    306:   unichar s[SMAX];
                    307:   NSString *ans;
                    308:   n = [ss length];
                    309:   if (n >= SMAX-1) {
                    310:     NSLog(@"Too big string for pruneThings.\n");
                    311:     return nil;
                    312:   }
                    313:   start = 0; end = n-1;
                    314:   for (i=0; i<n; i++) {
                    315:     c = [ss characterAtIndex: i];
                    316:        start = i;
                    317:        if (c > 0x20) break;
                    318:   }
                    319:   for (i=n-1; i>=0; i--) {
                    320:     c = [ss characterAtIndex: i];
                    321:     end = i;
                    322:        if (c > 0x20) break;
                    323:   }
                    324:   if (start > end) return nil;
                    325:   for (i=0; i<= end-start ;  i++) {
                    326:     s[i] = [ss characterAtIndex: (start+i)];
                    327:        s[i+1] =0;
                    328:   }
                    329:   ans = [NSString stringWithCharacters: s length: (end-start+1)];
                    330:   return ans;
                    331: }
                    332:
                    333: +(id)attributedStringWithPath: (NSString *)path {
                    334:    NSFileWrapper *theWrapper;
                    335:    NSTextAttachment *theAttachment;
                    336:    NSAttributedString *aStr;
                    337:    theWrapper = [[NSFileWrapper alloc] initWithPath:  path];
                    338:    if (!theWrapper) NSLog(@"theWrapper is nil. Path=[%@]\n",path);
                    339:    if (!theWrapper) return nil;
                    340:    theAttachment = [[NSTextAttachment alloc] initWithFileWrapper:theWrapper];
                    341:    aStr = [NSAttributedString attributedStringWithAttachment:theAttachment];
                    342:    return aStr;   // How should I do autorelease?
                    343: }
                    344:
                    345:
                    346: @end

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>