Пример #1
0
// ******** MAIN START **********
int main(int argc, char* argv[]) {

	// **** Create SDL Window - START ****
	// Start SDL2
	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_Window *window = NULL;

	SDL_Renderer *renderer = NULL;

	// Create an application window with the following settings:
	window = SDL_CreateWindow("CELL BLOCK Z", SDL_WINDOWPOS_CENTERED,
			SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_SHOWN);

	// Create an SDL RenderTarget
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	// **** Create SDL Window - END ****

	// **** Variable Creation

	// Event Handler
	SDL_Event e;

	// **** Turn on Game Controller Events ****
	SDL_GameControllerEventState(SDL_ENABLE);

	// **** Set up a Game Controller variable for player1 ****
	SDL_GameController* gGameControllerO = NULL;

	// **** Open Game Controller for player1 ****
	gGameControllerO = SDL_GameControllerOpen(0);

	// **** Open Audio Channel ****
	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);

	// **** Make Fires
	Fire fire1 = Fire(renderer, images_dir, 1, -40, 200);
	Fire fire2 = Fire(renderer, images_dir, 1, 620, 200);
	Fire fireBig = Fire(renderer, images_dir, 0, 700, 180);

	// **** Create Players
	Player player = Player(renderer, images_dir.c_str(), audio_dir.c_str(), 950.0f, 384.0f);

	// **** Create Turrets
	Turret turret1(renderer, images_dir, audio_dir, 75.0f, 50.0f);
	Turret turret2(renderer, images_dir, audio_dir, 725.0f, 50.0f);
	Turret turret3(renderer, images_dir, audio_dir, 725.0f, 580.0f);
	Turret turret4(renderer, images_dir, audio_dir, 75.0f, 580.0f);

	Explode explode1(renderer, images_dir, 500.f, 500.0f);
	Explode explode2(renderer, images_dir, -1000.f, -1000.0f);
	Explode explode3(renderer, images_dir, -1000.f, -1000.0f);
	Explode explode4(renderer, images_dir, -1000.f, -1000.0f);

	// **** Main Menu Textures
	Menu MainMenu = Menu(renderer, images_dir.c_str(), "mainMenu.png", 0.0f, 0.0f);
	Menu Barrels = Menu(renderer, images_dir.c_str(), "barrels.png", 0.0f, 0.0f);
	Menu Level1 = Menu(renderer, images_dir.c_str(), "level1.png", 0.0f, 0.0f);
	Menu Level2 = Menu(renderer, images_dir.c_str(), "level2.png", 0.0f, 0.0f);
	Menu Instructions = Menu(renderer, images_dir.c_str(), "instructions.png", 0.0f, 0.0f);
	Menu Backstory = Menu(renderer, images_dir.c_str(), "backstory.png", 0.0f, 0.0f);
	Menu BarrelBackInst = Menu(renderer, images_dir.c_str(), "barrelBackInst.png", 0.0f, 0.0f);
	Menu Win1 = Menu(renderer, images_dir.c_str(), "win1.png", 0.0f, 0.0f);
	Menu Win2 = Menu(renderer, images_dir.c_str(), "win2.png", 0.0f, 0.0f);
	Menu Lose = Menu(renderer, images_dir.c_str(), "lose.png", 0.0f, 0.0f);

	// **** Set Up Game State Enumerators
	enum GameState {
		MENU, INSTRUCTIONS, BACKSTORY, LEVEL1, LEVEL2, WIN, LOSE
	};

	GameState gameState = MENU;

	// bools for menu switching
	bool menu = false; bool instructions = false; bool backstory = false; bool level1 = false; bool level2 = false; bool win = false; bool lose = false; bool quit = false;

	bool level1won = false;


	//**** Create Cursor
	SDL_Texture *cursor;

	//create the SDL surface to hold texture file
	cursor = IMG_LoadTexture(renderer, (images_dir + "cursor.png").c_str());

	// create the SDL_Rectangle for the texture's position and size - x,y,w,h
	//SDL_Rect cursorPos, activePos;
	cursorPos.x = 0;
	cursorPos.y = 0;

	int w, h;
	SDL_QueryTexture(cursor, NULL, NULL, &w, &h);
	cursorPos.w = w;
	cursorPos.h = h;

	// set the X, Y, W, and H for the Rectangle - cursor collision
	activePos.x = 5;
	activePos.y = 5;
	activePos.w = 5;
	activePos.h = 5;

	// **** Create Buttons and selection
	Button selected(renderer, images_dir.c_str(), "selected.png", 750.0f, 500.0f);
	Button startN(renderer, images_dir.c_str(), "startN.png", 386.0f, 419.0f);
	Button instructionsN(renderer, images_dir.c_str(), "instructionsN.png", 386.0f, 478.0f);
	Button backstoryN(renderer, images_dir.c_str(), "backstoryN.png", 386.0f, 537.0f);
	Button quitN(renderer, images_dir.c_str(), "quitN.png", 386.0f, 598.0f);
	Button mainN(renderer, images_dir.c_str(), "mainN.png", 750.0f, 15.0f);
	Button playAgainN(renderer, images_dir.c_str(), "playN.png", 750.0f, 55.0f);
	Button continueN(renderer, images_dir.c_str(), "continueN.png", 750.0f, 55.0f);


	// **** Create ITEMS
	Item key1(renderer, images_dir.c_str(), audio_dir.c_str(), 0, -100, -100);
	Item key2(renderer, images_dir.c_str(), audio_dir.c_str(), 0, -100, -100);
	Item lvl1Gate(renderer, images_dir.c_str(), audio_dir.c_str(), 4, 58, 194);

	Item generator1(renderer, images_dir.c_str(), audio_dir.c_str(), 1, -100, -100);
	Item generator2(renderer, images_dir.c_str(), audio_dir.c_str(), 1, -100, -100);
	Item generator3(renderer, images_dir.c_str(), audio_dir.c_str(), 1, -100, -100);
	Item generator4(renderer, images_dir.c_str(), audio_dir.c_str(), 1, -100, -100);
	Item lvl2Gate(renderer, images_dir.c_str(), audio_dir.c_str(), 5, 0, 302);

	Item health(renderer, images_dir.c_str(), audio_dir.c_str(), 2, -100, -100);
	Item ammo(renderer, images_dir.c_str(), audio_dir.c_str(), 3, -100, -100);

	//**** Create LVL2 Bar
	// Textures for health bar
	SDL_Texture *eBar = IMG_LoadTexture(renderer, (images_dir + "genBack.png").c_str());;

	// Player's SDL_Rect for the X,Y,W, and H for Texture
	SDL_Rect eBarR;

	int wbar, hbar;
	SDL_QueryTexture(eBar, NULL, NULL, &wbar, &hbar);
	eBarR.w = 0;
	eBarR.h = hbar;
	eBarR.x = 402.0f;
	eBarR.y = 8.0f;

	float playerEnergy = 0, maxEnergy = 100;

	Mix_Music *menuM = Mix_LoadMUS((audio_dir + "menuMusic.wav").c_str());
	Mix_Music *lvlM = Mix_LoadMUS((audio_dir + "levelMusic.wav").c_str());

	Mix_FadeInMusic(menuM, 3, 1000);


	// **** MAIN LOOP GAME START ****

	while (!quit) {

		switch (gameState) {
		// *********************************************************************************************************
		// *********************************************************************************************************
		// *********************************************************************************************************
		case MENU:


			menu = true;

			backstoryN.posRect.x = 386.0f;
			backstoryN.posRect.y = 537.0f;

			instructionsN.posRect.x = 386.0f;
			instructionsN.posRect.y = 478.0f;

			startN.posRect.x = 386.0f;
			startN.posRect.y = 419.0f;

			cout << "The Game State is Menu..." << endl;

			while (menu) {

				// Create deltaTime
				thisTime = SDL_GetTicks();
				deltaTime = (float)(thisTime - lastTime) / 1000;
				lastTime = thisTime;

				// Handle Events on Queue - Key press and such - START ****
				while (SDL_PollEvent(&e) != 0) {

					// User Requests quit
					if (e.type == SDL_QUIT) {
						quit = true;
						menu = false;
						break;
					}

					switch (e.type) {

					case SDL_CONTROLLERBUTTONDOWN:

						if (e.cdevice.which == 0) {
							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {

								if (startGameOver) {
									menu = false;
									gameState = LEVEL1;
									startGameOver = false;
								}
								if (backstoryOver) {
									menu = false;
									gameState = BACKSTORY;
									backstoryOver = false;
								}
								if (instructionsOver) {
									menu = false;
									gameState = INSTRUCTIONS;
									instructionsOver = false;
								}
								if (quitOver) {
									menu = false;
									quit = true;
									quitOver = false;
								}
							}

							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_B) {
								menu = false;
								gameState = WIN;
							}

							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_X) {
								menu = false;
								gameState = LOSE;
							}

						}
						break;

					case SDL_CONTROLLERAXISMOTION:
						moveCursor(e.caxis);
						break;
					}

				} // END POLL EVENT

				// **** UPDATE
				fire1.Update(deltaTime);
				fire2.Update(deltaTime);

				UpdateCursor(deltaTime);

				// check for collision between cursor active state and buttons
				startGameOver = SDL_HasIntersection(&activePos, &startN.posRect);
				backstoryOver = SDL_HasIntersection(&activePos, &backstoryN.posRect);
				instructionsOver = SDL_HasIntersection(&activePos, &instructionsN.posRect);
				quitOver = SDL_HasIntersection(&activePos, &quitN.posRect);

				// **** DRAW PROCESS ****
				//Clear the SDL RenderTarget
				SDL_RenderClear(renderer);

				// Draw the main menu
				MainMenu.Draw(renderer);

				fire1.Draw(renderer);
				fire2.Draw(renderer);

				Barrels.Draw(renderer);

				if (startGameOver) {
					startN.Draw(renderer, startN.posRect);
					selected.Draw(renderer, startN.posRect);
				}
				else {
					startN.Draw(renderer, startN.posRect);
				}
				if (backstoryOver) {
					backstoryN.Draw(renderer, backstoryN.posRect);
					selected.Draw(renderer, backstoryN.posRect);
				}
				else {
					backstoryN.Draw(renderer, backstoryN.posRect);
				}
				if (instructionsOver) {
					instructionsN.Draw(renderer, instructionsN.posRect);
					selected.Draw(renderer, instructionsN.posRect);
				}
				else {
					instructionsN.Draw(renderer, instructionsN.posRect);
				}
				if (quitOver) {
					quitN.Draw(renderer, quitN.posRect);
					selected.Draw(renderer, quitN.posRect);
				}
				else {
					quitN.Draw(renderer, quitN.posRect);
				}



				//Draw Cursor
				SDL_RenderCopy(renderer, cursor, NULL, &cursorPos);


				// Present screen render
				SDL_RenderPresent(renderer);
			}
			break; //end main menu case

			// *********************************************************************************************************
			// *********************************************************************************************************
			// *********************************************************************************************************
		case LEVEL1:
			level1 = true;

			Mix_FadeOutMusic(1000);

			Mix_FadeInMusic(lvlM, 3, 1000);

			player.Reset();
			key1.Reset();
			key2.Reset();

			zombieList.clear();

			//create zombies
			for (int i = 0; i < 9; i++) {

				int kX = (rand() % 1024);
				int kY = (rand() % 768);

				// Create Enemy
				Zombie zombiE(renderer, images_dir, audio_dir, kX, kY);

				//add to Zlist
				zombieList.push_back(zombiE);
			}

			cout << "The Game State is LEVEL 1..." << endl;
			cout << "Press A Button for firing ..." << endl;

			while (level1) {

				//set up frame rate for the section, or CASE
				thisTime = SDL_GetTicks();
				deltaTime = (float)(thisTime - lastTime) / 1000;
				lastTime = thisTime;

				// check for input events
				while (SDL_PollEvent(&e) != 0) {

					//check to see if the SDL Window is closed - player clicks X in Window
					if (e.type == SDL_QUIT) {
						quit = true;
						level1 = false;
						break;

					}
					switch (e.type) {

					case SDL_CONTROLLERBUTTONDOWN:

						if (e.cdevice.which == 0) {
							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {

								player.OnControllerButton(e.cbutton);
								break;
							}
						}
						break;

					}
				}

				// Get x and Y values from game controller
				const Sint16 Xvalue = SDL_GameControllerGetAxis(gGameControllerO,
						SDL_CONTROLLER_AXIS_LEFTX);
				const Sint16 Yvalue = SDL_GameControllerGetAxis(gGameControllerO,
						SDL_CONTROLLER_AXIS_LEFTY);

				// Pass values to method
				player.OnControllerAxis(Xvalue, Yvalue);

				// **** Update
				// Update player
				player.Update(deltaTime);

				//Update Zombies
				for (int i = 0; i < zombieList.size(); i++) {
					zombieList[i].Update(deltaTime, player.posRect);
				}

				// **** COLLISIONS

				// **** Check for player shoots Zombies
				for (int i = 0; i < player.bulletList.size(); i++) {
					if (player.bulletList[i].active == true) {
						for (int j = 0; j < zombieList.size(); j++) {
							if (SDL_HasIntersection(&player.bulletList[i].posRect, &zombieList[j].zombieRect)) {


								player.bulletList[i].Reset();
								zombieList[j].RemoveHealth();

								if (zombieList[j].killed == true) {

									int dropRate = (rand() % 4);

									if (dropRate == 1) {

										if (player.key1 == false && player.key2 == false) {
											key1.Drop(zombieList[j].zombieRect.x, zombieList[j].zombieRect.y);
											zombieList[j].Reset();
										}

										if (player.key1 == true && player.key2 == false) {
											key2.Drop(zombieList[j].zombieRect.x, zombieList[j].zombieRect.y);
											zombieList[j].Reset();
										}

									}
									else if (dropRate == 2) {
										if (health.active == false) {
											health.Drop(zombieList[j].zombieRect.x, zombieList[j].zombieRect.y);
											zombieList[j].Reset();

										}

									}
									else if (dropRate == 3) {
										if (ammo.active == false) {
											ammo.Drop(zombieList[j].zombieRect.x, zombieList[j].zombieRect.y);
											zombieList[j].Reset();

										}

									}
									else {
										zombieList[j].Reset();
									}

									break;

								}
							}
						}
					}
				}
				//Check for hit by zombies
				for (int j = 0; j < zombieList.size(); j++) {
					if (SDL_HasIntersection(&player.posRect, &zombieList[j].zombieRect)) {

						// Play hit sound
						//	Mix_PlayChannel(-1, explosionSound, 0);

						player.eZombieHit();

						if (player.playerHealth <= 0) {

							player.Reset();
							level1 = false;
							gameState = LOSE;
							break;
						}
					}
				}


				//check for item collision
				if (SDL_HasIntersection(&player.posRect, &key1.posRect)) {
					key1.Reset();
					player.key1 = true;
					key1.MoveToHUD(458, 11);
				}
				if (SDL_HasIntersection(&player.posRect, &key2.posRect)) {
					key2.Reset();
					player.key2 = true;
					key2.MoveToHUD(521, 11);
				}
				//check for win condition
				if (SDL_HasIntersection(&player.posRect, &lvl1Gate.posRect)) {
					if (player.key1 == true && player.key2 == true) {
						level1 = false;
						level1won = true;
						gameState = WIN;
					}
				}
				//ammo
				if (SDL_HasIntersection(&player.posRect, &ammo.posRect)) {
					player.GiveAmmo();
					ammo.Reset();
				}
				//health
				if (SDL_HasIntersection(&player.posRect, &health.posRect)) {
					player.GiveHealth();
					health.Reset();
				}


				// **** Draw

				//Clear the SDL RenderTarget
				SDL_RenderClear(renderer);

				// Draw the main menu
				Level1.Draw(renderer);

				key1.Draw(renderer);

				key2.Draw(renderer);

				ammo.Draw(renderer);

				health.Draw(renderer);

				lvl1Gate.Draw(renderer);

				// Player Draw
				player.Draw(renderer);

				for (int i = 0; i < zombieList.size(); i++) {
					zombieList[i].Draw(renderer);
				}

				// Present screen render
				SDL_RenderPresent(renderer);
			}
			break; //end level1 case

			// *********************************************************************************************************
			// *********************************************************************************************************
			// *********************************************************************************************************
		case LEVEL2:
			level2 = true;

			playerEnergy = 0;
			eBarR.w = playerEnergy / maxEnergy * 223;

			player.Reset();
			generator1.Reset();
			generator2.Reset();
			generator3.Reset();
			generator4.Reset();


			turret1.levelReset(75.0f, 50.0f);
			turret2.levelReset(725.0f, 50.0f);
			turret3.levelReset(725.0f, 580.0f);
			turret4.levelReset(75.0f, 580.0f);


			zombieList.clear();

			//create zombies
			for (int i = 0; i < 5; i++) {

				int kX = (rand() % 1024);
				int kY = (rand() % 768);

				// Create Enemy
				Zombie zombiE(renderer, images_dir, audio_dir, kX, kY);

				//add to Zlist
				zombieList.push_back(zombiE);
			}


			cout << "The Game State is LEVEL 2..." << endl;
			cout << "Press A Button for firing ..." << endl;

			while (level2) {

				//set up frame rate for the section, or CASE
				thisTime = SDL_GetTicks();
				deltaTime = (float)(thisTime - lastTime) / 1000;
				lastTime = thisTime;

				// check for input events
				while (SDL_PollEvent(&e) != 0) {

					//check to see if the SDL Window is closed - player clicks X in Window
					if (e.type == SDL_QUIT) {
						quit = true;
						level2 = false;
						break;

					}
					switch (e.type) {

					case SDL_CONTROLLERBUTTONDOWN:

						if (e.cdevice.which == 0) {
							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {

								player.OnControllerButton(e.cbutton);
								break;
							}

						}
						break;

					}
				}

				// Get x and Y values from game controller
				const Sint16 Xvalue = SDL_GameControllerGetAxis(gGameControllerO,
						SDL_CONTROLLER_AXIS_LEFTX);
				const Sint16 Yvalue = SDL_GameControllerGetAxis(gGameControllerO,
						SDL_CONTROLLER_AXIS_LEFTY);

				// Pass values to method
				player.OnControllerAxis(Xvalue, Yvalue);

				// **** Update
				// Update player
				player.Update(deltaTime);

				//Update Turrets
				turret1.Update(deltaTime, player.posRect);
				turret2.Update(deltaTime, player.posRect);
				turret3.Update(deltaTime, player.posRect);
				turret4.Update(deltaTime, player.posRect);
				explode1.Update(deltaTime);
				explode2.Update(deltaTime);
				explode3.Update(deltaTime);
				explode4.Update(deltaTime);

				for (int i = 0; i < zombieList.size(); i++) {
					zombieList[i].Update(deltaTime, player.posRect);
				}

				// **** COLLISION

				// **** Check for player shoots Zombies
				for (int i = 0; i < player.bulletList.size(); i++) {
					if (player.bulletList[i].active == true) {
						for (int j = 0; j < zombieList.size(); j++) {
							if (SDL_HasIntersection(&player.bulletList[i].posRect, &zombieList[j].zombieRect)) {
								zombieList[j].RemoveHealth();

								if (zombieList[j].killed) {
									if (zombieList[j].killed == true) {

										int dropRate = (rand() % 4);

										if (dropRate == 2) {
											if (health.active == false) {
												health.Drop(zombieList[j].zombieRect.x, zombieList[j].zombieRect.y);
												zombieList[j].Reset();

											}

										}
										else if (dropRate == 3) {
											if (ammo.active == false) {
												ammo.Drop(zombieList[j].zombieRect.x, zombieList[j].zombieRect.y);
												zombieList[j].Reset();

											}

										}
										else {
											zombieList[j].Reset();
										}

										break;

									}
								}
								player.bulletList[i].Reset();
								break;
							}
						}
					}
				}

				// **** check for zombies hitting player
				for (int j = 0; j < zombieList.size(); j++) {
					if (SDL_HasIntersection(&player.posRect, &zombieList[j].zombieRect)) {

						// Play hit sound
						//	Mix_PlayChannel(-1, explosionSound, 0);

						player.eZombieHit();

						if (player.playerHealth <= 0) {

							player.Reset();
							level2 = false;
							gameState = LOSE;
							break;
						}
					}
				}

				// **** check for player hitting turrets
				for (int i = 0; i < player.bulletList.size(); i++) {
					if (player.bulletList[i].active == true) {

						if (SDL_HasIntersection(&player.bulletList[i].posRect, &turret1.baseRect)) {
							player.bulletList[i].Reset();
							turret1.RemoveHealth();

							if (turret1.killed == true) {
								generator1.Drop(turret1.baseRect.x, turret1.baseRect.y);
								explode1.makeExplosion(turret1.baseRect.x, turret1.baseRect.y);
								turret1.Reset();
								break;
							}
						}

						if (SDL_HasIntersection(&player.bulletList[i].posRect, &turret2.baseRect)) {
							player.bulletList[i].Reset();
							turret2.RemoveHealth();

							if (turret2.killed == true) {
								generator2.Drop(turret2.baseRect.x, turret2.baseRect.y);
								explode2.makeExplosion(turret2.baseRect.x, turret2.baseRect.y);
								turret2.Reset();				
								break;
							}

						}

						if (SDL_HasIntersection(&player.bulletList[i].posRect, &turret3.baseRect)) {
							player.bulletList[i].Reset();
							turret3.RemoveHealth();

							if (turret3.killed == true) {
								generator3.Drop(turret3.baseRect.x, turret3.baseRect.y);
								explode3.makeExplosion(turret3.baseRect.x, turret3.baseRect.y);
								turret3.Reset();
								break;
							}
						}

						if (SDL_HasIntersection(&player.bulletList[i].posRect, &turret4.baseRect)) {
							player.bulletList[i].Reset();
							turret4.RemoveHealth();

							if (turret4.killed == true) {
								generator4.Drop(turret4.baseRect.x, turret4.baseRect.y);
								explode4.makeExplosion(turret4.baseRect.x, turret4.baseRect.y);
								turret4.Reset();
								break;
							}
						}

					}
				}

				// **** CHeck for turret hit Player
				for (int i = 0; i < turret1.bulletList.size(); i++) {
					if (turret1.bulletList[i].active == true) {
						if (SDL_HasIntersection(&turret1.bulletList[i].posRect, &player.posRect)) {
							player.eBulletHit();
							turret1.bulletList[i].Reset();

							if (player.playerHealth <= 0) {

								player.Reset();
								level2 = false;
								gameState = LOSE;
								break;
							}
						}
					}
				}
				for (int i = 0; i < turret2.bulletList.size(); i++) {
					if (turret2.bulletList[i].active == true) {
						if (SDL_HasIntersection(&turret2.bulletList[i].posRect, &player.posRect)) {
							player.eBulletHit();
							turret2.bulletList[i].Reset();

							if (player.playerHealth <= 0) {

								player.Reset();
								level2 = false;
								gameState = LOSE;
								break;
							}
						}
					}
				}
				for (int i = 0; i < turret3.bulletList.size(); i++) {
					if (turret3.bulletList[i].active == true) {
						if (SDL_HasIntersection(&turret3.bulletList[i].posRect, &player.posRect)) {
							player.eBulletHit();
							turret3.bulletList[i].Reset();

							if (player.playerHealth <= 0) {

								player.Reset();
								level2 = false;
								gameState = LOSE;
								break;
							}
						}
					}
				}
				for (int i = 0; i < turret4.bulletList.size(); i++) {
					if (turret4.bulletList[i].active == true) {
						if (SDL_HasIntersection(&turret4.bulletList[i].posRect, &player.posRect)) {
							player.eBulletHit();
							turret4.bulletList[i].Reset();

							if (player.playerHealth <= 0) {

								player.Reset();
								level2 = false;
								gameState = LOSE;
								break;
							}
						}
					}
				}

				if (SDL_HasIntersection(&player.posRect, &generator1.posRect)) {
					generator1.Reset();
					player.gen1 = true;
					generator1.MoveToHUD(-100,-100);
					playerEnergy += 25;
					eBarR.w = playerEnergy / maxEnergy * 223;
				}
				if (SDL_HasIntersection(&player.posRect, &generator2.posRect)) {
					generator2.Reset();
					player.gen2 = true;
					generator2.MoveToHUD(-100, -100);
					playerEnergy += 25;
					eBarR.w = playerEnergy / maxEnergy * 223;
				}
				if (SDL_HasIntersection(&player.posRect, &generator3.posRect)) {
					generator3.Reset();
					player.gen3 = true;
					generator3.MoveToHUD(-100, -100);
					playerEnergy += 25;
					eBarR.w = playerEnergy / maxEnergy * 223;
				}
				if (SDL_HasIntersection(&player.posRect, &generator4.posRect)) {
					generator4.Reset();
					player.gen4 = true;
					generator4.MoveToHUD(-100, -100);
					playerEnergy += 25;
					eBarR.w = playerEnergy / maxEnergy * 223;
				}

				//ammo
				if (SDL_HasIntersection(&player.posRect, &ammo.posRect)) {
					player.GiveAmmo();
					ammo.Reset();
				}
				//health
				if (SDL_HasIntersection(&player.posRect, &health.posRect)) {
					player.GiveHealth();
					health.Reset();
				}

				//check for win condition
				if (SDL_HasIntersection(&player.posRect, &lvl2Gate.posRect)) {
					if (player.gen1 == true && player.gen2 == true && player.gen3 == true && player.gen4 == true) {
						level1won = false;
						level2 = false;
						gameState = WIN;
					}
				}

				// **** Draw
				//Clear the SDL RenderTarget
				SDL_RenderClear(renderer);

				// Draw the main menu
				Level2.Draw(renderer);

				lvl2Gate.Draw(renderer);

				SDL_RenderCopy(renderer, eBar, NULL, &eBarR);

				generator1.Draw(renderer);
				generator2.Draw(renderer);
				generator3.Draw(renderer);
				generator4.Draw(renderer);

				health.Draw(renderer);
				ammo.Draw(renderer);


				// Player Draw
				player.Draw(renderer);

				for (int i = 0; i < zombieList.size(); i++) {
					zombieList[i].Draw(renderer);
				}

				turret1.Draw(renderer);
				turret2.Draw(renderer);
				turret3.Draw(renderer);
				turret4.Draw(renderer);
	
				explode1.Draw(renderer);
				explode2.Draw(renderer);
				explode3.Draw(renderer);
				explode4.Draw(renderer);

				// Present screen render
				SDL_RenderPresent(renderer);
			}
			break; //end level2 case

			// *********************************************************************************************************
			// *********************************************************************************************************
			// *********************************************************************************************************

		case INSTRUCTIONS:
			instructions = true;

			backstoryN.posRect.x = 750.0f;
			backstoryN.posRect.y = 55.0f;

			startN.posRect.x = 750.0f;
			startN.posRect.y = 100.0f;

			cout << "The Game State is INSTRUCTIONS..." << endl;

			while (instructions) {

				//set up frame rate for the section, or CASE
				thisTime = SDL_GetTicks();
				deltaTime = (float)(thisTime - lastTime) / 1000;
				lastTime = thisTime;

				// check for input events
				while (SDL_PollEvent(&e) != 0) {

					//check to see if the SDL Window is closed - player clicks X in Window
					if (e.type == SDL_QUIT) {
						quit = true;
						instructions = false;
						break;

					}
					switch (e.type) {

					case SDL_CONTROLLERBUTTONDOWN:

						if (e.cdevice.which == 0) {
							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {


								if (startGameOver) {
									instructions = false;
									gameState = LEVEL1;
									startGameOver = false;
								}
								if (menuOver) {
									instructions = false;
									gameState = MENU;
									menuOver = false;
								}
								if (backstoryOver) {
									instructions = false;
									gameState = BACKSTORY;
									backstoryOver = false;
								}
							}

						}
						break;

					case SDL_CONTROLLERAXISMOTION:
						moveCursor(e.caxis);
						break;
					}
				}

				// **** UPDATE
				UpdateCursor(deltaTime);

				fireBig.Update(deltaTime);

				// check for collision between cursor active state and buttons
				startGameOver = SDL_HasIntersection(&activePos, &startN.posRect);
				backstoryOver = SDL_HasIntersection(&activePos, &backstoryN.posRect);
				menuOver = SDL_HasIntersection(&activePos, &mainN.posRect);

				// **** DRAW PROCESS ****
				//Clear the SDL RenderTarget
				SDL_RenderClear(renderer);

				// Draw the main menu
				Instructions.Draw(renderer);

				fireBig.Draw(renderer);

				BarrelBackInst.Draw(renderer);

				if (startGameOver) {
					startN.Draw(renderer, startN.posRect);
					selected.Draw(renderer, startN.posRect);
				}
				else {
					startN.Draw(renderer, startN.posRect);
				}
				if (backstoryOver) {
					backstoryN.Draw(renderer, backstoryN.posRect);
					selected.Draw(renderer, backstoryN.posRect);
				}
				else {
					backstoryN.Draw(renderer, backstoryN.posRect);
				}
				if (menuOver) {
					mainN.Draw(renderer, mainN.posRect);
					selected.Draw(renderer, mainN.posRect);
				}
				else {
					mainN.Draw(renderer, mainN.posRect);
				}

				//Draw Cursor
				SDL_RenderCopy(renderer, cursor, NULL, &cursorPos);

				// Present screen render
				SDL_RenderPresent(renderer);


			}
			break; //end Instructions case

			// *********************************************************************************************************
			// *********************************************************************************************************
			// *********************************************************************************************************

		case BACKSTORY:
			backstory = true;

			instructionsN.posRect.x = 750.0f;
			instructionsN.posRect.y = 55.0f;

			startN.posRect.x = 750.0f;
			startN.posRect.y = 100.0f;

			cout << "The Game State is Backstory..." << endl;

			while (backstory) {

				//set up frame rate for the section, or CASE
				thisTime = SDL_GetTicks();
				deltaTime = (float)(thisTime - lastTime) / 1000;
				lastTime = thisTime;

				// check for input events
				while (SDL_PollEvent(&e) != 0) {

					//check to see if the SDL Window is closed - player clicks X in Window
					if (e.type == SDL_QUIT) {
						quit = true;
						backstory = false;
						break;

					}
					switch (e.type) {

					case SDL_CONTROLLERBUTTONDOWN:

						if (e.cdevice.which == 0) {
							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {


								if (startGameOver) {
									backstory = false;
									gameState = LEVEL1;
									startGameOver = false;
								}
								if (menuOver) {
									backstory = false;
									gameState = MENU;
									menuOver = false;
								}
								if (instructionsOver) {
									backstory = false;
									gameState = INSTRUCTIONS;
									instructionsOver = false;
								}

							}
						}
						break;

					case SDL_CONTROLLERAXISMOTION:
						moveCursor(e.caxis);
						break;
					}
				}

				// **** UPDATE
				UpdateCursor(deltaTime);

				fireBig.Update(deltaTime);

				// check for collision between cursor active state and buttons
				startGameOver = SDL_HasIntersection(&activePos, &startN.posRect);
				instructionsOver = SDL_HasIntersection(&activePos, &instructionsN.posRect);
				menuOver = SDL_HasIntersection(&activePos, &mainN.posRect);

				// **** DRAW PROCESS ****
				//Clear the SDL RenderTarget
				SDL_RenderClear(renderer);

				// Draw the main menu
				Backstory.Draw(renderer);

				fireBig.Draw(renderer);

				BarrelBackInst.Draw(renderer);

				if (startGameOver) {
					startN.Draw(renderer, startN.posRect);
					selected.Draw(renderer, startN.posRect);
				}
				else {
					startN.Draw(renderer, startN.posRect);
				}
				if (instructionsOver) {
					instructionsN.Draw(renderer, instructionsN.posRect);
					selected.Draw(renderer, instructionsN.posRect);
				}
				else {
					instructionsN.Draw(renderer, instructionsN.posRect);
				}
				if (menuOver) {
					mainN.Draw(renderer, mainN.posRect);
					selected.Draw(renderer, mainN.posRect);
				}
				else {
					mainN.Draw(renderer, mainN.posRect);
				}

				//Draw Cursor
				SDL_RenderCopy(renderer, cursor, NULL, &cursorPos);

				// Present screen render
				SDL_RenderPresent(renderer);


			}
			break; //end back story case

			// *********************************************************************************************************
			// *********************************************************************************************************
			// *********************************************************************************************************

		case WIN:
			win = true;

			if (level1won == true) {
				playAgainN.posRect.x = -100.0f;
				playAgainN.posRect.y = -100.0f;
				continueN.posRect.x = 750.0f;
				continueN.posRect.y = 55.0f;
			}
			else {
				continueN.posRect.x = -100.0f;
				continueN.posRect.y = -100.0f;
				playAgainN.posRect.x = 750.0f;
				playAgainN.posRect.y = 55.0f;
			}

			cout << "The Game State is WIN..." << endl;

			while (win) {

				//set up frame rate for the section, or CASE
				thisTime = SDL_GetTicks();
				deltaTime = (float)(thisTime - lastTime) / 1000;
				lastTime = thisTime;

				// check for input events
				while (SDL_PollEvent(&e) != 0) {

					//check to see if the SDL Window is closed - player clicks X in Window
					if (e.type == SDL_QUIT) {
						quit = true;
						win = false;
						break;

					}
					switch (e.type) {

					case SDL_CONTROLLERBUTTONDOWN:

						if (e.cdevice.which == 0) {
							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {

								if (continueOver) {
									win = false;
									gameState = LEVEL2;
									continueOver = false;
								}
								if (menuOver) {
									win = false;
									gameState = MENU;
									menuOver = false;

									Mix_FadeOutMusic(1000);

									Mix_FadeInMusic(menuM, 3, 1000);
								}
								if (playAgainOver) {
									win = false;
									gameState = LEVEL1;
									playAgainOver = false;
								}

							}
						}
						break;

					case SDL_CONTROLLERAXISMOTION:
						moveCursor(e.caxis);
						break;
					}
				}

				// **** UPDATE
				UpdateCursor(deltaTime);

				// check for collision between cursor active state and buttons
				playAgainOver = SDL_HasIntersection(&activePos, &playAgainN.posRect);
				continueOver = SDL_HasIntersection(&activePos, &continueN.posRect);
				menuOver = SDL_HasIntersection(&activePos, &mainN.posRect);

				// **** DRAW PROCESS ****
				//Clear the SDL RenderTarget
				SDL_RenderClear(renderer);

				// Draw the bkgd
				if (level1won == true) {
					Win1.Draw(renderer);
				}
				else {
					Win2.Draw(renderer);
				}

				if (level1won == true) {
					if (continueOver) {
						continueN.Draw(renderer, continueN.posRect);
						selected.Draw(renderer, continueN.posRect);
					}
					else {
						continueN.Draw(renderer, continueN.posRect);
					}
				}
				else {

					if (playAgainOver) {
						playAgainN.Draw(renderer, playAgainN.posRect);
						selected.Draw(renderer, playAgainN.posRect);
					}
					else {
						playAgainN.Draw(renderer, playAgainN.posRect);
					}
				}

				if (menuOver) {
					mainN.Draw(renderer, mainN.posRect);
					selected.Draw(renderer, mainN.posRect);
				}
				else {
					mainN.Draw(renderer, mainN.posRect);
				}

				//Draw Cursor
				SDL_RenderCopy(renderer, cursor, NULL, &cursorPos);

				// Present screen render
				SDL_RenderPresent(renderer);
			}
			break; //end win case

			// *********************************************************************************************************
			// *********************************************************************************************************
			// *********************************************************************************************************

		case LOSE:
			lose = true;
			cout << "The Game State is LOSE..." << endl;

			playAgainN.posRect.x = 750.0f;
			playAgainN.posRect.y = 55.0f;

			Mix_FadeOutMusic(1000);

			Mix_FadeInMusic(menuM, 3, 1000);

			while (lose) {

				//set up frame rate for the section, or CASE
				thisTime = SDL_GetTicks();
				deltaTime = (float)(thisTime - lastTime) / 1000;
				lastTime = thisTime;

				// check for input events
				while (SDL_PollEvent(&e) != 0) {

					//check to see if the SDL Window is closed - player clicks X in Window
					if (e.type == SDL_QUIT) {
						quit = true;
						lose = false;
						break;

					}
					switch (e.type) {

					case SDL_CONTROLLERBUTTONDOWN:

						if (e.cdevice.which == 0) {
							if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {

								if (playAgainOver) {
									lose = false;
									if (level1won == true)
									{
										gameState = LEVEL2;
										Mix_FadeOutMusic(1000);

										Mix_FadeInMusic(lvlM, 3, 1000);
									}
									else {
										gameState = LEVEL1;
										Mix_FadeOutMusic(1000);

										Mix_FadeInMusic(lvlM, 3, 1000);
									}
									playAgainOver = false;
								}
								if (menuOver) {
									lose = false;
									gameState = MENU;
									menuOver = false;


								}

							}
						}
						break;

					case SDL_CONTROLLERAXISMOTION:
						moveCursor(e.caxis);
						break;
					}
				}

				// **** UPDATE
				UpdateCursor(deltaTime);

				// check for collision between cursor active state and buttons
				playAgainOver = SDL_HasIntersection(&activePos, &playAgainN.posRect);
				menuOver = SDL_HasIntersection(&activePos, &mainN.posRect);

				// **** DRAW PROCESS ****
				//Clear the SDL RenderTarget
				SDL_RenderClear(renderer);

				// Draw the main menu
				Lose.Draw(renderer);

				if (playAgainOver) {
					playAgainN.Draw(renderer, playAgainN.posRect);
					selected.Draw(renderer, playAgainN.posRect);
				}
				else {
					playAgainN.Draw(renderer, playAgainN.posRect);
				}
				if (menuOver) {
					mainN.Draw(renderer, mainN.posRect);
					selected.Draw(renderer, mainN.posRect);
				}
				else {
					mainN.Draw(renderer, mainN.posRect);
				}

				//Draw Cursor
				SDL_RenderCopy(renderer, cursor, NULL, &cursorPos);

				// Present screen render
				SDL_RenderPresent(renderer);
			}
			break; //end lose case

		}//end gamestate
	}//end while !quit loop

	// Close and Destroy Window
	SDL_DestroyWindow(window);

	// Clean Up
	SDL_Quit();

	return 0;
}
int wmain()
{
#pragma region Other Stuff
			// Hide cursor
	SDL_ShowCursor(0);

	gameState = PLAY;
	//The window we'll be rendering to
	SDL_Window* window = NULL;
	//The surface contained by the window
	SDL_Surface* screenSurface = NULL;
	//SDL
	Renderer r = Renderer(SCREEN_WIDTH, SCREEN_HEIGHT);

	AudioManager::GetInstance()->Init();
	AudioManager::GetInstance()->LoadMedia();
	AudioManager::GetInstance()->PlaySoundEffect(1);

	SDL_Point ePos;
	ePos.x = 100;
	ePos.y = 100;
	Enemy en = Enemy(200, 200, 62 / 3, 77 / 3, r, 0);
	Enemy en1 = Enemy(400, 100, 62 / 3, 77 / 3, r, 0);
	Level lvl = Level(r);

	// MENU
	SDL_Point bPos;
	bPos.x = 0;
	bPos.y = 0;
	Menu background = Menu(bPos, SCREEN_WIDTH, SCREEN_HEIGHT, r, 0, "background");

	SDL_Point pPos;
	pPos.x = SCREEN_WIDTH / 3;
	pPos.y = SCREEN_HEIGHT / 4;
	Menu playBtn = Menu(pPos, 263, 44, r, 0, "playBtn");

	SDL_Point e2Pos;
	e2Pos.x = (SCREEN_WIDTH / 2) - 50;
	e2Pos.y = (SCREEN_HEIGHT / 2) - 20;
	Menu exitBtn = Menu(e2Pos, 111, 45, r, 0, "exitBtn");
	// Player
	SDL_Point playerPos;
	playerPos.x = SCREEN_WIDTH / 2 - 25;
	playerPos.y = SCREEN_HEIGHT / 2 - 10;
	Player player = Player(playerPos, 44, 32, r, 0);


	// THREADS \\

	// Run the thread
	//Player * p = (); // pointer to object
	void * pv = &player;          // pointer to void
	Player * p2 = static_cast<Player *>(pv); // pointer to the same object

	int data;
	SDL_Thread* threadID = SDL_CreateThread(renderThreadFunction, "RenderThread", (void*)p2);

	// THREADS \\
#pragma endregion


#pragma region SDL STUFF

			bool quit = false;
			float oldTime = SDL_GetTicks();
			float delta = 0.f;
			float newTime = 0.f;
			SDL_Event e;
			fpsTimer.start();


			while (!quit) 
			{
				while (SDL_PollEvent(&e) != 0) 
				{
					InputManager::GetInstance()->UpdatePolledEvents(e);
				}

				if (e.button.button == SDL_SCANCODE_ESCAPE)
					quit = true;

					// THREADS \\

					// THREADS \\

				//controls gameState added in game menu feature
				r.Begin();
				switch (gameState) 
				{
					
				case MENU:
					
					//detect button click
					if (e.type == SDL_MOUSEBUTTONDOWN) 
					{

						//If the left mouse button was pressed
						if (e.button.button == SDL_BUTTON_LEFT) 
						{
							//Get the mouse offsets
							int mouse_x = e.button.x;
							int mouse_y = e.button.y;

							if (playBtn.IsClicked(mouse_x, mouse_y)) 
							{
								gameState = PLAY;
							}
							else if (exitBtn.IsClicked(mouse_x, mouse_y))
							{
								quit = true;
							}
						}
					}// End detect button click


					background.Draw(r);
					playBtn.Draw(r);
					exitBtn.Draw(r);
					break;
				case PLAY:
					newTime = SDL_GetTicks();
					delta = newTime - oldTime;
					oldTime = newTime;

					en.Update(delta);
					lvl.Draw(r);
					en.Draw(r);
					en1.Update(delta);
					en1.Draw(r);
					en.Draw(r);
					player.Update(r, delta);
					player.Draw(r);

					//calculateFPS();

					break;
					
				}
				InputManager::GetInstance()->UpdateState();
				
				r.End();
			}

			//Remove timer in case the call back was not called
			SDL_WaitThread(threadID, NULL);

	return EXIT_SUCCESS;
}
Пример #3
0
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
	g_hInstance = hInstance;	// Store application handle
	g_bWindowed = true;			// Windowed mode or full-screen

	// Init the window
	InitWindow();

	// Use this msg structure to catch window messages
	MSG msg; 
	ZeroMemory(&msg, sizeof(msg));
	//perform the count for frames per second
	_int64 cntsPerSec = 0;
	QueryPerformanceFrequency((LARGE_INTEGER*)&cntsPerSec);//perform function QueryPerformanceFrequency: returns a BOOL value; true if timer exist
	float secsPerCnt = 1.0f / (float)cntsPerSec; //typecast the long int into a float
	//create an int for prev time
	_int64 prevTimeStamp = 0;
	QueryPerformanceCounter((LARGE_INTEGER*)&prevTimeStamp);

	//*************************************************************************
	// Initialize DirectX/Game here (call the Init method of your framwork)
	DXObj.Init(g_hWnd, g_hInstance, g_bWindowed);
	MenuObj.Init(g_hWnd, g_hInstance, g_bWindowed);
	//*************************************************************************

	// Main Windows/Game Loop
	while(msg.message != WM_QUIT)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		//begin current time stamp counter
		_int64 currTimeStamp = 0;
		//receive counter performance
		QueryPerformanceCounter((LARGE_INTEGER*)&currTimeStamp);
		float dt = (currTimeStamp - prevTimeStamp)*secsPerCnt;
		if(MenuObj.GetState() == 0) // Intro movie
		{
				DXObj.Update(dt);
				if(DXObj.GetMovieState() == true)
				{
					MenuObj.SetState(1);
				}
		}
		if(MenuObj.GetState() == 1)
		{
				//menu
				MenuObj.Update();
				MenuObj.Draw();
		}
		if(MenuObj.GetState() == 2)
		{
				//options
			MenuObj.Update();
			MenuObj.Draw();
		}
		if(MenuObj.GetState() == 3)
		{
				//game
			if(!g_bIsEnabled)
			{
				PFObj.Init(g_hWnd, g_hInstance, g_bWindowed);
				g_bIsEnabled = true;
			}
				PFObj.Update(dt);
				PFObj.Draw();
				if(PFObj.GetMenuBool())
				{
					MenuObj.SetState(1);
				}
		}
		if(MenuObj.GetState() == 4)
		{
				//credits
			MenuObj.Update();
			MenuObj.Draw();
		}
		if(MenuObj.GetState() == 5)
		{
				//quit game
				DXObj.Shutdown();
				PostQuitMessage(0);
		}
		//*************************************************************************
		// This is where you call your DirectXFramework/Game render/update calls
		
		//MenuObj.Draw();
		//PSObj.Draw();
		//*************************************************************************
		//Prepare for the next iteration: The current time
		//stamp becomes the previous time stamp for the next iteration
		
		prevTimeStamp = currTimeStamp; 

	}

	//*************************************************************************
	//Shutdown DirectXFramework/Game here
	DXObj.Shutdown();

	//*************************************************************************

	// Unregister window
	UnregisterClass(WINDOW_TITLE, g_hInstance);

	// Return successful
	return 0;
}
Пример #4
0
int main()
{
	//gamestates
	Menu* menu;
	PlayGame* play;
	Pause* pause;
	Splash* splash;
	//ScoreScreen* controls;
	Controls* controls;
	ScoreScreen* scoreScreen;

	// Create the main window 
	//Screen dimension constants

	
	sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32), "Space Wars!");
	
	/*Initialsie the game modes*/
	menu = new Menu(SCREEN_WIDTH, SCREEN_HEIGHT);
	play = new PlayGame(SCREEN_WIDTH, SCREEN_HEIGHT);
	pause = new Pause(SCREEN_WIDTH, SCREEN_HEIGHT);
	splash = new Splash(SCREEN_WIDTH, SCREEN_HEIGHT);
	controls = new Controls(SCREEN_WIDTH, SCREEN_HEIGHT);
	scoreScreen = new ScoreScreen(SCREEN_WIDTH, SCREEN_HEIGHT);

	//clock & frame rate
	sf::Clock mclock;
	sf::Time timeSinceLastUpdate;
	window.setFramerateLimit(60);
	// Start game loop 
	while (window.isOpen())
	{
		//get time between frames
		timeSinceLastUpdate = mclock.getElapsedTime();
		float time = timeSinceLastUpdate.asSeconds();//get update time
		mclock.restart();


		// Process events 
		sf::Event Event;
		while (window.pollEvent(Event))
		{
			// Close window : exit 
			if (Event.type == sf::Event::Closed)
				window.close();

			// Escape key : exit 
			if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
				window.close();
		}
		//updates
		switch (GameStateController::GetInstance()->getGameState())
		{
		case GameStateController::MENU:
			menu->Update(time, window, Event);
			break;
		case GameStateController::PLAY:
			play->Update(time, timeSinceLastUpdate);
			break;
		case GameStateController::CREDITS:
			break;
		case GameStateController::PAUSE:
			break;
		case GameStateController::SPLASH:
			break;
		case GameStateController::SCORESCREEN:
			scoreScreen->Update();
			break;
		case GameStateController::CONTROLS:
			controls->Update(time, window, Event);
			break;
		}//end switch

		//prepare frame
		window.clear(sf::Color::White);

		//draw
		switch (GameStateController::GetInstance()->getGameState())
		{
		case GameStateController::MENU:
			menu->Draw(window);
			break;
		case GameStateController::PLAY:
			play->Draw(window);
			break;
		case GameStateController::CREDITS:
			break;
		case GameStateController::PAUSE:
			break;
		case GameStateController::SPLASH:
			break;
		case GameStateController::SCORESCREEN:
			scoreScreen->Draw(window);
			break;
		case GameStateController::CONTROLS:
			controls->Draw(window);
			break;
		}//end switch


		// Finally, display rendered frame on screen 
		window.display();
	} //loop back for next frame
	
	//delete pointers
	delete menu;
	delete play;
	delete pause;
	delete splash;
	delete controls;

	return EXIT_SUCCESS;
}
Пример #5
0
int main()
{
	float boidsSize = 8;
	string action = "flock";


	// Create the main window
	sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
	const int window_height = desktop.height;
	const int window_width = desktop.width;

	
	sf::RenderWindow window(sf::VideoMode(window_width, window_height, 32), "AI Project", sf::Style::None);
	Camera::GetInstance()->Init(window_width, window_height);
	MiniMap::GetInstance()->Init(window_width, window_height);
	//load a font
	sf::Font font;
	font.loadFromFile("C:\\Windows\\Fonts\\GARA.TTF");
	//int SCREEN_WIDTH = 800, SCREEN_HEIGHT = 600;
	Menu* menu;
	Play* play;

	//Create flock, vector of shapes, and initialize boids
	//Flock flock;
	//vector<sf::CircleShape> shapes;

	//for (int i = 0; i < 25; i++) //Number of boids is hardcoded for testing pusposes.
	//{
	//	//Boid b(rand() % window_width, rand() % window_height); //Starts the boid with a random position in the window.
	//	Boid b((window_width * 3) / 2, (window_height * 3) / 2 -150); //Starts all boids in the center of the screen
	//	sf::CircleShape shape(boidsSize, 3); //Shape with a radius of 10 and 3 points (Making it a triangle)

	//	//Changing the Visual Properties of the shape
	//	//shape.setPosition(b.location.x, b.location.y); //Sets position of shape to random location that boid was set to.
	//	shape.setPosition(window_width, window_height); //Testing purposes, starts all shapes in the center of screen.
	//	shape.setOutlineColor(sf::Color(0, 255, 0));
	//	shape.setFillColor(sf::Color::Green);
	//	shape.setOutlineColor(sf::Color::White);
	//	shape.setOutlineThickness(1);
	//	shape.setRadius(boidsSize);

	//	//Adding the boid to the flock and adding the shapes to the vector<sf::CircleShape>
	//	flock.addBoid(b);
	//	shapes.push_back(shape);
	//	
	//}

	menu = new Menu(window_width, window_height);
	play = new Play(window_width, window_height);


	////create a circle
	//sf::CircleShape circle(50);
	//
	//sf::CircleShape circle2(100);

	sf::Time time;
	sf::Clock m_clock;

	// Start game loop 
	while (window.isOpen())
	{
		// Process events 
		sf::Event Event;
		while (window.pollEvent(Event))
		{
			// Close window : exit 
			if (Event.type == sf::Event::Closed)
				window.close();
		}

		time = m_clock.getElapsedTime();
		float t = time.asSeconds();
		m_clock.restart();


		//updates
		switch (StateController::GetInstance()->getState())
		{
		case StateController::MENU:
			menu->Update(t);
			break;
		case StateController::PLAY:
			play->Update(t, window);
			break;
		case StateController::CREDITS:
			break;
		case StateController::PAUSE:
			break;
		case StateController::SPLASH:
			break;
		case StateController::SCORESCREEN:
			break;
		}//end switch


		window.clear();
		
		//draw
		switch (StateController::GetInstance()->getState())
		{
		case StateController::MENU:
			menu->Draw(window);
			break;
		case StateController::PLAY:
			play->Draw(window);
			break;
		case StateController::CREDITS:
			break;
		case StateController::PAUSE:
			break;
		case StateController::SPLASH:
			break;
		case StateController::SCORESCREEN:
			break;
		}//end switch

		
		window.display(); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>DRAW WINDOW
	} //loop back for next frame
	
	return EXIT_SUCCESS;
}
Пример #6
0
int main(int argc, const char* argv[])
{
	al_init();
	
	al_init_font_addon();
	al_init_ttf_addon();
	al_init_image_addon();
	al_init_primitives_addon();

	ALLEGRO_DISPLAY *display;
	al_set_new_display_flags(ALLEGRO_WINDOWED);
	display = al_create_display(800, 600);

	al_install_keyboard();
	al_install_mouse();

	ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
	al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)display);
	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_mouse_event_source());

	ALLEGRO_FONT* font = al_load_ttf_font("data/times.ttf", 12, 0);

	Vector2 camera(-400, -300);
	
	Platforms platforms;
	Bitmaps bitmaps;
	Sprites sprites;
	
	File* file = new File(font);
	file->platforms = &platforms;
	file->bitmaps = &bitmaps;
	file->sprites = &sprites;
	
	Menu menu;
	menu.Add_entry(new Create_platform(platforms, camera, font));
	menu.Add_entry(new Edit_platform(platforms, camera, font));
	menu.Add_entry(file);
	menu.Add_entry(new Create_sprite(bitmaps, sprites, camera, font));
	menu.Add_entry(new Edit_sprite(sprites, camera, font));

	if(argc==2)
		file->Load(argv[1]);

	while(1)
	{
		ALLEGRO_EVENT event;
		if (al_get_next_event(event_queue, &event))
		{
			if (ALLEGRO_EVENT_DISPLAY_CLOSE == event.type ||
					ALLEGRO_EVENT_KEY_DOWN == event.type &&
					ALLEGRO_KEY_ESCAPE == event.keyboard.keycode)
			{
				break;
			}
			if(ALLEGRO_EVENT_MOUSE_AXES)
			{
				ALLEGRO_MOUSE_STATE mstate;
				al_get_mouse_state(&mstate);
				if(al_mouse_button_down(&mstate, 3))
				{
					camera.x -= event.mouse.dx;
					camera.y -= event.mouse.dy;
				}
			}
			menu.Event(event);
		}

		for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i)
		{
			(*i)->Draw(camera);
		}
		for(Platforms::iterator i = platforms.begin(); i != platforms.end(); ++i)
		{
			(*i)->Draw(camera, al_map_rgb_f(0, 1, 0));
		}
		
		al_draw_line(0, -camera.y, 800, -camera.y, al_map_rgba_f(0, 1, 0, 0.5), 0);
		al_draw_line(-camera.x, 0, -camera.x, 600, al_map_rgba_f(0, 0, 1, 0.5), 0);
		
		menu.Draw();

		al_flip_display();
		al_clear_to_color(al_map_rgb(0, 0, 0));

		al_rest(0.001);
	}

	al_destroy_event_queue(event_queue);
	al_destroy_display(display);
}
Пример #7
0
int main(int argc, char* args[])
{

	enum {MENU, RUNNING, GAMEOVER, END};
	int state = MENU;

	int score = 0;
	int deaths = 0;

	//Holds all game objects vectors
	vector<Net *> nets;
	vector<Coral *> corals;
	vector<School *> schools;
	vector<SceneryFish *> sceneryFish;
	vector<GobblerFish *> gobblerFish;

	//back buffer || bit messy can clean up
	System system("Fishy Game", false, 640, 480, 32, 60);
	if (!system.Setup())
	{
		MessageBox(NULL, "SDL DID NOT INITIALISE PROPERLY", NULL, MB_OK);
		return -1;
	}
	SDL_Surface* buffer = system.GetBuffer();

	Timer timer;
	Timer delta;
	Camera camera(640, 480);

	Level level;
	Menu menu;
	Hud hud;
	
	School* school;
	Net* net = NULL;
	Coral* coral = NULL;

	Mix_Music *music = NULL;
	Fish fish("Sprites/Player_Spritesheet.bmp", 64, 64, 0, 0, 200, 200);
	level.LoadLevel(fish, corals, nets, sceneryFish, gobblerFish);
	music = Mix_LoadMUS( "Sounds/DST-Aethereal.wav" );

	delta.Start();

	while(!system.Done())
	{
		srand(time(NULL));

		if(Mix_PlayingMusic () == 0)
		{
			Mix_PlayMusic(music, -1);
		}

		switch(state)
		{
		case MENU:

			state = (menu.Update( delta, system.GetEvent()));
			menu.Draw(buffer, camera);

			break;
		case RUNNING:


		hud.Update(score, deaths );

		//update
		fish.Update( delta.GetTicks() );

		for( int c = 0; c < corals.size(); c++)
		{
			corals[c]->Update(delta.GetTicks());
		}

		for( int i = 0; i < schools.size(); i++)
		{
			schools[i]->Update( delta.GetTicks(), fish, corals);
			for(int j = 0; j < 3; j++)
			{
				if(schools[i]->GetFish(j)->GetEaten()
					&& (!schools[i]->GetFish(j)->GetScored())) 
				{ 
						deaths += 1;
						schools[i]->GetFish(j)->SetScored(true);
				}
				if(schools[i]->GetFish(j)->GetSaved()
					&& (!schools[i]->GetFish(j)->GetScored()))
				{ 
					score += 10; 
					schools[i]->GetFish(j)->SetScored(true);
				}
			}
		}

		for( int g = 0; g < gobblerFish.size(); g++)
		{
			gobblerFish[g]->Update(delta.GetTicks(), schools);
		}

		for( int i = 0; i < nets.size(); i++)
		{
			nets[i]->Update(delta.GetTicks(), fish);

			if(nets[i]->GetReleased())
			{
				school = NULL;
				school = new School("Sprites/Player_SpriteSheet.bmp", 
					nets[i]->GetOffsetX(), nets[i]->GetOffsetY());
				schools.push_back(school);
			}

		}

		for( int f = 0; f < sceneryFish.size(); f++)
		{
			sceneryFish[f]->Update(delta.GetTicks(), fish);
		}

		camera.Update(fish.GetPosX(), fish.GetPosY(), fish.GetWidth(), fish.GetHeight());

		//Draw background first
		level.DrawBackdrop( buffer, camera );

		for( int f = 0; f < sceneryFish.size(); f++)
		{
			sceneryFish[f]->Draw( buffer, camera);
		}

		for(int s = 0; s < schools.size(); s++)
		{
			schools[s]->Draw( buffer, camera );
		}

		level.DrawBackground( buffer, camera );

		for(int i = 0; i < nets.size(); i++)
		{
			nets[i]->Draw( buffer, camera );
		}

		fish.Draw( buffer, camera );

		for(int g = 0; g < gobblerFish.size(); g++)
		{
			gobblerFish[g]->Draw( buffer, camera );
		}

		for(int c = 0; c < corals.size(); c++)
		{
			corals[c]->Draw( buffer, camera );
		}

		level.DrawForeground( buffer, camera );

		hud.Draw( buffer );

		if(hud.GetTime() <= 0)
		{
			state = GAMEOVER;
		}

			break;
		case GAMEOVER:

			if(!timer.GetStarted())
			{
				timer.Start();
			}

			menu.DrawGameOver( buffer, hud.GetScore(), hud.GetDeaths());

			if(timer.GetTicks() >= 5000)
			{
				state = MENU;

				for(int i = 0; i < schools.size(); i++)
				{
					delete schools[i];
				}


				schools.clear();
				score = 0;
				deaths = 0;
				hud.Reset();
				level.ResetLevel(fish, corals, nets, sceneryFish, gobblerFish);
				timer.Stop();
			}

			break;
		case END:
			Mix_FreeMusic(music);
			delete net;
			delete coral;
			system.CleanUp(buffer);
			return 0;
			break;
		}

		delta.Start();

		SDL_Flip( buffer );
		SDL_FillRect(buffer, NULL, SDL_MapRGB(buffer->format, 0, 0, 0));
	}

	if(net != NULL)
	{
		delete net;
	}
	if(coral != NULL)
	{
		delete coral;
	}
	system.CleanUp(buffer);

	return 0;
}