main.cpp 6.19 KB

#include <windows.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <../include/GL/freeglut.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include "../include/Input.hpp"
#include "../include/FpsCounter.hpp"
#include "../include/GameManager.hpp"
#include "../include/glm/glm.hpp"
#include <memory>
#include <iostream>
#include "../include/Text.h"

std::shared_ptr<GameManager> gm;
siut::FpsCounter counter;

int window;

bool keyPressed[30];
int mousePosX, mousePosY; 
float moveX, moveY;
Text _text;

bool isDown = false;

void init()
{
	_text.init();

	GLenum err = glewInit();
	if (GLEW_OK != err)
	{
		/* Problem: glewInit failed, something is seriously wrong. */
		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
	}
	
	fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

  glClearColor(0.0, 0.0, 0.0, 0.0);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_DEPTH_TEST);

  counter.start();

  gm.reset(new GameManager());
  gm->init();

  for(int i=0; i<30; i++)
    keyPressed[i]=false;
}


void display()
{
	//glClearColor(1, 1, 0.5, 1);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  gm->update(counter.fps());
  gm->render();

	if (keyPressed[KEY_ID_W] == true)				gm->getSpaceShip()->moveForward();
	if (keyPressed[KEY_ID_A] == true) 
	{
		auto offset = gm->getSpaceShip()->moveLeft();
		gm->getCam()->moveLeft(offset);
		//gm->getSkybox()->moveLeft(offset);
	}
	if (keyPressed[KEY_ID_D] == true)
	{
		auto offset = gm->getSpaceShip()->moveRight();
		gm->getCam()->moveRight(offset);
		//gm->getSkybox()->moveRight(offset);
	}
	if (keyPressed[KEY_ID_S] == true)				gm->getSpaceShip()->moveBackward();
	
	if (keyPressed[KEY_ID_C] == true && !isDown) {
		gm->getSpaceShip()->changeWeapon();
		keyPressed[KEY_ID_C] = false;
		isDown = true;
	}
	
	if (keyPressed[KEY_ID_SPACE] == true)   gm->fireFromSpaceship();

	if (keyPressed[KEY_ID_X] == true)
	{
		auto offset = gm->getCam()->moveUp();
		//gm->getSkybox()->moveUp(offset);
	}
	if (keyPressed[KEY_ID_Z] == true)
	{
		auto offset = gm->getCam()->moveDown();
		//gm->getSkybox()->moveDown(offset);
	}

	if (keyPressed[KEY_ID_H]) 
	{
		gm->getCam()->rotateLeft();
	}

	if (keyPressed[KEY_ID_J])
	{
		gm->getCam()->rotateRight();
	}

	if (keyPressed[KEY_ID_U])
	{
		gm->getCam()->rotateUp();
	}

	if (keyPressed[KEY_ID_N])
	{
		gm->getCam()->rotateDown();
	}

	/* TEXT */
	//glPushMatrix();
	//glLoadIdentity();
	// Draw the text
	char buffer[80];
	glColor3f(1.0, 0.0, 0.0);

	sprintf_s(buffer, "WASD: Move, C: Change Weapon, Space: Shoot");
	_text.setPos(-0.5f, -0.5f, -1.0f);
	_text.printString(buffer, Text::FONT_NORMAL);


	glClearColor(0, 0, 0, 1);
	glPopMatrix();


  glutSwapBuffers();
  glutPostRedisplay();
	
}

void keyDown(unsigned char key, int x, int y)
{
  switch (key) 
  {
    case 'q':
    case 27:
      glutDestroyWindow(window);
#ifndef _WIN32
      // Must use this with regular glut, since it never returns control to main().
      exit(0);
#endif
      break;
      
    case 'w':
      keyPressed[KEY_ID_W] = true;
      break;
    case 'a':
      keyPressed[KEY_ID_A] = true;
      break;
    case 's':
      keyPressed[KEY_ID_S] = true;
      break;
    case 'd':
      keyPressed[KEY_ID_D] = true;
      break;
    case ' ':
      keyPressed[KEY_ID_SPACE] = true;
      break;
    case 'c':
      keyPressed[KEY_ID_C] = true;
      break;
		case 'x':
			keyPressed[KEY_ID_X] = true;
			break;
		case 'z':
			keyPressed[KEY_ID_Z] = true;
			break;
		case 't':
			keyPressed[KEY_ID_UPARROW] = true;
			break;
		case 'g':
			keyPressed[KEY_ID_DOWNARROW] = true;
			break;
		case 'f':
			keyPressed[KEY_ID_LEFTARROW] = true;
			break;
		case 'h':
			keyPressed[KEY_ID_H] = true;
			break;
		case 'j':
			keyPressed[KEY_ID_J] = true;
			break;
		case 'u':
			keyPressed[KEY_ID_U] = true;
			break;
		case 'n':
			keyPressed[KEY_ID_N] = true;
			break;

    default:
      glutPostRedisplay();
  }
}

void keyUp(unsigned char key, int x, int y)
{
  switch (key) 
  {
    case 'w':
      keyPressed[KEY_ID_W] = false;
      break;
    case 'a':
      keyPressed[KEY_ID_A] = false;
      break;
    case 's':
      keyPressed[KEY_ID_S] = false;
      break;
    case 'd':
      keyPressed[KEY_ID_D] = false;
      break;
    case ' ':
      keyPressed[KEY_ID_SPACE] = false;
      break;
    case 'c':
			isDown = false;
      keyPressed[KEY_ID_C] = false;
      break;
		case 'x':
			keyPressed[KEY_ID_X] = false;
			break;
		case 'z':
			keyPressed[KEY_ID_Z] = false;
			break;
		case 't':
			keyPressed[KEY_ID_UPARROW] = false;
			break;
		case 'g':
			keyPressed[KEY_ID_DOWNARROW] = false;
			break;
		case 'f':
			keyPressed[KEY_ID_LEFTARROW] = false;
			break;
		case 'h':
			keyPressed[KEY_ID_H] = false;
			break;
		case 'j':
			keyPressed[KEY_ID_J] = false;
			break;
		case 'u':
			keyPressed[KEY_ID_U] = false;
			break;
		case 'n':
			keyPressed[KEY_ID_N] = false;
			break;


  }
}

void mousePressed(int button, int state, int posX, int posY)
{
  if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  {
    mousePosX = posX;
    mousePosY = posY;
    keyPressed[MOUSE_LEFT_BUTTON_DOWN] = true;
  }  
  if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
    keyPressed[MOUSE_LEFT_BUTTON_DOWN] = false;
}

void mouseMoved(int posX, int posY)
{
  if(keyPressed[MOUSE_LEFT_BUTTON_DOWN])
  {
    int diffX = posX - mousePosX;
    mousePosX = posX;
    int diffY = posY - mousePosY;
    mousePosY = posY;
    
    // Implement quaternion based mouse move

  }
}

void reshape(int w, int h)
{
  glViewport(0, 0, (GLsizei) w, (GLsizei) h); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
//  glOrtho(-50, 700, -50, 700, -50, 50);
  gluPerspective(60.0f, float(w)/float(h) ,1.0f, 3000.0f);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
//  gluLookAt(0.0, 0.0, 10.0,     0.0, 0.0, 0.0,    0.0, 1.0, 0.0);
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
  glutInitWindowSize(700, 700); 
  glutInitWindowPosition(10, 10);
  window = glutCreateWindow("Space Shooter 3D");
  init();
  glutKeyboardFunc(keyDown);
  glutKeyboardUpFunc(keyUp);
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMouseFunc(mousePressed);
  glutMotionFunc(mouseMoved);

  // Add other callback functions here..

  glutMainLoop();
  return 0;
}