]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
interpolated :)
authorClyne Sullivan <tullivan99@gmail.com>
Sun, 20 Sep 2015 13:42:34 +0000 (09:42 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sun, 20 Sep 2015 13:42:34 +0000 (09:42 -0400)
src/main.cpp
src/ui.cpp
src/world.cpp

index ff36b69b2619eb76836e296cece8fdc843986edb..d31b704cd889757f39806bcb0849608c91954e9b 100644 (file)
@@ -5,11 +5,18 @@
 #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;
@@ -76,9 +83,19 @@ int main(int argc, char *argv[]){
        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();
        }
        
        /**************************
@@ -111,7 +128,7 @@ void render(){
 
        currentWorld->draw(&player->loc);
        player->draw();
-       //ui::putString(0,0,"Hello");
+       ui::putString(0,0,"Hello");
 
        /**************************
        ****  CLOSE THE LOOP   ****
@@ -124,7 +141,7 @@ void render(){
 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;
 }      
index 5d3e143a766e7791d90b8ecd0ee7c1a11226fa7f..877dacbdd998f4fd85a49ec373d0054fe134e83a 100644 (file)
@@ -67,6 +67,7 @@ namespace ui {
                        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,...){
@@ -88,9 +89,9 @@ namespace ui {
                                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;
index f22de6c23abbfb21ad743f7beb904ad7e9b90d62..d7aece19b6248b55aba5402b2c75cf9a0984a446 100644 (file)
@@ -89,7 +89,7 @@ void World::detect(vec2 *v,vec2 *vel,const float width){
                vel->y=0;
                v->y=line[i].y+HLINE/2;
        }else{
-               vel->y-=.05;
+               vel->y-=.01;
        }
        // Horizontal checks
        if(v->x<x_start){