]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
stuff
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 19 Sep 2015 12:23:11 +0000 (08:23 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 19 Sep 2015 12:23:11 +0000 (08:23 -0400)
include/world.h
src/main.cpp
src/ui.cpp
src/world.cpp

index 21d6de8c96e70ce29b9b9f67ce6f56d19f740fac..3b76f192868cce64b53879dc8fb2cfe5c484cd49 100644 (file)
@@ -11,9 +11,11 @@ private:
        } __attribute__ ((packed)) *line;
        unsigned int lineCount;
        int x_start;
+       World *behind,*infront;
 public:
        World(unsigned int width);
        ~World(void);
+       void addLayer(unsigned int width);
        void draw(vec2 *vec);
        void detect(vec2 *v,vec2 *vel,const float width);
 };
index 5d578aafba0a191a55c32121547425b40bfe3754..ff36b69b2619eb76836e296cece8fdc843986edb 100644 (file)
@@ -71,6 +71,7 @@ int main(int argc, char *argv[]){
        **************************/
 
        World *test=new World(SCREEN_WIDTH/2);
+       test->addLayer(400);
        currentWorld=test;
        player=new Player();
        player->spawn(0,100);
index d7aabc05953562e4e98f0f7c0bf9ae7d7fdc06ee..5d3e143a766e7791d90b8ecd0ee7c1a11226fa7f 100644 (file)
@@ -89,11 +89,12 @@ namespace ui {
                        case SDL_KEYDOWN:
                                if(SDL_KEY==SDLK_ESCAPE)gameRunning=false;
                                if(SDL_KEY==SDLK_a)player->vel.x=-2;
-                               else if(SDL_KEY==SDLK_d)player->vel.x=2;
+                               if(SDL_KEY==SDLK_d)player->vel.x=2;
+                               if(SDL_KEY==SDLK_SPACE)player->vel.y=2;
                                break;
                        case SDL_KEYUP:
                                if(SDL_KEY==SDLK_a)player->vel.x=0;
-                               else if(SDL_KEY==SDLK_d)player->vel.x=0;
+                               if(SDL_KEY==SDLK_d)player->vel.x=0;
                                break;
                        default:
                                break;
index 12d9591280767ec42bbc61c054e076f5795b2639..f22de6c23abbfb21ad743f7beb904ad7e9b90d62 100644 (file)
@@ -1,10 +1,20 @@
 #include <world.h>
 
-#define getWidth() (lineCount*HLINE)
+#define getWidth() ((lineCount-GEN_INC)*HLINE)
 
 #define GEN_INC 10
 #define GRASS_HEIGHT 4
 
+void safeSetColor(int r,int g,int b){
+       if(r>255)r=255;
+       if(g>255)g=255;
+       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){
        unsigned int i;
        float inc;
@@ -24,11 +34,8 @@ World::World(unsigned int width){
                }
                line[i].color=rand()%20+130;
        }
-       /*line[0].y=50;
-       for(i=1;i<lineCount-20;i++){
-               line[i].y=rand()%5-2+line[i-1].y;
-               if(line[i].y<30)line[i].y=30;
-       }*/
+       x_start=0-getWidth()/2+GEN_INC/2*HLINE;
+       behind=infront=NULL;
 }
 
 World::~World(void){
@@ -36,27 +43,42 @@ World::~World(void){
 }
 
 void World::draw(vec2 *vec){
+       static float yoff=50;
+       static int shade=0;
        int i,ie,v_offset;
-       x_start=0-getWidth()/2+GEN_INC/2*HLINE;
+       if(behind){
+               yoff+=50;
+               shade+=40;
+               behind->draw(vec);
+       }
        v_offset=(vec->x-x_start)/HLINE;
-       i=v_offset-SCREEN_WIDTH/2;
+       i=v_offset-SCREEN_WIDTH/(yoff/25);
        if(i<0)i=0;
-       ie=v_offset+SCREEN_WIDTH/2;
+       ie=v_offset+SCREEN_WIDTH/(yoff/25);
        if(ie>lineCount)ie=lineCount;
        glBegin(GL_QUADS);
                for(i=i;i<ie;i++){
-                       glColor3ub(0,200,0);
+                       line[i].y+=(yoff-50);
+                       safeSetColor(shade,200+shade,shade);
                        glVertex2i(x_start+i*HLINE      ,line[i].y);
                        glVertex2i(x_start+i*HLINE+HLINE,line[i].y);
                        glVertex2i(x_start+i*HLINE+HLINE,line[i].y-GRASS_HEIGHT);
                        glVertex2i(x_start+i*HLINE              ,line[i].y-GRASS_HEIGHT);
-                       glColor3ub(line[i].color,line[i].color-50,line[i].color-100);
+                       safeSetColor(line[i].color+shade,line[i].color-50+shade,line[i].color-100+shade);
                        glVertex2i(x_start+i*HLINE      ,line[i].y-GRASS_HEIGHT);
                        glVertex2i(x_start+i*HLINE+HLINE,line[i].y-GRASS_HEIGHT);
                        glVertex2i(x_start+i*HLINE+HLINE,0);
                        glVertex2i(x_start+i*HLINE              ,0);
+                       line[i].y-=(yoff-50);
                }
        glEnd();
+       if(infront){
+               yoff-=50;
+               shade-=40;
+       }else{
+               yoff=50;
+               shade=40;
+       }
 }
 
 void World::detect(vec2 *v,vec2 *vel,const float width){
@@ -73,8 +95,17 @@ void World::detect(vec2 *v,vec2 *vel,const float width){
        if(v->x<x_start){
                vel->x=0;
                v->x=x_start+HLINE/2;
-       }else if(v->x>x_start+getWidth()){
+       }else if(v->x+width+HLINE>x_start+getWidth()){
                vel->x=0;
-               v->x=x_start+getWidth()-width-HLINE/2;
+               v->x=x_start+getWidth()-width-HLINE;
+       }
+}
+
+void World::addLayer(unsigned int width){
+       if(behind){
+               behind->addLayer(width);
+               return;
        }
+       behind=new World(width);
+       behind->infront=this;
 }