]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
basic layered worlds :)
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 12 Sep 2015 02:34:39 +0000 (22:34 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 12 Sep 2015 02:34:39 +0000 (22:34 -0400)
include/World.h
include/common.h
src/UIClass.cpp
src/World.cpp
src/main.cpp

index 08ecabb4095803150ea2412cec5ef48d4923a5c8..91eb81e8bca3eca0c4065e4b33ad31a0060df936 100644 (file)
@@ -6,6 +6,8 @@
 #define goWorldLeft(w)  if(w->toLeft){w=w->toLeft;}\r
 #define goWorldRight(w) if(w->toRight){w=w->toRight;}\r
 \r
+#define LAYER_SCALE 1\r
+\r
 class World {\r
 private:\r
        struct line_t {\r
@@ -13,7 +15,9 @@ private:
                double start; // Where to change to dirt, going down (y)\r
        } __attribute__ ((packed)) *line;\r
        unsigned int lineCount;\r
+       bool root;\r
 public:\r
+       World *behind,*infront;\r
        World *toLeft,*toRight;\r
        World(void);\r
        World(const float width,World *l,World *r);\r
@@ -22,6 +26,8 @@ public:
        float getWidth(void);\r
        void saveToFile(FILE *f,World *parent);\r
        void loadFromFile(FILE *f,World *parent);\r
+       void addLayer(void);\r
+       void setRoot(void);\r
 };\r
 \r
 #endif // WORLD_H
index 2a6d66b696cefcaf96c467d3691043f07f41fe66..559bfd26fb50efa78e11a27330b81b7f9dbca8bd 100644 (file)
@@ -16,7 +16,7 @@ typedef struct{float x; float y;}vec2;
 
 #define SCREEN_WIDTH  1280
 #define SCREEN_HEIGHT 800
-#define FULLSCREEN
+//#define FULLSCREEN
 
 #define HLINE (2.0f / (SCREEN_WIDTH / 4))
 
index 56a9f47594c1295e9a5652b9d78e6ccfdfeb9cfc..731e3879d28828a572bbce4b56353f4b19050fb2 100644 (file)
@@ -1,6 +1,7 @@
 #include <UIClass.h>
 
 extern Player player;
+extern World *currentWorld;
 
 void UIClass::handleEvents(){
        SDL_Event e;
@@ -20,6 +21,12 @@ void UIClass::handleEvents(){
                        case SDLK_a:                                    ///A
                                player.velg.x = -10;
                                break;
+                       case SDLK_i:
+                               if(currentWorld->behind)currentWorld=currentWorld->behind;
+                               break;
+                       case SDLK_k:
+                               if(currentWorld->infront)currentWorld=currentWorld->infront;
+                               break;
                        default:
                                break;
                        }
index c2dc62dc92720eee084dad520108d5bf93708810..6b1b317359eb20c91e0ddc65ca2e4c1ebb120590 100644 (file)
@@ -4,7 +4,8 @@
 World::World(void){\r
        line=NULL;\r
        lineCount=0;\r
-       toLeft=toRight=NULL;\r
+       toLeft=toRight=behind=infront=NULL;\r
+       root=false;\r
 }\r
 World::World(const float width,World *l,World *r){\r
        unsigned int i;\r
@@ -16,6 +17,8 @@ World::World(const float width,World *l,World *r){
        }\r
        toLeft=l;\r
        toRight=r;\r
+       behind=infront=NULL;\r
+       root=false;\r
        if(toLeft){\r
                if(toLeft->toRight){\r
                        std::cout<<"There's already a world to the left!"<<std::endl;\r
@@ -46,35 +49,45 @@ World::World(const float width,World *l,World *r){
                }\r
        }\r
 }\r
+static float hline=HLINE;\r
+static float back=0;\r
 void World::draw(void){\r
        unsigned int i;\r
+       if(behind){\r
+               hline/=1.5;\r
+               back+=.2;\r
+               behind->draw();\r
+       }\r
+       if(root){\r
+               hline=HLINE;\r
+               back=0;\r
+       }\r
        glBegin(GL_QUADS);\r
                for(i=0;i<lineCount-10;i++){\r
                        glColor3ub(0,255,0);\r
-                       glVertex2f((HLINE*i)-1      ,line[i].start);\r
-                       glVertex2f((HLINE*i)-1+HLINE,line[i].start);\r
-                       glVertex2f((HLINE*i)-1+HLINE,line[i].start-HLINE*2);\r
-                       glVertex2f((HLINE*i)-1      ,line[i].start-HLINE*2);\r
+                       glVertex2f((hline*i)-1      ,line[i].start+back);\r
+                       glVertex2f((hline*i)-1+hline,line[i].start+back);\r
+                       glVertex2f((hline*i)-1+hline,line[i].start-hline*2+back);\r
+                       glVertex2f((hline*i)-1      ,line[i].start-hline*2+back);\r
                        glColor3ub(150,100,50);\r
-                       glVertex2f((HLINE*i)-1      ,line[i].start-HLINE*2);\r
-                       glVertex2f((HLINE*i)-1+HLINE,line[i].start-HLINE*2);\r
-                       glVertex2f((HLINE*i)-1+HLINE,-1);\r
-                       glVertex2f((HLINE*i)-1      ,-1);\r
+                       glVertex2f((hline*i)-1      ,line[i].start-hline*2+back);\r
+                       glVertex2f((hline*i)-1+hline,line[i].start-hline*2+back);\r
+                       glVertex2f((hline*i)-1+hline,-1+back);\r
+                       glVertex2f((hline*i)-1      ,-1+back);\r
                }\r
        glEnd();\r
 }\r
 void World::detect(vec2 *v,const float width){\r
        unsigned int i;\r
+       // hey\r
        for(i=0;i<lineCount-10;i++){\r
                if(v->y<line[i].start){\r
                        if(v->x>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){\r
                                v->y=line[i].start;\r
                                return;\r
-                               //v->x=(HLINE*i)-1+HLINE;\r
                        }else if(v->x+width>(HLINE*i)-1&&v->x+width<(HLINE*i)-1+HLINE){\r
                                v->y=line[i].start;\r
                                return;\r
-                               //v->x=(HLINE*i)-1-width;\r
                        }\r
                }else if(v->y>line[i].start+HLINE/4){\r
                        v->y-=HLINE/8;\r
@@ -107,4 +120,14 @@ void World::loadFromFile(FILE *f,World *parent){
                toRight->loadFromFile(f,toRight);\r
        }\r
 }\r
-\r
+void World::addLayer(void){\r
+       if(behind){\r
+               behind->addLayer();\r
+       }else{\r
+               behind=new World((lineCount-1)*HLINE+LAYER_SCALE,NULL,NULL);\r
+               behind->infront=this;\r
+       }\r
+}\r
+void World::setRoot(void){\r
+       root=true;\r
+}\r
index 67ba8a6891d0a8557630473ce5693d7abca6ef65..8768edd99e60cc9de30315eed86fd5bd2e6d95dc 100644 (file)
@@ -93,6 +93,8 @@ int main(int argc,char **argv){
        w=new World(2,NULL,w2);
        
        currentWorld=w;
+       currentWorld->addLayer();
+       currentWorld->setRoot();
        
        // Save the world if necessary
        /*FILE *f=fopen("world.dat","r");