class Animation { public: FILE *fp; double position; double updatePosition( double delta ){ if(position<0.0) position=delta; else position+=delta; return position; } }; class _Origami{ public: int maxStage; Stage *stage[256]; // a sequence of foldings FaceProperty faceProperty; bool tentative; Adjust adjust; Animation animation; _Origami( char *filename ){ //generation FILE *fp; if( fopen_s( &fp, filename, "r" ) !=0 ){ this->saveDefaultSetup( filename ); if( fopen_s( &fp, filename, "r" ) !=0 ){ printf( "Can't open %s\n", filename ); exit( 1 ); } } maxStage = 0; char str[64]; while(fscanf_s(fp,"%s",str,64)!=EOF){ if(!strcmp(str, "VertexSize")){ stage[0] = new Stage( fp );//construct an original state }else if(!strcmp(str, "FrontColor")){ faceProperty.loadColor( fp, 0 ); }else if(!strcmp(str, "BackColor")){ faceProperty.loadColor( fp, 1 ); }else if(!strcmp(str, "BorderColor")){ faceProperty.loadColor( fp, 2 ); }else if(!strcmp(str, "FrontTexture")){ faceProperty.loadTexture( fp, 0 ); }else if(!strcmp(str, "BackTexture")){ faceProperty.loadTexture( fp, 1 ); } } fclose( fp ); tentative = false; } void _Origami::saveDefaultSetup( char *filename ){ FILE *fp; if( fopen_s( &fp, filename, "w" ) !=0 ){ printf( "Can't create %s\n", filename ); exit( 1 ); } fprintf(fp, "VertexSize 4\n"); fprintf(fp, " 10.0 10.0\n"); fprintf(fp, "-10.0 10.0\n"); fprintf(fp, "-10.0 -10.0\n"); fprintf(fp, " 10.0 -10.0\n"); fprintf(fp, "FrontColor 255 0 0\n"); fprintf(fp, "BackColor 0 255 255\n"); fprintf(fp, "BorderColor 255 255 255\n"); fprintf(fp, "/FrontTexture chiyo1.raw\n"); fprintf(fp, "/BackTexture chiyo3.raw\n"); fclose( fp ); } //renew and reset Vector pickVertex( Line &cursorLine ){ return stage[maxStage]->pick( cursorLine ); } void moveVertex( Vector &destination, Vector &eyePosition, bool inBending ){ if(tentative){ this->reset(); tentative = false; } if( stage[maxStage]->fold.update( destination, eyePosition, inBending ) ){ tentative = false; return; } adjust.correctDestination( *stage[maxStage] ); if( stage[maxStage]->renew() ){ tentative = false; return; } stage[maxStage+1] = new Stage( *stage[maxStage] ); maxStage++; tentative = true; } void releaseVertex(){ tentative = false; } void reset(){ if( maxStage == 0 ) return; maxStage--; delete stage[maxStage+1]; stage[maxStage]->reset(); } void reset( int maxStage ){ while( this->maxStage > maxStage ) this->reset(); } //draw void draw( Vector &eyePosition ){ stage[maxStage]->draw( eyePosition, faceProperty ); } void drawAdjust(){ if(tentative) adjust.draw(); } // file io void save( char *filename ){ FILE *fp; if( fopen_s( &fp, filename, "wb" ) !=0 ){ printf( "Can't create %s\n", filename ); return; } for( int i=0; isaveFold( fp ); fclose(fp); } void load( char *filename ){ FILE *fp; if( fopen_s( &fp, filename, "rb" ) !=0 ){ printf( "Can't open %s\n", filename ); return; } this->reset( 0 ); while( stage[maxStage]->loadFold( fp ) ){ stage[maxStage]->renew(); stage[maxStage+1] = new Stage( *stage[maxStage] ); maxStage++; } fclose(fp); } // animation void reset( char *filename ){ if( fopen_s( &animation.fp, filename, "rb" ) !=0 ){ printf( "Can't open %s\n", filename ); return; } this->reset( 0 ); animation.position = -1.0; } bool play( int fps ){ printf("%f\n",animation.position); if( animation.position < 0.0 ){ if( !stage[maxStage]->loadFold( animation.fp ) ){ fclose( animation.fp ); return true; } }else{ this->reset(); } stage[maxStage]->fold.modify( animation.updatePosition( 1.0/double(fps) ) ); stage[maxStage]->renew(); stage[maxStage+1] = new Stage( *stage[maxStage] ); maxStage++; if( animation.position >= 1.0 ) animation.position = -1.0; return false; } };