www.pudn.com > screensaver.rar > screensaver.c


/* 
 * Project: homework_3 
 * Created by CS106 Assignment Wizard 0.8 
 * 
 * Name: [TODO: enter name here] 
 * Section: [TODO: enter section leader here] 
 * [TODO: Describe assignment] 
 */ 
#include  
#include "genlib.h" 
#include "simpio.h" 
#include "extgraph.h" 
#include "random.h" 
#include "sound.h" 
#include "math.h" 
#include "DoShapes.h" 
#include "DoBouncingBall.h" 
#include "DoFireworks.h" 
#include "DoImages.h" 
#include "commonlib.h" 
#include "GUILib.h" 
 
#define MENU_STARTHEIGHT		5.0 
#define MENU_SPACING			-0.8 
 
void Initialize (void); 
int GetChoice (void); 
bool ProcessChoice (int intChoice); 
void DrawMenus (void); 
bool DoubleEqual(double n1, double n2); 
 
void main (void) 
{ 
	int intChoice; 
 
	Initialize(); 
 
	intChoice = GetChoice(); 
	while ( ProcessChoice(intChoice) ) 
	{ 
		WaitForMouseUp(); 
		intChoice = GetChoice(); 
	} 
 
	EndGUI(); 
 
	ExitGraphics(); 
} 
 
void Initialize (void) 
{ 
	InitGUI(); 
	Randomize(); 
} 
 
void DrawMenus (void) 
{ 
	string strMenus[6]; 
	string strMenusColor[6]; 
	double dblScreenWidth; 
	double dblCurWidth; 
	int i; 
	int intOldSize; 
 
	strMenus[0] = "SCREEN SAVER MENU"; 
	strMenus[1] = "SHAPE"; 
	strMenus[2] = "BOUNCING BALL"; 
	strMenus[3] = "FIREWORKS"; 
	strMenus[4] = "IMAGES"; 
	strMenus[5] = "QUIT"; 
 
	strMenusColor[0] = "Black"; 
	strMenusColor[1] = "Yellow"; 
	strMenusColor[2] = "Yellow"; 
	strMenusColor[3] = "Yellow"; 
	strMenusColor[4] = "Yellow"; 
	strMenusColor[5] = "Yellow"; 
	 
	dblScreenWidth = GetWindowWidth(); 
	intOldSize = GetPointSize(); 
	SetPointSize(36); 
	 
	for(i = 0; i <= 5; i++) 
	{ 
		dblCurWidth = TextStringWidth(strMenus[i]); 
 
		MovePen( (dblScreenWidth - dblCurWidth) / 2, MENU_STARTHEIGHT + MENU_SPACING * i); 
 
		SetPenColor(strMenusColor[i]); 
 
		DrawTextString(strMenus[i]); 
	} 
	SetPointSize(intOldSize); 
} 
 
int GetChoice (void) 
{ 
	double dblCurY; 
	double dblL, dblH; 
	double dblOldL = 0; 
	bool blnFlag, blnExit; 
	int i; 
 
	InitGraphics(); 
	DrawMenus(); 
 
	blnExit = FALSE; 
	while (!blnExit) 
	{ 
		dblCurY = GetMouseY(); 
 
		blnFlag = TRUE; 
 
		for(i = 0; i < 5; i++) 
		{ 
			dblL = MENU_STARTHEIGHT + i * MENU_SPACING - 0.2; 
			dblH = dblL + MENU_SPACING; 
			if ( InRange(dblCurY, dblL, dblH) ) 
			{ 
				if ( (dblOldL != 0) && (dblOldL != dblL)) 
				{ 
					SetPenColor ("White"); 
					DrawBoxEx (1, dblOldL, GetWindowWidth() - 2, MENU_SPACING); 
				} 
 
				SetPenColor ("Red"); 
				DrawBoxEx (1, dblL, GetWindowWidth() - 2, MENU_SPACING); 
 
				dblOldL = dblL; 
 
				if (MouseButtonIsDown()) 
				{ 
					WaitForMouseUp(); 
					blnExit = TRUE; 
					return ((i + 1) % 5); 
					break; 
				} 
 
				blnFlag = FALSE; 
			} 
		} 
 
		if (blnFlag) 
		{ 
			SetPenColor ("White"); 
			DrawBoxEx (1, dblOldL, GetWindowWidth() - 2, MENU_SPACING); 
		} 
	} 
	return 0; 
} 
 
bool ProcessChoice (int intChoice) 
{ 
	switch ( intChoice ) 
	{ 
		case 0: 
			return FALSE; 
		case 1: 
			DoShapes(); 
			break; 
		case 2: 
			DoBouncingBall(); 
			break; 
		case 3: 
			DoFireworks(); 
			break; 
		case 4: 
			DoImages(); 
			break; 
	} 
	 
	return TRUE; 
} 
 
bool DoubleEqual(double n1, double n2) 
{ 
	return ( ((n1 - n2) < 0.001) || ((n2 - n1) < 0.001) ); 
}