making movement

hello

I'm trying to make a pacman game, and the pacman needs to move (duh!). I'm trying to make it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void myIdle(){
	glutPostRedisplay();
}

void pressKey(unsigned char key, int x, int y){
	if(key == 'a'){
		board->_pacman->_o = 'l';
		board->_pacman->_left = true;
	}
	else if(key == 'd'){
		board->_pacman->_o = 'r';
		board->_pacman->_right = true;
	}
	if(key == 'w'){
		board->_pacman->_o = 'u';
		board->_pacman->_up = true;
	}
	else if(key == 's'){
		board->_pacman->_o = 'd';
		board->_pacman->_down = true;
	}
}

void unpressKey(unsigned char key, int x, int y){
	if(key == 'a'){
		board->_pacman->_left = false;
	}
	if(key == 'd'){
		board->_pacman->_right = false;
	}
	if(key == 'w'){
		board->_pacman->_up = false;
	}
	if(key == 's'){
		board->_pacman->_down = false;
	}
}

void main(int argc, char** argv){
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize (600, 600);
	glutInitWindowPosition (-1, -1);
	glutCreateWindow("Jogo");
	glutDisplayFunc(myDisplay);
	glutReshapeFunc(myReshape);
	glutIdleFunc(myIdle);
	glutKeyboardFunc(pressKey);
	glutKeyboardUpFunc(unpressKey);
	glutMainLoop();
}


this is on the main file
the myDisplay function calls
1
2
3
4
5
6
7
8
9
void drawBoard(){
	if(board){
		board->drawBoard();
		board->_pacman->update();
	}
	else{
		board = new Board();
	}
}


the update pacman function is my problem. I need a way to make a "timer" that knows how much time has passed since it was last called, so i can make something like pacman->x += elapsedtime*pacman->speed, or something

maybe it was a bit confusing, but I'm hopping you can help me, I'm really stuck
Is there a reason you're using OpenGL for a 2D game? SFML or SDL would be better suited for this.
To answer your question you can use GlutGet(GLUT_ELAPSED_TIME)
http://pyopengl.sourceforge.net/documentation/manual/glutGet.3GLUT.html
its a 3d pacman lol.

I already tried, but I dont know why, its incredibly slow to react. when i press a key, it takes about 0.5 seconds to move, and when i release, it takes the same time to stop moving.

And, the least i draw from the board (i.e., if i dont draw the balls, for example), the performance lowers. If i draw the balls twice (it's stupid, it was just to try), the performance increases..
I really dont get it
i believe it's because if the ball arent drawn, the idleFunc is called too many times and the timer doesn't have time to update properly and the pacman freezes, or something
I guess I would use SDL/SMFL to manage my keyboard inputs in accordance with the OS I was running on. I don't know how glut implements it. In SDL/SMFL you can mange the 3d objects a little more cleanly and I can control how fast or slow I draw the objects at depth, which I am assuming glut may be chewing time up to do managing all the objects loaded at once. You only need to deal with the objects in a reasonable field of view. This also cuts down on processor time, either in the gpu or the cpu.
Topic archived. No new replies allowed.