diff options
-rw-r--r-- | Changelog | 9 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | assets/bg.png | bin | 0 -> 1072758 bytes | |||
-rw-r--r-- | include/common.h | 2 | ||||
-rw-r--r-- | include/world.h | 7 | ||||
-rw-r--r-- | main.cpp | 18 | ||||
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/common.cpp | 17 | ||||
-rw-r--r-- | src/entities.cpp | 2 | ||||
-rw-r--r-- | src/world.cpp | 81 |
10 files changed, 89 insertions, 52 deletions
@@ -86,3 +86,12 @@ - Makefile only builds edited files now - improved sprites - improved world drawing + +10/8/2015: +========== + + - added background image + - added grass + - added running textures to player + - added crouching + - improved world draw, world now draws player as well @@ -3,8 +3,9 @@ LIBS = -lGL -lSDL2_image -lSDL2_mixer FLAGS = -m32 -std=c++11 -Iinclude -Iinclude/freetype2 -lSDL2main -lSDL2 -lfreetype
all:
+ @rm -f out/*.o
@cd src; $(MAKE) $(MFLAGS)
- @echo " CXX main.cpp"
+ @echo " CXX main.cpp"
@g++ $(FLAGS) -o main main.cpp out/*.o $(LIBS)
clean:
diff --git a/assets/bg.png b/assets/bg.png Binary files differnew file mode 100644 index 0000000..2dbc3f9 --- /dev/null +++ b/assets/bg.png diff --git a/include/common.h b/include/common.h index b283ec8..03385cd 100644 --- a/include/common.h +++ b/include/common.h @@ -30,7 +30,7 @@ enum GENDER{ #include <entities.h> #define SCREEN_WIDTH 1280 -#define SCREEN_HEIGHT 720 +#define SCREEN_HEIGHT 740 //#define FULLSCREEN #define HLINE 3 //base unit of the world diff --git a/include/world.h b/include/world.h index 7095c3b..1f8b7e1 100644 --- a/include/world.h +++ b/include/world.h @@ -24,7 +24,8 @@ protected: * */ struct line_t { - float y,gh; + bool gs; + float y,gh[2]; unsigned char color; } __attribute__ ((packed)) *line; unsigned int lineCount; // Size of the array 'line' (aka the width of the world) @@ -45,7 +46,7 @@ public: void addLayer(unsigned int width); // Generates a new world and makes 'behind' point to it. If 'behind' // already points to a world, the new world will be set to be behind 'behind'. - virtual void draw(vec2 *vec); // Draws the world around the coordinates 'vec' + virtual void draw(Player *p); // Draws the world around the coordinates 'vec' void detect(Player *p); // Insures objects/entities stored in an Entity class stay outside of the @@ -78,7 +79,7 @@ public: ~IndoorWorld(void); void generate(unsigned int width); // Generates a flat world of width 'width' - void draw(vec2 *vec); // Draws the world (ignores layers) + void draw(Player *p); // Draws the world (ignores layers) }; #endif // WORLD_H @@ -11,6 +11,7 @@ SDL_Window *window = NULL; SDL_Surface *renderSurface = NULL; SDL_GLContext mainGLContext = NULL; +static GLuint bgImage; bool gameRunning = true; @@ -107,6 +108,8 @@ int main(int argc, char *argv[]){ } Mix_PlayMusic( music, -1 ); + bgImage=loadTexture("assets/bg.png"); + while(gameRunning){ mainLoop(); } @@ -184,10 +187,19 @@ void render(){ **** RENDER STUFF HERE **** **************************/ - currentWorld->draw(&player->loc); // Draw the world around the player - glColor3ub(255,255,255); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D,bgImage); + glBegin(GL_QUADS); + glTexCoord2i(0,1);glVertex2i(-SCREEN_WIDTH*2,0); + glTexCoord2i(1,1);glVertex2i( SCREEN_WIDTH*2,0); + glTexCoord2i(1,0);glVertex2i( SCREEN_WIDTH*2,SCREEN_HEIGHT); + glTexCoord2i(0,0);glVertex2i(-SCREEN_WIDTH*2,SCREEN_HEIGHT); + glEnd(); + glDisable(GL_TEXTURE_2D); + player->near=true; - player->draw(); // Draw the player + currentWorld->draw(player); // Draw the world & the player + glColor3ub(255,255,255); player->inv->draw(); ui::draw(); // Draw any UI elements if they need to be diff --git a/src/Makefile b/src/Makefile index f75bee9..b88dc53 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,4 +8,4 @@ OUT = `echo "" $$(ls -c $(wildcard *.cpp)) | sed s/.cpp/.o/g | sed 's/ / ..\/out @echo " CXX " $(shell echo $@ | sed 's/..\/out\///g' | sed 's/\.o/\.cpp/') @g++ $(FLAGS) -o $@ -c $(shell echo $@ | sed 's/..\/out\///g' | sed 's/\.o/\.cpp/') $(LIBS) -all: $(shell echo $(OUT)) +all: $(shell echo $(OUT)) diff --git a/src/common.cpp b/src/common.cpp index 124d999..373c08c 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,20 +1,5 @@ #include <common.h> -/*SDL_Surface* loadTexture(char* filename){ - SDL_Surface *optimizedSurface = NULL; - SDL_Surface *texture = IMG_Load(filename); - if(texture == NULL){ - std::cout << "Unable to load an image properly from " << filename << "; Error: " << IMG_GetError() << std::endl; - }else{ - optimizedSurface = SDL_ConvertSurface(texture, renderSurface->format, NULL); - if(optimizedSurface == NULL){ - std::cout << "Unable to optimize image properly from " << filename << "; Error: " << IMG_GetError() << std::endl; - } - SDL_FreeSurface(texture); - } - return optimizedSurface; -}*/ - GLuint loadTexture(const char *fileName){ SDL_Surface *image = IMG_Load(fileName); @@ -38,4 +23,4 @@ GLuint loadTexture(const char *fileName){ SDL_FreeSurface(image); return object; -}
\ No newline at end of file +} diff --git a/src/entities.cpp b/src/entities.cpp index 46d445b..49fce0f 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -32,7 +32,7 @@ void Entity::draw(void){ //draws the entities glColor3ub(255,255,0); glRectf(loc.x+width/3,loc.y+height,loc.x+width*2/3,loc.y+height+width/3); } - }if(type==STRUCTURET){ + }else{ glColor3ub(255,255,255); } if(left){ diff --git a/src/world.cpp b/src/world.cpp index ef11002..18a0725 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -11,7 +11,7 @@ #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 10 // Defines a shade increment for draw() +#define DRAW_SHADE 30 // Defines a shade increment for draw() #define INDOOR_FLOOR_HEIGHT 100 // Defines how high the base floor of an IndoorWorld should be @@ -50,9 +50,12 @@ void World::generate(unsigned int width){ // Generates the world and sets all va }else{ // If this line's y hasn't been set yet line[i].y=line[i-1].y+inc; // Set it by incrementing the previous line's y by 'inc'. } - line[i].color=rand()%20+90; // Generate a color for the dirt area of this line. This value will be used - // in the form (where n represents the color) glColor3ub(n,n-50,n-100) - line[i].gh=(getRand()%20)/3; // Create a random grass height so it looks cool + line[i].color=rand()%20+100; // Generate a color for the dirt area of this line. This value will be used + // in the form (where n represents the color) glColor3ub(n,n-50,n-100) + + line[i].gh[0]=(getRand()%16)/3.5+2; // Create a random grass height so it looks cool + line[i].gh[1]=(getRand()%16)/3.5+2; + line[i].gs=true; } x_start=0-getWidth(this)/2+GEN_INC/2*HLINE; // Calculate x_start (explained in world.h) behind=infront=NULL; // Set pointers to other worlds to NULL @@ -63,11 +66,11 @@ World::~World(void){ free(line); // Free (de-allocate) the array 'line' } -void World::draw(vec2 *vec){ +void World::draw(Player *p){ static float yoff=DRAW_Y_OFFSET; // Initialize stuff static int shade=0; static World *current; - int i,ie,v_offset,cx_start; + int i,is,ie,v_offset,cx_start; struct line_t *cline; current=this; // yeah glClearColor(.1,.3,.6,0); @@ -79,32 +82,62 @@ LOOP1: // Check for worlds behind the current one and set 'current' to th 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 + v_offset=(p->loc.x-current->x_start)/HLINE; // Calculate the player's offset in the array 'line' using the player's location 'vec' + is=v_offset-SCREEN_WIDTH/(yoff/(DRAW_Y_OFFSET/2)); // Set 'i' to that somehow + if(is<0)is=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; - shade*=-1; + //shade*=-1; glBegin(GL_QUADS); - for(i=i;i<ie-GEN_INC;i++){ // For lines in array 'line' from 'i' to 'ie' + for(i=is;i<ie-GEN_INC;i++){ // For lines in array 'line' from 'i' to 'ie' cline[i].y+=(yoff-DRAW_Y_OFFSET); // 'yoff' is always one incrementation ahead of what it should be - safeSetColor(shade,150+shade,shade); // Safely set the grass color - glVertex2i(cx_start+i*HLINE ,cline[i].y); // Draw the grass area of the line - glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y); - glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y-GRASS_HEIGHT-cline[i].gh); - glVertex2i(cx_start+i*HLINE ,cline[i].y-GRASS_HEIGHT-cline[i].gh); safeSetColor(cline[i].color+shade,cline[i].color-50+shade,cline[i].color-100+shade); // Set the shaded dirt color (safely) - glVertex2i(cx_start+i*HLINE ,cline[i].y-GRASS_HEIGHT-cline[i].gh); // Draw the dirt area of the line - glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y-GRASS_HEIGHT-cline[i].gh); + glVertex2i(cx_start+i*HLINE ,cline[i].y-GRASS_HEIGHT); + glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y-GRASS_HEIGHT); glVertex2i(cx_start+i*HLINE+HLINE,0); glVertex2i(cx_start+i*HLINE ,0); cline[i].y-=(yoff-DRAW_Y_OFFSET); // Reset 'cline[i]'`s y to what it was } glEnd(); - shade*=-1; + if(current==this){ + int ph; + ph=(p->loc.x+p->width/2-x_start)/HLINE; // Calculate what line the player is currently on + for(i=0;i<lineCount-GEN_INC;i++){ + if(p->ground==1&&i<ph+6&&i>ph-6)cline[i].gs=false; + else cline[i].gs=true; + } + for(i=0;i<entity.size()+1;i++){ + if(entity[i]->inWorld==this){ + entity[i]->draw(); + } + } + p->draw(); + } + float cgh[2]; + glBegin(GL_QUADS); + for(i=is;i<ie-GEN_INC;i++){ + memcpy(cgh,cline[i].gh,2*sizeof(float)); + if(!cline[i].gs){ + cgh[0]/=4; + cgh[1]/=4; + } + cline[i].y+=(yoff-DRAW_Y_OFFSET); + safeSetColor(shade,150+shade,shade); + glVertex2i(cx_start+i*HLINE ,cline[i].y+cgh[0]); + glVertex2i(cx_start+i*HLINE+HLINE/2,cline[i].y+cgh[0]); + glVertex2i(cx_start+i*HLINE+HLINE/2,cline[i].y-GRASS_HEIGHT); + glVertex2i(cx_start+i*HLINE ,cline[i].y-GRASS_HEIGHT); + glVertex2i(cx_start+i*HLINE+HLINE/2,cline[i].y+cgh[1]); + glVertex2i(cx_start+i*HLINE+HLINE ,cline[i].y+cgh[1]); + glVertex2i(cx_start+i*HLINE+HLINE ,cline[i].y-GRASS_HEIGHT); + glVertex2i(cx_start+i*HLINE+HLINE/2,cline[i].y-GRASS_HEIGHT); + cline[i].y-=(yoff-DRAW_Y_OFFSET); + } + glEnd(); + //shade*=-1; safeSetColor(255+shade*2,0+shade,0+shade); for(i=0;i<current->platform.size();i++){ glRectf(current->platform[i].p1.x,current->platform[i].p1.y+yoff-DRAW_Y_OFFSET, @@ -118,11 +151,6 @@ LOOP2: // Draw each world }else{ // Otherwise reset static values and return yoff=DRAW_Y_OFFSET; shade=0; - for(i=0;i<entity.size()+1;i++){ - if(entity[i]->inWorld==this){ - entity[i]->draw(); - } - } } } @@ -269,9 +297,9 @@ void IndoorWorld::generate(unsigned int width){ // Generates a flat area of wid x_start=0-getWidth(this)/2+GEN_INC/2*HLINE; // Calculate x_start (explained in world.h) } -void IndoorWorld::draw(vec2 *vec){ +void IndoorWorld::draw(Player *p){ int i,ie,v_offset; - v_offset=(vec->x-x_start)/HLINE; // Calculate the player's offset in the array 'line' using the player's location 'vec' + v_offset=(p->loc.x-x_start)/HLINE; // Calculate the player's offset in the array 'line' using the player's location 'vec' i=v_offset-SCREEN_WIDTH/2; // um if(i<0)i=0; // If the player is past the start of that world 'i' should start at the beginning // of the world @@ -291,4 +319,5 @@ void IndoorWorld::draw(vec2 *vec){ if(entity[i]->inWorld==this) entity[i]->draw(); } + p->draw(); } |