Sunday, April 08, 2007


I'm baack....I got some looovin, I returned to do a little more programming.
We are gonn be a little more serios this this time, but we're gonna talk about an interesting subject, a timer function.
How can a timer function help us in any way?

It turns out to be a handy place to run our game's logic rules - a few programming tricks can help us to run the logic as often as we want (if, for example, you want to drop the block by a line every second, you can play some tricks with either the value parameter or global variables to have this function run the logic module after a second has passed). Furthermore, we can also use it for specified programatically. Something like a combination of "As fast as we can" and "as often as we want," this method can be used to specify a programatically-varied framerate. Basically, this allows those with better video cards to get a more visually stimulating experience, while still preserving as much of the flow as possible.

Now I have to admit, I'm not as familiar with this method , but the leap of logic to get there isn't all that large to begin with. Basically, you use C++'s time library to obtain a reference to the system clock (the number of milliseconds), which you then post against a target frame rate. This way, if you're doing some intense 3D stuff, and your computer can't really handle it, you can still roughly regulate the motion that takes place on the screen by multiplying the desired motion per frame by the ratio of target frames/actual frames.

I made a simple code, where I display a timer. This timer is running like crazy, incrementing itself every 1000 miliseconds. But, when the user right clicks the counter resets itself, aaand when the user left clicks the mouse, the timer takes a little pause.
Here's the code->




#include
#include "./glut.h"
#include
#include
#include //por using sprintf, convers int to a string


float angle=0.0,deltaAngle = 0.0,ratio;
float x=0.0f,y=1.75f,z=30.0f;
float lx=0.0f,ly=0.0f,lz=-1.0f;
int deltaMove = 0;
float scaling=1;
int font=(int)GLUT_STROKE_ROMAN;
int counter=0;
int delta=1;
void myTimer(int value);

void changeSize(int w, int h)
{

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

ratio = 1.0f * w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the clipping volume
gluPerspective(45,ratio,1,10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);


}



void initScene() {

glEnable(GL_DEPTH_TEST);
glLineWidth(4.0);
glutTimerFunc(1000,myTimer,1);

}

void myTimer(int value){



counter=counter+delta;

glutPostRedisplay();

glutTimerFunc( 1000,myTimer, 1);

}


void orientMe(float ang) {


lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}


void moveMeFlat(int i) {
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}


void renderStrokeCharacter(float x, float y, float z, void *font,char *string)
{

char *c;
glPushMatrix();
glTranslatef(x, y, z);
glScalef(scaling,scaling,0);
for (c=string; *c != '\0'; c++) {
glutStrokeCharacter(font, *c);
}
glPopMatrix();
}

void renderScene(void) {

if (deltaMove)
moveMeFlat(deltaMove);
if (deltaAngle) {
angle += deltaAngle;
orientMe(angle);
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
char result[100];

sprintf( result, "%d", counter );


renderStrokeCharacter(-400,150,-800,(void *)font,result);
renderStrokeCharacter(-400,0, -800,(void *)font,"Ich liebe Viktor!");


glPopMatrix();

glutSwapBuffers();
}





void pressKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT : scaling=2; deltaAngle = -0.01f;break;
case GLUT_KEY_RIGHT : scaling=2; deltaAngle = 0.01f;break;
case GLUT_KEY_UP : deltaMove = 1;break;
case GLUT_KEY_DOWN : deltaMove = -1;break;
}
}

void releaseKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT : deltaAngle = 0.0f;break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = 0;break;
}
}

void processMenuEvents(int option) {

font = option;
}


/*void createMenus() {
int menu = glutCreateMenu(processMenuEvents);

glutAddMenuEntry("Roman",(int)GLUT_STROKE_ROMAN);

glutAddMenuEntry("Mono Roman",(int)GLUT_STROKE_MONO_ROMAN);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}*/
void mouse(int button, int state, int x, int y){
switch( button ){
case GLUT_LEFT_BUTTON:
if( state == GLUT_DOWN ){
scaling=1.3;
counter=0;//para riniciar el contador...
delta=1;//reiniciando el contador, de nuevo se debe poder ir icremntando


}
break;
case GLUT_RIGHT_BUTTON:
if( state == GLUT_DOWN ){
scaling=.5;

delta=0;//pausa al contador.

}
break;
}



}// end mouse



void processNormalKeys(unsigned char key, int x, int y) {

if (key == 27)
exit(0);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,360);
glutCreateWindow("Saiph's love Declaration");

initScene();

glutKeyboardFunc(processNormalKeys);
glutIgnoreKeyRepeat(1);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);
// createMenus();
glutMouseFunc(mouse);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);

glutReshapeFunc(changeSize);

glutMainLoop();

return(0);
}

No comments: