PDA

View Full Version : Controlling sprite speed



anakastakis
09-05-2009, 11:35 AM
I'm working on a 2d game, and I,m stuck on sprite movement speed. I can move my sprite, but how do I control the speed at which it moves/translates?

here is my code

#include <GL/gl.h>
#include <GL/freeglut.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
#include <string>
#include "keyboard.h"
#include "texture.h"

using namespace std;

GLuint sprite[4];
GLuint tile[4];
GLfloat meow = 0.0;
GLuint emerald;

int cMap[10][10] = { //our map
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

void drawTiles (void) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (cMap[i][j] == 0) {
glBindTexture( GL_TEXTURE_2D, tile[1] ); //bind our grass texture to our shape
}
else
{
glBindTexture( GL_TEXTURE_2D, tile[0] ); //bind our dirt texture to our shape
}
glPushMatrix();
glTranslatef(j * 16, -i * 16, 0);

glBegin (GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex3f(400.0, 400.0, 0.9);
glTexCoord2d(1.0, 0.0); glVertex3f(416.0, 400.0, 0.9);
glTexCoord2d(1.0, 1.0); glVertex3f(416.0, 416.0, 0.9);
glTexCoord2d(0.0, 1.0); glVertex3f(400.0, 416.0, 0.9);
glEnd();
glPopMatrix();
}
}
}

void background(void) {
glColor4f(1.0f,1.0f,1.0f,1.0f);
glBindTexture( GL_TEXTURE_2D, sprite[0]);
glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3f(0.0,0.0,0.0);
glTexCoord2d(0.0,1.0); glVertex3f(0.0,768.0,0.0);
glTexCoord2d(1.0,1.0); glVertex3f(1024.0,768.0,0.0);
glTexCoord2d(1.0,0.0); glVertex3f(1024.0,0.0,0.0);
glEnd();
glPopMatrix();
}

void square(void) {

glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glTranslatef(0.0, meow, 0.0);
//glDisable(GL_COLOR_MATERIAL);
glPushMatrix();
glBindTexture( GL_TEXTURE_2D, emerald);
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0);
glVertex3f(400.0,400.0,1.0);
glTexCoord2d(0.0,1.0);
glVertex3f(400.0,432.0,1.0);
glTexCoord2d(1.0,1.0);
glVertex3f(416.0,432.0,1.0);
glTexCoord2d(1.0,0.0);
glVertex3f(416.0,400.0,1.0);
glDisable(GL_BLEND);
glEnd();
glPopMatrix();
}

void display (void) {
glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glOrtho(0,640,0,480,-1,10);
glEnable( GL_TEXTURE_2D );
//background();
drawTiles(); //draw our tiles
square();
glutSwapBuffers();
emerald = sprite[1];
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
glutInit (&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutGameModeString( "640x480:32@70" );
glutEnterGameMode();
glutDisplayFunc (display);
//glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(controls);


tile[0] = loadTexture("C:\\Dev-Cpp\\code\\files\\grass.png", 16, 16);
tile[1] = loadTexture("C:\\Dev-Cpp\\code\\files\\stonewall.png", 16, 16);

sprite[0] = loadTexture("C:\\Dev-Cpp\\code\\files\\background.png", 1024, 768);
sprite[1] = loadTexture("C:\\Dev-Cpp\\code\\files\\emerald_a.png", 32, 16);
sprite[2] = loadTexture("C:\\Dev-Cpp\\code\\files\\emeraldfs_a.png", 32, 16);

emerald = sprite[1];

glutTimerFunc(40,timer,1);
glutMainLoop ();

//Free our texture
FreeTexture(sprite[0]);
FreeTexture(sprite[1]);
FreeTexture(sprite[2]);

FreeTexture(tile[0]);
FreeTexture(tile[1]);

return 0;
}


here is my keyboard include file

#ifndef KEYBOARD_H
#define KEYBOARD_H

extern GLfloat meow;
extern GLuint emerald;
extern GLuint sprite[4];
int count;

void movedown(void) {
meow = meow - 16.0;
emerald = sprite[2];
}

void timer(int x) {
glutPostRedisplay();
glutTimerFunc(40, timer, 1);
}

void keyboard(unsigned char key, int x, int y) {
if (key==27) {
glutLeaveGameMode();
exit(0);
}
/* if (key==113) {
meow = meow - 16.0;
emerald = sprite[2];
}
*/
}

void controls(int key, int x, int y) {
switch(key) {
case GLUT_KEY_DOWN:
//for (count = 0; count < 1600; count++)
movedown();
break;
}
}
#endif

vivek vyas
09-05-2009, 02:39 PM
its on increment/decrement operater
take it as example
meow = meow - 16.0(tspeed);
case 'i';
tspeed+=2.0f;
break;
case 'k';
tspeed-=2.0f;
break;
i think it might be programming based question

todayman
09-06-2009, 06:51 AM
Essentially you need to know the elapsed time from the last frame to the current frame, then use that to adjust the displacement of the sprite proportionally.