#include <cstdio>
#include <chrono>
+#define TICKS_PER_SEC 20
+#define MSEC_PER_TICK (1000/TICKS_PER_SEC)
+
SDL_Window *window = NULL;
SDL_Surface *renderSurface = NULL;
SDL_GLContext mainGLContext = NULL;
bool gameRunning = true;
+static unsigned int tickCount = 0,
+ prevTime = 0,
+ currentTime = 0,
+ deltaTime = 0;
World *currentWorld=NULL;
Player *player;
player=new Player();
player->spawn(0,100);
+ currentTime=millis();
+
while(gameRunning){
+ prevTime = currentTime;
+ currentTime = millis();
+ deltaTime = currentTime - prevTime;
+
+ if(prevTime + MSEC_PER_TICK >= millis()){
+ logic();
+ prevTime = millis();
+ }
+
render();
- logic();
}
/**************************
currentWorld->draw(&player->loc);
player->draw();
- //ui::putString(0,0,"Hello");
+ ui::putString(0,0,"Hello");
/**************************
**** CLOSE THE LOOP ****
void logic(){
ui::handleEvents();
currentWorld->detect(&player->loc,&player->vel,player->width);
- player->loc.y+=player->vel.y;
- player->loc.x+=player->vel.x;
+ player->loc.y+=player->vel.y*deltaTime;
+ player->loc.x+=player->vel.x*deltaTime;
std::cout<<"("<<player->loc.x<<","<<player->loc.y<<")"<<std::endl;
}
glDisable(GL_TEXTURE_2D);
xo+=w;
free(buf);
+ glDeleteTextures(1,&ftex);
}while(s[i++]);
}
void putText(const float x,const float y,const char *str,...){
break;
case SDL_KEYDOWN:
if(SDL_KEY==SDLK_ESCAPE)gameRunning=false;
- if(SDL_KEY==SDLK_a)player->vel.x=-2;
- if(SDL_KEY==SDLK_d)player->vel.x=2;
- if(SDL_KEY==SDLK_SPACE)player->vel.y=2;
+ if(SDL_KEY==SDLK_a)player->vel.x=-.15;
+ if(SDL_KEY==SDLK_d)player->vel.x=.15;
+ if(SDL_KEY==SDLK_SPACE)player->vel.y=.25;
break;
case SDL_KEYUP:
if(SDL_KEY==SDLK_a)player->vel.x=0;