Merge branch 'master' of http://skoegl.net/uni/numpty
[numtypysics.git] / Scene.cpp
blob0f60cf3202b8f2e28c13f5cca831be6a304410e4
1 /*
2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
16 #include "Common.h"
17 #include "Array.h"
18 #include "Config.h"
19 #include "Scene.h"
21 #include <sstream>
22 #include <fstream>
25 Transform::Transform( float32 scale, float32 rotation, const Vec2& translation )
27 set( scale, rotation, translation );
30 void Transform::set( float32 scale, float32 rotation, const Vec2& translation )
32 if ( scale==0.0f && rotation==0.0f && translation==Vec2(0,0) ) {
33 m_bypass = true;
34 } else {
35 m_rot.Set( rotation );
36 m_pos = translation;
37 m_rot.col1.x *= scale;
38 m_rot.col1.y *= scale;
39 m_rot.col2.x *= scale;
40 m_rot.col2.y *= scale;
41 m_invrot = m_rot.Invert();
42 m_bypass = false;
46 Transform worldToScreen( 0.5f, M_PI/2, Vec2(240,0) );
48 void configureScreenTransform( int w, int h )
50 SCREEN_WIDTH = w;
51 SCREEN_HEIGHT = h;
52 FULLSCREEN_RECT = Rect(0,0,w-1,h-1);
53 if ( w==WORLD_WIDTH && h==WORLD_HEIGHT ) { //unity
54 worldToScreen.set( 0.0f, 0.0f, Vec2(0,0) );
55 } else {
56 float rot = 0.0f;
57 Vec2 tr(0,0);
58 if ( h > w ) { //portrait
59 rot = M_PI/2;
60 tr = Vec2( w, 0 );
61 b2Swap( h, w );
63 float scalew = (float)w/(float)WORLD_WIDTH;
64 float scaleh = (float)h/(float)WORLD_HEIGHT;
65 if ( scalew < scaleh ) {
66 worldToScreen.set( scalew, rot, tr );
67 } else {
68 worldToScreen.set( scaleh, rot, tr );
74 class Stroke
76 public:
78 private:
79 struct JointDef : public b2RevoluteJointDef
81 JointDef( b2Body* b1, b2Body* b2, const b2Vec2& pt )
83 Initialize( b1, b2, pt );
84 maxMotorTorque = 10.0f;
85 motorSpeed = 0.0f;
86 enableMotor = true;
90 struct BoxDef : public b2PolygonDef
92 void init( const Vec2& p1, const Vec2& p2, int attr )
94 b2Vec2 barOrigin = p1;
95 b2Vec2 bar = p2 - p1;
96 bar *= 1.0f/PIXELS_PER_METREf;
97 barOrigin *= 1.0f/PIXELS_PER_METREf;;
98 SetAsBox( bar.Length()/2.0f, 0.1f,
99 0.5f*bar + barOrigin, vec2Angle( bar ));
100 // SetAsBox( bar.Length()/2.0f+b2_toiSlop, b2_toiSlop*2.0f,
101 // 0.5f*bar + barOrigin, vec2Angle( bar ));
102 friction = 0.3f;
103 if ( attr & ATTRIB_GROUND ) {
104 density = 0.0f;
105 } else if ( attr & ATTRIB_GOAL ) {
106 density = 100.0f;
107 } else if ( attr & ATTRIB_TOKEN ) {
108 density = 3.0f;
109 friction = 0.1f;
110 } else {
111 density = 5.0f;
113 restitution = 0.2f;
117 public:
118 Stroke( const Path& path, int attributes )
119 : m_rawPath(path)
121 m_colour = brushColours[DEFAULT_BRUSH];
122 m_attributes = attributes;
123 m_origin = m_rawPath.point(0);
124 m_rawPath.translate( -m_origin );
125 setPlayer(0);
126 reset();
129 Stroke( const std::string& str )
131 setPlayer(0);
132 int col = 0;
133 m_colour = brushColours[DEFAULT_BRUSH];
134 m_attributes = 0;
135 m_origin = Vec2(400,240);
136 reset();
137 const char *s = str.c_str();
138 while ( *s && *s!=':' && *s!='\n' ) {
139 switch ( *s ) {
140 case 't': setAttribute( ATTRIB_TOKEN ); setPlayer(0); break;
141 case 'T': setAttribute( ATTRIB_TOKEN ); setPlayer(1); break;
142 case 'g': setAttribute( ATTRIB_GOAL ); setPlayer(0); break;
143 case 'G': setAttribute( ATTRIB_GOAL ); setPlayer(1); break;
144 case 'f': setAttribute( ATTRIB_GROUND ); break;
145 case 's': setAttribute( ATTRIB_SLEEPING ); break;
146 case 'd': setAttribute( ATTRIB_DECOR ); break;
147 default:
148 if ( *s >= '0' && *s <= '9' ) {
149 col = col*10 + *s -'0';
151 break;
153 s++;
155 if ( col >= 0 && col < NUM_BRUSHES ) {
156 m_colour = brushColours[col];
158 if ( *s++ == ':' ) {
159 m_rawPath = Path(s);
161 if ( m_rawPath.size() < 2 ) {
162 throw "invalid stroke def";
164 m_origin = m_rawPath.point(0);
165 m_rawPath.translate( -m_origin );
166 setAttribute( ATTRIB_DUMMY );
169 void reset( b2World* world=NULL )
171 if (m_body && world) {
172 world->DestroyBody( m_body );
174 m_body = NULL;
175 m_xformAngle = 7.0f;
176 m_drawnBbox.tl = m_origin;
177 m_drawnBbox.br = m_origin;
178 m_jointed[0] = m_jointed[1] = false;
179 m_shapePath = m_rawPath;
180 m_hide = 0;
181 m_drawn = false;
184 std::string asString()
186 std::stringstream s;
187 s << 'S';
188 if ( hasAttribute(ATTRIB_TOKEN) ) s<<'t';
189 if ( hasAttribute(ATTRIB_GOAL) ) s<<'g';
190 if ( hasAttribute(ATTRIB_GROUND) ) s<<'f';
191 if ( hasAttribute(ATTRIB_SLEEPING) ) s<<'s';
192 if ( hasAttribute(ATTRIB_DECOR) ) s<<'d';
193 for ( int i=0; i<NUM_BRUSHES; i++ ) {
194 if ( m_colour==brushColours[i] ) s<<i;
196 s << ":";
197 transform();
198 for ( int i=0; i<m_xformedPath.size(); i++ ) {
199 const Vec2& p = m_xformedPath.point(i);
200 s <<' '<< p.x << ',' << p.y;
202 s << std::endl;
203 return s.str();
206 void setAttribute( Attribute a )
208 m_attributes |= a;
209 if ( m_attributes & ATTRIB_TOKEN ) m_colour = brushColours[RED_BRUSH];
210 else if ( m_attributes & ATTRIB_GOAL ) m_colour = brushColours[YELLOW_BRUSH];
213 void setPlayer( int player )
215 m_player = player;
218 int getPlayer()
220 return m_player;
223 void clearAttribute( Attribute a )
225 m_attributes &= ~a;
228 bool hasAttribute( Attribute a )
230 return (m_attributes&a) != 0;
232 void setColour( int c )
234 m_colour = c;
237 void createBodies( b2World& world )
239 process();
240 if ( hasAttribute( ATTRIB_DECOR ) ){
241 return; //decorators have no physical embodiment
243 int n = m_shapePath.numPoints();
244 if ( n > 1 ) {
245 b2BodyDef bodyDef;
246 bodyDef.position = m_origin;
247 bodyDef.position *= 1.0f/PIXELS_PER_METREf;
248 bodyDef.userData = this;
249 if ( m_attributes & ATTRIB_SLEEPING ) {
250 bodyDef.isSleeping = true;
252 m_body = world.CreateBody( &bodyDef );
253 for ( int i=1; i<n; i++ ) {
254 BoxDef boxDef;
255 boxDef.init( m_shapePath.point(i-1),
256 m_shapePath.point(i),
257 m_attributes );
258 m_body->CreateShape( &boxDef );
260 m_body->SetMassFromShapes();
263 transform();
266 void setNoMass()
268 static b2MassData m;
269 if (m_body) {
270 m_body->SetMass(&m);
274 void setDefaultMass()
276 if (m_body) {
277 m_body->SetMassFromShapes();
281 bool maybeCreateJoint( b2World& world, Stroke* other )
283 if ( (m_attributes&ATTRIB_CLASSBITS)
284 != (other->m_attributes&ATTRIB_CLASSBITS) ) {
285 return false; // can only joint matching classes
286 } else if ( hasAttribute(ATTRIB_GROUND) ) {
287 return true; // no point jointing grounds
288 } else if ( m_body && other->body() ) {
289 transform();
290 int n = m_xformedPath.numPoints();
291 for ( int end=0; end<2; end++ ) {
292 if ( !m_jointed[end] ) {
293 const Vec2& p = m_xformedPath.point( end ? n-1 : 0 );
294 if ( other->distanceTo( p ) <= JOINT_TOLERANCE ) {
295 //printf("jointed end %d d=%f\n",end,other->distanceTo( p ));
296 b2Vec2 pw = p;
297 pw *= 1.0f/PIXELS_PER_METREf;
298 JointDef j( m_body, other->m_body, pw );
299 world.CreateJoint( &j );
300 m_jointed[end] = true;
305 if ( m_body ) {
306 return m_jointed[0] && m_jointed[1];
308 return true; ///nothing to do
311 void draw( Canvas& canvas )
313 if ( m_hide < HIDE_STEPS ) {
314 bool thick = (canvas.width() > 400);
315 transform();
316 canvas.drawPath( m_screenPath, canvas.makeColour(m_colour), thick );
317 m_drawn = true;
319 m_drawnBbox = m_screenBbox;
322 void addPoint( const Vec2& pp )
324 Vec2 p = pp; p -= m_origin;
325 if ( p == m_rawPath.point( m_rawPath.numPoints()-1 ) ) {
326 } else {
327 m_rawPath.append( p );
328 m_drawn = false;
332 void origin( const Vec2& p )
334 // todo
335 if ( m_body ) {
336 b2Vec2 pw = p;
337 pw *= 1.0f/PIXELS_PER_METREf;
338 m_body->SetXForm( pw, m_body->GetAngle() );
340 m_origin = p;
343 b2Body* body() { return m_body; }
345 float32 distanceTo( const Vec2& pt )
347 float32 best = 100000.0;
348 transform();
349 for ( int i=1; i<m_xformedPath.numPoints(); i++ ) {
350 Segment s( m_xformedPath.point(i-1), m_xformedPath.point(i) );
351 float32 d = s.distanceTo( pt );
352 //printf(" d[%d]=%f %d,%d\n",i,d,m_rawPath.point(i-1).x,m_rawPath.point(i-1).y);
353 if ( d < best ) {
354 best = d;
357 return best;
360 Rect screenBbox()
362 transform();
363 return m_screenBbox;
366 Rect lastDrawnBbox()
368 return m_drawnBbox;
371 Rect worldBbox()
373 return m_xformedPath.bbox();
376 bool isDirty()
378 return (!m_drawn || transform()) && !hasAttribute(ATTRIB_DELETED);
381 void hide()
383 if ( m_hide==0 ) {
384 m_hide = 1;
386 if (m_body) {
387 // stash the body where no-one will find it
388 m_body->SetXForm( b2Vec2(0.0f,SCREEN_HEIGHT*2.0f), 0.0f );
389 m_body->SetLinearVelocity( b2Vec2(0.0f,0.0f) );
390 m_body->SetAngularVelocity( 0.0f );
395 bool hidden()
397 return m_hide >= HIDE_STEPS;
400 int numPoints()
402 return m_rawPath.numPoints();
405 private:
406 static float32 vec2Angle( b2Vec2 v )
408 return b2Atan2(v.y, v.x);
411 void process()
413 float32 thresh = SIMPLIFY_THRESHOLDf;
414 m_rawPath.simplify( thresh );
415 m_shapePath = m_rawPath;
417 while ( m_shapePath.numPoints() > MULTI_VERTEX_LIMIT ) {
418 thresh += SIMPLIFY_THRESHOLDf;
419 m_shapePath.simplify( thresh );
423 bool transform()
425 // distinguish between xformed raw and shape path as needed
426 if ( m_hide ) {
427 if ( m_hide < HIDE_STEPS ) {
428 //printf("hide %d\n",m_hide);
429 Vec2 o = m_screenBbox.centroid();
430 m_screenPath -= o;
431 m_screenPath.scale( 0.99 );
432 m_screenPath += o;
433 m_screenBbox = m_screenPath.bbox();
434 m_hide++;
435 return true;
437 } else if ( m_body ) {
438 if ( hasAttribute( ATTRIB_DECOR ) ) {
439 return false; // decor never moves
440 } else if ( hasAttribute( ATTRIB_GROUND )
441 && m_xformAngle == m_body->GetAngle() ) {
442 return false; // ground strokes never move.
443 } else if ( m_xformAngle != m_body->GetAngle()
444 || ! (m_xformPos == m_body->GetPosition()) ) {
445 //printf("transform stroke - rot or pos\n");
446 b2Mat22 rot( m_body->GetAngle() );
447 b2Vec2 orig = PIXELS_PER_METREf * m_body->GetPosition();
448 m_xformedPath = m_rawPath;
449 m_xformedPath.rotate( rot );
450 m_xformedPath.translate( Vec2(orig) );
451 m_xformAngle = m_body->GetAngle();
452 m_xformPos = m_body->GetPosition();
453 worldToScreen.transform( m_xformedPath, m_screenPath );
454 m_screenBbox = m_screenPath.bbox();
455 } else {
456 //printf("transform none\n");
457 return false;
459 } else {
460 //printf("transform no body\n");
461 m_xformedPath = m_rawPath;
462 m_xformedPath.translate( m_origin );
463 worldToScreen.transform( m_xformedPath, m_screenPath );
464 m_screenBbox = m_screenPath.bbox();
465 return false;
467 return true;
470 Path m_rawPath;
471 int m_colour;
472 int m_attributes;
473 int m_player;
474 Vec2 m_origin;
475 Path m_shapePath;
476 Path m_xformedPath;
477 Path m_screenPath;
478 float32 m_xformAngle;
479 b2Vec2 m_xformPos;
480 Rect m_screenBbox;
481 Rect m_drawnBbox;
482 bool m_drawn;
483 b2Body* m_body;
484 bool m_jointed[2];
485 int m_hide;
488 Scene::Scene( bool noWorld )
489 : m_world( NULL ),
490 m_bgImage( NULL ),
491 m_protect( 0 )
493 if ( !noWorld ) {
494 b2AABB worldAABB;
495 worldAABB.lowerBound.Set(-100.0f, -100.0f);
496 worldAABB.upperBound.Set(100.0f, 100.0f);
498 b2Vec2 gravity(0.0f, 10.0f*PIXELS_PER_METREf/GRAVITY_FUDGEf);
499 bool doSleep = true;
500 m_world = new b2World(worldAABB, gravity, doSleep);
501 m_world->SetContactListener( this );
505 Scene::~Scene()
507 clear();
508 if ( m_world ) {
509 step();
510 delete m_world;
514 Stroke* Scene::newStroke( const Path& p, int colour, int attribs ) {
515 Stroke *s = new Stroke(p, attribs);
516 switch ( colour ) {
517 case 0: s->setAttribute( ATTRIB_TOKEN ); break;
518 case 1: s->setAttribute( ATTRIB_GOAL ); break;
519 default: s->setColour( brushColours[colour] ); break;
521 s->setPlayer(0);
522 m_strokes.append( s );
523 return s;
526 bool Scene::deleteStroke( Stroke *s ) {
527 if ( s ) {
528 int i = m_strokes.indexOf(s);
529 if ( i >= m_protect ) {
530 reset(s);
531 m_strokes.erase( m_strokes.indexOf(s) );
532 return true;
535 return false;
539 void Scene::extendStroke( Stroke* s, const Vec2& pt )
541 if ( s ) {
542 s->addPoint( pt );
546 void Scene::moveStroke( Stroke* s, const Vec2& origin )
548 if ( s ) {
549 int i = m_strokes.indexOf(s);
550 if ( i >= m_protect ) {
551 s->origin( origin );
556 void Scene::setNoMass(Stroke* s)
558 s->setNoMass();
561 void Scene::setDefaultMass(Stroke* s)
563 s->setDefaultMass();
566 bool Scene::activate( Stroke *s )
568 if ( s->numPoints() > 1 ) {
569 s->createBodies( *m_world );
570 createJoints( s );
571 return true;
573 return false;
576 void Scene::activateAll()
578 m_num_goal = 0;
579 m_num_token = 0;
580 m_num_player = 0;
581 m_winner = -1;
583 for ( int i=0; i < m_strokes.size(); i++ ) {
584 m_strokes[i]->createBodies( *m_world );
585 if (m_strokes[i]->hasAttribute(ATTRIB_TOKEN)) { m_num_token++;}
586 if (m_strokes[i]->hasAttribute(ATTRIB_GOAL)) { m_num_goal++; }
587 if (m_strokes[i]->getPlayer() + 1 > m_num_player) { m_num_player = m_strokes[i]->getPlayer() + 1; }
589 for ( int i=0; i < m_strokes.size(); i++ ) {
590 createJoints( m_strokes[i] );
594 void Scene::createJoints( Stroke *s )
596 for ( int j=m_strokes.size()-1; j>=0; j-- ) {
597 if ( s != m_strokes[j] ) {
598 //printf("try join to %d\n",j);
599 s->maybeCreateJoint( *m_world, m_strokes[j] );
600 m_strokes[j]->maybeCreateJoint( *m_world, s );
605 void Scene::step()
607 m_world->Step( ITERATION_TIMESTEPf, SOLVER_ITERATIONS );
608 // clean up delete strokes
609 for ( int i=0; i< m_strokes.size(); i++ ) {
610 if ( m_strokes[i]->hasAttribute(ATTRIB_DELETED) ) {
611 m_strokes[i]->clearAttribute(ATTRIB_DELETED);
612 m_strokes[i]->hide();
615 // check for token respawn
616 for ( int i=0; i < m_strokes.size(); i++ ) {
617 if ( m_strokes[i]->hasAttribute( ATTRIB_TOKEN )
618 && !BOUNDS_RECT.intersects( m_strokes[i]->worldBbox() ) ) {
619 reset( m_strokes[i] );
620 activate( m_strokes[i] );
625 // b2ContactListener callback when a new contact is detected
626 void Scene::Add(const b2ContactPoint* point)
628 // check for completion
629 //if (c->GetManifoldCount() > 0) {
630 Stroke* s1 = (Stroke*)point->shape1->GetBody()->GetUserData();
631 Stroke* s2 = (Stroke*)point->shape2->GetBody()->GetUserData();
633 if ( s1 && s2 ) {
634 if ( s2->hasAttribute(ATTRIB_TOKEN) ) {
635 b2Swap( s1, s2 );
637 if ( s1->hasAttribute(ATTRIB_TOKEN)
638 && s2->hasAttribute(ATTRIB_GOAL) ) {
639 s2->setAttribute(ATTRIB_DELETED);
640 if (m_num_goal == 1) {
641 m_winner = s1->getPlayer();
647 bool Scene::isCompleted()
649 /* if we only have one goal, m_winner has already been set */
650 if ((m_num_goal == 1) && (m_winner != -1)) {
651 return true;
654 else if (m_num_goal > 1)
656 int left[m_num_player];
657 for(int n=0; n<m_num_player; n++) {
658 left[n] = 0;
661 for ( int i=0; i < m_strokes.size(); i++ ) {
662 if ( m_strokes[i]->hasAttribute( ATTRIB_GOAL )
663 && !m_strokes[i]->hidden() ) {
664 left[m_strokes[i]->getPlayer()]++;
668 for ( int n=0; n<m_num_player; n++) {
669 if (left[n] == 0) {
670 m_winner = n;
671 return true;
676 return false;
679 int Scene::getWinner()
681 return m_num_player == 1 ? -1 : m_winner;
684 Rect Scene::dirtyArea()
686 Rect r(0,0,0,0),temp;
687 int numDirty = 0;
688 for ( int i=0; i<m_strokes.size(); i++ ) {
689 if ( m_strokes[i]->isDirty() ) {
690 // acumulate new areas to draw
691 temp = m_strokes[i]->screenBbox();
692 if ( !temp.isEmpty() ) {
693 if ( numDirty==0 ) {
694 r = temp;
695 } else {
696 r.expand( m_strokes[i]->screenBbox() );
698 // plus prev areas to erase
699 r.expand( m_strokes[i]->lastDrawnBbox() );
700 numDirty++;
704 if ( !r.isEmpty() ) {
705 // expand to allow for thick lines
706 r.tl.x--; r.tl.y--;
707 r.br.x++; r.br.y++;
709 return r;
712 void Scene::draw( Canvas& canvas, const Rect& area )
714 if ( m_bgImage ) {
715 canvas.setBackground( m_bgImage );
716 } else {
717 canvas.setBackground( 0 );
719 canvas.clear( area );
720 Rect clipArea = area;
721 clipArea.tl.x--;
722 clipArea.tl.y--;
723 clipArea.br.x++;
724 clipArea.br.y++;
725 for ( int i=0; i<m_strokes.size(); i++ ) {
726 if ( area.intersects( m_strokes[i]->screenBbox() ) ) {
727 m_strokes[i]->draw( canvas );
730 //canvas.drawRect( area, 0xffff0000, false );
733 void Scene::reset( Stroke* s )
735 for ( int i=0; i<m_strokes.size(); i++ ) {
736 if (s==NULL || s==m_strokes[i]) {
737 m_strokes[i]->reset(m_world);
742 Stroke* Scene::strokeAtPoint( const Vec2 pt, float32 max )
744 Stroke* best = NULL;
745 for ( int i=0; i<m_strokes.size(); i++ ) {
746 float32 d = m_strokes[i]->distanceTo( pt );
747 //printf("stroke %d dist %f\n",i,d);
748 if ( d < max ) {
749 max = d;
750 best = m_strokes[i];
753 return best;
756 void Scene::clear()
758 reset();
759 while ( m_strokes.size() ) {
760 delete m_strokes[0];
761 m_strokes.erase(0);
763 if ( m_world ) {
764 //step is required to actually destroy bodies and joints
765 m_world->Step( ITERATION_TIMESTEPf, SOLVER_ITERATIONS );
769 void Scene::setGravity( const std::string& s )
771 float32 x,y;
772 if ( sscanf( s.c_str(), "%f,%f", &x, &y )==2) {
773 if ( m_world ) {
774 b2Vec2 g(x,y);
775 g *= PIXELS_PER_METREf/GRAVITY_FUDGEf;
776 m_world->SetGravity( g );
778 } else {
779 fprintf(stderr,"invalid gravity vector\n");
783 bool Scene::load( unsigned char *buf, int bufsize )
785 std::string s( (const char*)buf, bufsize );
786 std::stringstream in( s, std::ios::in );
787 return load( in );
790 bool Scene::load( const std::string& file )
792 std::ifstream in( file.c_str(), std::ios::in );
793 return load( in );
796 bool Scene::load( std::istream& in )
798 clear();
799 if ( g_bgImage==NULL ) {
800 g_bgImage = new Image("paper.png");
801 g_bgImage->scale( SCREEN_WIDTH, SCREEN_HEIGHT );
803 m_bgImage = g_bgImage;
804 std::string line;
805 while ( !in.eof() ) {
806 getline( in, line );
807 parseLine( line );
809 protect();
810 return true;
813 void Scene::trimWhitespace( std::string& s )
815 static const char* whitespace = " \t";
816 size_t start = s.find_first_not_of(whitespace);
817 size_t end = s.find_last_not_of(whitespace);
819 if (std::string::npos == start || std::string::npos == end) {
820 s = "";
821 } else {
822 s = s.substr(start, end-start+1);
826 bool Scene::parseLine( const std::string& line )
828 try {
829 switch( line[0] ) {
830 case 'T': m_title = line.substr(line.find(':')+1);
831 trimWhitespace(m_title); return true;
832 case 'B': m_bg = line.substr(line.find(':')+1); return true;
833 case 'A': m_author = line.substr(line.find(':')+1);
834 trimWhitespace(m_author);
835 /* The original levels have their author set
836 * to "test". Remove the author in that case. */
837 if (m_author.compare("test") == 0) {
838 m_author = "";
839 } return true;
840 case 'S': {
841 Stroke *s = new Stroke(line);
842 m_strokes.append(s);
843 return true;
845 case 'G': setGravity(line.substr(line.find(':')+1));return true;
847 } catch ( const char* e ) {
848 printf("Stroke error: %s\n",e);
850 return false;
853 void Scene::protect( int n )
855 m_protect = (n==-1 ? m_strokes.size() : n );
858 bool Scene::save( const std::string& file )
860 printf("saving to %s\n",file.c_str());
861 std::ofstream o( file.c_str(), std::ios::out );
862 if ( o.is_open() ) {
863 o << "Title: "<<m_title<<std::endl;
864 o << "Author: "<<m_author<<std::endl;
865 o << "Background: "<<m_bg<<std::endl;
866 for ( int i=0; i<m_strokes.size(); i++ ) {
867 o << m_strokes[i]->asString();
869 o.close();
870 return true;
871 } else {
872 return false;
877 Image *Scene::g_bgImage = NULL;