Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Viktor-Kolaric-ID.8311933-217CR-Resit-Assignment/main.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
223 lines (177 sloc)
5.33 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <GL/glew.h> | |
#include <GL/freeglut.h> | |
#include <algorithm> | |
#pragma comment(lib, "glew32.lib") | |
#include <iostream> | |
#include <vector> | |
#include "Cube.h" | |
#include "Sphere.h" | |
#include "Particle.h" | |
#include "Player.h" | |
#include "SphereCollider.h" | |
using namespace std; | |
// -- global variables -- | |
vector<GameObject*> objects; | |
int oldTimeSinceStart; | |
int newTimeSinceStart; | |
// Initialization | |
void setup(void) | |
{ | |
glClearColor(0.0, 0.0, 0.0, 0.0); | |
//Create objects and place them in the objects vector | |
//If they aren't in the objects vector, they will not be drawn / updated! | |
//constructor - position and size only | |
GameObject* cube1 = new Cube(5, 2, 0, 0); | |
//default constructor used | |
GameObject* cube2 = new Cube; | |
//constructor - position, colour, size | |
GameObject* cube3 = new Cube(2, 2, -2, 0, 3, 0, 0.6f); | |
//constructor - position, colour, radius, moveSpeed | |
GameObject* sphere1 = new Sphere(-1, 0, -1, 0, 0, 1, 1.5f, 10.0f); | |
//GameObject* particle = new Particle(1.0f, CR217::CRVec3(0.3, 1.7, 0)); | |
//GameObject* particle2 = new Particle(1.0f, CR217::CRVec3(0, 0, 0)); | |
GameObject* player = new Player(1.0f, CR217::CRVec3(0,0,0)); | |
/*GameObject* sphereColliderA = new SphereColliderA(0.8f, position); | |
GameObject* sphereColliderB1 = new SphereColliderB(0.5f, CR217::CRVec3(2, 2, -2));*/ | |
//GameObject* sphereColliderB2 = new SphereColliderB(0.5f, CR217::CRVec3(2, 0, 0)); | |
//GameObject* sphereColliderB3 = new SphereColliderB(0.5f, CR217::CRVec3(3, 0, 0)); | |
objects.push_back(cube1); | |
objects.push_back(cube2); | |
objects.push_back(cube3); | |
//objects.push_back(sphere1); | |
/*objects.push_back(particle); | |
objects.push_back(particle2);*/ | |
objects.push_back(player); | |
/*objects.push_back(sphereColliderA); | |
objects.push_back(sphereColliderB1);*/ | |
/*objects.push_back(sphereColliderB2); | |
objects.push_back(sphereColliderB3);*/ | |
} | |
//Drawing | |
void drawScene() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glLoadIdentity(); | |
// Position the objects for viewing | |
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
//Draw all our game objects | |
for (int i = 0; i < objects.size(); ++i) | |
{ | |
objects[i]->Draw(); | |
} | |
glutSwapBuffers(); | |
} | |
float clamp(float value, float min, float max) { | |
return std::max(min, std::min(max, value)); | |
} | |
bool checkCollision(SphereCollider& one, SphereCollider& two) | |
{ | |
CR217::CRVec3 center1(one.position.x + one.radius, one.position.y + one.radius, one.position.z + one.radius); | |
CR217::CRVec3 center2(two.position.x + two.radius, two.position.y + two.radius, two.position.z + two.radius); | |
CR217::CRVec3 aabb_center( | |
one.position.x + two.position.x, | |
one.position.y + two.position.y, | |
one.position.z + two.position.z | |
); | |
CR217::CRVec3 difference = center1 - aabb_center; | |
} | |
void doCollisons() | |
{ | |
for (GameObject& cube : this->GameObject[this->cube1]) | |
{ | |
} | |
} | |
//Called when nothing else is happening (such as rendering) | |
void idle() | |
{ | |
//Work out the delta time - the time between the last frame and this frame | |
oldTimeSinceStart = newTimeSinceStart; | |
newTimeSinceStart = glutGet(GLUT_ELAPSED_TIME); | |
float deltaTime = (newTimeSinceStart - oldTimeSinceStart) / 1000.0f; | |
//cout << "Delta Time (seconds): " << deltaTime << endl; - if you want to check the delta time | |
//Update all our game objects | |
for (int i = 0; i < objects.size(); ++i) | |
{ | |
objects[i]->Update(deltaTime); | |
} | |
glutPostRedisplay(); | |
} | |
//Delete any memory gotten from the new keyword | |
//Is only called when the ESC key is used to leave the game | |
void CleanUp() | |
{ | |
for (int i = objects.size() - 1; i >= 0; --i) | |
{ | |
delete objects[i]; | |
} | |
objects.clear(); | |
} | |
// Keyboard input processing functions | |
void keyInputDown(unsigned char key, int x, int y) | |
{ | |
GameObject::keys[key] = true; | |
//If you want to see keys pressed printed to the console | |
//std::cout << "Key pressed: " << key << std::endl; | |
switch (key) | |
{ | |
case 27: // Esc key | |
CleanUp(); | |
exit(0); | |
break; | |
default: | |
break; | |
} | |
} | |
void keyInputUp(unsigned char key, int x, int y) | |
{ | |
GameObject::keys[key] = false; | |
//If you want to see keys pressed printed to the console | |
//std::cout << "Key lifted: " << key << std::endl; | |
} | |
void keySpecialDown(int key, int x, int y) | |
{ | |
GameObject::specialKeys[key] = true; | |
//If you want to see keys pressed printed to the console | |
//std::cout << "Special Key pressed: " << key << std::endl; | |
} | |
void keySpecialUp(int key, int x, int y) | |
{ | |
GameObject::specialKeys[key] = false; | |
//If you want to see keys pressed printed to the console | |
//std::cout << "Special Key lifted: " << key << std::endl; | |
} | |
//OpenGL window reshape | |
void resize(int w, int h) | |
{ | |
glViewport(0, 0, w, h); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(60.0, (float)w / (float)h, 1.0, 500.0); | |
glMatrixMode(GL_MODELVIEW); | |
} | |
//Entry point into the program | |
int main(int argc, char** argv) | |
{ | |
//Setup for legacy OpenGL usage | |
glutInit(&argc, argv); | |
glutInitContextVersion(2, 0); | |
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); | |
//Window settings | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); | |
glutInitWindowSize(500, 500); | |
glutInitWindowPosition(100, 100); | |
glutCreateWindow("Basic Resit Project"); | |
//Callbacks | |
glutDisplayFunc(drawScene); | |
glutReshapeFunc(resize); | |
glutKeyboardFunc(keyInputDown); | |
glutKeyboardUpFunc(keyInputUp); | |
glutSpecialFunc(keySpecialDown); | |
glutSpecialUpFunc(keySpecialUp); | |
glutIdleFunc(idle); | |
glewExperimental = GL_TRUE; | |
glewInit(); | |
setup(); | |
glutMainLoop(); | |
} |