The gameobjects are currently not translated at all. As far as I can tell the translations are performed on the ModelView matrix it is the last thing set in the reshape function.
Here is my current code It is a long way from complete and is basicly an adaptation of the double.c program from the redbook.
Code:
#include <iostream>
#include <GL/glut.h>
#include <stdlib.h>
#include <list>
#include <functional>
#include <algorithm>
#include <time.h>
#include "gobject.h"
#include "ship.h"
#include "astroid.h"
using namespace std;
extern const int SCREENWIDTH = 450;
extern const int SCREENHEIGHT = 450;
static GLfloat spin = 0.0;
list<GObject*> gameObjects;
list<Astroid*> astroids;
Ship* spaceShip = new Ship();
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
for_each(gameObjects.begin(),gameObjects.end(),mem_fun(&GObject::translate));
glutSwapBuffers();
}
void moveAstroids(void)
{
// for_each(astroids.begin(),astroids.end(),mem_fun(&Astroid::translate));
glutPostRedisplay();
}
void idleGameLoop(void)
{
moveAstroids();
glutPostRedisplay();
}
void init(void)
{
srand(time(NULL));
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
gameObjects.push_front(spaceShip);
for (int i = 0; i<10; i++){
Astroid* sharedTempAstroid = new Astroid();
gameObjects.push_back(sharedTempAstroid);
astroids.push_back(sharedTempAstroid);
}
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-SCREENWIDTH/2,SCREENWIDTH/2,-SCREENHEIGHT/2,SCREENHEIGHT/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* ARGSUSED2 */
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(idleGameLoop);
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
static void
key(unsigned char k, int x, int y)
{
switch (k) {
case 27: /* Escape */
exit(0);
break;
case 65:
case 97:
spin = (int)spin % 360 + 2.0;
glutIdleFunc(idleGameLoop);
break;
case 68:
case 100:
spin = (int)spin % 360 - 2.0;
glutIdleFunc(idleGameLoop);
break;
default:
return;
}
glutPostRedisplay();
}
/*
* Request double buffer display mode.
* Register mouse input callback functions
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (SCREENWIDTH, SCREENHEIGHT);
glutInitWindowPosition (10, 10);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutKeyboardFunc(key);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Code:
#include "astroid.h"
extern const int SCREENWIDTH;
extern const int SCREENHEIGHT;
Astroid::Astroid()
{
xcoord=rand()%(SCREENWIDTH/2)+1;
ycoord=rand()%(SCREENHEIGHT/2)+1;
velocity = 1*(rand()%4);
}
void Astroid::draw(){
glColor3f(256.0, 0.0 , 0.0);
glRectf(xcoord, ycoord,xcoord+10,ycoord+10);
}
void Astroid::rotate(){}
void Astroid::translate(){
glPushMatrix();
glTranslatef(velocity,velocity,0);
Astroid::draw();
glPopMatrix();
}
Astroid::~Astroid(){}