Example #1
0
void setup(){
	//SDL
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("William He - CS3113 HW#5 Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 853, 480, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);

#ifdef _WINDOWS
	glewInit();
#endif

	glViewport(0, 0, 853, 480);
	projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);
	program = new ShaderProgram(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	GLuint oreoTexture = loadTexture(RESOURCE_FOLDER"cookieBrown.png");
	GLuint spriteSheetTexture = loadTexture(RESOURCE_FOLDER"aliens.png");
	fontSheet = loadTexture(RESOURCE_FOLDER"font1.png");
	glUseProgram(program->programID);
	
	for (int i = 0; i < 10; i++)
	{
		tiles[i] = new Entity(SheetSprite(oreoTexture, 0.0f, 0.0f, 1.0f, 1.0F, 0.6f), true, -2.7f + 1.5f*i, -0.8f);
	}	
	Player =  Entity(SheetSprite(spriteSheetTexture, 676.0f / 1024.0f, 184.0f / 512.0f, 66.0f / 1024.0f, 92.0f /
		512.0f, 0.6f),false, 2.0f, -1.0f);
	loadLevel();
}
Example #2
0
// …
int main(int argc, char *argv[])
{
	Matrix projectionMatrix;
		Matrix modelMatrix;
		Matrix viewMatrix;

		projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
		ShaderProgram program(RESOURCE_FOLDER"vertex.glsl", RESOURCE_FOLDER"fragment.glsl");

		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

		void glViewport(GLint x, GLint y, GLsizei width, GLsizei height);

		glUseProgram(program.programID);

		float vertices[] = { 0.5f, -0.5f, 0.0f, 0.5f, -0.5f, -0.5f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);

		glEnableVertexAttribArray(program.positionAttribute);

		glDrawArrays(GL_TRIANGLES, 0, 3);

		glDisableVertexAttribArray(program.positionAttribute);
}
Example #3
0
ShaderProgram Setup(){
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif
    
    ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
    
    //this regulates zoom
    projectionMatrix.setOrthoProjection(-1.7777f, 1.77777f, -1.0f, 1.0f, -1.0f, 1.0f);
    
    glViewport(0, 0, 800, 600);
    
    //glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearColor(0.46f, 0.81f, 1.0f, 1.0f);
    
    
    setupEntities(&program);

    
    program.setModelMatrix(modelMatrix);
    program.setProjectionMatrix(projectionMatrix);
    program.setViewMatrix(viewMatrix);
    
    glUseProgram(program.programID);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    
    return program;
}
Example #4
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("Assignment 1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	#ifdef _WINDOWS
		glewInit();
	#endif

	SDL_Event event;
	bool done = false;
	
	//size and pixel offset
	glViewport(0, 0, 640, 360);
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	GLuint player = LoadTexture("assets/alien_sheet.png", GL_RGB);

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;

	//projection matrix
	projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);
	
	float lastFrameTicks = 0.0f;

	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
				done = true;
			}
		}
		//tick tick tick
		float ticks = (float)SDL_GetTicks() / 1000.0f;
		float elapsed = ticks - lastFrameTicks;
		lastFrameTicks = ticks;

		glClear(GL_COLOR_BUFFER_BIT);

		//drawing
		draw(program, modelMatrix, projectionMatrix, viewMatrix, player);


		glDisableClientState(GL_VERTEX_ARRAY);

		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #5
0
int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif

    glViewport(0, 0, 640, 360);
    srand(time(0));
    
    ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
    
    Matrix projectionMatrix;
    Matrix modelMatrix;
    Matrix viewMatrix;
    projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);
    //Setup(program, projectionMatrix);
    
    //Animation, Matrix, and Entitys variables
    
    initializeEntities();
    
    //enable blending
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    while (!done) {
        ticks = (float)SDL_GetTicks() / 1000.0f;
        elapsed = ticks - lastFrameTicks;
        lastFrameTicks = ticks;
        ProcessEvents();
        
        glUseProgram(program.programID);
        //p1Entity.Update(elapsed);
        setBackgroundColorAndClear();
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        Update(lastFrameTicks, modelMatrix, program);
        
        Render(program, modelMatrix);
        
        SDL_GL_SwapWindow(displayWindow);
    }
    
    SDL_Quit();
    return 0;
}
Example #6
0
void Setup(){
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif
    
    glViewport(0, 0, 640, 360);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //glBlendFunc (GL_SRC_ALPHA, GL_ONE);
    glClearColor(.0f,.0f,.3f,.0f);
    projectionMatrix.setOrthoProjection(-5.55, 5.55, -3.0f, 3.0f, -1.0f, 1.0f);
}
Example #7
0
File: main.cpp Project: ts2562/hw3
void renderMenu(){
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	SDL_Event event;
	bool done = false;
#ifdef _WINDOWS
	glewInit();
#endif

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");

	const Uint8 *key = SDL_GetKeyboardState(NULL);
	if (key[SDL_SCANCODE_SPACE]){
		state = STATE_GAME_LEVEL;
	}
	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	glUseProgram(program.programID);

	GLuint font = LoadTexture("font2.png");
	string start = "SPACE INVADERS-CLOSE TO START";
	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE){
				done = true;
			}
		}
		glClear(GL_COLOR_BUFFER_BIT);
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		SDL_GL_SwapWindow(displayWindow);

		DrawText(&program, font, start, 0.3, 1.0);
	}

	SDL_Quit();
}
Example #8
0
void render(ShaderProgram &program, string &text, Matrix &modelM, Matrix &viewM, Matrix &projectionM, Entity &ball, Entity &lPaddle, Entity &rPaddle, Entity &tWall, Entity &bWall) {
    (GL_COLOR_BUFFER_BIT);
    
    projectionM.setOrthoProjection(-5.325, 5.325, -3.0f, 3.0f, -1.5f, 1.5f);
    glUseProgram(program.programID);
    
    program.setProjectionMatrix(projectionM);
    program.setViewMatrix(viewM);
    
    ball.drawEntity(program);
    lPaddle.drawEntity(program);
    rPaddle.drawEntity(program);
    tWall.drawEntity(program);
    bWall.drawEntity(program);
    
    //if (text != "")
    //	drawText(&program, modelM, loadTexture("font1.png"), text, 0.3f, 0.05f);
    
    SDL_GL_SwapWindow(displayWindow);
}
Example #9
0
void setup(){
	//SDL
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("William He - CS3113 HW#3 Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 853, 480, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);

#ifdef _WINDOWS
	glewInit();
#endif

	//OpenGL
	glViewport(0, 0, 853, 480);

	//Projection Matrix
	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	program = new ShaderProgram(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	Player->textureID = loadTexture(RESOURCE_FOLDER"flat_medal1.png");
	GLuint spriteSheet = loadTexture(RESOURCE_FOLDER"characters_3.png");
	fontSheet = loadTexture(RESOURCE_FOLDER"font1.png");
	glUseProgram(program->programID);
	
	for (int i = 0; i < 55; i++)
	{
		int row = i / 11;
		int col = i % 11;
		aliens[i] = new Entity(-0.7f + 0.17f*col, 0.8f - 0.15f*row, 0.0f, spriteSheet, 0.20f, 0.20f, 0.10f, 1.0f, 0.0f);
	}
	for (int i = 0; i < MAX_BULLETS; i++)
	{
		bullets[i] = Bullet();
		enemyBullets[i] = Bullet();
	}
	
}
Example #10
0
ShaderProgram Setup(){//Ball* ball, Paddle* paddle,Paddle* paddle2
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif

    ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
    
    Matrix projectionMatrix;
    Matrix modelMatrix;
    Matrix viewMatrix;
    
    projectionMatrix.setOrthoProjection(-1.7777f, 1.77777f, -1.0f, 1.0f, -1.0f, 1.0f);
    
    glViewport(0, 0, 800, 600);
    
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    
    ballTextureID = LoadTexture(RESOURCE_FOLDER"ballBlue.png");
    ball = new Ball(-0.15f, -0.15f, 0.15f, 0.15f);
    paddleTextureID = LoadTexture(RESOURCE_FOLDER"brickWall.png");
    
    paddle1 = new Paddle(-1.5f, 0.0f, 0.15f, 0.30f);
    paddle2 = new Paddle(1.5f, 0.0f, 0.15f, 0.30f);

    
    program.setModelMatrix(modelMatrix);
    program.setProjectionMatrix(projectionMatrix);
    program.setViewMatrix(viewMatrix);

    glUseProgram(program.programID);
    
    lastFrameTicks = 0.0f;
    newGame = true;
    return program;
}
Example #11
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	#ifdef _WINDOWS
		glewInit();
	#endif

	glViewport(0, 0, 640, 360);

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

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;

	Matrix panelOneMatrix;
	Matrix panelTwoMatrix;

	projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);

	GLuint textureID = LoadTexture("ball.png");
	GLuint panelTexID = LoadTexture("red_panel.png");

	glEnable(GL_BLEND);

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	Ball *ball = new Ball(0.0f, 0.0f, 3.0f, 3.0f, 0.2f);
	Panel *panelOne = new Panel(-3.55f, ball);
	Panel *panelTwo = new Panel(3.55f, ball);

	float lastFrameTicks = 0.0f;
	float angle = 0.0f;

	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;

		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

		glUseProgram(program.programID);

		glClearColor(0.2f, 0.2f, 0.2, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);

		// BALL
		float vertices[] = { -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
		glEnableVertexAttribArray(program.positionAttribute);

		float texCoords[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
		glEnableVertexAttribArray(program.texCoordAttribute);


		glBindTexture(GL_TEXTURE_2D, textureID);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		program.setModelMatrix(panelOneMatrix);
		// PANEL ONE
		float panelVTwo[] = { -0.1f, -0.75f, 0.1f, 0.75f, -0.1f, 0.75f, 0.1f, 0.75f, -0.1f, -0.75f, 0.1f, -0.75f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, panelVTwo);
		glEnableVertexAttribArray(program.positionAttribute);

		float texPanelOne[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texPanelOne);
		glEnableVertexAttribArray(program.texCoordAttribute);
		panelOneMatrix.Translate(panelOne->starty, panelOne->pos, 0.0f);

		glBindTexture(GL_TEXTURE_2D, panelTexID);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);
		program.setModelMatrix(panelTwoMatrix);

		// PANEL TWO
		float panelVOne[] = { -0.1f, -0.75f, 0.1f, 0.75f, -0.1f, 0.75f, 0.1f, 0.75f, -0.1f, -0.75f, 0.1f, -0.75f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, panelVOne);
		glEnableVertexAttribArray(program.positionAttribute);

		float texPanelTwo[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texPanelTwo);
		glEnableVertexAttribArray(program.texCoordAttribute);

		glBindTexture(GL_TEXTURE_2D, panelTexID);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		modelMatrix.identity();
		panelOneMatrix.identity();
		panelTwoMatrix.identity();

		panelOne->update();
		panelTwo->update();

		panelOne->pos += panelOne->vel*elapsed;
		panelTwo->pos += panelTwo->vel*elapsed;

		ball->update();
		ball->xPos += ball->xVel*elapsed;
		ball->yPos += ball->yVel*elapsed;

		angle += elapsed;

		modelMatrix.Translate(ball->xPos, ball->yPos, 0.0f);		
		modelMatrix.Scale(0.6f, 0.6f, 1.0f);
		modelMatrix.Rotate(angle);

		panelOneMatrix.Translate(panelOne->starty, panelOne->pos, 0.0f);
		panelOneMatrix.Scale(0.5f, 0.5f, 1.0f);

		panelTwoMatrix.Translate(panelTwo->starty, panelTwo->pos, 0.0f);
		panelTwoMatrix.Scale(0.5f, 0.5f, 1.0f);

		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #12
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	#ifdef _WINDOWS
		glewInit();
	#endif
	///setup
		//ShaderProgram program = ShaderProgram(RESOURCE_FOLDER"vertex.glsl", RESOURCE_FOLDER"fragment.glsl");
		ShaderProgram program = ShaderProgram(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
		Matrix projectionMatrix;
		Matrix modelMatrix;
		Matrix viewMatrix;

		projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0, 2.0, -1.0, 1.0);
	
		GLuint Charmander = LoadTexture("Charmander.png");
		GLuint Bulbasaur = LoadTexture("Bulbasaur.png");
		GLuint Squirtle = LoadTexture("Squirtle.png");

		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		float lastFrameTicks = 0.0f;
		float angle = 0.0f;

	SDL_Event event;
	bool done = false;
	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
				done = true;
			}
		}
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

		glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glUseProgram(program.programID);

		float vertices[] = { -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
		glEnableVertexAttribArray(program.positionAttribute);

		float texCoords[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
		glEnableVertexAttribArray(program.texCoordAttribute);

		glBindTexture(GL_TEXTURE_2D, Charmander);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		float vertices2[] = { -1.5f, -0.5f, -0.5f, 0.5f, -1.5f, 0.5f, -0.5f, 0.5f, -1.5f, -0.5f, -0.5f, -0.5f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);
		glEnableVertexAttribArray(program.positionAttribute);

		float texCoords2[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords2);
		glEnableVertexAttribArray(program.texCoordAttribute);

		glBindTexture(GL_TEXTURE_2D, Squirtle);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		float vertices3[] = { 1.5f, -0.5f, 0.5f, 0.5f, 1.5f, 0.5f, 0.5f, 0.5f, 1.5f, -0.5f, 0.5f, -0.5f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);
		glEnableVertexAttribArray(program.positionAttribute);

		float texCoords3[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords3);
		glEnableVertexAttribArray(program.texCoordAttribute);

		glBindTexture(GL_TEXTURE_2D, Bulbasaur);
		glDrawArrays(GL_TRIANGLES, 0, 6);

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

		angle += elapsed;
		glMatrixMode(Bulbasaur);
		modelMatrix.identity();
		modelMatrix.Rotate(angle);
		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #13
0
int main(int argc, char *argv[])
{
	setup();

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

	Matrix modelMatrix;
	Matrix modelMatrixc;
	Matrix viewMatrix;
	Matrix startMenu;

	GLuint mainChar = LoadTexture("largeMagikarp.png");
	GLuint phone = LoadTexture("sword.png");
	GLuint trainers = LoadTexture("trainers1.png");
	GLuint font = LoadTexture("font1.png");
	GLuint onigiri = LoadTexture("onigiri.png");
	//	GLuint bg = LoadTexture("bg.png");

	float verticesP1[] = { -3.8f, -1.0f, -2.0f, -1.0f, -2.0f, 0.5f, -3.8f, -1.0f, -2.0f, 0.5f, -3.8f, 0.5f };
	float bulletVert[] = { -0.5f, -0.5f,
		-0.1f, -0.5f,
		-0.1f, -0.3f,
		-0.5f, -0.5f,
		-0.1f, -0.3f,
		-0.5f, -0.3f
	};

	float verticesP2[] = { 2.0f, 1.5f,
		2.5f, 1.5f,
		2.5f, 2.0f,
		2.0f, 1.5f,
		2.5f, 2.0f,
		2.0f, 2.0f
	};
	float startPos[] = { -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f };
	float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };

	//vector<Entity> bullets;
	//vector<Matrix> bulletMatrixVec;

	//Player
	Entity magikarp;
	magikarp.player();

	//Enemies
	vector<Entity> enemies;
	int rowE = 5;
	int columnE = 4;
	float space = 0.35f; //space between enemies
	float xDiff = 0.0f;
	float yDiff = 0.0f;

	//Set up grid
	for (int i = 0; i < rowE; i++){
		yDiff = space * i;
		for (int j = 0; j < columnE; j++){
			xDiff = space* j;
			Entity e;
			e.enemy();
			e.y -= yDiff;
			e.x += xDiff;
			enemies.push_back(e);
		}
	}

	//Bullets
	vector<Entity>bullets;
	for (int i = 0; i < MAX_BULLETS; i++){
		Entity b;
		b.bullet();
		bullets.push_back(b);
	}
	float lastFrameTicks = 0.0f;
	const Uint8 *keys = SDL_GetKeyboardState(NULL);
	bool done = false;

	Matrix projectionMatrix;
	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	SDL_Event event;
	//int winner = 0;
	const int runAnimation[] = { 0, 1, 2, 12, 13, 14, 24, 25, 26, 36, 37, 38 };
	//{3,4,5, 15,16,17, 27,28,29, 39,40,41}
	//{6,7,8, 18,19,20, 30,31,32, 42,43,45}
	const int numFrames = 12;
	float animationElapsed = 0.0f;
	float framesPerSecond = 30.0f;
	int currentIndex = 0;
	Matrix magikarpMatrix;
	Matrix gameMatrix;
	Matrix textMatrix;
	Matrix surprise;
	int startScreen = 0;

	Entity b;
	b.bullet();

	while (!done) {

		glClear(GL_COLOR_BUFFER_BIT);

		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

		if (startScreen == 0)
		{
			program.setModelMatrix(startMenu);
			//DrawText(&program, font, "Ready Player One:", .3f, 0.0f); //Reference to book Ready Player One
			DrawText(&program, font, "Press Space to Start", .2f, 0.0f);
			startMenu.setPosition(-2.50f, -1.0f, 0.0f);

			program.setModelMatrix(textMatrix);
			DrawText(&program, font, "Press B for food", .2f, 0.0f);
			startMenu.setPosition(-2.50f, -1.0f, 0.0f);

			while (SDL_PollEvent(&event)) {
				if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
					done = true;
				}
				if (keys[SDL_SCANCODE_SPACE]){
					startScreen = 1;
				}
				if (keys[SDL_SCANCODE_B]){
					startScreen = 10;
				}
			}
		}
		else if (startScreen == 10){

			glClear(GL_COLOR_BUFFER_BIT);
			program.setModelMatrix(surprise);
			program.setProjectionMatrix(projectionMatrix);
			program.setViewMatrix(viewMatrix);

			glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, startPos);
			glEnableVertexAttribArray(program.positionAttribute);

			glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
			glEnableVertexAttribArray(program.texCoordAttribute);
			glBindTexture(GL_TEXTURE_2D, onigiri);
			surprise.setScale(4, 4, 0);
			glDrawArrays(GL_TRIANGLES, 0, 6);



			while (SDL_PollEvent(&event)) {
				if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
					done = true;
				}
				if (keys[SDL_SCANCODE_SPACE]){
					startScreen = 1;
				}
				if (keys[SDL_SCANCODE_B]){
					startScreen = 10;
				}
			}

		}
		else if (startScreen == 1){


			processEvents();
			update();
			render();
			float ticks = (float)SDL_GetTicks() / 7000.0;
			float elapsed = ticks - lastFrameTicks;
			lastFrameTicks = ticks;

			float fixedElapsed = elapsed;
			if (fixedElapsed > FIXED_TIMESTEP * MAX_TIMESTEPS) {
				fixedElapsed = FIXED_TIMESTEP * MAX_TIMESTEPS;
			}
			while (fixedElapsed >= FIXED_TIMESTEP) {
				fixedElapsed -= FIXED_TIMESTEP;
			}

			animationElapsed += fixedElapsed;
			if (animationElapsed > 1.0 / framesPerSecond) {
				currentIndex++;
				animationElapsed = 0.0;
				if (currentIndex > numFrames - 1) {
					currentIndex = 0;
				}
			}


			//	DrawSpriteSheetSprite(&program, runAnimation[currentIndex], 12, 12, verticesP2, trainers);


			program.setModelMatrix(modelMatrix);
			program.setViewMatrix(viewMatrix);
			program.setProjectionMatrix(projectionMatrix);

			magikarp.updateImage(&program, texCoords);
			int numDead = 0;

			//Enemies
			for (int i = 0; i < 20; i++){
				if (enemies[i].isAlive){
					//If enemy is alive ...
					float distance = 0.5f * fixedElapsed * framesPerSecond;
					enemies[i].x -= distance / 5;
					//Constantly move to the left
					if (enemies[i].directionY > 0.0f){
						//If you're moving upward
						if (enemies[i].y + distance < ENEMY_MAX_Y){
							enemies[i].y += distance;
						}
						else{
							//Turn around
							enemies[i].directionY = -1.0f;
						}
					}
					else if (enemies[i].directionY < 0.0f){
						//Moving down
						if (enemies[i].y + distance > ENEMY_MIN_Y){
							enemies[i].y -= distance;
						}
						else{
							//180 noscope
							enemies[i].directionY = 1.0f;
						}
					}
					if (!enemies[i].isAlive){ numDead++; }
					if (enemies[i].x - distance < ENEMY_MIN_X){
						//		startScreen = -1; // Game Over
					}
					if (collision(&magikarp, &enemies[i])){
						//		startScreen = -1;
					}
					if (collision(&b, &enemies[i])){
						enemies[i].died(&program, texCoords);
						//		startScreen = -1;
					}
					if (numDead == 20){
						//		startScreen = -2; //You win
					}
					enemies[i].updateImage(&program, texCoords);
				}
			}

			//Bullets
			for (int i = 0; i < bullets.size(); i++){
				float distance = fixedElapsed * framesPerSecond; //Bullet speed is 1.0f unless not active
				if (bullets[i].active){
					if (bullets[i].x + distance > 5.0f){
						//Bullet leaves screen
						bullets[i].died(&program, texCoords);
					}
					bullets[i].x += distance;
					for (int j = 0; j < enemies.size(); j++){
						if (collision(&bullets[i], &enemies[i])){
							//Bullet collide with enemy
							enemies[i].died(&program, texCoords);
							bullets[i].died(&program, texCoords);
						}
					}
				}
			}


			/*	for (int i = 0; i < 16; i++){
			enemies[i].updateImage(&program, texCoords);
			}
			*/	//food.updateImage(&program, texCoords);

			program.setModelMatrix(gameMatrix);
			b.x += framesPerSecond*fixedElapsed;

			DrawSpriteSheetSprite(&program, runAnimation[currentIndex], 12, 12, verticesP2, trainers);
			b.updateImage(&program, texCoords);

			if (keys[SDL_SCANCODE_W]) {
				if (magikarp.y + (framesPerSecond * fixedElapsed)< 1.50f){
					magikarp.y += framesPerSecond * fixedElapsed;
					//	magikarp.matrix.Translate(0, framesPerSecond * elapsed, 0);
				}

			}
			else if (keys[SDL_SCANCODE_S]) {
				if (magikarp.y - (framesPerSecond * fixedElapsed) > -1.50f){
					magikarp.y -= framesPerSecond * fixedElapsed;
					//	magikarp.matrix.Translate(0, -framesPerSecond * elapsed, 0);
				}
			}

			while (SDL_PollEvent(&event)) {
				if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
					done = true;
				}
				if (keys[SDL_SCANCODE_SPACE]){
					b.active = true;
					b.x = magikarp.x;
					b.y = magikarp.y;
				}

			}
		}
		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;

}
	void setOrthoProjection(){
		projection.setOrthoProjection(-1.777f, 1.777f, -1.0f, 1.0f, -1.0f, 1.0f);
	}
Example #15
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("Final Project", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
	glewInit();
#endif

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

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glViewport(0, 0, 640, 480);

	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096);

	Matrix modelMatrix;
	Matrix textMatrix;
	Matrix viewMatrix;
	Matrix projectionMatrix;
	projectionMatrix.setOrthoProjection(-10.0f, 10.0f, -7.5f, 7.5f, -1.0f, 1.0f);
	
	std::vector<Entity> entities;

	int lastWinner;

	// Load player sprites
	GLint paddleBlue = LoadTextureClamp(RESOURCE_FOLDER"assets\\paddleBlu.png");
	GLint paddleRed = LoadTextureClamp(RESOURCE_FOLDER"assets\\paddleRed.png");
	// Load item sprites
	GLint coinCopperSprite = LoadTextureClamp(RESOURCE_FOLDER"assets\\coinCopper.png");
	GLint coinSilverSprite = LoadTextureClamp(RESOURCE_FOLDER"assets\\coinSilver.png");
	GLint coinGoldSprite = LoadTextureClamp(RESOURCE_FOLDER"assets\\coinGold.png");
	GLint coinPlatinumSprite = LoadTextureClamp(RESOURCE_FOLDER"assets\\coinPlatinum.png");
	GLint starCopperSprite = LoadTextureClamp(RESOURCE_FOLDER"assets\\starCopper.png");
	GLint starSilverSprite = LoadTextureClamp(RESOURCE_FOLDER"assets\\starSilver.png");
	GLint starGoldSprite = LoadTextureClamp(RESOURCE_FOLDER"assets\\starGold.png");
	GLint starPlatinumSprite = LoadTextureClamp(RESOURCE_FOLDER"assets\\starPlatinum.png");
	// Load font
	GLuint fontSheet = LoadTextureClamp(RESOURCE_FOLDER"assets\\font1.png");
	// Load level bg
	GLint mainBG = LoadTextureRepeat(RESOURCE_FOLDER"assets\\bg_main.png");
	GLint levelBG = LoadTextureRepeat(RESOURCE_FOLDER"assets\\bg_level.png");
	GLint overBG = LoadTextureRepeat(RESOURCE_FOLDER"assets\\bg_over.png");

	// Load music
	Mix_Music *music1;
	music1 = Mix_LoadMUS(RESOURCE_FOLDER"assets\\Im Free.mp3");
	Mix_Music *music2;
	music2 = Mix_LoadMUS(RESOURCE_FOLDER"assets\\Lagrima.mp3");
	Mix_Music *music3;
	music3 = Mix_LoadMUS(RESOURCE_FOLDER"assets\\The Old Song.mp3");
	Mix_Music *music4;
	music4 = Mix_LoadMUS(RESOURCE_FOLDER"assets\\FASE.mp3");
	// Load sounds
	Mix_Chunk* itemHit;
	itemHit = Mix_LoadWAV(RESOURCE_FOLDER"hit.wav");
	Mix_Chunk* buttonPress;
	buttonPress = Mix_LoadWAV(RESOURCE_FOLDER"Menu Selection Click.wav");


	// Initialize players
	Entity playerBlue(ENTITY_PLAYER, paddleBlue, fontSheet, SDL_SCANCODE_A, SDL_SCANCODE_D, -5.0f);
	Entity playerRed(ENTITY_PLAYER, paddleRed, fontSheet, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, 5.0f);

	Entity coinCopper(ENTITY_ITEM, coinCopperSprite, fontSheet, 10, buttonPress);
	Entity coinSilver(ENTITY_ITEM, coinSilverSprite, fontSheet, 25, buttonPress);
	Entity coinGold(ENTITY_ITEM, coinGoldSprite, fontSheet, 50, buttonPress);
	Entity coinPlatinum(ENTITY_ITEM, coinPlatinumSprite, fontSheet, 100, buttonPress);
	Entity starCopper(ENTITY_ITEM, starCopperSprite, fontSheet, 100, buttonPress);
	Entity starSilver(ENTITY_ITEM, starSilverSprite, fontSheet, 250, buttonPress);
	Entity starGold(ENTITY_ITEM, starGoldSprite, fontSheet, 500, buttonPress);
	Entity starPlatinum(ENTITY_ITEM, starPlatinumSprite, fontSheet, 1000, buttonPress);

	entities.push_back(playerBlue);
	entities.push_back(playerRed);
	entities.push_back(coinCopper);
	entities.push_back(coinSilver);
	entities.push_back(coinGold);
	entities.push_back(coinPlatinum);
	entities.push_back(starCopper);
	entities.push_back(starSilver);
	entities.push_back(starGold);
	entities.push_back(starPlatinum);

	Level level1(LEVEL_FREEPLAY, fontSheet, levelBG, music3, entities);
	Level level2(LEVEL_CLOCKED, fontSheet, levelBG, music4, entities);
	Level level3(LEVEL_SCORED, fontSheet, levelBG, music1, entities);

	Mix_PlayMusic(music3, -1);

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

		program->setModelMatrix(modelMatrix);
		program->setViewMatrix(viewMatrix);
		program->setProjectionMatrix(projectionMatrix);
		glUseProgram(program->programID);

		switch (state)
		{
		case MAIN_MENU:
			RenderBG(program, mainBG);
			if (keys[SDL_SCANCODE_1]) { // Press 1 to get into game level
				state = GAME_LEVEL_1;
				Mix_PlayChannel(-1, buttonPress, 0);
				Mix_PlayMusic(level1.getBgm(), -1);
			}
			if (keys[SDL_SCANCODE_2]) { // Press 2 to get into game level
				state = GAME_LEVEL_2;
				Mix_PlayChannel(-1, buttonPress, 0);
				Mix_PlayMusic(level2.getBgm(), -1);
			}
			if (keys[SDL_SCANCODE_3]) { // Press 3 to get into game level
				state = GAME_LEVEL_3;
				Mix_PlayChannel(-1, buttonPress, 0);
				Mix_PlayMusic(level3.getBgm(), -1);
			}
			if (keys[SDL_SCANCODE_ESCAPE]){
				done = true;
			}
			break;
		case GAME_LEVEL_1:
			if (!level1.isComplete()){
				level1.Update();
				level1.FixedUpdate();
				level1.Render(program);
				level1.RenderText(program);
			}
			else {
				lastWinner = level1.getWinner();
				state = MAIN_MENU;
				level1.reset();
				Mix_PlayMusic(music3, -1);
			}
			break;
		case GAME_LEVEL_2:
			if (!level2.isComplete()){
				level2.Update();
				level2.FixedUpdate();
				level2.Render(program);
				level2.RenderText(program);
			}
			else {
				lastWinner = level2.getWinner();
				state = GAME_OVER;
				level2.reset();
				Mix_PlayMusic(music3, -1);
			}
			break;
		case GAME_LEVEL_3:
			if (!level3.isComplete()){
				level3.Update();
				level3.FixedUpdate();
				level3.Render(program);
				level3.RenderText(program);
			}
			else {
				lastWinner = level3.getWinner();
				state = GAME_OVER;
				level3.reset();
				Mix_PlayMusic(music3, -1);
			}
			break;
		case GAME_OVER:
			RenderBG(program, overBG);
			program->setModelMatrix(textMatrix);
			textMatrix.identity();
			textMatrix.Translate(-7.5f, 0.0f, 0.0f);
			if (lastWinner == 0)
				DrawText(program, fontSheet, "Blue Player wins!", 1.0f, 0.005f);
			else if (lastWinner == 1)
				DrawText(program, fontSheet, "Red Player wins!", 1.0f, 0.005f);
			if (keys[SDL_SCANCODE_RETURN]) {
				level1.reset();
				level2.reset();
				level3.reset();
				state = MAIN_MENU;
				Mix_PlayChannel(-1, buttonPress, 0);
			}
			else if (keys[SDL_SCANCODE_ESCAPE]) {
				done = true;
			}
			break;
		}

		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #16
0
int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
    
    glViewport(0, 0, 1280, 720);
    ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
    
    Matrix projectionMatrix;
    Matrix modelMatrix;
    Matrix viewMatrix;
    
    float lastFrameTicks = 0.0f;
    float elapsed = 0.0f;
    
    //Block
    float PositionY = -1.0f;
    float PositionX = -2.5f;
    
    float timer = 0.2f;
//    float num = -3.0f;
//    std::vector<float> points;
//    for (int i = 0; i < 12; i++) {
//        points[i] = num;
//        num += 0.5f;
//    }
    
//    int cord1 = rand() % 3 - 2;
//    int cord2 = rand() % 3;
//    int cord3 = rand() % 3 - 3;
//    int cord1 = points[rand()%12];
//    int cord2 = points[rand()%12];
//    int cord3 = points[rand()%12];
    
//    unsigned char levelData[1080][720];
    
    
    projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
    
    glUseProgram(program.programID);
    
    
    
#ifdef _WINDOWS
    glewInit();
#endif
    
    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;
        elapsed = ticks - lastFrameTicks;
        lastFrameTicks = ticks;
        
        //Tile
        timer -= elapsed;
//        if (timer <= 0){
//            glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
//            glClear(GL_COLOR_BUFFER_BIT);
//            timer = 0.2f;
//        }
        
//        int cord1 = rand() % 3 - 2;
//        int cord2 = rand() % 3;
//        int cord3 = rand() % 3 - 3;
        
        glClearColor(0.4f, 0.2f, 0.4f, 1.0f);
        //glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        
        const Uint8 * keys = SDL_GetKeyboardState(NULL);
        if (keys[SDL_SCANCODE_UP] == 1){
            PositionY += elapsed * 3.1415926f / 2.0f;
            if (PositionY + 0.35f >= 2.0f) {
                PositionY -= elapsed * 3.1415926f / 2.0f;
            }
        }
        if (keys[SDL_SCANCODE_DOWN] == 1){
            PositionY -= elapsed * 3.1415926f / 2.0f;
            if (PositionY - 0.35f <= -2.0f) {
                PositionY += elapsed * 3.1415926f / 2.0f;
            }
        }
        if (keys[SDL_SCANCODE_RIGHT] == 1){
            PositionX += elapsed * 3.0f / 2.0f;
        }
        if (keys[SDL_SCANCODE_LEFT] == 1){
            PositionX -= elapsed * 3.0f / 2.0f;
        }
        
        
        glUseProgram(program.programID);
        
        //Player
        modelMatrix.identity();
        modelMatrix.Translate(PositionX, PositionY, 0.0f);
        modelMatrix.Scale(0.25f, 0.25f, 0.0f);
        
        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        float square[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, square);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float texcoords[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texcoords);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        /*---------------------------------------------------------------------------------------*/
        //LBanner
        modelMatrix.identity();
        modelMatrix.Translate(-3.5f, 0.0f, 0.0f);
        modelMatrix.Scale(1.0f, 4.0f, 0.0f);
        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        float leftBanner[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, leftBanner);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float texcoordsLeft[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texcoordsLeft);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        /*---------------------------------------------------------------------------------------*/
        //RBanner
        modelMatrix.identity();
        modelMatrix.Translate(3.5f, 0.0f, 0.0f);
        modelMatrix.Scale(1.0f, 4.0f, 0.0f);
        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        float rightBanner[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, rightBanner);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float texcoordsRight[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texcoordsRight);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        
        /*---------------------------------------------------------------------------------------*/
        //Tile
        modelMatrix.identity();
        modelMatrix.Translate(1.5f, 1.0f, 0.0f);
        modelMatrix.Scale(0.5f, 2.5f, 0.0f);
        
        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        float s1[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, s1);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float t1[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, t1);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        /*---------------------------------------------------------------------------------------*/
        //Tile
        modelMatrix.identity();
        modelMatrix.Translate(-1.5f, -1.0f, 0.0f);
        modelMatrix.Scale(0.5f, 2.5f, 0.0f);
        
        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        float s2[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, s2);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float t2[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, t2);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        /*---------------------------------------------------------------------------------------*/
        //End
        modelMatrix.identity();
        modelMatrix.Translate(-PositionX, -PositionY, 0.0f);
        modelMatrix.Scale(0.25f, 0.25f, 0.0f);
        
        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        float s3[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, s3);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float t3[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, t3);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        /*---------------------------------------------------------------------------------------*/
        //Tile
        modelMatrix.identity();
        modelMatrix.Translate(0.0, 0.0, 0.0f);
        modelMatrix.Scale(0.25f, 0.25f, 0.0f);
        
        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        float s4[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, s4);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float t4[] = { 0.0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, t4);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        /*---------------------------------------------------------------------------------------*/

        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        program.setProjectionMatrix(projectionMatrix);
        
        
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        
        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.positionAttribute);
        
        SDL_GL_SwapWindow(displayWindow);
    }
    
    SDL_Quit();
    return 0;
}
Example #17
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	

	SDL_Event event;
	bool done = false;
#ifdef _WINDOWS
	glewInit();
#endif

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;

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



	GLuint dogTexture = LoadTexture("dog.png");
	GLuint ballTexture = LoadTexture("ball.png");
	GLuint boneTexture = LoadTexture("bone.png");

	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	glUseProgram(program.programID);

	float lastFrameTicks = 0.0f;

	double movement = 0.0;

	double ballBounce = 0.0;

	bool bounceHigh = false;

	bool firstHit = false;

	const Uint8 *keys = SDL_GetKeyboardState(NULL);

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

		glClear(GL_COLOR_BUFFER_BIT);
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		glBindTexture(GL_TEXTURE_2D, ballTexture);

		float ticks = (float)SDL_GetTicks() / 1000.0f;

		float elapsed = ticks - lastFrameTicks;
			
		lastFrameTicks = ticks;

		if (firstHit == false){
			movement = movement + elapsed;
		}

		//makes the ball bounce
		if ((ballBounce * 0.75) >= 1){
			bounceHigh = true;
		}

		if ((ballBounce * 0.75) <= -1){
			bounceHigh = false;
		}
		
		if (bounceHigh == false){
			ballBounce = ballBounce + elapsed;
		}
		else if (bounceHigh == true){
			ballBounce = ballBounce - elapsed;
		}

		
		float vertices[] = { -1.4 + (movement * 0.25), -0.4 + (ballBounce * 0.25), -0.4 + (movement * 0.25), -0.4 + (ballBounce * 0.25),
			-0.4 + (movement * 0.25), 0.4 + (ballBounce * 0.25), -1.4 + (movement * 0.25), -0.4 + (ballBounce * 0.25),
			-0.4 + (movement * 0.25), 0.4 + (ballBounce * 0.25), -1.4 + (movement * 0.25), 0.4 + (ballBounce * 0.25) };
		
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);


		if (keys[SDL_SCANCODE_A]) {
			--p1X;
		}
		else if (keys[SDL_SCANCODE_D]) {
			++p1X;
		}

		if (keys[SDL_SCANCODE_W]) {
			++p1Y;
		}
		else if (keys[SDL_SCANCODE_S]) {
			--p1Y;
		}

		glBindTexture(GL_TEXTURE_2D, dogTexture);

		float vertices2[] = { -2.5 + (p1X * 0.001), -0.5 + (p1Y * 0.001), -1.5 + (p1X * 0.001), -0.5 + (p1Y * 0.001),
			-1.5 + (p1X * 0.001), 0.5 + (p1Y * 0.001), -2.5 + (p1X * 0.001), -0.5 + (p1Y * 0.001),
			-1.5 + (p1X * 0.001), 0.5 + (p1Y * 0.001), -2.5 + (p1X * 0.001), 0.5 + (p1Y * 0.001) };


		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords2[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords2);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);


		if (keys[SDL_SCANCODE_J]) {
			--p2X;
		}
		else if (keys[SDL_SCANCODE_L]) {
			++p2X;
		}

		if (keys[SDL_SCANCODE_I]) {
			++p2Y;
		}
		else if (keys[SDL_SCANCODE_K]) {
			--p2Y;
		}

		glBindTexture(GL_TEXTURE_2D, dogTexture);

		float vertices3[] = { 1.5 + (p2X * 0.001), -0.5 + (p2Y * 0.001), 2.5 + (p2X * 0.001), -0.5 + (p2Y * 0.001),
			2.5 + (p2X * 0.001), 0.5 + (p2Y * 0.001), 1.5 + (p2X * 0.001), -0.5 + (p2Y * 0.001),
			2.5 + (p2X * 0.001), 0.5 + (p2Y * 0.001), 1.5 + (p2X * 0.001), 0.5 + (p2Y * 0.001) };

		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords3[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords3);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);
		SDL_GL_SwapWindow(displayWindow);

		//collision
		if ((vertices[11] <= vertices2[5] && vertices[11] >= vertices2[3])
			|| (vertices[1] <= vertices2[5] && vertices[1] >= vertices2[3])
			&& vertices[10] == vertices2[4])
		{
			firstHit = true;
			movement = movement + elapsed;
		}
		else if ((vertices[5] <= vertices3[11] && vertices[5] >= vertices3[1])
			|| (vertices[3] <= vertices3[11] && vertices[3] >= vertices3[1])
			&& vertices[4] == vertices3[10])
		{
			firstHit = true;
			movement = movement - elapsed;
		}

		if (vertices[10] <= vertices2[4])
		{
			std::cout << "Player 2 Wins" << std::endl;
		}

		else if (vertices[4] >= vertices3[10])
		{
			std::cout << "Player 1 Wins" << std::endl;
		}
	}

	SDL_Quit();
	return 0;
}
Example #18
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("Nick's Space Invader Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
	glewInit();
#endif

	SDL_Event event;
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	Matrix projectionMatrix;
	Matrix viewMatrix;
	
	int screenWidth = 640;
	int screenHeight = 360;
	glViewport(0, 0, screenWidth, screenHeight);   //axis starts from lower left (x,y,w,h)
	glUseProgram(program.programID);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	float projectionWidth = 7.1f;
	float projectionHeight = 4.0f;
	projectionMatrix.setOrthoProjection(-projectionWidth/2, projectionWidth/2, 
										-projectionHeight/2, projectionHeight/2, 
										-1.0f, 1.0f); //(l,r,b,t,n,f) n/f doesnt do anything in ortho

	//PLAYER OBJECT STUFF
	Entity shipPlayer;
	shipPlayer.y = -1.75f;
	shipPlayer.width = .25f;
	shipPlayer.height = .5f;
	Matrix shipPlayerModelMatrix;

	float shipPlayerModelVerticies[] = {-.125,-2, .125,-2, .125,-1.5, -.125,-2, .125,-1.5, -.125,-1.5};

	//PLAYER PROJECTILE OBJECT STUFF
	vector<Matrix> playerProjectileModelMatrixHolder = {};
	vector<Entity> playerProjectileHolder = {};
	int projectileBulletClipSize = 40;
	int bulletCounter = 0;
	for (int i = 0; i < projectileBulletClipSize; i++)
	{ 
		Entity playerProjectile;
		playerProjectile.enabled = false;
		playerProjectile.x = 0;
		playerProjectile.y = 2.5f;
		playerProjectile.width = .01f;
		playerProjectile.height = .25f;
		playerProjectileHolder.push_back(playerProjectile);

		Matrix playerProjectileMatrix;
		playerProjectileMatrix.setPosition(playerProjectile.x, playerProjectile.y, 0);
		playerProjectileModelMatrixHolder.push_back(playerProjectileMatrix);
		
	}
	float playerProjectileModelVerticies[] = {-.005f,-.125f, .005f,-.125f, .005f, .125f, 
											  -.005f,-.125f, .005f,.125f, -.005f, .125f};

	//ENEMY SHIP OBJECT STUFF
	vector<Matrix> shipEnemyModelMatrixHolder = {};
	vector<Entity> shipEnemyHolder = {};
	int numberOfEnemyRows = 3;
	int numberOfEnemyColumns = 8;
	float upperLeftShipX = -3.0f;
	float upperLeftShipY = 1.7f;
	float offsetX = 0.2f;
	float offsetY = 0.2f;
	float initialShipEnemyModelVerticies[] = {-.1f,-.2f, .1f,-.2f, .1f,.2f, -.1f,-.2f, .1f,.2f, -.1f,.2f};
	for (int i = 0; i < numberOfEnemyRows; i++)
	{
		for (int j = 0; j < numberOfEnemyColumns; j++)
		{
			Entity shipEnemy;
			shipEnemy.width = 0.2f;
			shipEnemy.height = 0.4f;
			shipEnemy.x = upperLeftShipX + j*(shipEnemy.width + offsetX);
			shipEnemy.y = upperLeftShipY - i*(shipEnemy.height + offsetY);
			Matrix shipEnemyModelMatrix;
			shipEnemyModelMatrix.setPosition(shipEnemy.x, shipEnemy.y, 0);

			shipEnemyModelMatrixHolder.push_back(shipEnemyModelMatrix);
			shipEnemyHolder.push_back(shipEnemy);
		}
	}

	/*
	vector<Matrix> enemyProjectileModelMatrixHolder = {};
	vector<Entity> enemyProjectileHolder = {};

	for (int i = 0; i < numberOfEnemyColumns; i++)
	{
		Entity enemyshot;
		enemyshot.width = .0f;
		enemyshot.height = 0.25f;
		enemyshot.x = shipEnemyHolder[i].x;
		enemyshot.y = shipEnemyHolder[i].y;
		Matrix enemyShotMatrix;
		//enemyShotMatrix.setPosition(enemyshot.x, shipEnemy.y, 0);

		shipEnemyModelMatrixHolder.push_back(shipEnemyModelMatrix);
		shipEnemyHolder.push_back(shipEnemy);
	}
	*/


	Matrix titleTextMatrix;
	GLuint font = LoadTexture("font1.png");

	Matrix spriteSheetMatrix;
	GLuint spriteSheetTexture = LoadTexture("spriteSheet.png");
	int index = 3;
	int spriteCountX = 5;
	int spriteCountY = 3;

	float texCoords[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0};

	int gameStatus = 0;

	float ticks = 0.0f;
	float lastFrameTicks = 0.0f;
	float elapsed = 0.0f;
	
	const Uint8 *keys = SDL_GetKeyboardState(NULL);
	bool done = false;
	while (!done) 
	{
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

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

		if (gameStatus == 0 || gameStatus == 2 || gameStatus == 3)
		{
			program.setModelMatrix(titleTextMatrix);
			if (gameStatus == 0)
				DrawText(&program, font, "Press space to begin", .3f, 0.0f);
			else if (gameStatus == 2)
				DrawText(&program, font, "You Lose", .3f, 0.0f);
			else if (gameStatus == 3)
				DrawText(&program, font, "You Win", .3f, 0.0f);
			titleTextMatrix.setPosition(-2.75f, 0, 0.0f);
		}
		else if (gameStatus == 1)
		{
			program.setModelMatrix(shipPlayerModelMatrix);
			DrawSpriteSheetSprite(&program, spriteSheetTexture, 4, spriteCountX, spriteCountY, shipPlayerModelVerticies);

			for (int i = 0; i < numberOfEnemyRows * numberOfEnemyColumns; i++)
			{
				if (shipEnemyHolder[i].enabled)
					break;
				if (i == numberOfEnemyRows * numberOfEnemyColumns - 1)
					gameStatus = 3;
			}
			
			for (int i = 0; i < numberOfEnemyRows * numberOfEnemyColumns; i++)
			{
				shipEnemyHolder[i].x += shipEnemyHolder[i].xTimeMove;
				if (shipEnemyHolder[i].enabled)
				{
					program.setModelMatrix(shipEnemyModelMatrixHolder[i]);
					DrawSpriteSheetSprite(&program, spriteSheetTexture, 5, spriteCountX, spriteCountY, initialShipEnemyModelVerticies);
					shipEnemyModelMatrixHolder[i].Translate(shipEnemyHolder[i].xTimeMove, 0, 0);
					if (shipEnemyHolder[i].y <= shipPlayer.y || checkForCollision(&shipEnemyHolder[i], &shipPlayer))
					{
						gameStatus = 2;
						break;
					}
				}
			}
			
			if (shipEnemyHolder[0].x <= upperLeftShipX)
			{
				for (int i = 0; i < numberOfEnemyRows * numberOfEnemyColumns; i++)
				{
					shipEnemyHolder[i].xTimeMove = elapsed /2;
					shipEnemyHolder[i].y -= elapsed*200;
					shipEnemyModelMatrixHolder[i].Translate(0, -elapsed*200, 0);
				}
			}
			else if (shipEnemyHolder[numberOfEnemyColumns - 1].x >= -upperLeftShipX)
			{
				for (int i = 0; i < numberOfEnemyRows * numberOfEnemyColumns; i++)
				{
					shipEnemyHolder[i].xTimeMove = -elapsed /2;
					shipEnemyHolder[i].y -= elapsed*200;
					shipEnemyModelMatrixHolder[i].Translate(0, -elapsed*200, 0);
				}
			}
			
			for (int i = 0; i < projectileBulletClipSize; i++)
			{
				playerProjectileHolder[i].yTimeMove = elapsed * 2.0f;

				if (playerProjectileHolder[i].enabled)
				{
					program.setModelMatrix(playerProjectileModelMatrixHolder[i]);
					DrawSpriteSheetSprite(&program, spriteSheetTexture, 6, spriteCountX, spriteCountY, playerProjectileModelVerticies);
					playerProjectileHolder[i].y += playerProjectileHolder[0].yTimeMove;
					playerProjectileModelMatrixHolder[i].Translate(0.0f, playerProjectileHolder[i].yTimeMove, 0.0f);
					
					for (int j = 0; j < numberOfEnemyRows * numberOfEnemyColumns; j++)
					{
						if (checkForCollision(&shipEnemyHolder[j], &playerProjectileHolder[i]))
						{
							if (shipEnemyHolder[j].enabled)
								playerProjectileHolder[i].enabled = false;
							shipEnemyHolder[j].enabled = false;
							break;
						}
					}
				}

				if (playerProjectileHolder[bulletCounter].y >= projectionHeight + playerProjectileHolder[bulletCounter].height / 2)
					playerProjectileHolder[i].enabled = false;
			}
		}

		if (keys[SDL_SCANCODE_RIGHT])
		{
			if (gameStatus == 1)
			{
				if (shipPlayer.xTimeMove < 0)
					shipPlayer.xTimeMove = 0;
				shipPlayer.xTimeMove = elapsed * 2;
				shipPlayer.x += shipPlayer.xTimeMove;

				if (shipPlayer.x > projectionWidth / 2 - shipPlayer.width / 2)
					shipPlayer.x = projectionWidth / 2 - shipPlayer.width / 2;
				else
					shipPlayerModelMatrix.Translate(shipPlayer.xTimeMove, 0.0f, 0.0f);
			}
		}
		else if (keys[SDL_SCANCODE_LEFT])
		{
			if (gameStatus == 1)
			{
				if (shipPlayer.xTimeMove > 0)
					shipPlayer.xTimeMove = 0;
				shipPlayer.xTimeMove = -elapsed * 2;
				shipPlayer.x += shipPlayer.xTimeMove;

				if (shipPlayer.x < -projectionWidth / 2 + shipPlayer.width / 2)
					shipPlayer.x = -projectionWidth / 2 + shipPlayer.width / 2;
				else
					shipPlayerModelMatrix.Translate(shipPlayer.xTimeMove, 0.0f, 0.0f);
			}
		}

		while (SDL_PollEvent(&event))
		{
			if (keys[SDL_SCANCODE_SPACE])
			{
				if (gameStatus == 0)
					gameStatus++;	
			}
			if (keys[SDL_SCANCODE_UP])
			{
				if (gameStatus == 1)
				{
					if (!playerProjectileHolder[bulletCounter].enabled)
					{
						playerProjectileHolder[bulletCounter].x = shipPlayer.x;
						playerProjectileHolder[bulletCounter].y = shipPlayer.y + shipPlayer.height / 2;
						playerProjectileModelMatrixHolder[bulletCounter].setPosition(shipPlayer.x, shipPlayer.y + shipPlayer.height / 2, 0.0f);
						playerProjectileHolder[bulletCounter].enabled = true;
					}
					bulletCounter++;
					if (bulletCounter == projectileBulletClipSize)
						bulletCounter = 0;
				}
			}
			

			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE)
			{
				done = true;
			}
		}

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);
		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #19
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);


	SDL_Event event;
	bool done = false;
#ifdef _WINDOWS
	glewInit();
#endif

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;

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



	GLuint dogTexture = LoadTexture("dog.png");
	GLuint ballTexture = LoadTexture("ball.png");
	GLuint boneTexture = LoadTexture("bone.png");

	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	glUseProgram(program.programID);

	float lastFrameTicks = 0.0f;

	double movement = 0.0;

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


		glClear(GL_COLOR_BUFFER_BIT);
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		glBindTexture(GL_TEXTURE_2D, dogTexture);
		float ticks = (float)SDL_GetTicks() / 1000.0f;

		float elapsed = ticks - lastFrameTicks;

		lastFrameTicks = ticks;

		movement = movement + elapsed;

		float vertices[] = { -1.5 + (movement * 0.1), -0.5, -0.5 + (movement* 0.1), -0.5, -0.5 + (movement * 0.1), 0.5,
			-1.5 + (movement * 0.1), -0.5, -0.5 + (movement * 0.1), 0.5, -1.5 + (movement * 0.1), 0.5 };


		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);

		glEnableVertexAttribArray(program.positionAttribute);

		float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };

		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);

		glEnableVertexAttribArray(program.texCoordAttribute);

		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);

		glDisableVertexAttribArray(program.texCoordAttribute);




		glBindTexture(GL_TEXTURE_2D, boneTexture);

		float vertices2[] = { -2.5, -0.5, -1.5, -0.5, -1.5, 0.5, -2.5, -0.5, -1.5, 0.5, -2.5, 0.5 };

		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);

		glEnableVertexAttribArray(program.positionAttribute);

		float texCoords2[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };

		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords2);

		glEnableVertexAttribArray(program.texCoordAttribute);

		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);

		glDisableVertexAttribArray(program.texCoordAttribute);


		glBindTexture(GL_TEXTURE_2D, ballTexture);

		float vertices3[] = { 1.5, -0.5, 2.5, -0.5, 2.5, 0.5, 1.5, -0.5, 2.5, 0.5, 1.5, 0.5 };

		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);

		glEnableVertexAttribArray(program.positionAttribute);

		float texCoords3[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };

		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords3);

		glEnableVertexAttribArray(program.texCoordAttribute);

		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);

		glDisableVertexAttribArray(program.texCoordAttribute);

		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #20
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	#ifdef _WINDOWS
		glewInit();
	#endif

	glViewport(0, 0, 640, 360);


	//For No Textures used
	//ShaderProgram program = ShaderProgram("vertex.glsl", "fragment.glsl");

	//For Textures used
	ShaderProgram program = ShaderProgram("vertex_textured.glsl", "fragment_textured.glsl");

	//Sets clear color of screen. (0.0f, 0.0f, 0.0f, 1.0f) is black
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Black Color

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;

	//HW2
	Matrix modelMatrixleftpaddle;
	Matrix modelMatrixrightpaddle;
	Matrix modelMatrixBall;
	Matrix modelMatrixBorder;

	projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);

	//Declare variables outisde of event loop
	float lastFrameTicks = 0.0f;

	float angle = 0.0f;

	GLuint redTextureID = LoadTexture(RESOURCE_FOLDER"red.png");
	GLuint blueTextureID = LoadTexture(RESOURCE_FOLDER"blue.png");
	GLuint yellowTextureID = LoadTexture(RESOURCE_FOLDER"yellow.png");
	GLuint purpleTextureID = LoadTexture(RESOURCE_FOLDER"purple.png");
	GLuint greenTextureID = LoadTexture(RESOURCE_FOLDER"green.png");
	GLuint orangeTextureID = LoadTexture(RESOURCE_FOLDER"orange.png");

	//HW2
	float paddleLength = 0.4f;
	float paddleWidth = 0.1f;
	float leftpaddleY = 0.0f;
	float leftpaddleX = 0.0f;
	float rightpaddleY = 0.0f;
	float rightpaddlex = 0.0f;

	float ballPosiX = 0.0f;
	float ballPosiY = 0.0f;
	float ballDirectX = 0.0f;
	float ballDirectY = 0.0f;
	float ballSpeed = 1.0f;
	float ballSize = 0.1f;


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

			else if (event.type == SDL_KEYDOWN){
				if (event.key.keysym.scancode == SDL_SCANCODE_SPACE) {
					//make character jump
					//character should be affected by gravity
				}

			}

			else if (event.type == SDL_MOUSEMOTION){
				//event.motion.x is our new x
				//float unitX = (((float)event.motion.x / 640.0f) * 3.554f) - 1.777f;

				//event.motion.y is our new y
				//float unitY = (((float)(360-event.motion.y) / 360.0f) * 2.0f) - 1.0f;
			}

			else if (event.type == SDL_MOUSEBUTTONDOWN){
				//event.button.x = click x position
				//event.button.y = click y position
				//event.button.button is the mouse button clicked (1,2,3,etc.)
			}
		}

		const Uint8 *keys = SDL_GetKeyboardState(NULL); //returns an array of keyboard keys, 1 if key is pressed
		
		//entities[0].acceleration_x = 0.0f //sets player motion to 0 on x axis


		//Polling Input
		if (keys[SDL_SCANCODE_LEFT]) {
			//MOVE CHARACTER LEFT/ROTATE LEFT
		}
		else if (keys[SDL_SCANCODE_RIGHT]) {
			//MOVE CHARACTER RIGHT/ROTATE RIGHT
		}

		else if (keys[SDL_SCANCODE_UP]){
			//MOVE CHARACTER UP
		}

		else if (keys[SDL_SCANCODE_DOWN]){
			//MOVE CHARACTER DOWN
		}

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

		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);


		glClear(GL_COLOR_BUFFER_BIT);
		glUseProgram(program.programID);



		//HW2 Pong

		//Left Paddle
		program.setModelMatrix(modelMatrixleftpaddle);

		if (keys[SDL_SCANCODE_W] && leftpaddleY < 0.8f) { leftpaddleY += elapsed; } // Up
		else if (keys[SDL_SCANCODE_S] && leftpaddleY > -0.8f){ leftpaddleY -= elapsed; } // Down

		float leftvertices[] = { 
		    -paddleWidth / 2, -paddleLength / 2,
			-paddleWidth / 2, paddleLength / 2,
			paddleWidth / 2, paddleLength / 2,
			-paddleWidth / 2, -paddleLength / 2,
			paddleWidth / 2, -paddleLength / 2,
			paddleWidth / 2, paddleLength / 2 
			};

		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, leftvertices);
		glEnableVertexAttribArray(program.positionAttribute);


		modelMatrixleftpaddle.identity();
		modelMatrixleftpaddle.Translate(-1.652f, leftpaddleY, 0.0f);

		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);

		//Right Paddle
		program.setModelMatrix(modelMatrixrightpaddle);

		if (keys[SDL_SCANCODE_UP] && rightpaddleY < 0.8f) { rightpaddleY += elapsed; } // Up
		else if (keys[SDL_SCANCODE_DOWN] && rightpaddleY > -0.8f){ rightpaddleY -= elapsed; } // Down

		float rightvertices[] = { 
		    -paddleWidth / 2, -paddleLength / 2, 
			-paddleWidth / 2, paddleLength / 2,
			paddleWidth / 2, paddleLength / 2,
			-paddleWidth / 2, -paddleLength / 2,
			paddleWidth / 2, -paddleLength / 2, 
			paddleWidth / 2, paddleLength / 2 
			};

		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, rightvertices);
		glEnableVertexAttribArray(program.positionAttribute);

		modelMatrixrightpaddle.identity();
		modelMatrixrightpaddle.Translate(1.652f, rightpaddleY, 0.0f);

		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);

		//Ball
		program.setModelMatrix(modelMatrixBall);

		if (keys[SDL_SCANCODE_SPACE] && ballDirectX == 0 && ballDirectY == 0)
		{
			ballDirectX = ballStart();
			ballDirectY = ballStart();
		}

		ballPosiX += elapsed * ballSpeed * ballDirectX;
		ballPosiY += elapsed * ballSpeed * ballDirectY;
		
		if (ballPosiY <= -0.975f) { ballDirectY = 1.0f; } // Ball impacting bottom
		else if (ballPosiY >= 0.975f) { ballDirectY = -1.0f; } // Ball impacting top

		// Ball impacting left paddle
		if (-1.652f + paddleWidth / 2 >= ballPosiX - ballSize / 2 &&
			leftpaddleY - paddleLength / 2 <= ballPosiY - ballSize / 2 &&
			leftpaddleY + paddleLength / 2 >= ballPosiY + ballSize / 2) {
			ballDirectX = 1.0f;
		} 

		// Ball impact right paddle
		if (1.652f - paddleWidth / 2 <= ballPosiX + ballSize / 2 &&
			rightpaddleY - paddleLength / 2 <= ballPosiY - ballSize / 2 &&
			rightpaddleY + paddleLength / 2 >= ballPosiY + ballSize / 2) {
			ballDirectX = -1.0f;
		}

		// Ball impacting goal
		if (ballPosiX >= 1.752f || ballPosiX <= -1.752) {
			ballPosiX = 0.0f; ballPosiY = 0.0f; ballDirectX = 0.0f; ballDirectY = 0.0f;
			leftpaddleY = 0.0f;  rightpaddleY = 0.0f; //Resets the paddles
		} 

		float ballvertices[] = { -ballSize / 2, -ballSize / 2, ballSize / 2, ballSize / 2, -ballSize / 2, ballSize / 2,
			-ballSize / 2, -ballSize / 2, ballSize / 2, -ballSize / 2, ballSize / 2, ballSize / 2 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, ballvertices);
		glEnableVertexAttribArray(program.positionAttribute);

		modelMatrixBall.identity();
		modelMatrixBall.Translate(ballPosiX, ballPosiY, 0.0f);

		glDrawArrays(GL_TRIANGLES, 0, 6);

		glDisableVertexAttribArray(program.positionAttribute);

		//Border?
		program.setModelMatrix(modelMatrixBorder);
		float bottomvertices[] = {
			1.77f, -1.0f, //top right 
			-1.77f, -1.0f, //top left
			-1.77f, -1.0f, //bottom left
			1.77f, -1.0f }; //bottom right
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, bottomvertices);
		glEnableVertexAttribArray(program.positionAttribute);

		float bottomtexcoords[] = { 1.5f, 1.5f, -1.5f, 1.5f, -1.5f, -1.5f, 1.5f, -1.5f };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, bottomtexcoords);
		glEnableVertexAttribArray(program.texCoordAttribute);




		glBindTexture(GL_TEXTURE_2D, blueTextureID);
		glDrawArrays(GL_QUADS, 0, 4);

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);
		

		//HW1 Textures
		/*
		//Blue Box
		modelMatrix.identity();


		program.setModelMatrix(modelMatrix);

		float iconvertices2[] = {
			3.56f, 2.0f, //top right 
			-3.56f, 2.0f, //top left
			-3.56f, -2.0f, //bottom left
			3.56f, -2.0f }; //bottom right
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, iconvertices2);
		glEnableVertexAttribArray(program.positionAttribute);

		float iconTexCoords2[] = { 1.5f, 1.5f, -1.5f, 1.5f, -1.5f, -1.5f, 1.5f, -1.5f };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, iconTexCoords2);
		glEnableVertexAttribArray(program.texCoordAttribute);




		glBindTexture(GL_TEXTURE_2D, blueTextureID);
		glDrawArrays(GL_QUADS, 0, 4);

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		//yellow Box
		modelMatrix.identity();
		modelMatrix.Rotate(-ticks * ticks);

		program.setModelMatrix(modelMatrix);

		float iconvertices1[] = {
			0.0f, 0.0f, //top right 
			-1.0f, 0.0f, //top left
			-1.0f, -1.0f, //bottom left
			0.0f, -1.0f }; //bottom right
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, iconvertices1);
		glEnableVertexAttribArray(program.positionAttribute);

		float iconTexCoords1[] = { 1.5f, 1.5f, -1.5f, 1.5f, -1.5f, -1.5f, 1.5f, -1.5f };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, iconTexCoords1);
		glEnableVertexAttribArray(program.texCoordAttribute);




		glBindTexture(GL_TEXTURE_2D, yellowTextureID);
		glDrawArrays(GL_QUADS, 0, 4);

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);



		//Red Box
		modelMatrix.identity();
		modelMatrix.Rotate(ticks);

		program.setModelMatrix(modelMatrix);

		float iconvertices3[] = { 
			0.5f, 0.5f, //top right 
			-0.5f, 0.5f, //top left
			-0.5f, -0.5f, //bottom left
			0.5f, -0.5f }; //bottom right
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, iconvertices3);
		glEnableVertexAttribArray(program.positionAttribute);

		float iconTexCoords3[] = { 1.5f, 1.5f, -1.5f, 1.5f, -1.5f, -1.5f, 1.5f, -1.5f};
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, iconTexCoords3);
		glEnableVertexAttribArray(program.texCoordAttribute);




		glBindTexture(GL_TEXTURE_2D, redTextureID);
		glDrawArrays(GL_QUADS, 0, 4);

		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);
		*/
		
		//HW1 No Textures
		/* //HW1 no textures
		modelMatrix.identity();
		modelMatrix.Rotate(ticks);
		program.setModelMatrix(modelMatrix);

		//Triangle Declaration
		float vertices[] = {
			-0.5f, -0.5f, //Bottom Left
			0.5f, -0.5f, // bottom right
			0.5f, 0.5f, //Top right
			-0.5f, 0.5f //top left
		};
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
		glEnableVertexAttribArray(program.positionAttribute);

		//Triangle Drawing
		glDrawArrays(GL_QUADS, 0, 4);

		glDisableVertexAttribArray(program.positionAttribute);

		modelMatrix.identity();
		modelMatrix.Rotate(-ticks);
		program.setModelMatrix(modelMatrix);

		//Triangle Declaration
		float vertices2[] = {
			-0.5f, -0.5f, //Bottom Left
			-1.5f, -1.5f, // bottom right
			-1.5f, 0.5f, //Top right
			-0.5f, 0.5f //top left
		};
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);
		glEnableVertexAttribArray(program.positionAttribute);

		//Triangle Drawing
		glDrawArrays(GL_QUADS, 0, 4);

		glDisableVertexAttribArray(program.positionAttribute);

		modelMatrix.identity();
		modelMatrix.Rotate((ticks * ticks));
		program.setModelMatrix(modelMatrix);

		//Triangle Declaration
		float vertices3[] = {
			0.5f, 0.0f, //Bottom Left
			1.5f, 0.0f, // bottom right
			1.5f, 1.5f, //Top right
			1.0f, 1.5f //top left
		};
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);
		glEnableVertexAttribArray(program.positionAttribute);

		//Triangle Drawing
		glDrawArrays(GL_QUADS, 0, 4);

		glDisableVertexAttribArray(program.positionAttribute);

		*/

		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #21
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("Marcus Piazzola Scene", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	#ifdef _WINDOWS
		glewInit();
	#endif

	SDL_Event event;
	bool done = false;

	glViewport(0, 0, 640, 360);
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	GLuint text1 = LoadTexture("cardSpadesA.png");
	GLuint text2 = LoadTexture("cardHeartsA.png");
	GLuint text3 = LoadTexture("cardDiamondsA.png");

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;
	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	float lastFrameTicks = 0.0f;
	glUseProgram(program.programID);

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

		glClear(GL_COLOR_BUFFER_BIT);
		

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

		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		program.setModelMatrix(modelMatrix);

		//Card 1 (Ace of Hearts)
		float vertices1[] = { -2.0, -0.5, -1.0, -0.5, -1.0, 0.5, -2.0, -0.5, -1.0, 0.5, -2.0, 0.5 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices1);
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glBindTexture(GL_TEXTURE_2D, text2);
		glDrawArrays(GL_TRIANGLES, 0, 6);	
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);


		//Card 2 (Ace of Spades)
		modelMatrix.Rotate(lastFrameTicks);
		program.setModelMatrix(modelMatrix);
		modelMatrix.identity();
		float vertices2[] = { -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords2[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords2);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glBindTexture(GL_TEXTURE_2D, text1);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);


		//Card 3 (Ace of Diamonds)
		program.setModelMatrix(modelMatrix);
		float vertices3[] = { 1.0, -0.5, 2.0, -0.5, 2.0, 0.5, 1.0, -0.5, 2.0, 0.5, 1.0, 0.5 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);	
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords3[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords3);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glBindTexture(GL_TEXTURE_2D, text3);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		SDL_GL_SwapWindow(displayWindow);

	}

	SDL_Quit();
	return 0;
}
Example #22
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("HW5", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
	glewInit();
#endif
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glClearColor(0.0f, 0.3f, 0.5f, 1.0f);

	SDL_Event event;
	bool done = false;


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

	Matrix modelMatrix;
	Matrix modelMatrixc;
	Matrix viewMatrix;
	Matrix startMenu;

	Matrix projectionMatrix;
	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);

	Entity toilet;
	toilet.player();
	vector<Entity> entities;
	Entity block1;
	block1.blocks(0.0f, -1.5f);
	Entity block2;
	block2.blocks(0.5f, -1.5f);
	Entity block3;
	block3.blocks(1.0f, -1.5f);
	entities.push_back(block1);
	entities.push_back(block2);
	entities.push_back(block3);
	float friction_x = .5f;
	float friction_y = .1f;
	//float verticesP1[] = { -3.5f, -1.0f, -2.0f, -1.0f, -2.0f, 0.25f, -3.5f, -1.0f, -2.0f, 0.25f, -3.5f, 0.25f };
	float verticesP1[] = { -1.5f, -1.0f, 0.0f, -1.0f, 0.0f, 0.25f, -1.5f, -1.0f, 0.0f, 0.25f, -1.5f, 0.25f };
	float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
	//int winner = 0;
	const int runAnimation[] = { 0, 1, 2, 3, 4, 5, 6 };
	//{3,4,5, 15,16,17, 27,28,29, 39,40,41}
	//{6,7,8, 18,19,20, 30,31,32, 42,43,45}
	const int numFrames = 7;
	float animationElapsed = 0.0f;
	float framesPerSecond = 30.0f;
	int currentIndex = 0;
	float lastFrameTicks = 0.0f;
	const Uint8 *keys = SDL_GetKeyboardState(NULL);
	std::vector<float> vertexData;
	std::vector<float> texCoordData;         
	for (int y = 0; y < LEVEL_HEIGHT; y++) {
		for (int x = 0; x < LEVEL_WIDTH; x++) { 
			if (level1Data[y][x] == 6) {          
				//if 6, put block
				Entity block1;
				block1.blocks(y * BLOCK_LENGTH, x * BLOCK_LENGTH);
				entities.push_back(block1);
			} 
		} 
	}
	
	while (!done) {
		glClear(GL_COLOR_BUFFER_BIT);

		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

		//glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, verticesP1);
		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		//glBindTexture(GL_TEXTURE_2D, kyoukoIDLE);
		//startMenu.setScale(4, 4, 0);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		float ticks = (float)SDL_GetTicks() / 1000;
		float elapsed = ticks - lastFrameTicks;
		lastFrameTicks = ticks;

		float fixedElapsed = elapsed;
		if (fixedElapsed > FIXED_TIMESTEP * MAX_TIMESTEPS) {
			fixedElapsed = FIXED_TIMESTEP * MAX_TIMESTEPS;
		}
		while (fixedElapsed >= FIXED_TIMESTEP) {
			fixedElapsed -= FIXED_TIMESTEP;
		}

		toilet.velocity_x += toilet.acceleration_x *FIXED_TIMESTEP;
		toilet.velocity_y -= .01f * elapsed;
		toilet.x += toilet.velocity_x * FIXED_TIMESTEP;
		toilet.y += toilet.velocity_y *FIXED_TIMESTEP;
		
		toilet.matrix.Translate(toilet.velocity_x* FIXED_TIMESTEP, toilet.velocity_x* FIXED_TIMESTEP, 0.0f);

		for (Entity e : entities){
			e.updateImage(texCoords, &program);
		};


		//DrawSpriteSheetSprite(&program, runAnimation[currentIndex], 7, 1, verticesP1, toilet);
		viewMatrix.setPosition(-toilet.x, -toilet.y, 0);

		if (keys[SDL_SCANCODE_A]) {
			animationElapsed += fixedElapsed;
			if (animationElapsed > 1.0 / framesPerSecond) {
				currentIndex++;
				animationElapsed = 0.0;
				if (currentIndex > numFrames - 1) {
					currentIndex = 0;
				}
			}
			toilet.x -= framesPerSecond * fixedElapsed / 5;
			toilet.updateImage(&program, runAnimation[currentIndex], entities);
		}
		else if (keys[SDL_SCANCODE_D]) {
			animationElapsed += fixedElapsed;
			if (animationElapsed > 1.0 / framesPerSecond) {
				currentIndex++;
				animationElapsed = 0.0;
				if (currentIndex > numFrames - 1) {
					currentIndex = 0;
				}
			}
			toilet.x += framesPerSecond * fixedElapsed / 5;
			toilet.updateImage(&program, runAnimation[currentIndex], entities);
		}
		else{
			currentIndex = 4;
			toilet.updateImage(&program, runAnimation[currentIndex], entities);
		}
		//else if (keys[SDL_SCANCODE_D]) {
		//	if (toilet.x + (framesPerSecond * fixedElapsed) < 1.50f){
		//		toilet.x += framesPerSecond * fixedElapsed;
		//		//	magikarp.matrix.Translate(0, -framesPerSecond * elapsed, 0);
		//	}
		//}

		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
				done = true;
			}
			/*
			if (keys[SDL_SCANCODE_W] && toilet.collideBottom){
				toilet.y += framesPerSecond * FIXED_TIMESTEP;
				toilet.updateImage(&program, runAnimation[currentIndex], entities);
			}*/
			if (keys[SDL_SCANCODE_W]){
				toilet.y += framesPerSecond * FIXED_TIMESTEP;
				toilet.updateImage(&program, runAnimation[currentIndex], entities);
			}
		}
		//	glClear(GL_COLOR_BUFFER_BIT);
		SDL_GL_SwapWindow(displayWindow);
	}
	SDL_Quit();
	return 0;
}
Example #23
0
void PlayerInput(bool& done, ShaderProgram program){
    SDL_Event event;
    
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
            done = true;
        }
        
        else if (event.type == SDL_KEYDOWN){
            if (state == STATE_MAIN_MENU) {
                if (event.key.keysym.scancode == SDL_SCANCODE_SPACE){
                    state = STATE_GAME_LEVEL;
                    startGame();
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE){
                    done = true;
                }
            }
            else{
                if (event.key.keysym.scancode == SDL_SCANCODE_LEFT){
                    if (PLAYER_ENT.godmode) {
                        PLAYER_ENT.acceleration_x += -2.2;
                    }
                    else{
                        if (false && PLAYER_ENT.leftContact) {
                            puts("not lefting");//do nothing
                        }
                        else{
                            PLAYER_ENT.acceleration_x = -2.2;
                            if (PLAYER_ENT.rightContact) {
                                PLAYER_ENT.rightContact = false;
                            }
                        }
                        
                    }
                }
                
                else if (event.key.keysym.scancode == SDL_SCANCODE_RIGHT){
                    if (PLAYER_ENT.godmode) {
                        PLAYER_ENT.acceleration_x += 2.2;
                    }
                    else{
                        if (false && PLAYER_ENT.rightContact) {
                            puts("not righting");//do nothing
                        }
                        else{
                            PLAYER_ENT.acceleration_x = 2.2;
                            if (PLAYER_ENT.leftContact) {
                                PLAYER_ENT.leftContact = false;
                            }
                        }
                        
                    }
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_W){
                    shootBullet(0);
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_D){
                    shootBullet(1);
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_A){
                    shootBullet(2);
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_S){
                    shootBullet(3);
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_J){//jetpack TODO: remove later
                    if (state != STATE_MAIN_MENU) {
                        PLAYER_ENT.velocity_y += 3.0f;
                    }
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_I){//toggle immunity to gravity TODO: remove later
                    if (state != STATE_MAIN_MENU) {
                        PLAYER_ENT.gravity = !PLAYER_ENT.gravity;
                    }
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_G){
                    PLAYER_ENT.godmode = !PLAYER_ENT.godmode;
                    
                }
                
                else if (event.key.keysym.scancode == SDL_SCANCODE_Z){
                    zoom += 0.5;
                    projectionMatrix.setOrthoProjection(zoom*-1.7777f, zoom*1.77777f, zoom*-1.0f, zoom*1.0f, zoom*-1.0f, zoom*1.0f);
                    program.setModelMatrix(modelMatrix);
                    program.setProjectionMatrix(projectionMatrix);
                    program.setViewMatrix(viewMatrix);
                    glUseProgram(program.programID);
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_X){
                    zoom -= 0.5;
                    projectionMatrix.setOrthoProjection(zoom*-1.7777f, zoom*1.77777f, zoom*-1.0f, zoom*1.0f, zoom*-1.0f, zoom*1.0f);
                    program.setModelMatrix(modelMatrix);
                    program.setProjectionMatrix(projectionMatrix);
                    program.setViewMatrix(viewMatrix);
                    glUseProgram(program.programID);
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_P){
                    peaceful = !peaceful;
                }
                
                
                else if (event.key.keysym.scancode == SDL_SCANCODE_UP){
                        if (PLAYER_ENT.godmode) {
                            PLAYER_ENT.acceleration_y += 2.2;
                        }
                        else{
                            if (PLAYER_ENT.bottomContact) {
                                PLAYER_ENT.velocity_y += 3.0f;
                                PLAYER_ENT.bottomContact = false;
                            }
                            else if (PLAYER_ENT.leftContact) {
                                PLAYER_ENT.velocity_y += 3.0f;
                                PLAYER_ENT.velocity_x += 1.7f;
                                PLAYER_ENT.leftContact = false;
                                PLAYER_ENT.rightContact = false;
                            }
                            else if (PLAYER_ENT.rightContact) {
                                PLAYER_ENT.velocity_y += 3.0f;
                                PLAYER_ENT.velocity_x -= 1.7f;
                                PLAYER_ENT.rightContact = false;
                                PLAYER_ENT.leftContact = false;
                            }
                        }
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_DOWN){
                    if (PLAYER_ENT.godmode) {
                        PLAYER_ENT.acceleration_y += -2.2;
                    }
                }
                else if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE){
                    done = true;
                }
            }

            }
        else if (event.type == SDL_KEYUP){
            if (event.key.keysym.scancode == SDL_SCANCODE_LEFT){
                PLAYER_ENT.acceleration_x = 0;
            }
            
            else if (event.key.keysym.scancode == SDL_SCANCODE_RIGHT){
                PLAYER_ENT.acceleration_x = 0;
            }
            
        }

    }
}
Example #24
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
	glewInit();
#endif
	float lastFrameTicks = 0.0f;
	glViewport(0, 0, 640, 360);
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	Matrix projectionMatrix;
	Matrix modelMatrix;
	modelMatrix.Rotate(180 * (3.1415926 / 180.0));
	size_t angle = 0;
	Matrix viewMatrix;
	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	SDL_Event event;
	bool done = false;
	glClearColor(0.4f, 0.2f, 0.8f, 1.0f);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
				done = true;
			}
		}
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		glUseProgram(program.programID);

		glClear(GL_COLOR_BUFFER_BIT);
		
		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		float vertices[] = { 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f};
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, vertices);
		LoadTexture("smiley1.png");
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.texCoordAttribute);
		glDisableVertexAttribArray(program.positionAttribute);
		
		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		float vertices2[] = { 2.0f, 2.0f, 1.0f, 1.0f, 2.0f, 1.0f, 2.0f, 2.0f, 1.0f, 2.0f, 1.0f, 1.0f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, vertices2);
		LoadTexture("smiley2.png");
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.texCoordAttribute);
		glDisableVertexAttribArray(program.positionAttribute);
		
		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		float vertices3[] = { 3.0f, 1.0f, 2.0f, 0.0f, 3.0f, 0.0f, 3.0f, 1.0f, 2.0f, 1.0f, 2.0f, 0.0f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, vertices3);
		LoadTexture("smiley3.png");
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.texCoordAttribute);
		glDisableVertexAttribArray(program.positionAttribute);

		float ticks = (float)SDL_GetTicks() / 100.0f;
		float elapsed = ticks - lastFrameTicks;
		lastFrameTicks = ticks;
		angle += elapsed;
		modelMatrix.Rotate(angle * .1);
		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #25
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	#ifdef _WINDOWS
		glewInit();
	#endif

	glViewport(0, 0, 640, 360);
	ShaderProgram program(RESOURCE_FOLDER"vertex.glsl", RESOURCE_FOLDER"fragment.glsl");
	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;
	projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);
	float lastFrameTicks = 0.0f;
	float distance = 1.0f;
	const Uint8 *keys = SDL_GetKeyboardState(NULL);
	float ypos1 = 0.4f;
	float ypos2 = 0.4f;
	float ybp = 0.05f;
	float xbp = 0.05f;
	srand(time(NULL));
	int angle = getAngle();

	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;

		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		glUseProgram(program.programID);
		glClear(GL_COLOR_BUFFER_BIT);

#pragma region draw centerline, topline, bottomline
		glEnableVertexAttribArray(program.positionAttribute);
		float topbottom[] = { 3.5f, 2.0f, -3.5f, 2.0f, -3.5f, 1.9f, -3.5f, 1.9f, 3.5f, 1.9f, 3.5f, 2.0f, 
			3.5f, -2.0f, -3.5f, -2.0f, -3.5f, -1.9f, -3.5f, -1.9f, 3.5f, -1.9f, 3.5f, -2.0f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, topbottom);
		glDrawArrays(GL_TRIANGLES, 0, 12);
		glDisableVertexAttribArray(program.positionAttribute);

		glEnableVertexAttribArray(program.positionAttribute);
		float centerline[] = { .01f, 2.0f, -.01f, 2.0f, -.01f, -2.0f, -.01f, -2.0f, .01f, -2.0f, .01f, 2.0f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, centerline);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
#pragma endregion

		glEnableVertexAttribArray(program.positionAttribute);
		float p1[] = { -3.4f, ypos1, -3.4f, (ypos1 - 0.8f), -3.3f, (ypos1 - 0.8f), -3.3f, (ypos1 - 0.8f), -3.3f, ypos1, -3.4f, ypos1 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, p1);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);

		glEnableVertexAttribArray(program.positionAttribute);
		float p2[] = { 3.4f, ypos2, 3.4f, (ypos2 - 0.8f), 3.3f, (ypos2 - 0.8f), 3.3f, (ypos2 - 0.8f), 3.3f, ypos2, 3.4f, ypos2 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, p2);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);

		glEnableVertexAttribArray(program.positionAttribute);
		float ball[] = {xbp, ybp, xbp - .1, ybp, xbp - .1, ybp - .1, xbp, ybp, xbp - .1, ybp - .1, xbp, ybp - .1};
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, ball);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);

		if (keys[SDL_SCANCODE_W]){
			if (ypos1 < 2) {ypos1 += elapsed * distance;}
		}
		else if (keys[SDL_SCANCODE_S]){
			if ((ypos1 - 0.8) > -2){ypos1 -= elapsed * distance;}
		}

		if (keys[SDL_SCANCODE_UP]){
			if (ypos2 < 2){ ypos2 += elapsed * distance; }
		}
		else if (keys[SDL_SCANCODE_DOWN]){
			if ((ypos2 - 0.8) > -2){ ypos2 -= elapsed *	distance; }
		}

		xbp += cos(angle * RAD) * elapsed * distance * 2;
		ybp += sin(angle * RAD) * elapsed * distance * 2;

		if ((xbp - .1) <= -3.5){
			xbp = .05f;
			ybp = .05f;
			angle = getAngle();
		}
		if (xbp >= 3.5){
			xbp = .05f;
			ybp = .05f;
			angle = getAngle();
		}
		if (ybp >= 1.88){
			if (angle > 90){
				angle += 90;
			}
			else{
				angle -= 90;
			}
			ybp = 1.87;
		}
		if ((ybp - .1) <= -1.88){
			if (angle < 270){
				angle -= 90;
			}
			else{
				angle += 90;
			}
			ybp = -1.77;
		}

		if (xbp <= -3.2){
			if (ybp >= (ypos1 - .8) && (ybp - .1) <= ypos1){
				if (angle < 180){ angle -= 90; }
				else{ angle += 90; }
				xbp = -3.19;
			}
		}

		if (xbp >= 3.3){
			if (ybp >= (ypos2 - .8) && (ybp - .1) <= ypos2){
				if (angle < 45){ angle += 270; }
				else{ angle -= 90; }
				xbp = 3.29;
			}
		}

		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #26
0
int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif
    
    SDL_Event event;
    bool done = false;
    
    glViewport(0, 0, 640, 360);
    
    ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
    
    GLuint p1Texture = LoadTexture("p1_front.png");
    GLuint shipTexture = LoadTexture("enemyRed1.png");
    GLuint tileTexture = LoadTexture("tileStone_full.png");
    
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    projectionMatrix.setOrthoProjection(-320.0f, 320.0f, -180.0f, 180.0f, -1.0f, 1.0f);
    
    glUseProgram(program.programID);
    
    while (!done) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
                done = true;
            }
        }
        
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        
        program.setModelMatrix(shipMatrix);
        program.setProjectionMatrix(projectionMatrix);
        program.setViewMatrix(shipView);
        
        glBindTexture(GL_TEXTURE_2D, shipTexture);
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, ship_vertices);
        glEnableVertexAttribArray(program.positionAttribute);
        
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, ship_texCoords);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        moveShip(6.0);
        
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        glBindTexture(GL_TEXTURE_2D, tileTexture);
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, tile_vertices);
        glEnableVertexAttribArray(program.positionAttribute);
        
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, tile_texCoords);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        glBindTexture(GL_TEXTURE_2D, p1Texture);
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, p1_vertices);
        glEnableVertexAttribArray(program.positionAttribute);
        
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, p1_texCoords);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);
        SDL_GL_SwapWindow(displayWindow);
    }
    
    SDL_Quit();
    return 0;
}
Example #27
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
	glewInit();
#endif

	glViewport(0, 0, 640, 360);
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	glUseProgram(program.programID);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	Matrix projectionMatrix;
	Matrix modelMatrixAkari;
	Matrix modelMatrixLeft;
	Matrix modelMatrixRight;
	Matrix modelMatrixBall;
	Matrix modelMatrixText;
	Matrix viewMatrix;

	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);

	GLuint akariTex = LoadTexture("akari2.png");
	GLuint playerTex = LoadTexture("blue.png");
	GLuint ballTex = LoadTexture("ballBlue.png");
	GLuint font = LoadTexture("font1.png");

	float akari[] = { -2.75f, -2.0f, 2.75f, -2.0f, 2.75f, 2.0f, -2.75f, -2.0f, 2.75f, 2.0f, -2.75f, 2.0f };
	float left[] = { -4.0f, -0.5f, -3.35f, -0.5f, -3.35f, 0.5f, -4.0f, -0.5f, -3.35f, 0.5f, -4.0f, 0.5f };
	float right[] = { 3.35f, -0.5f, 4.0f, -0.5f, 4.0f, 0.5f, 3.35f, -0.5f, 4.0f, 0.5f, 3.35f, 0.5f };
	float ball[] = { -0.1f, -0.1f, 0.1f, -0.1f, 0.1f, 0.1f, -0.1f, -0.1f, 0.1f, 0.1f, -0.1f, 0.1f };
	float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };

	float lastFrameTicks = 0.0f;

	Player rightPlayer(3.675f);
	Player leftPlayer(-3.675f);
	Ball b;

	SDL_Event event;
	bool done = false;
	while (!done) {
		glClearColor(0.9f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);

		//Akari
		program.setModelMatrix(modelMatrixAkari);
		glBindTexture(GL_TEXTURE_2D, akariTex);
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, akari);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		/*modelMatrixAkari.Translate(2.75f, 2.0f, 0.0f);
		modelMatrixAkari.Rotate(30*3.1415/180);
		modelMatrixAkari.Translate(2.75f, -2.0f, 0.0f);*/

		//Left
		program.setModelMatrix(modelMatrixLeft);
		glBindTexture(GL_TEXTURE_2D, playerTex);
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, left);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		//Right
		program.setModelMatrix(modelMatrixRight);
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, right);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		//Ball
		program.setModelMatrix(modelMatrixBall);
		glBindTexture(GL_TEXTURE_2D, ballTex);
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, ball);
		glDrawArrays(GL_TRIANGLES, 0, 6);

		//glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

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

		b.xDist = b.xDir * elapsed * b.speed;
		b.yDist = b.yDir * elapsed * b.speed;
		b.x += b.xDist;
		b.y += b.yDist;

		//If ball hits rightPlayer
		if (b.x > rightPlayer.x - rightPlayer.width / 2 - b.width / 2 &&
			b.y < rightPlayer.y + rightPlayer.height / 2 + b.height / 2 &&
			b.y > rightPlayer.y - rightPlayer.height / 2 - b.height / 2) {
			b.xDir = -1.0f; //reverse direction
			b.xDist = b.xDir * elapsed * b.speed;
		}

		//If ball hits leftPlayer
		if (b.x < leftPlayer.x + leftPlayer.width / 2 + b.width / 2 &&
			b.y < leftPlayer.y + leftPlayer.height / 2 + b.height / 2 &&
			b.y > leftPlayer.y - leftPlayer.height / 2 - b.height / 2) {
			b.xDir = 1.0f; //reverse direction
			b.xDist = b.xDir * elapsed * b.speed;
		}

		//If ball hits top
		if (b.y > 2.0f - b.height / 2) {
			b.yDir = -1.0f;
			b.yDist = b.yDir * elapsed * b.speed;
		}

		//If ball hits bottom
		if (b.y < -2.0f + b.height / 2) {
			b.yDir = 1.0f;
			b.yDist = b.yDir * elapsed * b.speed;
		}

		//If ball flies out right
		if (b.x > 3.55f + b.width / 2) {
			b.speed = 0.0f;
			//Hide player bars
			modelMatrixRight.Translate(2.0f, 0.0f, 0.0f);
			modelMatrixLeft.Translate(-2.0f, 0.0f, 0.0f);
			program.setModelMatrix(modelMatrixText);
			DrawText(&program, font, "Left Side Wins", 0.4f, 0.02f);
		}

		//If ball flies out left
		if (b.x < -3.55f - b.width / 2) {
			b.speed = 0.0f;
			//Hide player bars
			modelMatrixRight.Translate(2.0f, 0.0f, 0.0f);
			modelMatrixLeft.Translate(-2.0f, 0.0f, 0.0f);
			program.setModelMatrix(modelMatrixText);
			DrawText(&program, font, "Right Side Wins", 0.4f, 0.02f);
		}

		modelMatrixBall.Translate(b.xDist, b.yDist, 0.0f);

		const Uint8* keys = SDL_GetKeyboardState(NULL);


		//Keyboard polling

		//playerRight Up
		if (keys[SDL_SCANCODE_UP]) {
			//Prevent player bar from going out of view
			if (rightPlayer.y + rightPlayer.height / 2 < 2.0f) {
				rightPlayer.yDist += elapsed*rightPlayer.speed;
				rightPlayer.y += rightPlayer.yDist;
				modelMatrixRight.Translate(0.0f, rightPlayer.yDist, 0.0f);
			}
		}

		//playerRight Down
		if (keys[SDL_SCANCODE_DOWN]) {
			//Prevent player bar from going out of view
			if (rightPlayer.y - rightPlayer.height / 2 > -2.0f) {
				rightPlayer.yDist -= elapsed*rightPlayer.speed;
				rightPlayer.y += rightPlayer.yDist;
				modelMatrixRight.Translate(0.0f, rightPlayer.yDist, 0.0f);
			}
		}

		//playerLeft Up
		if (keys[SDL_SCANCODE_W]) {
			//Prevent player bar from going out of view
			if (leftPlayer.y + leftPlayer.height / 2 < 2.0f) {
				leftPlayer.yDist += elapsed*leftPlayer.speed;
				leftPlayer.y += leftPlayer.yDist;
				modelMatrixLeft.Translate(0.0f, leftPlayer.yDist, 0.0f);
			}

			/*leftPlayer.yDist += elapsed / 10.0f;
			leftPlayer.y += leftPlayer.yDist;
			if (leftPlayer.y > 2.0f - leftPlayer.height / 2) {
			leftPlayer.y = leftPlayer.height / 2;
			}
			else {

			modelMatrixLeft.Translate(0.0f, leftPlayer.yDist, 0.0f);
			}*/
		}

		//playerLeft Down
		if (keys[SDL_SCANCODE_S]) {
			//Prevent player bar from going out of view
			if (leftPlayer.y - leftPlayer.height / 2 > -2.0f) {
				leftPlayer.yDist -= elapsed*leftPlayer.speed;
				leftPlayer.y += leftPlayer.yDist;
				modelMatrixLeft.Translate(0.0f, leftPlayer.yDist, 0.0f);
			}
			/*leftPlayer.yDist -= elapsed / 10.0f;
			leftPlayer.y += leftPlayer.yDist;
			if (leftPlayer.y < -2.0f + leftPlayer.height / 2) {
			leftPlayer.y = leftPlayer.height / 2;
			}
			else {

			modelMatrixLeft.Translate(0.0f, leftPlayer.yDist, 0.0f);
			}*/
		}


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

			else if (event.type == SDL_KEYUP) {
				//Reset back to 0 so yDist won't be a huge number since it is constantly being added by elapsed time
				rightPlayer.yDist = 0;
				leftPlayer.yDist = 0;
			}
		}

		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #28
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("Platformer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
	glewInit();
#endif
	SDL_Event event;
	glViewport(0, 0, 640, 360);
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");

	GLuint playerTexture = LoadTexture("player.png");
	GLuint spriteSheetTexture = LoadTexture("sheet.png");

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;

	SheetSprite playerSprite(&program, playerTexture, 0, 0, 1, 1, 0.3);
	Entity player(playerSprite, 1.0, -11.5, .5, .4, 0, 0, 0, 0);

	projectionMatrix.setOrthoProjection(-2.55f, 2.55f, -1.5f, 1.5f, -1.0f, 1.0f);
	program.setProjectionMatrix(projectionMatrix);

	readFile();

	glUseProgram(program.programID);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

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

		//draw level
		modelMatrix.identity();
		program.setModelMatrix(modelMatrix);
		glUseProgram(program.programID);
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertexData.data());
		glEnableVertexAttribArray(program.positionAttribute);
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoordData.data());
		glEnableVertexAttribArray(program.texCoordAttribute);
		glBindTexture(GL_TEXTURE_2D, spriteSheetTexture);
		glDrawArrays(GL_TRIANGLES, 0, tileCount * 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		//update and draw player
		updatePlayer(&player);
		checkPlayer(&player);
		player.Render(&program);
		
		//camera following player
		viewMatrix.identity();
		viewMatrix.Translate(-player.x, -player.y, 0);
		program.setViewMatrix(viewMatrix);

		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
Example #29
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	#ifdef _WINDOWS
		glewInit();
	#endif

	SDL_Event event;
	bool done = false;

	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	GLuint blank = LoadTexture("blank.png");

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;
	projectionMatrix.setOrthoProjection(-1.7777f, 1.7777f, -1.0f, 1.0f, -1.0f, 1.0f);
	float portWidth = 640;
	float portHeight = 320;

	float lastFrameTicks = 0.0f;
	float ballPosx = .0f;
	float ballPosy = 0.0f;
	float ballAngle = 180.0f;
	float ballSpeed = 2.0f;
	float paddlePos1 = 0.0f;
	float paddlePos2 = 0.0f;
	float paddleSpeed = 0.0015f;
	float server = 0.0f;


	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
				done = true;
			}
		}
		const Uint8 *keys = SDL_GetKeyboardState(NULL); 
//player1 input
		if (keys[SDL_SCANCODE_W]){
			paddlePos1 += paddleSpeed;
			if (paddlePos1 + 0.2 >= 1)
				paddlePos1 = 1 - 0.2;
		}
		else if (keys[SDL_SCANCODE_S]){
			paddlePos1 += -1 * paddleSpeed;
			if (paddlePos1 - 0.2 <= -1)
			paddlePos1 = -1 + 0.2;
		}
		
//player2 input		
		if (keys[SDL_SCANCODE_O]){
			paddlePos2 += paddleSpeed;
			if (paddlePos2 + 0.2 >= 1)
				paddlePos2 = 1 - 0.2;
		}
		else if (keys[SDL_SCANCODE_L]){
			paddlePos2 += -1 * paddleSpeed;
			if (paddlePos2 - 0.2 <= -1)
				paddlePos2 = -1 + 0.2;
		}

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

		if (abs(ballAngle) - 90 <= 20 && abs(ballAngle) -90 >= -20)
			ballSpeed = 4;
		else
			ballSpeed = 2;

		float dist = elapsed*ballSpeed;
		ballPosx += cos(ballAngle * PI / 180)*dist;
		ballPosy += sin(ballAngle * PI / 180)*dist;		



//collison test
		if (ballPosy >= 1){
			ballAngle = -1 * ballAngle;
			ballPosy = 1;
		}
		else if (ballPosy <= -1){
			ballAngle = -1 * ballAngle;
			ballPosy = -1;
		}

		if (ballPosx <= -1.5 && ballPosx >= -1.6 && ballPosy <= paddlePos1 + 0.2 && ballPosy >= paddlePos1 - 0.2){
			if (ballPosy > paddlePos1 + 0.1 || ballPosy < paddlePos1 - 0.1){
				if (ballAngle > 0)
					ballAngle = 180 - ballAngle + 20;
				else
					ballAngle = -180 - ballAngle - 20;
			}
			else if (ballPosy > paddlePos1 + 0.175 || ballPosy < paddlePos1 - 0.175){
				if (ballAngle > 0)
					ballAngle = 180 - ballAngle + 15;
				else
					ballAngle = -180 - ballAngle - 15;
			}
			else {
				if (ballAngle > 0)
					ballAngle = 180 - ballAngle;
				else
					ballAngle = -180 - ballAngle;
			}
			ballPosx = -1.5;
		}
		else if (ballPosx >= 1.5 && ballPosx <= 1.6 && ballPosy <= paddlePos2 + 0.2 && ballPosy >= paddlePos2 - 0.2){
			if (ballPosy > paddlePos2 + 0.1 || ballPosy < paddlePos2 - 0.1){
				if (ballAngle > 0)
					ballAngle = 180 - ballAngle - 20;
				else
					ballAngle = -180 - ballAngle + 20;
			}
			else if (ballPosy > paddlePos2 + 0.175 || ballPosy < paddlePos2 - 0.175){
				if (ballAngle > 0)
					ballAngle = 180 - ballAngle - 15;
				else
					ballAngle = -180 - ballAngle + 15;
			}
			else {
				if (ballAngle > 0)
					ballAngle = 180 - ballAngle;
				else
					ballAngle = -180 - ballAngle;
			}
			ballPosx = 1.5;
		}

//victory condition test
		if (ballPosx <= -1.8){
			ballPosx = 0;
			ballPosy = 0;
			ballAngle = 0;
			paddlePos1 = 0;
			paddlePos2 = 0;
			server = 1;
		}
		else if (ballPosx >= 1.8){
			ballPosx = 0;
			ballPosy = 0;
			ballAngle = 180;
			paddlePos1 = 0;
			paddlePos2 = 0;
			server = -1;
		}


//render
		glViewport(0, 0, portWidth, portHeight);
		float verticesBall[] = { -0.03f, -0.03f, 
								0.03f, 0.03f, 
								-0.03f, 0.03f, 
								0.03f, 0.03f, 
								-0.03f, -0.03f, 
								0.03f, -0.03f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, verticesBall);
		glEnableVertexAttribArray(program.positionAttribute);

		float texCoordsBall[] = { 0.0, 1.0f, 
								1.0f, 0.0f, 
								0.0f, 0.0f, 
								1.0, 0.0, 
								0.0, 1.0, 
								1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoordsBall);
		glEnableVertexAttribArray(program.texCoordAttribute);

		glBindTexture(GL_TEXTURE_2D, blank);

		modelMatrix.identity();
		modelMatrix.Translate(ballPosx,ballPosy,0.0);
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		float verticesPad1[] = { -0.1f, -0.2f,
			0.0f, 0.2f,
			-0.1f, 0.2f,
			0.0f, 0.2f,
			-0.1f, -0.2f,
			0.0f, -0.2f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, verticesPad1);
		glEnableVertexAttribArray(program.positionAttribute);

		float texCoordsPad1[] = { 0.0, 1.0f,
			1.0f, 0.0f,
			0.0f, 0.0f,
			1.0, 0.0,
			0.0, 1.0,
			1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoordsPad1);
		glEnableVertexAttribArray(program.texCoordAttribute);


		modelMatrix.identity();
		modelMatrix.Translate(-1.5, paddlePos1, 0.0);
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		float verticesPad2[] = { 0.1f, -0.2f,
			0.0f, 0.2f,
			0.1f, 0.2f,
			0.0f, 0.2f,
			0.1f, -0.2f,
			0.0f, -0.2f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, verticesPad2);
		glEnableVertexAttribArray(program.positionAttribute);

		float texCoordsPad2[] = { 0.0, 1.0f,
			1.0f, 0.0f,
			0.0f, 0.0f,
			1.0, 0.0,
			0.0, 1.0,
			1.0, 1.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoordsPad2);
		glEnableVertexAttribArray(program.texCoordAttribute);
		

		modelMatrix.identity();
		modelMatrix.Translate(1.5, paddlePos2, 0.0);
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);

		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		if (server){
			float verticesfl[] = { -0.025f, 0.0f,
				0.025f, 0.1f,
				-0.025f, 0.1f,
				0.025f, 0.1f,
				-0.025f, 0.0f,
				0.0f, 0.0f };
			glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, verticesfl);
			glEnableVertexAttribArray(program.positionAttribute);

			float texCoordsfl[] = { 0.0, 1.0f,
				1.0f, 0.0f,
				0.0f, 0.0f,
				1.0, 0.0,
				0.0, 1.0,
				1.0, 1.0 };
			glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoordsfl);
			glEnableVertexAttribArray(program.texCoordAttribute);


			modelMatrix.identity();
			modelMatrix.Translate(server, -1, 0.0);
			program.setModelMatrix(modelMatrix);
			program.setProjectionMatrix(projectionMatrix);
			program.setViewMatrix(viewMatrix);

			glDrawArrays(GL_TRIANGLES, 0, 6);
			glDisableVertexAttribArray(program.positionAttribute);
			glDisableVertexAttribArray(program.texCoordAttribute);
		}

		SDL_GL_SwapWindow(displayWindow);
		glClear(GL_COLOR_BUFFER_BIT);

	}


	/*Game app;
	while (!app.UpdateAndRender()) {}*/

	//SDL_Quit();
	return 0;
}
Example #30
0
int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif




    glViewport(0, 0, 640, 360);

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

    Matrix projectionMatrix;
    Matrix modelMatrix;
    Matrix viewMatrix;




    GLuint skeletonTex = loadTexture("skeleton.png");
    GLuint nyanTex = loadTexture("nyan.png");
    GLuint eyeTex = loadTexture("eye.png");

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);



    SDL_Event event;
    bool done = false;
    while (!done) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
                done = true;
            }
            else if (event.type == SDL_KEYDOWN) {
                if (event.key.keysym.scancode == SDL_SCANCODE_SPACE) {
                    angle = 0.0f;
                    std::cout << "Space pressed!" << std::endl;
                }
            }
        }

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


        glClearColor(0.5f, 0.5f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        const Uint8 *keys = SDL_GetKeyboardState(NULL);

        if (keys[SDL_SCANCODE_LEFT]) {
            //go left
            std::cout << "Pressing Left" << std::endl;
            angle += elapsed;
        }
        else if (keys[SDL_SCANCODE_RIGHT]) {
            // go right
            std::cout << "Pressing Right" << std::endl;
            angle -= elapsed;
        }



        projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);
        //modelMatrix.identity();
        modelMatrix.Rotate(elapsed * 20 * (3.1415926f / 180.0f));

        program.setModelMatrix(modelMatrix);
        program.setProjectionMatrix(projectionMatrix);
        program.setViewMatrix(viewMatrix);

        glUseProgram(program.programID);





        //eye
        loadTexture("eye.png");

        float vertices1[] = {-0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f };
        //float vertices2[] = { -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f };

        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices1);
        glEnableVertexAttribArray(program.positionAttribute);

        float texCoords1[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f};
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords1);
        glEnableVertexAttribArray(program.texCoordAttribute);



        glDrawArrays(GL_TRIANGLES, 0, 12);


        //nyan
        loadTexture("nyan.png");

        float vertices3[] = { 1.5f, -0.5f, 2.5f, 0.5f, 1.5f, 0.5f, 1.5f, -0.5f, 2.5f, -0.5f, 2.5f, 0.5f };


        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);
        glEnableVertexAttribArray(program.positionAttribute);

        float texCoords3[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f };

        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords3);
        glEnableVertexAttribArray(program.texCoordAttribute);




        glDrawArrays(GL_TRIANGLES, 0, 12);

        //skeleton
        loadTexture("skeleton.png");

        float vertices5[] = { -1.5f, 0.5f, -1.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -1.5f, 0.5f };
        //float vertices6[] = { -1.5f, 0.5f, -1.5f, -0.5f, -0.5f, 0.5f };

        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices5);
        glEnableVertexAttribArray(program.positionAttribute);

        float texCoords5[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f };

        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords5);
        glEnableVertexAttribArray(program.texCoordAttribute);



        glDrawArrays(GL_TRIANGLES, 0, 12);

        //test

        /*
        glBegin(GL_TRIANGLES);

        glVertex3f(-1.0f, 1.0f, 0.0f);
        glVertex3f(0.0f, 2.0f, 0.0f);
        glVertex3f(1.0f, 2.0f, 0.0f);

        glEnd();
        */




        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);

        SDL_GL_SwapWindow(displayWindow);

    }

    SDL_Quit();
    return 0;
}