/* * CSCI 1300 * A simple graphics demo. * * This program plots a spaceship (like from the old * asteroids game). * * The user can drive the spaceship using 'a' and 'f' to * rotate the ship left and right, and 's' to fire the * thrusters. * * 'q' exits the program. */ #ifdef __TINY__ #error BGIDEMO will not run in the tiny model. #endif #include #include #include #include #include #include #include #define ESC 0x1b /* Define the escape key */ #define TRUE 1 /* Define some handy constants */ #define FALSE 0 /* Define some handy constants */ /* * Some global variables that were in the Borland graphics * demo so I left them here. */ int GraphDriver; /* The Graphics device driver */ int GraphMode; /* The Graphics mode value */ int MaxX, MaxY; /* The maximum resolution of the screen */ int MaxColors; /* The maximum # of colors available */ int ErrorCode; /* Reports any graphics errors */ struct palettetype palette; /* Used to read palette info */ /* Useful constants */ const int MaxDelta = 10; const int MinDelta = 2; const double pi = 3.14159; const int rspeed = 5; const int tspeed = 1; /* Function prototypes */ void Initialize(void); void PlayGame(void); void Pause(void); void MainWindow(); int gprintf( int *xloc, int *yloc, const char *fmt, ... ); /* * The Ship class. * Objects of this type (usually just one) are driven around * the screen by the user. */ class Ship { private: double x; double y; double deltax; double deltay; int aim; int color; int size; void right(void); void left(void); void draw(int color); void move(void); public: Ship(void); void update(char c); void thrust(void); }; /* * main() - Initializes the graphics, calls the game function, * then cleans up afterwards. */ int main() { Initialize(); /* Set system into Graphics mode */ PlayGame(); closegraph(); /* Return the system to text mode */ return(0); } /* */ /* INITIALIZE: Initializes the graphics system and reports */ /* any errors which occured. */ /* */ void Initialize(void) { GraphDriver = DETECT; /* Request auto-detection */ initgraph( &GraphDriver, &GraphMode, "../../BC5/BGI" ); ErrorCode = graphresult(); /* Read result of initialization*/ if( ErrorCode != grOk ){ /* Error occured during init */ printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) ); exit( 1 ); } getpalette( &palette ); /* Read the palette from board */ MaxColors = getmaxcolor() + 1; /* Read maximum number of colors*/ MaxX = getmaxx(); MaxY = getmaxy(); /* Read size of screen */ cleardevice(); /* Clear graphics screen */ setcolor( MaxColors - 1 ); /* Set current color to white */ setviewport( 0, 0, MaxX, MaxY, 1 ); /* Open port to full screen */ randomize(); } /* * This is the main part of the game. * It creates the objects used in the game, sets up a * background ("stars"), then starts the event loop * which reades characters from the user and updates * the objects. */ void PlayGame(void) { char c; int PauseTime, i; Ship ship; /* Plot some "stars" */ for ( i=0 ; i<1000; ++i ) putpixel(random(MaxX), random(MaxY), random( MaxColors-1 )+1); PauseTime = 40; while ( 1 ) { c = 0; if(kbhit()) { c = getch(); if(c == 'q') break; } /* Update and redraw the objects on the screen */ ship.update(c); delay(PauseTime); } cleardevice(); } /********************************************************************/ /******************** Ship Member Functions *********************/ /********************************************************************/ /* * Ship::Ship() - constructor for the Ship class. * It initializes the private member variables. */ Ship::Ship(void) { x = MaxX/2; y = MaxY/2; deltax = 0; deltay = 0; aim = 0; color = WHITE; size = 10; draw(color); } /* * Ship::update() accelerates, erases, steers, and then redraws * the ship. It should be called once each time through the * main event loop of the program. */ void Ship::update(char c) { draw(BLACK); switch(c) { case 's': thrust(); break; case 'a': left(); break; case 'f': right(); break; } move(); draw(color); } /* * Ship::draw() draws the ship in whatever color is specified. * Calling draw() with the background color set to BLACK * erases the ship. */ void Ship::draw(int color) { double aima = pi * ((aim + 225)%360)/180; double aimb = pi * aim / 180; double aimc = pi * ((aim + 135)%360)/180; int polypoints[10] = {x, y, x+6*cos(aima), y+6*sin(aima), x+10*cos(aimb), y+10*sin(aimb), x+6*cos(aimc), y+6*sin(aimc), x, y}; setcolor(color); // line(x, y, x + 10*cos(aimr), y + 10*sin(aimr)); drawpoly(5, polypoints); } /* * Ship::move() moves the ship in whatever direction * it is currently heading. The ship should be erased, * moved, then redrawn after it has been moved. */ void Ship::move(void) { x += deltax; y += deltay; if(x >= MaxX) x -= MaxX; if(x < 0) x += MaxX; if(y >= MaxY) y -= MaxY; if(y < 0) y += MaxY; } /* * Ship::right() rotates the ship clockwise 1 * increment. */ void Ship::right(void) { aim += rspeed; if(aim >= 360) aim -= 360; } /* * Ship::left() rotates the ship counterclockwise * 1 increment. */ void Ship::left(void) { aim -= rspeed; if(aim < 0) aim += 360; } /* * Ship::thrust() accelerates the ship in whatever * direction it is currently aiming. */ void Ship::thrust(void) { double aimr = pi*aim/180; deltax += tspeed*cos(aimr); deltay += tspeed*sin(aimr); } /************************************************************************/ /* * GPRINTF: Used like PRINTF except the output is sent to the * screen in graphics mode at the specified co-ordinate. */ int gprintf( int xloc, int yloc, const char *fmt, ... ) { va_list argptr; /* Argument list pointer */ char str[140]; /* Buffer to build sting into */ int cnt; /* Result of SPRINTF for return */ va_start( argptr, fmt ); /* Initialize va_ functions */ cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */ outtextxy( xloc, yloc, str ); /* Send string in graphics mode */ va_end( argptr ); /* Close va_ functions */ return( cnt ); /* Return the conversion count */ }