From f28fda996331de5dac8fc2f20ea0898527239fe5 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 7 Oct 2015 08:39:53 -0400 Subject: world improvement --- Changelog | 9 +++++++ Makefile | 3 ++- assets/bg.png | Bin 0 -> 1072758 bytes include/common.h | 2 +- include/world.h | 7 ++--- main.cpp | 18 ++++++++++--- src/Makefile | 2 +- src/common.cpp | 17 +----------- src/entities.cpp | 2 +- src/world.cpp | 81 +++++++++++++++++++++++++++++++++++++------------------ 10 files changed, 89 insertions(+), 52 deletions(-) create mode 100644 assets/bg.png diff --git a/Changelog b/Changelog index fecb56d..a55b872 100644 --- a/Changelog +++ b/Changelog @@ -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 diff --git a/Makefile b/Makefile index 983f636..c9f143a 100644 --- a/Makefile +++ b/Makefile @@ -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 new file mode 100644 index 0000000..2dbc3f9 Binary files /dev/null and b/assets/bg.png differ 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 #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 diff --git a/main.cpp b/main.cpp index e1b8d3d..9a457d1 100644 --- a/main.cpp +++ b/main.cpp @@ -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 -/*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;iloc.x+p->width/2-x_start)/HLINE; // Calculate what line the player is currently on + for(i=0;iground==1&&iph-6)cline[i].gs=false; + else cline[i].gs=true; + } + for(i=0;iinWorld==this){ + entity[i]->draw(); + } + } + p->draw(); + } + float cgh[2]; + glBegin(GL_QUADS); + for(i=is;iplatform.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;iinWorld==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(); } -- cgit v1.2.3