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?
217CR-Deferral-Assignment-Basic-Project/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.
168 lines (138 sloc)
3.8 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
#include <GL/glew.h> | |
#include <GL/freeglut.h> | |
#pragma comment(lib, "glew32.lib") | |
#include <iostream> | |
#include <vector> | |
#include "Cube.h" | |
#include "Sphere.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, 0, 0, 2); | |
//default constructor used | |
GameObject* cube2 = new Cube; | |
//constructor - position, colour, size | |
GameObject* cube3 = new Cube(2, 2, -2, 0, 1, 0, 0.5f); | |
//constructor - position, colour, radius, moveSpeed | |
GameObject* sphere1 = new Sphere(-1, 0, -1, 0, 0, 1, 1.5f, 10.0f); | |
objects.push_back(cube1); | |
objects.push_back(cube2); | |
objects.push_back(cube3); | |
objects.push_back(sphere1); | |
} | |
//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(); | |
} | |
//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(); | |
} |