I’m creating a 2d game in which there are 24 things (squares) bouncing around inside window off of the sides. I was able to get 1 out of the 4 to stay in bounds but the other 3 only check the left side and the bottom…what am I doing wrong??
If I can’t get four things to stay in bounds, how can I do 24???
Here’s what I have:
#include <stdio.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <time.h>
#include <stdlib.h>
const int NUM_OF_THINGS = 4;
//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];
GLuint Square_DL;
//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; //or 100.0f
GLfloat ypos = 150.0f; //or 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; //speed or .25
GLfloat ystep = 1.0f;
// Keep track of windows changing width and height
GLfloat windowWidth = 400;
GLfloat windowHeight = 400;
void createThing ()
{
int xSize = .1;
int ySize = .1;
// Draw a filled rectangle with current color
glColor3fv(YELLOW);
glRectf(xSize, ySize, xSize+rsize, ySize+rsize);
}
// Called to draw scene
void RenderScene()
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
for (int i = 0; i < NUM_OF_THINGS; i++)
{
glColor3fv(YELLOW);
glTranslatef(xpos, ypos, 0.0);
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(10,TimerFunction, 0);
}
// 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(10, TimerFunction, 0);
myInit();
glutMainLoop();
return 0;
}