diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-12 18:44:23 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-12 18:44:23 -0400 |
commit | a744d3312dd2f0253f7965253b3eff683cca3453 (patch) | |
tree | 6877af9fc2bd27fdab00b8fd946195939035f21e | |
parent | d68b30ee201579e06f050a9448e4dcf883860064 (diff) |
Real layered worlds n stuff
-rw-r--r-- | include/World.h | 6 | ||||
-rw-r--r-- | src/UIClass.cpp | 12 | ||||
-rw-r--r-- | src/World.cpp | 71 | ||||
-rw-r--r-- | 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!"<<std::endl;
@@ -49,39 +47,47 @@ World::World(const float width,World *l,World *r){ }
}
}
-static float hline=HLINE;
-static float back=0;
+static float drawOffsetX=0,
+ drawOffsetY=0;
void World::draw(void){
unsigned int i;
- if(behind){
- hline*=.5;
- back+=.2;
- behind->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;i<lineCount-10;i++){
- glColor3ub(0,255,0);
- glVertex2f((hline*i)-1 ,line[i].start+back);
- glVertex2f((hline*i)-1+hline,line[i].start+back);
- glVertex2f((hline*i)-1+hline,line[i].start-hline*2+back);
- glVertex2f((hline*i)-1 ,line[i].start-hline*2+back);
+ for(i=0;i<cur->lineCount-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("<<player.loc.x<<","<<player.loc.y<<")"; currentWorld->detect(&player.loc,player.width); currentWorld->detect(&npc.loc,npc.height); |