Hello!
I’ve been trying to make a simple “snake” game but I found a few problems that I can’t solve.
When I run the program, it starts consuming the memory and simply doesn’t stop.
After a few hours checking out the code to find any bugs, came out the idea to check one examples from “OpenGL SuperBible” ( 4th edition ) and the given examples has the same problem!
I would like to know if its a problem at my code or it’s just a regular problem at all.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h> // Must have for Windows platform builds
#include <gl\gl.h> // Microsoft OpenGL headers (version 1.1 by themselves)
#include <gl\glu.h> // OpenGL Utilities
#include <gl\glut.h>
#include <math.h>
struct Position
{
GLfloat x;
GLfloat y;
GLbyte wait;
};
GLbyte xMove = 0;
GLbyte yMove = 0;
GLshort wSize = 1;
struct Position Worm[20];
struct Position Food;
void CreateFood()
{
srand ( rand() );
bool accept = true;
while ( accept )
{
bool found = false;
Food.x = ( rand() % 10 )*5;
Food.y = ( rand() % 10 )*5;
for ( int i = 0; i < 20 && Worm[i].wait != -1; i++ )
{
if ( Worm[i].x == Food.x || Worm[i].y == Food.y )
{
found = true;
break;
}
}
if ( !found )
{
accept = false;
}
}
}
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(Food.x, Food.y, 0);
glVertex3f(Food.x, Food.y+5, 0);
glVertex3f(Food.x+5, Food.y+5, 0);
glVertex3f(Food.x+5, Food.y, 0);
glEnd();
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(Worm[0].x, Worm[0].y, 0);
glVertex3f(Worm[0].x, Worm[0].y+5, 0);
glVertex3f(Worm[0].x+5, Worm[0].y+5, 0);
glVertex3f(Worm[0].x+5, Worm[0].y, 0);
glEnd();
glColor3f(0.0f, 1.0f, 0.0f);
for ( int i = 1; i < 20 && Worm[i].wait == 0; i++ )
{
// Draw the point
glBegin(GL_QUADS);
glVertex3f(Worm[i].x, Worm[i].y, 0);
glVertex3f(Worm[i].x, Worm[i].y+5, 0);
glVertex3f(Worm[i].x+5, Worm[i].y+5, 0);
glVertex3f(Worm[i].x+5, Worm[i].y, 0);
glEnd();
}
// Flush drawing commands
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glPointSize(20);
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
{
xMove = 0;
yMove = 5;
}
if(key == GLUT_KEY_DOWN)
{
xMove = 0;
yMove = -5;
}
if(key == GLUT_KEY_LEFT)
{
xMove = -5;
yMove = 0;
}
if(key == GLUT_KEY_RIGHT)
{
xMove = 5;
yMove = 0;
}
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 50.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Start()
{
xMove = 0;
yMove = 0;
wSize = 1;
for ( int i = 1; i < 20; i++ )
{
Worm[i].wait = -1;
}
Worm[0].wait = 0;
Worm[0].x = 0;
Worm[0].y = 0;
CreateFood();
}
void MoveWorm ( int value )
{
for ( int i = 19; i != 0; i-- )
{
if ( Worm[i].wait == 0 )
{
Worm[i].x = Worm[i-1].x;
Worm[i].y = Worm[i-1].y;
}
}
Worm[0].x += xMove;
Worm[0].y += yMove;
for ( int i = 0; i < 20; i++ )
{
if ( Worm[i].wait > 0 )
{
Worm[i].wait--;
}
}
if ( Worm[0].x >= 50 || Worm[0].x <= -55 || Worm[0].y >= 50 || Worm[0].y <= -55 )
{
Start();
}
if ( Worm[0].x == Food.x && Worm[0].y == Food.y )
{
printf ( "Worm grow %d
", wSize );
if ( wSize < 20 )
{
Worm[wSize].wait = wSize;
Worm[wSize].x = Food.x;
Worm[wSize].y = Food.y;
wSize++;
CreateFood();
}
else
{
Start();
}
}
glutTimerFunc(500,MoveWorm, 1);
RenderScene();
}
int main(int argc, char* argv[])
{
Start();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Snake v0.1");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutTimerFunc(500,MoveWorm, 1);
glutMainLoop();
return 0;
}
PS: Don’t worry about some non-optimized codes, like adding a bitmap and lists with pointers, it’s my first attempt on making a game, I just want it to work.
PS2: I’m using Windows Vista x64
PS3: Sorry about my bad English
Thanks!