First cut of drag/draw support (MultiTouch); drag fixes
[numtypysics.git] / Scene.h
blob974797e459cde5440c5f9545da2307a948f65c5b
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 "Path.h"
19 #include "Canvas.h"
21 #include <string>
22 #include <fstream>
25 class Stroke;
26 class b2World;
28 typedef enum {
29 ATTRIB_DUMMY = 0,
30 ATTRIB_GROUND = 1,
31 ATTRIB_TOKEN = 2,
32 ATTRIB_GOAL = 4,
33 ATTRIB_DECOR = 8,
34 ATTRIB_SLEEPING = 16,
35 ATTRIB_HIDDEN = 32,
36 ATTRIB_DELETED = 64,
37 ATTRIB_CLASSBITS = ATTRIB_TOKEN | ATTRIB_GOAL
38 } Attribute;
41 class Scene : private b2ContactListener
43 public:
45 Scene( bool noWorld=false );
46 ~Scene();
48 Stroke* newStroke( const Path& p, int colour, int attributes );
49 bool deleteStroke( Stroke *s );
50 void extendStroke( Stroke* s, const Vec2& pt );
51 void moveStroke( Stroke* s, const Vec2& origin );
52 void setNoMass(Stroke* s);
53 void setDefaultMass(Stroke* s);
55 bool activate( Stroke *s );
56 void activateAll();
58 int numStrokes() {
59 return m_strokes.size();
62 Array<Stroke*>& strokes() {
63 return m_strokes;
66 void step();
67 bool isCompleted();
68 int getWinner();
69 void setPlayerMax(int);
70 Rect dirtyArea();
71 void draw( Canvas& canvas, const Rect& area );
72 void reset( Stroke* s=NULL );
73 Stroke* strokeAtPoint( const Vec2 pt, float32 max );
74 void clear();
75 void setGravity( const std::string& s );
77 bool load( unsigned char *buf, int bufsize );
78 bool load( const std::string& file );
79 bool load( std::istream& in );
80 void protect( int n=-1 );
81 bool save( const std::string& file );
84 private:
85 void createJoints( Stroke *s );
86 bool parseLine( const std::string& line );
88 // b2ContactListener callback when a new contact is detected
89 virtual void Add(const b2ContactPoint* point) ;
92 b2World *m_world;
93 Array<Stroke*> m_strokes;
94 std::string m_title, m_author, m_bg;
95 Image *m_bgImage;
96 static Image *g_bgImage;
97 int m_protect;
98 int m_num_token;
99 int m_num_goal;
100 int m_num_player;
101 int m_winner;
106 class Transform
108 public:
109 Transform( float32 scale, float32 rotation, const Vec2& translation );
110 void set( float32 scale, float32 rotation, const Vec2& translation );
112 inline void transform( const Path& pin, Path& pout ) {
113 pout = pin;
114 if ( !m_bypass ) {
115 pout.rotate( m_rot );
116 pout.translate( m_pos );
119 inline void transform( Vec2& vec ) {
120 if ( !m_bypass ) {
121 vec = Vec2( b2Mul( m_rot, vec ) ) + m_pos;
124 inline void inverseTransform( Vec2& vec ) {
125 if ( !m_bypass ) {
126 vec = Vec2( b2Mul( m_invrot, vec-m_pos ) );
129 private:
130 Transform() {}
131 bool m_bypass;
132 b2Mat22 m_rot;
133 b2Mat22 m_invrot;
134 Vec2 m_pos;
137 extern Transform worldToScreen;
139 extern void configureScreenTransform( int w, int h );