From a744d3312dd2f0253f7965253b3eff683cca3453 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 12 Sep 2015 18:44:23 -0400 Subject: Real layered worlds n stuff --- include/World.h | 6 +---- src/UIClass.cpp | 12 ++++++++-- src/World.cpp | 71 ++++++++++++++++++++++++++++++--------------------------- src/main.cpp | 7 +++--- 4 files changed, 51 insertions(+), 45 deletions(-) diff --git a/include/World.h b/include/World.h index 13ccafc..09e06d9 100644 --- a/include/World.h +++ b/include/World.h @@ -6,8 +6,6 @@ #define goWorldLeft(w) if(w->toLeft){w=w->toLeft;} #define goWorldRight(w) if(w->toRight){w=w->toRight;} -#define LAYER_SCALE 2 - class World { private: struct line_t { @@ -15,7 +13,6 @@ private: double start; // Where to change to dirt, going down (y) } __attribute__ ((packed)) *line; unsigned int lineCount; - bool root,drawn; public: World *behind,*infront; World *toLeft,*toRight; @@ -26,8 +23,7 @@ public: float getWidth(void); void saveToFile(FILE *f,World *parent); void loadFromFile(FILE *f,World *parent); - void addLayer(void); - void setRoot(void); + void addLayer(const float width); }; #endif // WORLD_H diff --git a/src/UIClass.cpp b/src/UIClass.cpp index 318ba00..c1c1180 100644 --- a/src/UIClass.cpp +++ b/src/UIClass.cpp @@ -17,8 +17,16 @@ void UIClass::handleEvents(){ if(e.key.keysym.sym == SDLK_d) player.right = true; if(e.key.keysym.sym == SDLK_a) player.left = true; if(e.key.keysym.sym == SDLK_SPACE) player.loc.y += 10; - if(e.key.keysym.sym == SDLK_i)if(currentWorld->behind) currentWorld=currentWorld->behind; - if(e.key.keysym.sym == SDLK_k)if(currentWorld->infront)currentWorld=currentWorld->infront; + if(e.key.keysym.sym == SDLK_i) + if(currentWorld->behind){ + player.loc.x-=(currentWorld->getWidth()-currentWorld->behind->getWidth())/2; + currentWorld=currentWorld->behind; + } + if(e.key.keysym.sym == SDLK_k) + if(currentWorld->infront){ + player.loc.x+=(currentWorld->infront->getWidth()-currentWorld->getWidth())/2; + currentWorld=currentWorld->infront; + } break; case SDL_KEYUP: if(e.key.keysym.sym == SDLK_d) player.right = false; diff --git a/src/World.cpp b/src/World.cpp index fb9e315..82735bd 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -5,7 +5,6 @@ World::World(void){ line=NULL; lineCount=0; toLeft=toRight=behind=infront=NULL; - root=false; } World::World(const float width,World *l,World *r){ unsigned int i; @@ -18,7 +17,6 @@ World::World(const float width,World *l,World *r){ toLeft=l; toRight=r; behind=infront=NULL; - root=false; if(toLeft){ if(toLeft->toRight){ std::cout<<"There's already a world to the left!"<draw(); - }else{ - hline*=.5; - back+=.2; - } - if(root){ - hline=HLINE; - back=0; - }else{ - hline*=2; - back-=.2; + float x,y; + static World *root,*cur; + root=cur=this; +LOOP: + if(cur->behind){ + drawOffsetX+=(cur->getWidth()-cur->behind->getWidth())/2; + drawOffsetY+=.3; + cur=cur->behind; + goto LOOP; + //behind->draw(); } +LOOP2: glBegin(GL_QUADS); - for(i=0;ilineCount-10;i++){ + x=(HLINE*i)-1+drawOffsetX; + y=cur->line[i].start+drawOffsetY; + glColor3ub(0,200,0); + glVertex2f(x ,y); + glVertex2f(x+HLINE,y); + y-=HLINE*2; + glVertex2f(x+HLINE,y); + glVertex2f(x ,y); glColor3ub(150,100,50); - glVertex2f((hline*i)-1 ,line[i].start-hline*2+back); - glVertex2f((hline*i)-1+hline,line[i].start-hline*2+back); - glVertex2f((hline*i)-1+hline,-1+back); - glVertex2f((hline*i)-1 ,-1+back); + glVertex2f(x ,y); + glVertex2f(x+HLINE,y); + glVertex2f(x+HLINE,-1); + glVertex2f(x ,-1); } glEnd(); + if(root!=cur){ + cur=cur->infront; + drawOffsetX-=(cur->getWidth()-cur->behind->getWidth())/2; + drawOffsetY-=.3; + goto LOOP2; + }else{ + drawOffsetX=drawOffsetY=0; + } } void World::detect(vec2 *v,const float width){ unsigned int i; @@ -127,14 +133,11 @@ void World::loadFromFile(FILE *f,World *parent){ toRight->loadFromFile(f,toRight); } } -void World::addLayer(void){ +void World::addLayer(const float width){ if(behind){ - behind->addLayer(); + behind->addLayer(width); }else{ - behind=new World((lineCount-1)*HLINE+LAYER_SCALE,NULL,NULL); + behind=new World(width,NULL,NULL); behind->infront=this; } } -void World::setRoot(void){ - root=true; -} diff --git a/src/main.cpp b/src/main.cpp index 3e8d04f..60564c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -97,12 +97,10 @@ int main(int argc,char **argv){ // Generate the world World *w=NULL,*w2=NULL; w2=new World(4,w,NULL); - w=new World(10,NULL,w2); + w=new World(2,NULL,w2); currentWorld=w; - currentWorld->setRoot(); - currentWorld->addLayer(); - //currentWorld->addLayer(); + currentWorld->addLayer(3); //currentWorld->addLayer(); // Save the world if necessary @@ -213,6 +211,7 @@ void logic(){ if(player.left == true) {player.vel.x = -.002;} if(player.right == false && player.left == false) {player.vel.x = 0;} + std::cout<<"\r("<detect(&player.loc,player.width); currentWorld->detect(&npc.loc,npc.height); -- cgit v1.2.3