From: Clyne Sullivan Date: Sat, 12 Sep 2015 02:34:39 +0000 (-0400) Subject: basic layered worlds :) X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=a55903b8784df7c7033983212ca974e472c51dbf;p=clyne%2Fgamedev.git basic layered worlds :) --- diff --git a/include/World.h b/include/World.h index 08ecabb..91eb81e 100644 --- a/include/World.h +++ b/include/World.h @@ -6,6 +6,8 @@ #define goWorldLeft(w) if(w->toLeft){w=w->toLeft;} #define goWorldRight(w) if(w->toRight){w=w->toRight;} +#define LAYER_SCALE 1 + class World { private: struct line_t { @@ -13,7 +15,9 @@ private: double start; // Where to change to dirt, going down (y) } __attribute__ ((packed)) *line; unsigned int lineCount; + bool root; public: + World *behind,*infront; World *toLeft,*toRight; World(void); World(const float width,World *l,World *r); @@ -22,6 +26,8 @@ public: float getWidth(void); void saveToFile(FILE *f,World *parent); void loadFromFile(FILE *f,World *parent); + void addLayer(void); + void setRoot(void); }; #endif // WORLD_H diff --git a/include/common.h b/include/common.h index 2a6d66b..559bfd2 100644 --- a/include/common.h +++ b/include/common.h @@ -16,7 +16,7 @@ typedef struct{float x; float y;}vec2; #define SCREEN_WIDTH 1280 #define SCREEN_HEIGHT 800 -#define FULLSCREEN +//#define FULLSCREEN #define HLINE (2.0f / (SCREEN_WIDTH / 4)) diff --git a/src/UIClass.cpp b/src/UIClass.cpp index 56a9f47..731e387 100644 --- a/src/UIClass.cpp +++ b/src/UIClass.cpp @@ -1,6 +1,7 @@ #include extern Player player; +extern World *currentWorld; void UIClass::handleEvents(){ SDL_Event e; @@ -20,6 +21,12 @@ void UIClass::handleEvents(){ case SDLK_a: ///A player.velg.x = -10; break; + case SDLK_i: + if(currentWorld->behind)currentWorld=currentWorld->behind; + break; + case SDLK_k: + if(currentWorld->infront)currentWorld=currentWorld->infront; + break; default: break; } diff --git a/src/World.cpp b/src/World.cpp index c2dc62d..6b1b317 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -4,7 +4,8 @@ World::World(void){ line=NULL; lineCount=0; - toLeft=toRight=NULL; + toLeft=toRight=behind=infront=NULL; + root=false; } World::World(const float width,World *l,World *r){ unsigned int i; @@ -16,6 +17,8 @@ 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(); + } + if(root){ + hline=HLINE; + back=0; + } glBegin(GL_QUADS); for(i=0;iyx>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){ v->y=line[i].start; return; - //v->x=(HLINE*i)-1+HLINE; }else if(v->x+width>(HLINE*i)-1&&v->x+width<(HLINE*i)-1+HLINE){ v->y=line[i].start; return; - //v->x=(HLINE*i)-1-width; } }else if(v->y>line[i].start+HLINE/4){ v->y-=HLINE/8; @@ -107,4 +120,14 @@ void World::loadFromFile(FILE *f,World *parent){ toRight->loadFromFile(f,toRight); } } - +void World::addLayer(void){ + if(behind){ + behind->addLayer(); + }else{ + behind=new World((lineCount-1)*HLINE+LAYER_SCALE,NULL,NULL); + behind->infront=this; + } +} +void World::setRoot(void){ + root=true; +} diff --git a/src/main.cpp b/src/main.cpp index 67ba8a6..8768edd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,6 +93,8 @@ int main(int argc,char **argv){ w=new World(2,NULL,w2); currentWorld=w; + currentWorld->addLayer(); + currentWorld->setRoot(); // Save the world if necessary /*FILE *f=fopen("world.dat","r");