diff options
-rw-r--r-- | include/world.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 1 | ||||
-rw-r--r-- | src/ui.cpp | 5 | ||||
-rw-r--r-- | src/world.cpp | 57 |
4 files changed, 50 insertions, 15 deletions
diff --git a/include/world.h b/include/world.h index 21d6de8..3b76f19 100644 --- a/include/world.h +++ b/include/world.h @@ -11,9 +11,11 @@ private: } __attribute__ ((packed)) *line; unsigned int lineCount; int x_start; + World *behind,*infront; public: World(unsigned int width); ~World(void); + void addLayer(unsigned int width); void draw(vec2 *vec); void detect(vec2 *v,vec2 *vel,const float width); }; diff --git a/src/main.cpp b/src/main.cpp index 5d578aa..ff36b69 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,6 +71,7 @@ int main(int argc, char *argv[]){ **************************/ World *test=new World(SCREEN_WIDTH/2); + test->addLayer(400); currentWorld=test; player=new Player(); player->spawn(0,100); @@ -89,11 +89,12 @@ namespace ui { case SDL_KEYDOWN: if(SDL_KEY==SDLK_ESCAPE)gameRunning=false; if(SDL_KEY==SDLK_a)player->vel.x=-2; - else if(SDL_KEY==SDLK_d)player->vel.x=2; + if(SDL_KEY==SDLK_d)player->vel.x=2; + if(SDL_KEY==SDLK_SPACE)player->vel.y=2; break; case SDL_KEYUP: if(SDL_KEY==SDLK_a)player->vel.x=0; - else if(SDL_KEY==SDLK_d)player->vel.x=0; + if(SDL_KEY==SDLK_d)player->vel.x=0; break; default: break; diff --git a/src/world.cpp b/src/world.cpp index 12d9591..f22de6c 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,10 +1,20 @@ #include <world.h> -#define getWidth() (lineCount*HLINE) +#define getWidth() ((lineCount-GEN_INC)*HLINE) #define GEN_INC 10 #define GRASS_HEIGHT 4 +void safeSetColor(int r,int g,int b){ + if(r>255)r=255; + if(g>255)g=255; + if(b>255)b=255; + if(r<0)r=0; + if(g<0)g=0; + if(b<0)b=0; + glColor3ub(r,g,b); +} + World::World(unsigned int width){ unsigned int i; float inc; @@ -24,11 +34,8 @@ World::World(unsigned int width){ } line[i].color=rand()%20+130; } - /*line[0].y=50; - for(i=1;i<lineCount-20;i++){ - line[i].y=rand()%5-2+line[i-1].y; - if(line[i].y<30)line[i].y=30; - }*/ + x_start=0-getWidth()/2+GEN_INC/2*HLINE; + behind=infront=NULL; } World::~World(void){ @@ -36,27 +43,42 @@ World::~World(void){ } void World::draw(vec2 *vec){ + static float yoff=50; + static int shade=0; int i,ie,v_offset; - x_start=0-getWidth()/2+GEN_INC/2*HLINE; + if(behind){ + yoff+=50; + shade+=40; + behind->draw(vec); + } v_offset=(vec->x-x_start)/HLINE; - i=v_offset-SCREEN_WIDTH/2; + i=v_offset-SCREEN_WIDTH/(yoff/25); if(i<0)i=0; - ie=v_offset+SCREEN_WIDTH/2; + ie=v_offset+SCREEN_WIDTH/(yoff/25); if(ie>lineCount)ie=lineCount; glBegin(GL_QUADS); for(i=i;i<ie;i++){ - glColor3ub(0,200,0); + line[i].y+=(yoff-50); + safeSetColor(shade,200+shade,shade); glVertex2i(x_start+i*HLINE ,line[i].y); glVertex2i(x_start+i*HLINE+HLINE,line[i].y); glVertex2i(x_start+i*HLINE+HLINE,line[i].y-GRASS_HEIGHT); glVertex2i(x_start+i*HLINE ,line[i].y-GRASS_HEIGHT); - glColor3ub(line[i].color,line[i].color-50,line[i].color-100); + safeSetColor(line[i].color+shade,line[i].color-50+shade,line[i].color-100+shade); glVertex2i(x_start+i*HLINE ,line[i].y-GRASS_HEIGHT); glVertex2i(x_start+i*HLINE+HLINE,line[i].y-GRASS_HEIGHT); glVertex2i(x_start+i*HLINE+HLINE,0); glVertex2i(x_start+i*HLINE ,0); + line[i].y-=(yoff-50); } glEnd(); + if(infront){ + yoff-=50; + shade-=40; + }else{ + yoff=50; + shade=40; + } } void World::detect(vec2 *v,vec2 *vel,const float width){ @@ -73,8 +95,17 @@ void World::detect(vec2 *v,vec2 *vel,const float width){ if(v->x<x_start){ vel->x=0; v->x=x_start+HLINE/2; - }else if(v->x>x_start+getWidth()){ + }else if(v->x+width+HLINE>x_start+getWidth()){ vel->x=0; - v->x=x_start+getWidth()-width-HLINE/2; + v->x=x_start+getWidth()-width-HLINE; + } +} + +void World::addLayer(unsigned int width){ + if(behind){ + behind->addLayer(width); + return; } + behind=new World(width); + behind->infront=this; } |