示例#1
0
文件: main.cpp 项目: ezliang/CS3113
int main(int argc, char *argv[])
{
	
	SDL_Event event;
	bool done = false;
	float lastFrameTicks = 0.0f;
	float ball_angle = 0.0f;
	srand(time(NULL));

	Matrix projectionMatrix;
	Matrix viewMatrix;
	Setup(&displayWindow, &projectionMatrix);

	GLuint paddle = LoadTexture("blue_panel.png", GL_RGBA);

	Matrix leftM;
	Entity leftPaddle(paddle);
	leftPaddle.width = 0.3f;
	leftPaddle.x = -3.2f;
	leftPaddle.speed = 2.0f;

	Matrix rightM;
	Entity rightPaddle(paddle);
	rightPaddle.width = 0.3f;
	rightPaddle.x = 3.2f;
	rightPaddle.speed = 2.0f;

	Matrix ballM;
	GLuint ballT = LoadTexture("green_panel.png", GL_RGBA);
	Entity ball(ballT);
	ball.height = 0.25f;
	ball.width = 0.25f;
	ball.speed = 3.0f;
	if (rand() % 1){
		ball_angle = (rand() % 90 + 135);
	}
	else{
		ball_angle = (rand() % 90 + 316);
	}

	std::vector<Entity> entities;
	entities.push_back(leftPaddle);
	entities.push_back(rightPaddle);


	std::vector<Matrix> modelM;
	modelM.push_back(leftM);
	modelM.push_back(rightM);


	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");

	glUseProgram(program.programID);
	
	while (!done) {
		

		float ticks = (float)SDL_GetTicks() / 1000.0f;
		float elapsed = ticks - lastFrameTicks;
		lastFrameTicks = ticks;
		ProcessEvents(&event, &done, elapsed, entities);

		Update(entities,ball,ball_angle,elapsed);
		glClear(GL_COLOR_BUFFER_BIT);

		Render(&program, &projectionMatrix,&viewMatrix, modelM, entities,ball,ballM);
		
	}
	
	SDL_Quit();
	return 0;
}
示例#2
0
int main()
{

	//Création de la fenêtre 
	sf::RenderWindow window;
	window.create(sf::VideoMode(640, 480), "Pong");
	//Modification de la position de la fenêtre ( x=L , y=H)
	window.setPosition(sf::Vector2i(150, 50));
	//Définition des images par secondes maximal 
	window.setFramerateLimit(60);

	//Création du Paddle de gauche 
	Paddle lPaddle(10,60,40,210);
	sf::RectangleShape leftPaddle(sf::Vector2f(lPaddle.getm_width(),lPaddle.getm_height()));
	leftPaddle.setPosition(sf::Vector2f(lPaddle.getm_x(), lPaddle.getm_y()));
	
	//Création du Paddle de droite
	Paddle rPaddle(10, 60, 590, 210);
	sf::RectangleShape rightPaddle(sf::Vector2f(rPaddle.getm_width(), rPaddle.getm_height()));
	rightPaddle.setPosition(sf::Vector2f(rPaddle.getm_x(), rPaddle.getm_y()));
		
	//Création de la balle 
	Ball cball;
	sf::CircleShape ball(cball.getm_radius());
	ball.setPosition(sf::Vector2f(cball.getm_x(), cball.getm_y()));
	sf::Vector2f ballSpeed(cball.getm_speed(),cball.getm_speed());
	
	
	//Tant que la fenêtre est ouverte...
	while (window.isOpen()) {

		sf::Event event;
		//Test des évènements 
		while (window.pollEvent(event))	{ 

			//Si on clique sur fermer
			if (event.type == sf::Event::Closed)
				//On ferme la fenêtre
				window.close();
		}

			/* Gestion des déplacements clavier du paddle de gauche */
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) {
			leftPaddle.move(0, -lPaddle.getm_speed());
			}
			else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
			leftPaddle.move(0, lPaddle.getm_speed());
			}

		    /* Gestion des déplacements clavier du paddle de droite */
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ) {
				rightPaddle.move(0, -rPaddle.getm_speed());
			}
			else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
				rightPaddle.move(0, rPaddle.getm_speed());
			}

			/*Initialisation de la direction de la balle au départ*/
			ball.move(-ballSpeed.x, ballSpeed.y*2);

			/*Gestion de collision avec un mur*/
			if (ball.getPosition().y <= 0) {
				std::cout << "Balle collision Haut" << std::endl;
				ballSpeed.y = -ballSpeed.y;
				
			}
			else if (ball.getPosition().y >= 463) {
				std::cout << "Balle collision Bas" << std::endl;
				ballSpeed.y = -ballSpeed.y;
			}
			else if (ball.getPosition().x <= 20) {
				std::cout << "Joueur 2 a marqué" << std::endl;
				ball.setPosition(sf::Vector2f(cball.getm_x(), cball.getm_y()));
				
			}
			else if (ball.getPosition().x >= 620) {
				std::cout << "Joueur 1 a marqué" << std::endl;
				ball.setPosition(sf::Vector2f(cball.getm_x(), cball.getm_y()));
				
			}

			/*Gestion de collision avec un paddle*/
			sf::FloatRect ballBox = ball.getGlobalBounds();
			sf::FloatRect leftBox = leftPaddle.getGlobalBounds();
			sf::FloatRect rightBox = rightPaddle.getGlobalBounds();

			if (leftBox.intersects(ballBox)) {
				ballSpeed.x = -ballSpeed.x;
			}
			else if (rightBox.intersects(ballBox)) {
				ballSpeed.x = -ballSpeed.x;
			}
			
			/*Gestion de collision paddle gauche & mur*/
			if (leftPaddle.getPosition().y <= 0) {
				std::cout << "Padlle gauche collision Haut " << std::endl;
				leftPaddle.setPosition (40,0);
			}
			else if (leftPaddle.getPosition().y >= 420) {
				std::cout << "Padlle gauche collision Bas " << std::endl;
				leftPaddle.setPosition(40, 420);
			}

			/*Gestion de collision paddle droite & mur*/
			if (rightPaddle.getPosition().y <= 0) {
				std::cout << "Padlle droite collision Haut " << std::endl;
				rightPaddle.setPosition(590, 0);
			}
			else if (rightPaddle.getPosition().y >= 420) {
				std::cout << "Padlle droite collision Bas " << std::endl;
				rightPaddle.setPosition(590, 420);
			}

		window.clear();
		window.draw(leftPaddle);
		window.draw(rightPaddle);
		window.draw(ball);
		window.display();
		
	}

	return 0;
}
int main(int argc, char** argv){
	float lastFrameTicks = 0.0f;

	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 690, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);

	glClearColor(1.0, 0.0, 2.0, 0.0);

	int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize);
	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096);
	
	Mix_Music *music;
	music = Mix_LoadMUS("hyruletemple.mp3");
	Mix_PlayMusic(music, -1);

	//Mix_FreeChunk(someSound);
	//Mix_FreeMusic(music);
	//SDL_Quit();


#ifdef _WINDOWS
	glewInit();
#endif
	glViewport(0, 0, 1280, 690);
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");

	Object leftPaddle(0.1f, 0.7f, 1.0f, 1.0f, -5.1f, 0.0f, 3.0f, 0.0f);
	leftPaddle.setOrthoProj();
	leftPaddle.setObjMatrices(program);
	leftPaddle.translateObj(-3.2, -1.5, 0.0);
	leftPaddle.scaleObj(leftPaddle.width, leftPaddle.height, 1.0);
	
	
	Object rightPaddle(0.1f, 0.7f, 1.0f, 1.0f, 5.1f, 0.0f, 3.0f, 0.0);
	rightPaddle.setOrthoProj();
	rightPaddle.setObjMatrices(program);
	rightPaddle.translateObj(3.2, 1.5, 0.0);
	rightPaddle.scaleObj(rightPaddle.width, rightPaddle.height, 1.0);
	

	Object ball(0.1f, 0.1f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
	ball.setOrthoProj();
	ball.setObjMatrices(program);
	ball.translateObj(0.0, 0.0, 0.0);
	ball.scaleObj(ball.width, ball.height, 1.0);
	
	Object topWall(7.65f, 0.25f, 1.0f, 1.0f, 0.0f, 1.95f, 0.0f, 0.0f);
	topWall.setOrthoProj();
	topWall.setObjMatrices(program);
	topWall.translateObj(topWall.posX, topWall.posY, 0.0);
	topWall.scaleObj(topWall.width, topWall.height, 1.0);


	Object bottomWall(7.65f, 0.25f, 1.0f, 1.0f, 0.0f, -1.95f, 0.0f, 0.0f);
	bottomWall.setOrthoProj();
	bottomWall.setObjMatrices(program);
	bottomWall.translateObj(bottomWall.posX, bottomWall.posY, 0.0);
	bottomWall.scaleObj(bottomWall.width, bottomWall.height, 1.0);

	

	float paddleSpeed = 0.0;


	SDL_Event event;
	bool done = false;
	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
				done = true;
			}
		}

			float ticks = (float)SDL_GetTicks() / 1000.0f;
			float elapsed = ticks - lastFrameTicks;
			lastFrameTicks = ticks;

			glClear(GL_COLOR_BUFFER_BIT);

			leftPaddle.setObjMatrices(program);
			leftPaddle.drawObject(program);

			rightPaddle.setObjMatrices(program);
			rightPaddle.drawObject(program);

			ball.setObjMatrices(program);
			ball.drawObject(program);

			topWall.setObjMatrices(program);
			topWall.drawObject(program);

			bottomWall.setObjMatrices(program);
			bottomWall.drawObject(program);

			ball.posX += 0.0025 *elapsed * ball.xDir;
			ball.posY += 0.0001 * elapsed * ball.yDir;

			ball.translateObj(ball.posX, ball.posY, 0.0);

			paddleSpeed += 0.0015 * elapsed;

			
			// Ball dimensions
			float ballTop, ballBottom, ballLeft, ballRight;
			ballTop = ball.posY + (ball.height / 2.0f);
			ballBottom = ball.posY - (ball.height / 2.0f);
			ballLeft = ball.posX - (ball.width / 2.0f);
			ballRight = ball.posX + (ball.width / 2.0f);

			// Wall Dimension
			float topWallBottom = topWall.posY - (topWall.height / 2.0);
			float topWallTop = topWall.posY + (topWall.height / 2.0);
			float bottomWallBottom = bottomWall.posY - (bottomWall.height / 2.0);
			float bottomWallTop = bottomWall.posY + (bottomWall.height / 2.0);

			// Left Paddle Dimensions
			float leftPaddleTop = leftPaddle.posY + (leftPaddle.height / 2.0f);
			float leftPaddleBottom = leftPaddle.posY - (leftPaddle.height / 2.0f);
			float leftPaddleRight = leftPaddle.posX + (leftPaddle.width / 2.0f);
			float leftPaddleLeft = leftPaddle.posX - (leftPaddle.width / 2.0f);
			// Right Paddle Dimensions
			float rightPaddleTop = rightPaddle.posY + (rightPaddle.height / 2.0f);
			float rightPaddleBottom = rightPaddle.posY - (rightPaddle.height / 2.0f);
			float rightPaddleRight = rightPaddle.posX + (rightPaddle.width / 2.0f);
			float rightPaddleLeft = rightPaddle.posX - (rightPaddle.width / 2.0f);

			// Collision with the top
			if ((ballTop >= topWallBottom) || (ballBottom <= bottomWallTop)) {
				ball.yDir = -ball.yDir;
			}

			// Collisions Left Paddle - Ball
			if ((
				(leftPaddleBottom > ballTop) ||
				(leftPaddleTop < ballBottom) ||
				(leftPaddleLeft > ballRight) ||
				(leftPaddleRight < ballLeft))) {
				
			}
			else{
				ball.xDir = -ball.xDir;
				ball.yDir = -ball.yDir;
			}
			if ((
				(rightPaddleBottom < ballTop) ||
				(rightPaddleTop > ballBottom) ||
				(rightPaddleLeft > ballRight) ||
				(rightPaddleRight < ballLeft))) {
				
			}
			else{
				ball.xDir = -ball.xDir;
				ball.yDir = -ball.yDir;
			}


			/*if ((ball.getObjPos())[0] == (rightPaddle.getObjPos())[0] && (ball.getObjPos())[1] == (rightPaddle.getObjPos())[1]){
				float newX = (ball.getObjPos())[0] * -1;
				ball.setObjYpos(newX);
			}

			if ((ball.getObjPos())[0] == (leftPaddle.getObjPos())[0] && (ball.getObjPos())[1] == (leftPaddle.getObjPos())[1]){
				float newX = (ball.getObjPos())[0] * -1;
				ball.setObjYpos(newX);
			}

			if ((ball.getObjPos())[1] == 1.6 || (ball.getObjPos())[1] == -1.6){
				float newX = (ball.getObjPos())[1] * -1;
				ball.setObjYpos(newX);
			}*/




			const Uint8 *keys = SDL_GetKeyboardState(NULL);

			if (keys[SDL_SCANCODE_UP]) {
				rightPaddle.setObjMatrices(program);
				rightPaddle.translateObj(0.0, paddleSpeed, 0.0);
			}
			else if (keys[SDL_SCANCODE_DOWN]) {
				rightPaddle.setObjMatrices(program);
				rightPaddle.translateObj(0.0, -paddleSpeed, 0.0);
			}

			if (keys[SDL_SCANCODE_W]) {
				leftPaddle.setObjMatrices(program);
				leftPaddle.translateObj(0.0, paddleSpeed, 0.0);
			}
			else if (keys[SDL_SCANCODE_S]) {
				leftPaddle.setObjMatrices(program);
				leftPaddle.translateObj(0.0, -paddleSpeed, 0.0);
			}


			////If left paddle wins
			//if ((ball.getObjPos())[0] > 3.5){
			//	glClearColor(1.0, 0.0, 0.0, 0.0);
			//}

			////If right paddle wins
			//if ((ball.getObjPos())[0]  < -3.5){
			//	glClearColor(0.0, 0.0, 1.0, 0.0);
			//}


		

		SDL_GL_SwapWindow(displayWindow);

	}


	SDL_Quit();
	return 0;

}