aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2015-09-20 09:42:34 -0400
committerClyne Sullivan <tullivan99@gmail.com>2015-09-20 09:42:34 -0400
commit1118f22a60dffeb204ec4845d009cf057b51f088 (patch)
tree3d38b4d96c36111fc46a50909fbe909980fd188c /src
parentb806a2d9bd6932525263a172ea2837eb1a94717d (diff)
interpolated :)
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp25
-rw-r--r--src/ui.cpp7
-rw-r--r--src/world.cpp2
3 files changed, 26 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp
index ff36b69..d31b704 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -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;
}
diff --git a/src/ui.cpp b/src/ui.cpp
index 5d3e143..877dacb 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -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;
diff --git a/src/world.cpp b/src/world.cpp
index f22de6c..d7aece1 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -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){