1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <cmath>
#include <list>
//Headers--------------------
#include "GameObject.h"
#include "Globals.h"
#include "Rollingball.h"
#include "Ground.h"
//Screen Constants
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 400;
const int SCREEN_FPS = 60;
bool* keyStates = new bool[256];
float Cameraposition[2] = {0,0};
//Object list
std::list<GameObject *> objects;
std::list<GameObject *>::iterator iter;
std::list<GameObject *>::iterator iter2;
void update()
{
}
void runMainLoop( int val );
void display (void);
void keyOperations (void);
void keyPressed (unsigned char key, int x, int y);
void keyUp (unsigned char key, int x, int y);
//Globals to main
Rollingball *ball;
Ground *ground;
int main( int argc, char* args[] )
{
glutInit( &argc, args ); //Initialize FreeGLUT
glutInitContextVersion( 2, 1 ); //Create OpenGL 2.1 context
glutInitDisplayMode( GLUT_DOUBLE ); //Create Double Buffered Window
glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );
glutCreateWindow( "SuperBall" );
//Do post window/context creation initialization
if( !initGL() )
return 1;
//-----------------------
ball = new Rollingball();
ground = new Ground();
ball->Init();
objects.push_back(ball);
ground->Init();
objects.push_back(ground);
//Set keyboard handler
glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events
//Set main loop
glutTimerFunc( 1.0 / SCREEN_FPS, runMainLoop, 0 );
//Start GLUT main loop
glutMainLoop();
return 0;
}
void runMainLoop( int val )
{
glPushMatrix();
ball->Cameraupdate();
glTranslatef( -Cameraposition[0], -Cameraposition[1], 0.f );
ball->Moveball();
update();
// render--------------------------
for(iter = objects.begin(); iter != objects.end(); ++iter )
(*iter)->Render();
glPopMatrix();
//Run frame one more time
glutTimerFunc( 1.0/ SCREEN_FPS, runMainLoop, val );
//Flip Buffer display-------
glutSwapBuffers();
glClear( GL_COLOR_BUFFER_BIT );
}
void keyOperations (void)
{
if (keyStates['a'])
{
ball->Jumpball();
}
}
void display (void)
{
keyOperations();
}
void keyPressed (unsigned char key, int x, int y)
{
keyStates[key] = true; // Set the state of the current key to pressed
}
void keyUp (unsigned char key, int x, int y)
{
keyStates[key] = false; // Set the state of the current key to not pressed
}
|