Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay well i just pulled out this code to test the movement of a square, but i have two problems and one question on structure. first two problems are every time the frame renders the square becomes smaller till it can't be seen in about 3 renders, secondly the movement function doesn't seem to work when the place i pulled it from says it apparently should work which brings me to my question about structure, What is the switch, case thing that i have written in movement, it there a tutorial on it because it seems interesting and useful.




#include <iostream>
#include <gl\glut.h>
using namespace std;
unsigned int Character(0);
float Posx(1);
float Posy(1);
int Test(0);
void DisplayListInit()
{
	Character = glGenLists(1);
	glNewList(Character, GL_COMPILE);
		glBegin(GL_QUADS);
			glColor3i(0, 0, 1);
			glVertex2f(-1, -1);
			glVertex2f(-1, 1 );
			glVertex2f( 1, 1 );
			glVertex2f( 1, -1);
		glEnd();
	glEndList();
	cout << "List Declared\n";
}
void Render()
{
	glClearColor(1, 1, 1, 1);
	glClearDepth(0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glOrtho(-10, 10, -10, 10, -1, 1);
	glPushMatrix();
		glTranslatef(Posx, Posy, 1);	
		glCallList(Character);
	glPopMatrix();
	glutSwapBuffers();
	cout << "Render Complete\n";
	if(Test < 1){
		glutPostRedisplay();
		Test ++;
	}
}
void Movement(unsigned char key, int x, int y)

	switch (key){
		case 'a':
			Posx -= 0.5;
			break;
		case 's':
			Posy -= 0.5;
			break;
		case 'd':
			Posx += 0.5;
			break;
		case 'w':
			Posy += 0.5;
			break;
		default:
			break;
	}
	glutPostRedisplay();
	cout << "****************Movement done\n";
}
int main (int argc, char **argv )
{
	glutInit(&argc, argv);
	glutInitWindowSize(800, 800);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutCreateWindow("Boxs");
	DisplayListInit();
	glutDisplayFunc(Render);
	glutKeyboardFunc(Movement);
	//glutIdleFunc(Render);
	glutMainLoop();
}
Posted
Comments
CPallini 19-Jun-11 8:19am    
Adventuring in OpenGL development without knowing the basic constructs of the C/C++ programming languages makes you a very brave programmer.
Joshua Waring 24-May-14 4:50am    
Haha thank you, but I'd say after 3 years it's payed off!
CPallini 24-May-14 4:58am    
:thumbsup:

I hate to say this but there seems little point in you trying to debug this code when you apparently do not even understand what a switch statement does. Get hold of a C++ book, search Google, or look in MSDN[^] for some useful information about coding in C++.
 
Share this answer
 
First problem caused by improper usage of glOrtho.
The glOrtho function multiplies the current matrix by an orthographic matrix.
First time projection matrix is multipled for ortho projection.
Next renderings also multiplies previous matrix again and will cause issue.

This issue can be solved by calling glOrtho() once.
Or call glLoadIdendity() before calling glOrtho.

glClearColor(1, 1, 1, 1);
    glClearDepth(0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity(); // Reset your current matrix with idendity matrix.
    glOrtho(-10, 10, -10, 10, -1, 1);
    glPushMatrix();
    glTranslatef(Posx, Posy, 1);
    glCallList(Character);
    glPopMatrix();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900