Add support for displaying level names and authors
[numtypysics.git] / Scene.h
blob6a66e8f03858899e6bf656ebae9f0ce35e859d9d
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 );
82 const char* getTitle() {
83 return m_title.c_str();
86 const char* getAuthor() {
87 return m_author.c_str();
90 private:
91 void createJoints( Stroke *s );
92 void trimWhitespace( std::string& s );
93 bool parseLine( const std::string& line );
95 // b2ContactListener callback when a new contact is detected
96 virtual void Add(const b2ContactPoint* point) ;
99 b2World *m_world;
100 Array<Stroke*> m_strokes;
101 std::string m_title, m_author, m_bg;
102 Image *m_bgImage;
103 static Image *g_bgImage;
104 int m_protect;
105 int m_num_token;
106 int m_num_goal;
107 int m_num_player;
108 int m_winner;
113 class Transform
115 public:
116 Transform( float32 scale, float32 rotation, const Vec2& translation );
117 void set( float32 scale, float32 rotation, const Vec2& translation );
119 inline void transform( const Path& pin, Path& pout ) {
120 pout = pin;
121 if ( !m_bypass ) {
122 pout.rotate( m_rot );
123 pout.translate( m_pos );
126 inline void transform( Vec2& vec ) {
127 if ( !m_bypass ) {
128 vec = Vec2( b2Mul( m_rot, vec ) ) + m_pos;
131 inline void inverseTransform( Vec2& vec ) {
132 if ( !m_bypass ) {
133 vec = Vec2( b2Mul( m_invrot, vec-m_pos ) );
136 private:
137 Transform() {}
138 bool m_bypass;
139 b2Mat22 m_rot;
140 b2Mat22 m_invrot;
141 Vec2 m_pos;
144 extern Transform worldToScreen;
146 extern void configureScreenTransform( int w, int h );