/* nikki's program, section AA SS * CSE142, Spring 2000 * Homework 4 * Idyllic landscape animation */ #include "gp142.h" #include #include #include /* Logical constants */ #define TRUE 1 #define FALSE 0 /* Fill the shape being drawn */ #define FILL 0 /*********************************************************************************************** /* MENU BUTTON: */ /*********************************************************************************************** /* Top-left coordinates of button palette */ #define MENU_TOP (GP142_YMAX - 5) #define MENU_LEFT (-GP142_XMAX + 5) /* Height and width of buttons in pixels */ #define BUTTON_HEIGHT 25 #define BUTTON_WIDTH 100 /* Number of buttons */ #define NUM_BUTTONS 8 /* Color of letter on buttons , modified 05/08*/ #define SELECTED_BUTTON YELLOW #define UNSELECTED_BUTTON ICE_BLUE /* Possible locations of mouse clicks: */ /* Mouse click in some button */ #define BUTTON_CLICK 1 /* Mouse click elsewhere */ #define OTHER_CLICK 0 /************************************************************************************************ /* Coordinates of objects in the drawing */ /************************************************************************************************ /* y-coordinate of the top of the ground in the landscape */ #define HORIZON (- 2 * (GP142_YMAX / 10)) /* Other constants pertaining to objects in the scene */ #define MOON_RADIUS 20 #define MOON_FULLNESS 2.5 #define MAX_STAR_RADIUS 4 #define MAX_BUG_SIZE 10 #define HILL_RADIUS 190 #define HILL_RADIUS2 130 /* the moon's hat */ #define HAT_LEN 5 /*For the grass' 3-D effect */ #define CONSTANT 200 /* to add to z for 3-d effect */ #define CONSTANT2 500 /* to determine eye perspective */ /* max number of objects */ #define MAX_BUGS 15 /* number of bugs flying around */ #define MAX_FLOWERS 10 /* number of flowers */ #define MAX_BAMBOO 3 #define MAX_STARS 100 #define MAX_STAR_RAD 4 /***************************************************************************** FUNCTION PROTOTYPES *****************************************************************************/ /* Initialize random number generator */ void initialize_random_numbers(); /*draw monk */ /*move the monk */ void move_monk(int *monk_x, int *monk_y, int *nirvana); void draw_monk(int monk_x, int monk_y, int nirvana ); /*draw fighters*/ void draw_left_fighter (int lfighter_x, int lfighter_y, int left_punch, int left_kick); void draw_right_fighter (int rfighter_x, int rfighter_y, int right_punch, int right_kick); /*move fighers*/ void move_left_fighter(int *lfighter_x, int *lfighter_y, int *fighter_move); void move_right_fighter(int *rfighter_x, int *rfighter_y, int *fighter_move); /* make the flowers move when the wind blows */ void move_flower(int *flower_x, int *flower_y, int *blow); /* DRAW INTRO*/ void draw_intro(int *yinyang_x, int *yinyang_y); /*draw the yin yang */ void draw_yinyang (int *yinyang_x, int *yinyang_y); /* move the yin yang */ void move_yinyang(int *yinyang_x, int *yinyang_y, double phi, int *r_yinyang_x, int *r_yinyang_y); /* function to move the grass area around */ void Rotate_Grass (double *x, double *y, double *z, int *grass_x, int *grass_y, double theta); /* move the wind randomly */ void move_wind(int *wind_x, int *wind_y, int *blow); /* Draw a bamboo */ void draw_bamboo(int bamboo_x, int bamboo_y, int bamboo_color, int blow); /* Draw a single star */ void draw_star(int star_x, int star_y, int star_radius, int star_color); /* Draw a single dragonfly*/ void draw_bug(int bug_x, int bug_y, int bug_size, int bug_color); /*draw a single flower */ void draw_flower(int flower_x, int flower_y, int flower_radius, int flower_color, int petal_radius, int petal_color, int blow, int bloom); /* Draw a single boat */ void draw_boat(int boat_x, int boat_y, int boat_len, int boat_mast); /* Move the boat around*/ void move_boat(int *boat_x, int *boat_y, int *boat_len, int *boat_mast, int *left_boat); /* Initializes the moon parameters */ void init_moon(int *moon_x, int *moon_y); /* Initializes the stars' parameters */ void init_star(int star_x[], int star_y[], int star_color[], int star_radius[]); /* Change flower field colors */ void change_colors(int petal_color[MAX_FLOWERS], int flower_color[MAX_FLOWERS]); /* Draw the background, including the moon at coordinates moon_x and moon_y, the stars */ /* the tree, the flowers, the boat, and the dragoncflies */ void draw_landscape(int moon_x, int moon_y, int *star_x, int *star_y, int *star_radius, int *star_color, int *flower_x, int *flower_y, int flower_radius, int *flower_color, int petal_radius, int *petal_color, int bloom, int boat_x, int boat_y, int boat_len, int boat_mast, int *bug_x, int *bug_y, int *bug_size, int *bug_color, int hill_x, int hill_y, int temple_x, int temple_y, int *bamboo_x, int *bamboo_y, int bamboo_color, int blow, int wind_x, int wind_y, int lfighter_x, int lfighter_y, int rfighter_x, int rfighter_y, int left_punch, int right_punch, int right_kick, int left_kick, int *grass_x, int *grass_y, int monk_x, int monk_y, int nirvana); /* Make stars blink */ void blink_star(int star_radius[], int star_color[]); /* Move bug(s) */ void move_bug(int *bug_x, int *bug_y, int *bug_size, int *bug_color); /* Update moon coordinates moon_x, moon_y for one tick */ void move_moon(int *moon_x, int *moon_y); /*********************************************************************************************** / All about the buttons /**********************************************************************************************/ /* Draw buttons */ void draw_buttons(int buttonNum); /* Return kind of mouse click (in a button, etc.) */ int classify_mouse_click(int mouse_x, int mouse_y); /* Process button click at location x, y. React appropriately, depending on the requested event */ int handle_button(int x, int y, int petal_color[MAX_FLOWERS], int flower_color[MAX_FLOWERS], int *blow, int *bloom, int *right_punch, int *left_punch, int *right_kick, int *left_kick, int *nirvana); /* Display error message if internal error is detected. */ void something_is_wrong (void); /***************************************************************************** MAIN *****************************************************************************/ /* * Main program contents: * - Major program variables that persist throughout execution * - Event loop that processes GP142 events until the user quits, * and updates variables and redraws the window as needed. */ int main(void){ /***************************************************************************** VARIABLE DECLARATION *****************************************************************************/ /* intro info*/ int intro; /* to determine whether the user is in intro mode or game mode*/ /* information about the yin yang and how to rotate it */ int yinyang_x[14] = {-50, 0, 50, 100, 100, 50, 0, -50, -100, -100, -50, 50, 0, 0}; int yinyang_y[14] = {100, 100, 100, 50, -50, -100, -100, -100, -50, 50, 50, -50, 50,-50}; double phi; int r_yinyang_x[14]; int r_yinyang_y[14]; /* info about left and right fighters, their coordinates and actions*/ int lfighter_x; int lfighter_y; int rfighter_x; int rfighter_y; /* kicks and punches from left and right fighter */ int left_kick; int right_kick; int left_punch; int right_punch; /* info about the monk */ int monk_x; int monk_y; int nirvana; /* information about the grass area and how to rotate it */ double x[4] = {-50, 50, 50, -50}; double y[4] = {-60, -60, -60, -60}; double z[4] = {50, 50, -50, -50}; double theta; int grass_x[4]; int grass_y[4]; int buttonNum; /* what button is currently selected */ int i; /* keep count */ /* draw wind delaring and init */ int wind_x; int wind_y; int blow; int bloom; /* information about the bamboo */ int bamboo_x[MAX_BAMBOO] = {-GP142_XMAX+50, -GP142_XMAX + 70, -GP142_XMAX + 85}; int bamboo_y[MAX_BAMBOO] = {HORIZON - 150, HORIZON -155, HORIZON -143}; int bamboo_color; /* information about the temple */ int temple_x; int temple_y; /*information about the hill */ int hill_x; int hill_y; /* flags to determine true/false*/ int left_boat; int fighter_move; /* Event handling and buttons: */ int quit; /* = "user has selected quit" */ int event; /* the most recent GP142 event */ char key_pressed; /* last keyboard character from key event */ int mouse_x, mouse_y; /* x and y coordinates of latest mouse event*/ int click_kind; /* kind of mouse click (button, etc.) */ /* Information about the moon and stars: */ int moon_x, moon_y; /* screen x and y coordinates */ int star_x [MAX_STARS]; int star_y[MAX_STARS]; /* screen x and y coordinates */ int star_radius[MAX_STAR_RAD]; int star_color[MAX_STARS]; /* radius and color of a star */ /* screen x and y coordinates */ /* Information about the flowers */ int flower_x[MAX_FLOWERS] = {-GP142_XMAX + 155, -GP142_XMAX + 165, -GP142_XMAX + 175, -GP142_XMAX + 185, -GP142_XMAX + 195,-GP142_XMAX + 205, -GP142_XMAX + 215, -GP142_XMAX + 225,-GP142_XMAX + 235, -GP142_XMAX + 245}; int flower_y[MAX_FLOWERS] = {-GP142_YMAX + 350,-GP142_YMAX + 325,-GP142_YMAX + 320, -GP142_YMAX + 325,-GP142_YMAX + 330, -GP142_YMAX + 340, -GP142_YMAX + 320, -GP142_YMAX + 310,-GP142_YMAX + 315, -GP142_YMAX + 324}; int flower_radius, petal_radius; /* flower and petal radii */ int flower_color[MAX_FLOWERS], petal_color[MAX_FLOWERS]; /* flower and petal colors */ /* Information about the bug(s) */ int bug_x[MAX_BUGS], bug_y[MAX_BUGS], bug_size[MAX_BUGS], /* screen x and y coordinates and size of the bug */ bug_color[MAX_BUGS]; /* Information about the boat */ int boat_x, boat_y; /* screen x and y coordinates */ int boat_len, boat_mast; /* length of the boat and height of the mast */ /************************************************************************************** * INITIALIZE **************************************************************************************/ /*initialize the screen and whether or not it's the intro or the main screen*/ intro = TRUE; /* initializing fighters' position */ rfighter_x = GP142_XMAX/5; rfighter_y = HORIZON-50; lfighter_x = -GP142_YMAX/5; lfighter_y = HORIZON-75; /*initializing the monk */ monk_x = -GP142_XMAX +250; monk_y = -GP142_YMAX +200; /* initializing flags */ nirvana = FALSE; blow = FALSE; right_kick = FALSE; left_kick = FALSE; right_punch = FALSE; left_punch = FALSE; bloom = FALSE; /* Initialize random number generator */ initialize_random_numbers(); /* Initialize graphics package, clear the window, */ /* turn logging off, and start animation. */ GP142_open(); GP142_logging(LOG_OFF); GP142_clear(); GP142_animate(ANI_RUN); /*initialize buttonNum so that every time a click occur it would highlight */ buttonNum = -1; theta = 0; /*information about the boat moving */ left_boat = TRUE; blow = FALSE; fighter_move = TRUE; bloom = FALSE; /* set initial location of the moon */ init_moon(&moon_x, &moon_y); /*set initial location of the stars */ init_star(star_x, star_y, star_color, star_radius); /* set initial location of the stars */ /* set initial flower parameters and conditions */ flower_radius = 6; petal_radius = 8; for (i=0; i< MAX_FLOWERS; i++){ petal_color[i] = 1+ rand()% MAX_COLORS; flower_color[i] = 1 + rand()% MAX_COLORS; } /* set initial condition for the wind */ wind_x = GP142_XMAX+150; wind_y = GP142_YMAX - 75; /* set initial conditions for the buttefly */ for (i=0; i< MAX_BUGS; i++){ bug_x[i] = rand()% GP142_XMAX; bug_y[i] = rand()%GP142_YMAX - 100; bug_size[i] = MAX_BUG_SIZE/2 + rand()% 5; bug_color[i] = 1+ rand()% MAX_COLORS; } /*set initial parameters for the bamboo */ bamboo_color = YELLOW; /* set initial conditions for the boat */ boat_x = GP142_XMAX; boat_y = HORIZON - 25; boat_len = 25; boat_mast = 40; /*set initial conditions for the temple */ temple_x = -GP142_XMAX + 250; temple_y = HORIZON + 200; /* 05/09 set initial conditions for the hill */ hill_x = -GP142_XMAX + 200; hill_y = HORIZON - 50; /* Draw initial landscape, buttons, and traffic */ draw_landscape(moon_x, moon_y, star_x, star_y, star_radius, star_color, flower_x, flower_y, flower_radius, flower_color, petal_radius, petal_color, bloom, boat_x, boat_y, boat_len, boat_mast, bug_x, bug_y, bug_size, bug_color, hill_x, hill_y, temple_x, temple_y, bamboo_x, bamboo_y, bamboo_color, blow, wind_x, wind_y, lfighter_x, lfighter_y, rfighter_x, rfighter_y, left_punch, right_punch, right_kick, left_kick, grass_x, grass_y, monk_x, monk_y, nirvana); draw_buttons(buttonNum); /* * Main event loop: * ---- ----- ----- * Wait for the next user action, decode it, and call an * appropriate function to handle it. Repeat until the * user selects quit. */ quit = FALSE; while (!quit) { /* get next event */ event = GP142_await_event(&mouse_x, &mouse_y, &key_pressed); /* perform appropriate action depending on kind of event */ switch (event) { case GP142_QUIT: /* Quit selected */ quit = TRUE; break; case GP142_KBD: intro = FALSE; break; case GP142_MOUSE: /* Mouse click. Determine if in a menu button */ intro = FALSE; click_kind = classify_mouse_click(mouse_x, mouse_y); if (click_kind == BUTTON_CLICK) { buttonNum = handle_button(mouse_x, mouse_y, petal_color, flower_color, &blow, &bloom, &right_punch, &left_punch, &right_kick, &left_kick, &nirvana); } else { /* insert code here (and additional else-if statements) */ /* to react to mouse clicks elsewhere on the screen (if needed) */ } break; case GP142_PERIODIC: /* Timer interval event. Update everything that needs the change */ /* as time advances. Animation should go here! */ if (intro == TRUE){ move_yinyang(yinyang_x, yinyang_y, phi, r_yinyang_x, r_yinyang_y); phi +=0.4; } else { move_moon(&moon_x, &moon_y); blink_star(star_radius, star_color); move_bug(bug_x, bug_y, bug_size, bug_color); move_boat(&boat_x, &boat_y, &boat_len, &boat_mast, &left_boat); Rotate_Grass(x, y, z, grass_x, grass_y, theta); move_wind(&wind_x, &wind_y, &blow); move_monk(&monk_x, &monk_y, &nirvana); theta-=0.1; move_left_fighter(&lfighter_x, &lfighter_y, &fighter_move); move_right_fighter(&rfighter_x, &rfighter_y, &fighter_move); } break; default: /* unknown event */ break; } /*end switch */ if (intro == TRUE){ draw_intro(r_yinyang_x, r_yinyang_y); } else { /* Redraw everything */ draw_landscape(moon_x, moon_y, star_x, star_y, star_radius, star_color, flower_x, flower_y, flower_radius, flower_color, petal_radius, petal_color, bloom, boat_x, boat_y, boat_len, boat_mast, bug_x, bug_y, bug_size, bug_color, hill_x, hill_y, temple_x, temple_y, bamboo_x, bamboo_y, bamboo_color,blow, wind_x, wind_y, lfighter_x, lfighter_y, rfighter_x, rfighter_y, left_punch, right_punch, right_kick, left_kick, grass_x, grass_y, monk_x, monk_y, nirvana); draw_buttons(buttonNum); } } /* end event loop */ /* Termination: shut down graphics window and exit */ GP142_close(); return 0; } /* end main */ /***************************************************************************** INITIALIZATION PROCEDURES *****************************************************************************/ /* Initializes the moon parameters */ void init_moon(int *moon_x, int *moon_y) { *moon_x = GP142_XMAX - 50; *moon_y = GP142_YMAX - 35; } /* Initializes the stars' paramters */ void init_star(int star_x[], int star_y[], int star_color[], int star_radius[]){ int i; for(i=0; i MENU_LEFT) && (mouse_x < MENU_LEFT + BUTTON_WIDTH) && (mouse_y < MENU_TOP) && (mouse_y > (MENU_TOP - NUM_BUTTONS * BUTTON_HEIGHT))) return BUTTON_CLICK; else return OTHER_CLICK; } /* Process button click at location x, y. React appropriately: */ /* i.e. change colors, tree tops, etc. if requested. */ int handle_button(int x, int y, int petal_color[], int flower_color[], int *blow, int *bloom, int *right_punch, int *left_punch, int *right_kick, int *left_kick, int *nirvana) { int buttonNum; /* number of selected button */ /* to keep track of count*/ /* Complain if x coordinate couldn't be inside a button */ buttonNum = 0; if (x < MENU_LEFT || x > MENU_LEFT + BUTTON_WIDTH) { something_is_wrong(); return buttonNum; } /* Convert y coordinate to button number from top. */ buttonNum = (MENU_TOP - y)/BUTTON_HEIGHT; switch(buttonNum) { case 0: /* change flower field colors */ change_colors(petal_color, flower_color); break; case 1: *right_punch = TRUE; break; case 2: *left_punch = TRUE; break; case 3: *right_kick = TRUE; break; case 4: *left_kick = TRUE; break; case 5: *blow= TRUE; break; case 6: *bloom = TRUE; break; case 7: *nirvana = TRUE; break; default: /*Bad news if this is ever reached! */ GP142_animate(ANI_HALT); something_is_wrong(); break; } return(buttonNum); } /***************************************************************************** ANIMATED OBJECTS ********************************** *******************************************/ /* rotate ying yang */ void move_yinyang(int *yinyang_x, int *yinyang_y, double phi, int *r_yinyang_x, int *r_yinyang_y){ int i; for (i=0; i<14; i++){ r_yinyang_x[i] = (int)(((double)yinyang_x[i]* cos(phi)) - ((double)yinyang_y[i] * sin(phi))); r_yinyang_y[i] = (int)(((double)yinyang_x[i] * sin(phi)) + ((double)yinyang_y[i] * cos(phi))); } } /* Make the stars blink */ void blink_star(int star_radius[], int star_color[]) { int i; for(i=0; i