So I am really new in OpenGL. I have never made a game so I decided to start with something easy like Snake. But I can't code the movement. I can't control point's movement. When I press a button it goes to a direction and doesn't stop although I press buttons. So excuse me for my silliness I hope you don't think for a retard
[source lang="cpp"]GLfloat camerax=0.0;
GLfloat cameray=0.0;
bool movingleft=false;
bool movingright=false;
bool movingup=false;
bool movingdown=false;
void keyPressed (unsigned char key, int x, int y) {
if(key=='a')
{
movingleft=true;
}
if(key=='d')
{
movingright=true;
}
if(key=='w')
{
movingup=true;
}
if(key=='s')
{
movingdown=true;
}
glutPostRedisplay();
}
void display (void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -6.0f);
if(movingleft==true)
{
camerax-=0.001;
glutPostRedisplay();
}
if(movingright==true)
{
camerax+=0.001;
glutPostRedisplay();
}
if(movingup==true)
{
cameray+=0.001;
glutPostRedisplay();
}
if(movingdown==true)
{
cameray-=0.001;
glutPostRedisplay();
}
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(camerax, cameray, 0.0f);
glEnd();
glFlush();
}
[/source]
Like Snake movement
When you start moving in a direction, you got to set the flags of all other directions to false.
if(key=='a')
{
movingleft=true;
movingright = false;
movingup = ...
...
}
Also there needs to be an else if in the last if blocks.
if(movingleft==true)
{
camerax-=0.001;
glutPostRedisplay();
}
else if (movingright==true)
{
...
}
else if .....
{
...
}
if(key=='a')
{
movingleft=true;
movingright = false;
movingup = ...
...
}
Also there needs to be an else if in the last if blocks.
if(movingleft==true)
{
camerax-=0.001;
glutPostRedisplay();
}
else if (movingright==true)
{
...
}
else if .....
{
...
}
There are much better ways to do it. When you got your way working, think about how you can possible make it better.
All the best.
All the best.
It doesn't work the way you told me ;( I tried that way before posting the code.
[source lang="cpp"]void keyPressed (unsigned char key, int x, int y) {
if(key=='a')
{
movingleft=true;
movingright=false;
movingup=false;
movingdown=false;
}
if(key=='d')
{
movingright=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='w')
{
movingup=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='s')
{
movingdown=true;
movingleft=false;
movingup=false;
movingdown=false;
}
glutPostRedisplay();
}
void display (void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -6.0f);
if(movingleft==true)
{
camerax-=0.001;
glutPostRedisplay();
}
else if(movingright==true)
{
camerax+=0.001;
glutPostRedisplay();
}
else if(movingup==true)
{
cameray+=0.001;
glutPostRedisplay();
}
else if(movingdown==true)
{
cameray-=0.001;
glutPostRedisplay();
}
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(camerax, cameray, 0.0f);
glEnd();
glFlush();
}[/source]
[source lang="cpp"]void keyPressed (unsigned char key, int x, int y) {
if(key=='a')
{
movingleft=true;
movingright=false;
movingup=false;
movingdown=false;
}
if(key=='d')
{
movingright=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='w')
{
movingup=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='s')
{
movingdown=true;
movingleft=false;
movingup=false;
movingdown=false;
}
glutPostRedisplay();
}
void display (void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -6.0f);
if(movingleft==true)
{
camerax-=0.001;
glutPostRedisplay();
}
else if(movingright==true)
{
camerax+=0.001;
glutPostRedisplay();
}
else if(movingup==true)
{
cameray+=0.001;
glutPostRedisplay();
}
else if(movingdown==true)
{
cameray-=0.001;
glutPostRedisplay();
}
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(camerax, cameray, 0.0f);
glEnd();
glFlush();
}[/source]
a few things:
1) You don't want to call glutPostRedisplay inside the conditionals, you want it after the render code.
2) You probably don't want to move the camera, once you get more bodyparts it won't make much sense.
1) You don't want to call glutPostRedisplay inside the conditionals, you want it after the render code.
2) You probably don't want to move the camera, once you get more bodyparts it won't make much sense.
1. Doesn' work that way at all
2. I'm not moving the camera, they are just names of the variables.
2. I'm not moving the camera, they are just names of the variables.
Oh i see what you're doing there
put the glutPostRedisplay in your update function (you should change the "camera" variable in there aswell), you shouldn't have game logic in the display function (like you do now).
The problem you have is caused by postredisplay being called from the display function (which causes the display function to run again immediatly afterwards).
Basically right now you have a gameloop that looks something like this:
while(running) {
keypressedfunction gets called here and sets the moving_left, etc
While (moving left etc is pressed) {
display(); //this also moves the "camera"
}
Yes, with glut your code look different but that is effectivly how glut treats your current code, (glut runs its own mainloop)
the problem with this is that once the keypressed function runs the first time you'll get stuck in an infinite loop of glutpostredisplay.
The solution:
Move all your game logic (if move_left ....) blabla
to the glutIdleFunc
put glutPostRedisplay() at the end of the glutIdlefunc (nowhere else).
only have rendering code in the display function (no logic)
put the glutPostRedisplay in your update function (you should change the "camera" variable in there aswell), you shouldn't have game logic in the display function (like you do now).
The problem you have is caused by postredisplay being called from the display function (which causes the display function to run again immediatly afterwards).
Basically right now you have a gameloop that looks something like this:
while(running) {
keypressedfunction gets called here and sets the moving_left, etc
While (moving left etc is pressed) {
display(); //this also moves the "camera"
}
Yes, with glut your code look different but that is effectivly how glut treats your current code, (glut runs its own mainloop)
the problem with this is that once the keypressed function runs the first time you'll get stuck in an infinite loop of glutpostredisplay.
The solution:
Move all your game logic (if move_left ....) blabla
to the glutIdleFunc
put glutPostRedisplay() at the end of the glutIdlefunc (nowhere else).
only have rendering code in the display function (no logic)
Like this?
[source lang="cpp"]#include <GL/glew.h>
#include <GL/glut.h>
#include<GL\GL.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
GLfloat snakex=0.0;
GLfloat snakey=0.0;
bool movingleft=false;
bool movingright=false;
bool movingup=false;
bool movingdown=false;
void keyPressed (unsigned char key, int x, int y) {
if(key=='a')
{
movingleft=true;
movingright=false;
movingup=false;
movingdown=false;
}
if(key=='d')
{
movingright=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='w')
{
movingup=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='s')
{
movingdown=true;
movingleft=false;
movingup=false;
movingdown=false;
}
glutPostRedisplay();
}
void idle()
{
if(movingleft==true)
{
snakex-=0.001;
}
else if(movingright==true)
{
snakex+=0.001;
}
else if(movingup==true)
{
snakey+=0.001;
}
else if(movingdown==true)
{
snakey-=0.001;
}
glutPostRedisplay();
}
void display (void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -6.0f);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(snakex, snakey, 0.0f);
glEnd();
glFlush();
}
void reshape (int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glEnable( GL_TEXTURE_2D );
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("You’re first OpenGL Window");
glutKeyboardFunc(keyPressed);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}[/source]
[source lang="cpp"]#include <GL/glew.h>
#include <GL/glut.h>
#include<GL\GL.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
GLfloat snakex=0.0;
GLfloat snakey=0.0;
bool movingleft=false;
bool movingright=false;
bool movingup=false;
bool movingdown=false;
void keyPressed (unsigned char key, int x, int y) {
if(key=='a')
{
movingleft=true;
movingright=false;
movingup=false;
movingdown=false;
}
if(key=='d')
{
movingright=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='w')
{
movingup=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='s')
{
movingdown=true;
movingleft=false;
movingup=false;
movingdown=false;
}
glutPostRedisplay();
}
void idle()
{
if(movingleft==true)
{
snakex-=0.001;
}
else if(movingright==true)
{
snakex+=0.001;
}
else if(movingup==true)
{
snakey+=0.001;
}
else if(movingdown==true)
{
snakey-=0.001;
}
glutPostRedisplay();
}
void display (void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -6.0f);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(snakex, snakey, 0.0f);
glEnd();
glFlush();
}
void reshape (int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glEnable( GL_TEXTURE_2D );
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("You’re first OpenGL Window");
glutKeyboardFunc(keyPressed);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}[/source]
Hmm, you can remove the postredisplay from the keyboard handling function atleast, it shouldn't be there, not sure why its not doing "anything" though.
Do you get a window ? any errors ? warnings ?
Does the debugger show the snakex changing ?
Do you get a window ? any errors ? warnings ?
Does the debugger show the snakex changing ?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement