diff options
-rw-r--r-- | include/World.h | 2 | ||||
-rw-r--r-- | include/common.h | 3 | ||||
-rw-r--r-- | src/World.cpp | 29 | ||||
-rw-r--r-- | src/main.cpp | 41 |
4 files changed, 36 insertions, 39 deletions
diff --git a/include/World.h b/include/World.h index 4d55c5d..fca108d 100644 --- a/include/World.h +++ b/include/World.h @@ -27,8 +27,6 @@ public: void draw(void); // Draws the world as well as any bound entities
void detect(vec2 *v,vec2 *vel,const float width); // Object / gravity detection
float getWidth(void); // Get coordinate width of world
- void saveToFile(FILE *f,World *parent); // WIP: Save the world (seed) to a file?
- void loadFromFile(FILE *f,World *parent); // No
void addLayer(const float width); // Creates a layer of said width behind the current one
void addEntity(void *e); // Adds (binds) an entity to the world
};
diff --git a/include/common.h b/include/common.h index f32aad0..290e47a 100644 --- a/include/common.h +++ b/include/common.h @@ -16,10 +16,11 @@ typedef struct{float x; float y;}vec2; #define SCREEN_WIDTH 1280 #define SCREEN_HEIGHT 720 -#define SCREEN_RATIO (float)SCREEN_WIDTH/(float)SCREEN_HEIGHT +#define SCREEN_RATIO ((float)SCREEN_WIDTH/(float)SCREEN_HEIGHT) //#define FULLSCREEN #define HLINE (2.0f / (SCREEN_WIDTH / 4)) +//#define HLINE (SCREEN_RATIO) #define irand srand #define grand rand diff --git a/src/World.cpp b/src/World.cpp index 8083d47..0e8d8fc 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -110,10 +110,10 @@ void World::detect(vec2 *v,vec2 *vel,const float width){ if(v->y<line[i].start){ // If we're inside the line
if(v->x>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){ // And we're inside it ;)
vel->y=0;v->y=line[i].start+HLINE/8; // Correct
- return; // :/
+// return; // :/
}else if(v->x+width>(HLINE*i)-1&&v->x+width<(HLINE*i)-1+HLINE){ // Same as above, but coming from right side instead of left
vel->y=0;v->y=line[i].start+HLINE/8;
- return; // ;)
+// return; // ;)
}
}else if(v->y>line[i].start+HLINE){ // Trashy gravity handling
vel->y-=.0000001;
@@ -124,31 +124,6 @@ void World::detect(vec2 *v,vec2 *vel,const float width){ float World::getWidth(void){
return (lineCount-11)*HLINE;
}
-// no
-void World::saveToFile(FILE *f,World *parent){
- fwrite(&lineCount,sizeof(unsigned int) ,1 ,f);
- fwrite(&line ,sizeof(struct line_t),lineCount,f);
- if(toLeft!=NULL&&toLeft!=parent->toLeft){
- toLeft->saveToFile(f,toLeft);
- }
- if(toRight!=NULL&&toRight!=parent->toRight){
- toRight->saveToFile(f,toRight);
- }
-}
-// no
-void World::loadFromFile(FILE *f,World *parent){
- fread(&lineCount,sizeof(unsigned int) ,1 ,f);
- line=(struct line_t *)malloc(lineCount*sizeof(struct line_t *));
- fread(&line ,sizeof(struct line_t),lineCount,f);
- if(toLeft!=NULL&&toLeft!=parent->toLeft){
- toLeft->loadFromFile(f,toLeft);
- }
- std::cout<<toRight<<" "<<parent->toRight<<std::endl;
- if(toRight!=NULL&&toRight!=parent->toRight){
- puts("A");
- toRight->loadFromFile(f,toRight);
- }
-}
void World::addLayer(const float width){
if(behind){ // If there's already a layer behind us
behind->addLayer(width); // Add it back there
diff --git a/src/main.cpp b/src/main.cpp index bed75c8..9f87c2b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,7 +28,13 @@ World *currentWorld;//u-huh void logic(); void render(); +static unsigned int initTime; +unsigned int millis(void){ + return ((float)(clock()-initTime)/(float)CLOCKS_PER_SEC)*2000; +} + int main(int argc,char **argv){ + float gw; //runs start-up procedures if(!SDL_Init(SDL_INIT_VIDEO)){ atexit(SDL_Quit); @@ -93,23 +99,22 @@ int main(int argc,char **argv){ for(jklasdf=0;jklasdf<npcAmt;jklasdf++){ currentWorld->addEntity((void *)entnpc[jklasdf]); } - - float gw; + initTime=clock(); + currentTime=initTime; while(gameRunning){ prevTime = currentTime; - currentTime = SDL_GetTicks(); + currentTime = millis(); deltaTime = currentTime - prevTime; - if(prevTime + MSEC_PER_TICK >= SDL_GetTicks()){ //the logic loop to run at a dedicated time + if(prevTime + MSEC_PER_TICK >= millis()){ //the logic loop to run at a dedicated time logic(); - prevTime = SDL_GetTicks(); + prevTime = millis(); } player.loc.x += (player.vel.x * player.speed) * deltaTime; //update the player's x based on player.loc.y += player.vel.y * deltaTime; - for(int i = 0; i < eAmt(entnpc); i++){ if(npc[i].alive == true){ npc[i].loc.y += npc[i].vel.y * deltaTime; @@ -194,6 +199,8 @@ void render(){ } void logic(){ + static unsigned int lastTime=0; + float gw; ui.handleEvents(); // Handle events if(player.right == true) {player.vel.x = .00075;} @@ -201,10 +208,27 @@ void logic(){ if(player.right == false && player.left == false) {player.vel.x = 0;} //std::cout<<"\r("<<player.loc.x<<","<<player.loc.y<<")"; - //std::cout << tickCount << std::endl; - + std::cout<<millis()-lastTime<<std::endl; + lastTime=millis(); currentWorld->detect(&player.loc,&player.vel,player.width); + gw=currentWorld->getWidth(); + if(player.loc.x+player.width>-1+gw){ + if(currentWorld->toRight){ + goWorldRight(currentWorld) + player.loc.x=-1+HLINE; + }else{ + player.loc.x=gw-1-player.width-HLINE; + } + } + if(player.loc.x<-1){ + if(currentWorld->toLeft){ + goWorldLeft(currentWorld); + player.loc.x=currentWorld->getWidth()-1-player.width-HLINE; + }else{ + player.loc.x=-1+HLINE; + } + } currentWorld->detect(&build.loc,&build.vel,build.width); for(int i = 0; i < eAmt(entnpc); i++){ @@ -213,6 +237,5 @@ void logic(){ entnpc[i]->wander((grand()%91 + 1), &npc[i].vel); } } - tickCount++; } |