Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

testing.h

Go to the documentation of this file.
00001 #include "cgame.h"
00002 
00003 #include <sstream>
00004 
00011 namespace Testing {
00015 class CFPSGraphicalObserver : public CTextLabel, public IObserver {
00016 public:
00019   CFPSGraphicalObserver(CPoint &pos, CFont *font) : CTextLabel("", pos, font) { }
00020 
00022   virtual void update(CModel *model) {
00023     std::stringstream streamer;
00024 
00025     streamer << "FPS: " << ((CFPSTimer *) model)->get_fps();
00026 
00027     set_text(streamer.str());
00028   }
00029 
00035   virtual int do_handle_event(CEvent &event) {
00036     if (event.get_type() == CEvent::TYPE_KEY_DOWN) {
00037       if (event.get_key_ascii_value() == 'f') {
00038               set_visible(!visible());
00039         return 1;
00040       }
00041     }
00042     return 0;
00043   }
00044 };
00045 
00048 class CPhysicsTickerModel : public ITickable, public CModel {
00049 private:
00050   /* Acceleration vector of physics system, in pixels/msec^2. */
00051   double x_acc;
00052   double y_acc;
00053   /* Velocity vector of physics system, in pixels/msec. */
00054   double x_vel;
00055   double y_vel;
00056   /* Position of the box, in pixels. */
00057   double x;
00058   double y;
00059   /* Width and height of the box, in pixels. */
00060   int width, height;
00061   /* Width and height of the screen, in pixels. */
00062   int scr_width, scr_height;
00063 
00064 public:
00067   CPhysicsTickerModel(CPoint &pos, int scr_width, int scr_height) : x(pos.x), y(pos.y),
00068     scr_width(scr_width), scr_height(scr_height) {
00069 
00070     x_acc = 0;
00071     y_acc = 0.0005;
00072 
00073     x_vel = 0.1;
00074     y_vel = 0;
00075   }
00076 
00078   void set_box_size(const CPoint &size) {
00079     width = size.x;
00080     height = size.y;
00081   }
00082 
00084   CPoint get_box_pos() {
00085     return CPoint((int) x, (int) y);
00086   }
00087 
00089   virtual void reverse_direction() {
00090     x_vel = -x_vel;
00091     y_vel = -y_vel;
00092   }
00093 
00095   virtual void tick(unsigned long time) {
00096     int old_x = (int) x;
00097     int old_y = (int) y;
00098 
00099     y_vel += (y_acc * time);
00100     x_vel += (x_acc * time);
00101     x += (x_vel * time);
00102     y += (y_vel * time);
00103     
00104     if (x + width > scr_width) {
00105       x_vel = -x_vel;
00106       x = scr_width - width;
00107     } else if (x < 0) {
00108       x_vel = -x_vel;
00109       x = 0;
00110     }
00111 
00112     if (y + height > scr_height) {
00113       y_vel = -y_vel;
00114       y = scr_height - height;
00115     } else if (y < 0) {
00116       y_vel = -y_vel;
00117       y = 0;
00118     }
00119 
00120     int new_x = (int) x;
00121     int new_y = (int) y;
00122 
00123     if (old_x != new_x || old_y != new_y) {
00124       notify();
00125     }
00126   }
00127 };
00128 
00131 class CPhysicsTickerObserver : public CColoredPanel, public IObserver {
00132   CPhysicsTickerModel *ticker;
00133   CTextLabel *label1;
00134   CFPSGraphicalObserver *panel1_fps_display;
00135   CFont *font;
00136   char reverse_direction_key;
00137 
00138 public:
00141   CPhysicsTickerObserver(CPhysicsTickerModel *n_ticker, CPanel &parent,
00142                          CFont *font, CColor n_color, CRect &bounding_rect, char reverse_direction_key) : ticker(n_ticker), reverse_direction_key(reverse_direction_key),
00143     font(font), CColoredPanel(bounding_rect, n_color)
00144   {
00145     parent.add(*this);
00146 
00147     ticker->set_box_size(get_size());
00148     ticker->attach(this);
00149 
00150     CPoint text_point(5, 5);
00151     std::stringstream streamer;
00152     streamer << "Box " << reverse_direction_key;
00153     label1 = new CTextLabel(streamer.str().c_str(), text_point, font);
00154 
00155     add(*label1);
00156 
00157     CPoint fps_point(5, 20);
00158 
00159     panel1_fps_display = new CFPSGraphicalObserver(fps_point, font);
00160     CGame::get_instance().timer.attach(panel1_fps_display);
00161 
00162     add(*panel1_fps_display);    
00163   }
00164 
00165   virtual ~CPhysicsTickerObserver() {
00166     delete label1;
00167     delete panel1_fps_display;
00168   }
00169 
00171   virtual void update(CModel *model) {
00172     if (model == ticker) {
00173       set_pos(ticker->get_box_pos());
00174     }
00175   }
00176 
00177   virtual int do_handle_event(CEvent &event) {
00178     if (event.get_type() == CEvent::TYPE_KEY_DOWN) {
00179       if (event.get_key_ascii_value() == reverse_direction_key) {
00180         ticker->reverse_direction();
00181         return 1;
00182       }
00183     }
00184     return 0;
00185   }
00186 };
00187 
00190 void add_components(CGame &g, int num_boxes) {
00191   for (int i = 1; i < num_boxes+1; i++) {
00192     CPoint start(i*20, i*20);
00193     CPhysicsTickerModel *model = new CPhysicsTickerModel(start, 640, 480);
00194 
00255     CRect rect(0, 0, 100, 100);
00256     /* I actually ran into this color formula through a bug, but it
00257     makes for some very pretty color sequences so I decided to keep
00258     it. */
00259     CColor color(150-(i+3)*20, 150-(i+3)*30, 150-(i+3)*40);
00260     /* The '1' key on the keyboard. */
00261     const char KEY_ONE = '1';
00262     CPhysicsTickerObserver *view = new CPhysicsTickerObserver(model, g, g.get_font_small(),
00263    color, rect, KEY_ONE+(i-1));
00264 
00265     g.tickables.push_back(model);
00266   }
00267 
00268   CPoint displayer_pos(5,5);
00269 
00270   CFPSGraphicalObserver *fps_displayer = new CFPSGraphicalObserver(displayer_pos, g.get_font_large());
00271 
00272   g.timer.attach(fps_displayer);
00273   g.add(*fps_displayer);
00274 }
00275 }

Generated on Wed Aug 27 11:58:42 2003 for GFW by doxygen 1.3.3