// CSE 143 // Homework 5 // http://www.cs.washington.edu/education/courses/143/00su/homework/ // 30 Jul 2000, Ken Yasuhara // class FireworksDisplay #ifndef _FIREWORKSDISPLAY_H_ #define _FIREWORKSDISPLAY_H_ #include "movingobject.h" #include "fireworksscript.h" // max. # of fireworks that can be active on screen at once const int MAX_FIREWORK_COUNT = 32; // max. # of background scene elements const int MAX_BACKGROUND_OBJECT_COUNT = 16; class FireworksDisplay { public: // A FireworksDisplay instance is associated with a script file that // describes the sequence and parameters of the fireworks to be // displayed. // slowdownFactor is periodicEventsPerFrame's init. value FireworksDisplay(const char scriptFilename[], const int slowdownFactor = 1); // deallocates any MovingObject (subclass) instances whose pointers // remain in arrays backgroundObjects and fireworks ~FireworksDisplay(); // // methods // // "launches" given firework by adding to fireworks array; must // pass in a dynamically allocated MovingObject or subclass // instance; deallocateInactiveFireworks will deallocate for you // when firework is done bool addFirework(MovingObject *newFirework); // add background scene element; These objects are drawn to appear // behind fireworks. Background objects are allowed to move. bool addBackgroundObject(MovingObject *newBackgroundObject); // starts fireworks show animation; infinite loop that increments // time as it updates the screen frame by frame void run(); protected: // launches fireworks scheduled for current time by adding them // to array fireworks void addScheduledFireworks(); // updates whole screen by painting all fireworks, background void paintFrame(); // paints objects in backgroundObjects array void paintBackground(); // same as above for fireworks array void paintFireworks(); // updates positions of background and active firework objects; // does not do any painting void updateObjects(); // removes and deletes finished fireworks, checking each using // method MovingObject::isDone void deallocateInactiveFireworks(); // delay until time to draw next frame void waitUntilNextFrame(); // // member variables // GP142Display display; int periodicEventsPerFrame; // incremented each time the frame is painted int time; // storage for ptrs. to dynamically allocated instances of // MovingObject or subclasses, one for background objects, the // other for fireworks. // count vars. used to keep track of how many elements are actually // being used in these arrays; initially all NULL; // invariant: count var. holds number of non-NULL elements in // corresponding ptr. array // invariant: For each background object, there is exactly one // pointer to it in the backgroundObjects array, and the // remainder of this array's elements are NULL. // invariant: Likewise, each non-NULL element of array fireworks // points to a unique active firework. MovingObject *backgroundObjects[MAX_BACKGROUND_OBJECT_COUNT]; int backgroundObjectCount; MovingObject *fireworks[MAX_FIREWORK_COUNT]; int fireworkCount; // get dyn. allocated MovingObjects through this instance FireworksScript script; // next scheduled firework and launch time; // invariant: nextFirework == NULL if no more fireworks MovingObject *nextFirework; int nextFireworkLaunchTime; }; #endif // _FIREWORKSDISPLAY_H_