]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
world improvement
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 7 Oct 2015 12:39:53 +0000 (08:39 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 7 Oct 2015 12:39:53 +0000 (08:39 -0400)
Changelog
Makefile
assets/bg.png [new file with mode: 0644]
include/common.h
include/world.h
main.cpp
src/Makefile
src/common.cpp
src/entities.cpp
src/world.cpp

index fecb56d83ca1c7fa926085eaa340940f558b696d..a55b87200d5ee3b0792d225c9f6fbccf924b04e0 100644 (file)
--- a/Changelog
+++ b/Changelog
        - 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
index 983f6365455310c3443f3ece46850d122525013e..c9f143a5d3ab50a91d68979fc882de1fbcc634ce 100644 (file)
--- 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\r
 \r
 all:\r
+       @rm -f out/*.o\r
        @cd src; $(MAKE) $(MFLAGS)\r
-       @echo "  CXX main.cpp"\r
+       @echo "  CXX  main.cpp"\r
        @g++ $(FLAGS) -o main main.cpp out/*.o $(LIBS)\r
 \r
 clean:\r
diff --git a/assets/bg.png b/assets/bg.png
new file mode 100644 (file)
index 0000000..2dbc3f9
Binary files /dev/null and b/assets/bg.png differ
index b283ec88e53f902291a304adaa47b6102b45e5db..03385cd020a1c4805e79a0ab23f7201d7770a2a6 100644 (file)
@@ -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
index 7095c3bb1f8feb6867249e53f43f5ef0266ce9dc..1f8b7e10bae16eda074f38d055ea0395292bb224 100644 (file)
@@ -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
index e1b8d3d02f9edf1d5461ceedc25604fc45bdc147..9a457d13b7b89364c6b84349790601903c8f3e1d 100644 (file)
--- 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
index f75bee977da920d34e3338a9181e08b6fe162cb4..b88dc5314620e430b4583fa94618d7da23c7187c 100644 (file)
@@ -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))
index 124d9990b145e12dc2c75086eb6cb53fb9888ce1..373c08c07771ef2b6c9dc8ae8418aef64c1a3d31 100644 (file)
@@ -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
+}
index 46d445b19b5ef8ca43566104f39555c7b308c661..49fce0f5eb6b61323cd3bdb4ec0ed19c81d0631c 100644 (file)
@@ -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){
index ef11002029e19be5343c007877f6239fcc9d7d5c..18a07259bb3e322146f87c175f87f1e7f9738c5f 100644 (file)
@@ -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();
 }