I am new to OpenGL and C and I need to create a 2d game that has about 24 moving squares with two different colors. It took me forever to create just one moving square but I am having a difficult time creating more than one! Does anyone have any hints or idea, or maybe can give me sum kinda structure to work off of? If i can create at least two…I can build off of that.
Heres what i have so far:
[#include <stdio.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <time.h>
#include <stdlib.h>
//Prototypes
void RenderScene(void);
void TimerFunction(int);
void myInit(void);
void ChangeSize(GLsizei, GLsizei);
void myKey(unsigned char, int, int);
void createThing();
GLfloat createThings[4];
//Array of clolors
GLfloat RED[3] = {1,0,0};
GLfloat YELLOW[3] = {1,1,0};
GLfloat PURPLE[3] = {1,0,1};
GLfloat BLACK[3] = {0,0,0};
// Initial square position and size
GLfloat xpos = 100.0f;
GLfloat ypos = 150.0f;
GLsizei rsize = 50;
// size of square
GLfloat x1 = 0.1;
GLfloat y1 = 0.1;
// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;
void createThing ()
{
// Draw a filled rectangle with current color
glRectf(xpos, ypos, xpos+rsize, ypos+rsize);
glBegin(GL_QUADS);
// Set current drawing color to yellow
glColor3fv(YELLOW);
glVertex2f(-x1, y1);
glVertex2f(x1, y1);
glVertex2f(x1, -y1);
glVertex2f(-x1, -y1);
glEnd();
}
// Called to draw scene
void RenderScene()
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
createThing();
// Flush drawing commands
glutSwapBuffers();
}
void myKey(unsigned char key, int x, int y)
{
if ((key == 'Q') || (key == 'q')) {
exit(0);
}
}
// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
{
// Reverse direction when you reach left or right edge
if(xpos > windowWidth-rsize || xpos < 0)
xstep = -xstep;
// Reverse direction when you reach top or bottom edge
if(ypos > windowHeight-rsize || ypos < 0)
ystep = -ystep;
// Check bounds. This is incase the window is made
// smaller and the rectangle is outside the new
// clipping volume
if(xpos > windowWidth-rsize)
xpos = windowWidth-rsize-1;
if(ypos > windowHeight-rsize)
ypos = windowHeight-rsize-1;
// Actually move the square
xpos += xstep;
ypos += ystep;
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(8,TimerFunction, 1);
}
// Setup the rendering state
void myInit(void)
{
// Set clear color to black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
// Called by GLUT library when the window has chanaged size
void ChangeSize(GLsizei w, GLsizei h)
{
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Keep the square square, this time, save calculated
// width and height for later use
if (w <= h) {
windowHeight = 400.0f*h/w;
windowWidth = 400.0f;
}
else {
windowWidth = 400.0f*w/h;
windowHeight = 400.0f;
}
// Set the clipping volume
gluOrtho2D(0.0f, windowWidth, 0.0f, windowHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Main program entry point
int main(int argc, char** argv)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInit(&argc, argv);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Bounce Beeatch");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(myKey);
glutTimerFunc(8, TimerFunction, 1);
myInit();
glutMainLoop();
return 0;
}
]