#include #define getWidth(w) ((w->lineCount-GEN_INC)*HLINE) // Calculates the width of world 'w' #define GEN_INC 10 // Defines at what interval y values should be calculated for the array 'line'. // As explained in World(), the last few lines in the array 'line' are incorrectly calculated // or not calculated at all, so GEN_INC is also used to decrease 'lineCount' in functions like draw() // and detect(). #define GRASS_HEIGHT 4 // Defines how long the grass layer of a line should be in multiples of HLINE. #define DRAW_Y_OFFSET 50 // Defines how many pixels each layer should be offset from each other on the y axis when drawn. #define DRAW_SHADE 40 // Defines a shade increment for draw() void safeSetColor(int r,int g,int b){ // safeSetColor() is an alternative to directly using glColor3ub() to set if(r>255)r=255; // the color for OpenGL drawing. safeSetColor() checks for values that are if(g>255)g=255; // outside the range of an unsigned character and sets them to a safer value. 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){ // Generates the world and sets all variables contained in the World class. unsigned int i; // Used for 'for' loops float inc; // See line 37 lineCount=width+GEN_INC; // Sets line count to the desired width plus GEN_INC to remove incorrect line calculations. line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)); // Allocate memory for the array 'line' line[0].y=80; // Sets a starting value for the world generation to be based off of for(i=GEN_INC;i110)line[i].y=110; // y value maximum } for(i=0;ibehind){ // so that once LOOP1 is exited 'current' contains the furthest back world. yoff+=DRAW_Y_OFFSET; shade+=DRAW_SHADE; current=current->behind; goto LOOP1; } LOOP2: // Draw each world v_offset=(vec->x-current->x_start)/HLINE; // Calculate the player's offset in the array 'line' using the player's location 'vec' i=v_offset-SCREEN_WIDTH/(yoff/(DRAW_Y_OFFSET/2)); // Set 'i' to that somehow if(i<0)i=0; // If the player is past the start of that world 'i' should start at the beginning // of the world ie=v_offset+SCREEN_WIDTH/(yoff/(DRAW_Y_OFFSET/2)); // Set how many lines should be drawn (the drawing for loop loops from 'i' to 'ie') if(ie>current->lineCount)ie=current->lineCount; // If the player is past the end of that world 'ie' should contain the end of that world cline=current->line; // 'cline' and 'cx_start' only exist to make the for loop clear (and maybe make it faster) cx_start=current->x_start; glBegin(GL_QUADS); for(i=i;iinfront){ // If there's a world in front of the one that was just drawn yoff-=DRAW_Y_OFFSET; // draw it as well. shade-=DRAW_SHADE; current=current->infront; goto LOOP2; }else{ // Otherwise reset static values and return yoff=DRAW_Y_OFFSET; shade=0; } } void World::detect(vec2 *v,vec2 *vel,const float width){ unsigned int i; i=(v->x+width/2-x_start)/HLINE; // Calculate what line the player is currently on if(v->y<=line[i].y){ // Snap the player to the top of that line if the player is inside it vel->y=0; v->y=line[i].y+HLINE/2; }else{ // If the player is above the ground do some gravity stuff vel->y-=.01; } if(v->xx=0; v->x=x_start+HLINE/2; }else if(v->x+width+HLINE>x_start+getWidth(this)){ vel->x=0; v->x=x_start+getWidth(this)-width-HLINE; } } /* * The rest of these functions are explained well enough in world.h ;) */ void World::addLayer(unsigned int width){ if(behind){ behind->addLayer(width); return; } behind=new World(width); behind->infront=this; } World *World::goWorldLeft(vec2 *loc,const float width){ if(toLeft&&loc->xx=toLeft->x_start+getWidth(toLeft)-HLINE*10; loc->y=toLeft->line[0].y; return toLeft; } return this; } World *World::goWorldRight(vec2 *loc,const float width){ if(toRight&&loc->x+width>x_start+getWidth(this)-HLINE*10){ loc->x=toRight->x_start+HLINE*10; loc->y=toRight->line[toRight->lineCount-GEN_INC-1].y; return toRight; } return this; } World *World::goWorldBack(vec2 *loc,const float width){ if(behind&&loc->x>(int)(0-getWidth(behind)/2)&&loc->xx>(int)(0-getWidth(infront)/2)&&loc->x