// ferrysimview.cpp // ---------------------------------------------------------------------- // // CSE 143 // Homework 4 // http://www.cs.washington.edu/education/courses/143/00su/homework/ // 21 Jul 2000, Man-Hing Wong, Martin Dickey, Ken Yasuhara #include #include #include "ferrysimview.h" #include "GP142.h" FerrySimView::FerrySimView(FerrySim *newSimPtr, double newTimeUnitsPerGP142Event) { assert(newSimPtr != NULL); simPtr = newSimPtr; // sets whether or not to animate; Run is an enum // animation can also be turned on and off by the end-user // via choices on the graphics window RUN menu display.setAnimation(Run); timeUnitsPerGP142Event = newTimeUnitsPerGP142Event; } void FerrySimView::paintSnapshot() { display.clear(White); paintBackground(); // paint the ferry if it is at the dock if(simPtr->isFerryDocked()) paintFerry(155, 10); // paint car queue at ticket office paintCarQueue(simPtr->getTicketingQueue(), -35, 60); // paint car queue at board station paintCarQueue(simPtr->getBoardingQueue(), 110, -25); // print statistics such as time, carBoarded and totalRevenue paintStatistics(); // force update of graphics display display.flush(); } void FerrySimView::paintCarQueue(const CarQueue &carQ, int x, int y) { int Qlength = carQ.getSize(); // size of car queue int currentX = x; for(int i = 0; i < Qlength; i++) { paintCar(carQ.getItemAt(i), currentX, y); currentX -= 35; // next car location // out of bound, draw 2nd row. if( currentX < -270) { y -= 25; // 2nd row of car currentX = x - 5; } } } void FerrySimView::paintCar(const Car *carPtr, int x, int y) { int passenger = carPtr->getPassengerCount(); // paint car display.drawOval(x - 30, y - 20, x, y, Green, 1); // always draw driver, at least display.drawCircle(x - 10, y - 5, 2, Red); // paint passengers if(passenger > 0) display.drawCircle(x - 20, y - 5, 2, Red); if(passenger > 1) display.drawCircle(x - 10, y - 15, 2, Red); if(passenger > 2) display.drawCircle(x - 20, y - 15, 2, Red); } void FerrySimView::paintFerry(int x, int y) { display.drawRectangle(x, y, x + 125, y - 60, Blue, 1); } void FerrySimView::paintBackground() { // heading display.write(-100, 200, "Ferry Simulator", Magenta, 30); // ticket station display.write(-80, 70, "Ticketing", Black, 20); display.drawRectangle(-100, 95, 0, 70, Black, 1); // ferry station display.write(32, -15, "Boarding", Black, 20); display.drawRectangle(25, 10, 147, -15, Black, 1); // dock display.drawLine(150, 150, 150, -150, Black, 2); } void FerrySimView::paintStatistics() { // print time display.write(-280, -200, "Time of event: ", Black, 15); display.write(-165, -200, simPtr->getCurrentTime(), Black, 15); // print the total ticket revenue display.write(-280, -240, "Ticket Revenue: ", Black, 15); display.write(-165, -240, simPtr->getTotalTicketRevenue(), Black, 15); } // wait a period of time proportional to the amount of // simulation time specified; // We do this by waiting until an appropriate number of GP142 // Periodic events have occurred. We don't actually do anthing // with the event except note that it happened. // Quit events cause an immediate exit from the program; keypress // events are ignored. void FerrySimView::pause(int simTimeUnits) { int GP142eventsNeeded = simTimeUnits / timeUnitsPerGP142Event; int eventCount = 0; while (eventCount < GP142eventsNeeded) { int mouseX, mouseY; char keyPress; GP142Event nextGP142Event = display.getNextEvent(mouseX, mouseY, keyPress); if (nextGP142Event == /*GP142Event::*/Periodic) eventCount++; else if (nextGP142Event == /**GP142Event::*/Quit) exit(0); } }