no , era para esto.
main.cpp
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "entidad.h"
using namespace std;
SDL_Surface *screen;
SDL_Event evento;
Uint8 *tecla;
float tiempo;
int framerate;
Centidad Epj;
Centidad Enpc[100];
Centidad Esuelo[100];
int main(int argc,char *argv[]){
SDL_Init(SDL_INIT_VIDEO);
screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
SDL_WM_SetCaption("App1",0);
//dando valores
Enpc[1].crear_entidad(TPJ);
Esuelo[0].setx(0);Esuelo[0].setw(640);Esuelo[0].sety(430);Esuelo[0].seth(20);
//suelo.crear_entidad(TCOLISION);
Epj.setx(320);
Epj.sety(50);
Epj.aloadimagen("ryu-01.png",6);
Epj.cani(0,0,0);
Epj.cani(1,1,4);
Enpc[1].setx(300);
Enpc[1].sety(360);
Enpc[1].loadimagen("ken-01.png",0);
while(1){
//iniciamos el reloj
tiempo=SDL_GetTicks();
framerate++;
//**************************************
SDL_FillRect(screen,0,SDL_MapRGB(screen->format,0,0,0));
tecla=SDL_GetKeyState(0);
SDL_PollEvent(&evento);
if(tecla[SDLK_ESCAPE])exit(1);
if(tecla[SDLK_a])pul=1;
if(framerate%6==0)Enpc[0].ani(pul);
Epj.gravedad(&Esuelo[0]);
Epj.gravedad(&Enpc[0]);
Epj.caida();
//actualizaciones visuales
Epj.drawimagen(screen,pul);
Enpc[1].drawimagen(screen,0);
SDL_Flip(screen);
//comprovamos el tiempo
tiempo =SDL_GetTicks()-tiempo;
if(tiempo < 33.33){ //si aun sobra tiempo hasta llegar a los 33.33 ms
SDL_Delay( 33.33-tiempo ); //vete a dormir durante 33.33-x milisegundos 33.33 ms es = 30 fps en 1 s
}
if(framerate==30)framerate=0;
}
}
remlet.h
int remlet(char buscar,char* entrada, char* salida,int num){
char conv[2];
for(int i=0;i<strlen(entrada);i++){
if(entrada[i]==buscar){
strcpy(salida,entrada);
itoa(num,conv,10);
if(num<10){
salida[i+1]='0';
salida[i+2]=conv[0];
}
else {
salida[i+1]= conv[0];
salida[i+2]= conv[1];
}
return 1;
}
}
return 0;
}
entidad.h
#include "remlet.h"
//TIPOS
#define TPJ 1
#define TMOB 2
#define TOBJETO 3
#define TCOLISION 4
//ESTADO
#define _EQUIETO 1
#define _EAIRE 2
#define _ESALTO 3
#define _ECAER 4
/**********************************************
clase para cargar 1 imagen y posicionarla en 1 posicion
*/
int pul=0;
class Centidad{
public:
Centidad();
struct STani{
int fmax;
int fn;
int tmp;
} stani[100];
SDL_Surface *plantilla[100];
SDL_Surface *imagen;
SDL_Rect pos;
int tipo;
int estado;
int posx;
int posy;
int posw;
int posh;
int tmp1;
int sani1;
int sani2;
int crear_entidad(int t);
int loadimagen(char* file,int f);
int aloadimagen(char* file,int fmax);
int drawimagen(SDL_Surface *scr, int v);
int getx();
int gety();
int getw();
int geth();
void setx(int x);
void sety(int y);
void setw(int w);
void seth(int h);
void ani(int v);
void cani(int v,int fn,int fmax);
int choca(Centidad *ent);
int gravedad(Centidad *ent);
int saltar();
void Centidad::caida();
};
Centidad::Centidad(){
estado=1;
tmp1=0;
}
int Centidad::crear_entidad(int e){
tipo=e;
}
int Centidad::loadimagen(char *file, int f){
plantilla[f]=IMG_Load(file);
if(plantilla[f]!=0)return 1;
else return 0;
}
int Centidad::aloadimagen(char *file, int fmax){
char salida[strlen(file)];
for(int i=0;i<fmax;i++){
remlet('-',file,salida,i+1);
plantilla[i]=IMG_Load(salida);
}
}
int Centidad::drawimagen(SDL_Surface *scr,int v){
imagen=plantilla[stani[v].tmp];
SDL_BlitSurface(imagen,0,scr,&pos);
}
int Centidad::getx(){
return posx;
}
int Centidad::gety(){
return posy;
}
int Centidad::getw(){
if(imagen!=0)return imagen->w;
else return posw;
}
int Centidad::geth(){
if(imagen!=0)return imagen->h;
else posh;
}
void Centidad::setx(int x){
pos.x=posx=x;
}
void Centidad::sety(int y){
pos.y=posy=y;
}
void Centidad::setw(int w){
pos.w=posw=w;
}
void Centidad::seth(int h){
pos.h=posh=h;
}
void Centidad::ani(int v){
sani1=v;
if(sani1!=sani2){
stani[v].tmp=stani[v].fn;
sani2=sani1;
}
if(stani[v].tmp <stani[v].fn+stani[v].fmax){
stani[v].tmp++;
}
else { stani[v].tmp=stani[v].fn;pul=0;}
}
void Centidad::cani(int v,int fn,int fmax){
stani[v].fn=fn;
stani[v].fmax=fmax;
}
int Centidad::choca(Centidad *ent){
int x1=getx();
int y1=gety();
int w1=getw();
int h1=geth();
int x2=ent[0].getx();
int y2=ent[0].gety();
int w2=ent[0].getw();
int h2=ent[0].geth();
for(int i=0;i<100;i++){
x2=ent[i].getx();
y2=ent[i].gety();
w2=ent[i].getw();
h2=ent[i].geth();
if( (((x1+w1) > x2) && (x1<(x2+w2))) && (((y1+h1) > y2) && (y1 < (y2+h2))) ){
//hay colision
return 1;
}
}
return 0;
}
int Centidad::gravedad(Centidad *ent){
int ctmp=choca(ent);
if(ctmp==1){
estado=_EQUIETO;
return 1;
}
else {
estado=_ECAER;
return 0;
}
}
void Centidad::caida(){
int y=gety();
if(estado==4)sety(y+6);
}
int Centidad::saltar(){
if(estado == _EQUIETO){
}
}