From 881a64b1db9048694e690422ab16e406e0ef3f2f Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 12 Sep 2015 21:53:09 -0400 Subject: comments --- src/World.cpp | 106 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 53 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/World.cpp b/src/World.cpp index e361608..4c5d119 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -1,56 +1,56 @@ #include #include +// Layer modulation in draw() static float drawOffsetX=0, drawOffsetY=0; +// Generates a blank world World::World(void){ line=NULL; lineCount=entCount=0; toLeft=toRight=behind=infront=NULL; } +// Generates a legit world World::World(const float width,World *l,World *r){ unsigned int i; double f; - lineCount=width/HLINE+11; - if((line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)))==NULL){ + lineCount=width/HLINE+11; // Last 10 lines won't be drawn + if((line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)))==NULL){ // Allocate buffer for lines std::cout<<"Failed to allocate memory!"<toRight){ std::cout<<"There's already a world to the left!"<toRight=this; - } + }else toLeft->toRight=this; } if(toRight){ if(toRight->toLeft){ std::cout<<"There's already a world to the right!"<toLeft=this; - } + }else toRight->toLeft=this; } - line[0].start=(grand()%100)/100.0f-0.8f; // lazy - if(line[0].start>-0.5f)line[0].start=-0.7f; - for(i=10;i-0.5f)line[0].start=-0.7f; // Don't let the ground take up too much of the window + for(i=10;i255)r=255;else if(r<0)r=0; if(g>255)g=255;else if(g<0)g=0; @@ -60,71 +60,70 @@ void safeSetColor(int r,int g,int b){ void World::draw(void){ unsigned int i; float x,y,hline=HLINE; - static World *root,*cur; + World *root,*cur; int shade; root=cur=this; LOOP: - if(cur->behind){ - drawOffsetX+=(cur->getWidth()-cur->behind->getWidth())/2; - drawOffsetY+=.3; + if(cur->behind){ // If there's a layer behind us, + drawOffsetX+=(cur->getWidth()-cur->behind->getWidth())/2; // set drawOffsetX so that it will be centered behind this one + drawOffsetY+=.3; // Push it back a bit for depth-feel //hline/=2; - cur=cur->behind; - goto LOOP; - //behind->draw(); + cur=cur->behind; // Go back one + goto LOOP; // loop } -LOOP2: - shade=30*(drawOffsetY/.3); +LOOP2: // Should be in furthest back layer once this is first reached + shade=30*(drawOffsetY/.3); // Trash shaders glBegin(GL_QUADS); - for(i=0;ilineCount-10;i++){ - x=(hline*i)-1+drawOffsetX; - y=cur->line[i].start+drawOffsetY; - safeSetColor(0,200+shade,0); - glVertex2f(x ,y); + for(i=0;ilineCount-10;i++){ // Draw the layer + x=(hline*i)-1+drawOffsetX; // Pre-calculate x for 'optimization' + y=cur->line[i].start+drawOffsetY; // same, but y + safeSetColor(0,200+shade,0); // Set shaded green for grass + glVertex2f(x ,y); // Doodle glVertex2f(x+hline,y); - y-=hline*2; + y-=hline*2; // 'optimization' glVertex2f(x+hline,y); glVertex2f(x ,y); - safeSetColor(150+shade,100+shade,50+shade); + safeSetColor(150+shade,100+shade,50+shade); // Set shaded brown for dirt 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; + if(root!=cur){ // If we're still in one of the behinds + cur=cur->infront; // Move one closer + drawOffsetX-=(cur->getWidth()-cur->behind->getWidth())/2; // Take off last layer's centering + drawOffsetY-=.3; // And back-pushing //hline*=2; - goto LOOP2; + goto LOOP2; // Loop the draw }else{ - drawOffsetX=drawOffsetY=0; - for(i=0;idraw(); } } } void World::detect(vec2 *v,vec2 *vel,const float width){ unsigned int i; - // hey - // oh hai - for(i=0;iyx>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){ - if(v->yy=0;v->y=line[i].start+HLINE;} - return; - }else if(v->x+width>(HLINE*i)-1&&v->x+width<(HLINE*i)-1+HLINE){ - if(v->yy=0;v->y=line[i].start+HLINE;} - return; + for(i=0;iyx>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){ // And we're inside it ;) + vel->y=0;v->y=line[i].start+HLINE; // Correct + 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; + return; // ;) } - }else if(v->y>line[i].start+HLINE){ + }else if(v->y>line[i].start+HLINE){ // Trashy gravity handling vel->y-=.00000001; } } } +// Calculate the world's width in coordinates 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); @@ -135,6 +134,7 @@ void World::saveToFile(FILE *f,World *parent){ 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 *)); @@ -149,13 +149,13 @@ void World::loadFromFile(FILE *f,World *parent){ } } void World::addLayer(const float width){ - if(behind){ - behind->addLayer(width); + if(behind){ // If there's already a layer behind us + behind->addLayer(width); // Add it back there }else{ - behind=new World(width,NULL,NULL); + behind=new World(width,NULL,NULL); // Otherwise add it directly behind us behind->infront=this; } } void World::addEntity(void *e){ - entity[entCount++]=e; + entity[entCount++]=e; // duh } -- cgit v1.2.3