Jump to content
  • Advertisement
Sign in to follow this  
madsravn

OpenGL Snake in OpenGL - help

This topic is 4260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there... I'm trying me with my first snake clone in opengl using glfw. Here is my code up until now:
#include <GL/glfw.h>

struct snakos
{
int x;
int y;
float color;
} snake[100];
int dir=0;
int snakecount;
double sleep=0.05;

void initsnakes()
{
int i;
for(i=0;i<5;i++)
{
snake.x=20-i;
snake.y=5;
}

snakecount=4;
}



//========================================================================
// main()
//========================================================================

int main( void )
{
int width, height, running, x, y,i;
char titlestr[ 200 ];

// Initialise GLFW
glfwInit();

// Open OpenGL window
if( !glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW ) )
{
glfwTerminate();
return 0;
}

// Enable sticky keys
glfwEnable( GLFW_STICKY_KEYS );

// Disable vertical sync (on cards that support it)
glfwSwapInterval( 0 );

// Main loop
running = GL_TRUE;

initsnakes();
while( running )
{
// Get time and mouse position

glfwGetMousePos( &x, &y );

// Get window size (may be different than the requested size)
glfwGetWindowSize( &width, &height );
height = height > 0 ? height : 1;

// Set viewport
glViewport( 0, 0, width, height );

// Clear color buffer
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );

// Select and setup the projection matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f,
100.0f );

// Select and setup the modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 10.0f, 10.0f, 20.0f, // Eye-position
10.0f, 10.0f, 0.0f, // View-point
0.0f, 1.0f, 0.0f ); // Up-vector

glColor3f(1.0f,1.0f,1.0f);
int check=1;


if((glfwGetKey(GLFW_KEY_RIGHT)==1) && (check==1))
{
                                dir=0;
                                check=0;
}


if((glfwGetKey(GLFW_KEY_UP)==1) && (check==1))
{
                                dir=1;
                                check=0;
}
if((glfwGetKey(GLFW_KEY_LEFT)==1) && (check==1))
{
                                dir=2;
                                check=0;
}
if((glfwGetKey(GLFW_KEY_DOWN)==1) && (check==1))
{
                                dir=3;
                                check=0;
}

switch(dir)
{
case 0:
{
          
          for(i=0;i<snakecount;i++)
          {
          snake[snakecount-i].x=snake[snakecount-i-1].x;                       
          snake[snakecount-i].y=snake[snakecount-i-1].y; 
          }
          snake[0].x++;
          glfwSleep(sleep);
}
break;                        

case 1:
{
          
          for(i=0;i<snakecount;i++)
          {
          snake[snakecount-i].x=snake[snakecount-i-1].x;                       
          snake[snakecount-i].y=snake[snakecount-i-1].y; 
          }
          snake[0].y++;
          glfwSleep(sleep);
}
break; 
case 2:
{
          
          for(i=0;i<snakecount;i++)
          {
          snake[snakecount-i].x=snake[snakecount-i-1].x;                       
          snake[snakecount-i].y=snake[snakecount-i-1].y; 
          }
          snake[0].x--;
          glfwSleep(sleep);
}    
break;                       
case 3:
{
          
          for(i=0;i<snakecount;i++)
          {
          snake[snakecount-i].x=snake[snakecount-i-1].x;                       
          snake[snakecount-i].y=snake[snakecount-i-1].y; 
          }
          snake[0].y--;
          glfwSleep(sleep);
}
break;
}
for(i=0;i<snakecount;i++)
{
glPushMatrix();

glRectf(snake.x,snake.y,snake.x+1,snake.y+1);


glPopMatrix();
}


for(i=0;i<21;i++)
{
glBegin(GL_LINES);
glVertex3f(0.0f,i,0.0f);
glVertex3f(20.0f,i,0.0f);
glEnd();
}
for(i=0;i<21;i++)
{
glBegin(GL_LINES);
glVertex3f(i,0.0f,0.0f);
glVertex3f(i,20.0f,0.0f);
glEnd();
}






glfwSwapBuffers();

// Check if the ESC key was pressed or the window was closed
running = !glfwGetKey( GLFW_KEY_ESC ) &&
glfwGetWindowParam( GLFW_OPENED );

}

// Close OpenGL window and terminate GLFW
glfwTerminate();

return 0;
} 




But it is reacting very slow on input. I can't click up+left very fast to get it to change direction. I think it is because of the Sleep() thingy. But is there a alternative for this? :) Thanks [Edited by - madsravn on April 19, 2007 7:57:51 AM]

Share this post


Link to post
Share on other sites
Advertisement
Hi,

Not sure about your speed issue or the left snake not moving, didn't really look at that.

but I would change your key input to something like below, as you are repeating code for no reason.


if(dir >= 0 || <= 3) // direction key pressed
{
for(i = 0; i < snakecount; i++)
{
snake[snakecount -i].x = snake[snakecount-i -1].x;
snake[snakecount -i].y = snake[snakecount-i -1].y;
}

switch (dir)
{
case 0: // RIGHT
{
snake[0].x++;
}
case 1: // UP
{
snake[0].y++;
}
case 2: // LEFT
{
snake[0].x--;
}
case 3: // DOWN
{
snake[0].y--;
}
}
glfwSleep(sleep);
}





Untested, but the syntax should be about right [wink]

[Edited by - darren_mfuk on April 19, 2007 8:57:59 AM]

Share this post


Link to post
Share on other sites
First there's no need for your check variable, use else statements instead.


if((glfwGetKey(GLFW_KEY_RIGHT)==1))
{
dir=0;
}
else if((glfwGetKey(GLFW_KEY_UP)==1))
{
dir=1;
}
else if((glfwGetKey(GLFW_KEY_LEFT)==1))
{
dir=2;
}
else if((glfwGetKey(GLFW_KEY_DOWN)==1))
{
dir=3;
}



Second, dump the sleep command. Games are supposed to use 100% of the CPU(except if minimized). If you're using it to regulate framerate/updates there are better methods, look at glfwGetTime().

Share this post


Link to post
Share on other sites
I see you have changed your code from your original post.

Firstly you have just repeated the same code over and over again, put the "for loop" outside of the switch/case statement as per my previous post.

Secondly, (personally) I would not edit your original post, but make a new post, with the new source code, as it will be difficult to follow the thread, and people see what your original problem was.
As the answers posted no longer relate to your original source.

And lastly, I would maybe look at breaking this down to a few more functions.
One to handle keys/input, and one to handle the drawing.

Then call these functions from the main loop, as you have way to much going on in your loop [wink]

Also adding "Scet's" code, and mine above, your new code should be:

void ProcessKeys()
{
if((glfwGetKey(GLFW_KEY_RIGHT) == 1))
{
dir=0;
}
else if((glfwGetKey(GLFW_KEY_UP) == 1))
{
dir=1;
}
else if((glfwGetKey(GLFW_KEY_LEFT) == 1))
{
dir=2;
}
else if((glfwGetKey(GLFW_KEY_DOWN) == 1))
{
dir=3;
}

if(dir >= 0 || <= 3) // a direction key was pressed
{
for(i = 0; i < snakecount; i++)
{
snake[snakecount -i].x = snake[snakecount -i -1].x;
snake[snakecount -i].y = snake[snakecount -i -1].y;
}

switch (dir)
{
case 0: // RIGHT
{
snake[0].x++;
}
case 1: // UP
{
snake[0].y++;
}
case 2: // LEFT
{
snake[0].x--;
}
case 3: // DOWN
{
snake[0].y--;
}
}
}
}






PS. Is this a spelling mistake in your snake structure ?? - snakos.

** EDIT **
As for speed I would edit the part where you draw the snake and place the push/pop matrix outside of the "for loop".
This might help speed issues.


void Draw()
{
glPushMatrix();

for(i = 0; i < snakecount; i++)
{
glRectf(snake.x,snake.y,snake.x+1,snake.y+1);
}

for(i = 0; i < 21; i++)
{
glBegin(GL_LINES);
glVertex3f(0.0f,i,0.0f);
glVertex3f(20.0f,i,0.0f);
glEnd();
}

for(i = 0; i < 21; i++)
{
glBegin(GL_LINES);
glVertex3f(i,0.0f,0.0f);
glVertex3f(i,20.0f,0.0f);
glEnd();
}

glPopMatrix();
}




[Edited by - darren_mfuk on April 19, 2007 12:20:36 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!