]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
added/fixed world linking
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 22 Sep 2015 01:52:28 +0000 (21:52 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 22 Sep 2015 01:52:28 +0000 (21:52 -0400)
include/common.h
include/world.h
src/main.cpp
src/ui.cpp
src/world.cpp

index c147d888ea9cb65768ba892c70111019353284e2..dde3c4acbafe41053849505d1e962153b3a159fb 100644 (file)
@@ -17,7 +17,7 @@ typedef struct { float x; float y; } vec2;
 #define SCREEN_HEIGHT 720
 //#define FULLSCREEN
 
-#define HLINE 2
+#define HLINE 3
 
 #define initRand(s) srand(s)
 #define getRand()      rand()
index 3b76f192868cce64b53879dc8fb2cfe5c484cd49..c89ced78fbb059ca4f03101136ab197fdb94d50c 100644 (file)
@@ -13,11 +13,18 @@ private:
        int x_start;
        World *behind,*infront;
 public:
+       World *toLeft,*toRight;
        World(unsigned int width);
        ~World(void);
+       
        void addLayer(unsigned int width);
        void draw(vec2 *vec);
        void detect(vec2 *v,vec2 *vel,const float width);
+       
+       World *goWorldLeft(vec2 *loc,const float width);
+       World *goWorldRight(vec2 *loc,const float width);
+       World *goWorldBack(vec2 *loc,const float width);
+       World *goWorldFront(vec2 *loc,const float width);
 };
 
 #endif // WORLD_H
index d31b704cd889757f39806bcb0849608c91954e9b..4f4c13aed54ee95d5c98558f24f982ef03b37adf 100644 (file)
@@ -77,8 +77,12 @@ int main(int argc, char *argv[]){
        ****     GAMELOOP      ****
        **************************/
 
-       World *test=new World(SCREEN_WIDTH/2);
+       World *test =new World(SCREEN_WIDTH/2),
+                 *test2=new World(SCREEN_WIDTH*2);
        test->addLayer(400);
+       test->addLayer(100);
+       test->toLeft=test2;
+       test2->toRight=test;
        currentWorld=test;
        player=new Player();
        player->spawn(0,100);
@@ -143,5 +147,4 @@ void logic(){
        currentWorld->detect(&player->loc,&player->vel,player->width);
        player->loc.y+=player->vel.y*deltaTime;
        player->loc.x+=player->vel.x*deltaTime;
-       std::cout<<"("<<player->loc.x<<","<<player->loc.y<<")"<<std::endl;
 }      
index 877dacbdd998f4fd85a49ec373d0054fe134e83a..a2fe7bf786ba64254e12bf7024f171cd96c6eaee 100644 (file)
@@ -1,10 +1,12 @@
 #include <ui.h>
+#include <world.h>
 #include <ft2build.h>
 #include FT_FREETYPE_H
 
 #define SDL_KEY e.key.keysym.sym
 
 extern Player *player;
+extern World  *currentWorld;
 
 static FT_Library   ftl;
 static FT_Face      ftf;
@@ -89,9 +91,17 @@ namespace ui {
                                break;
                        case SDL_KEYDOWN:
                                if(SDL_KEY==SDLK_ESCAPE)gameRunning=false;
-                               if(SDL_KEY==SDLK_a)player->vel.x=-.15;
-                               if(SDL_KEY==SDLK_d)player->vel.x=.15;
+                               if(SDL_KEY==SDLK_a){
+                                       player->vel.x=-.15;
+                                       currentWorld=currentWorld->goWorldLeft(&player->loc,player->width);
+                               }
+                               if(SDL_KEY==SDLK_d){
+                                       player->vel.x=.15;
+                                       currentWorld=currentWorld->goWorldRight(&player->loc,player->width);
+                               }
                                if(SDL_KEY==SDLK_SPACE)player->vel.y=.25;
+                               if(SDL_KEY==SDLK_i)currentWorld=currentWorld->goWorldBack(&player->loc,player->width);
+                               if(SDL_KEY==SDLK_k)currentWorld=currentWorld->goWorldFront(&player->loc,player->width);
                                break;
                        case SDL_KEYUP:
                                if(SDL_KEY==SDLK_a)player->vel.x=0;
index d7aece19b6248b55aba5402b2c75cf9a0984a446..d05d5c181e99705cd57c2caf5016aa3ab5c82b53 100644 (file)
@@ -1,6 +1,6 @@
 #include <world.h>
 
-#define getWidth() ((lineCount-GEN_INC)*HLINE)
+#define getWidth(w) ((w->lineCount-GEN_INC)*HLINE)
 
 #define GEN_INC 10
 #define GRASS_HEIGHT 4
@@ -34,8 +34,9 @@ World::World(unsigned int width){
                }
                line[i].color=rand()%20+130;
        }
-       x_start=0-getWidth()/2+GEN_INC/2*HLINE;
+       x_start=0-getWidth(this)/2+GEN_INC/2*HLINE;
        behind=infront=NULL;
+       toLeft=toRight=NULL;
 }
 
 World::~World(void){
@@ -45,36 +46,47 @@ World::~World(void){
 void World::draw(vec2 *vec){
        static float yoff=50;
        static int shade=0;
-       int i,ie,v_offset;
-       if(behind){
+       static World *root,*current;
+       int i,ie,v_offset,cx_start;
+       struct line_t *cline;
+       root=current=this;
+LOOP1:
+       if(current->behind){
                yoff+=50;
                shade+=40;
-               behind->draw(vec);
+               current=current->behind;
+               goto LOOP1;
        }
-       v_offset=(vec->x-x_start)/HLINE;
+LOOP2:
+       v_offset=(vec->x-current->x_start)/HLINE;
        i=v_offset-SCREEN_WIDTH/(yoff/25);
        if(i<0)i=0;
        ie=v_offset+SCREEN_WIDTH/(yoff/25);
-       if(ie>lineCount)ie=lineCount;
+       if(ie>current->lineCount)ie=current->lineCount;
+       //std::cout<<v_offset<<" "<<i<<" "<<ie<<yoff<<std::endl;
+       cline=current->line;
+       cx_start=current->x_start;
        glBegin(GL_QUADS);
-               for(i=i;i<ie;i++){
-                       line[i].y+=(yoff-50);
+               for(i=i;i<ie-GEN_INC;i++){
+                       cline[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);
-                       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);
+                       glVertex2i(cx_start+i*HLINE      ,cline[i].y);
+                       glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y);
+                       glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y-GRASS_HEIGHT);
+                       glVertex2i(cx_start+i*HLINE             ,cline[i].y-GRASS_HEIGHT);
+                       safeSetColor(cline[i].color+shade,cline[i].color-50+shade,cline[i].color-100+shade);
+                       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-50);
                }
        glEnd();
-       if(infront){
+       if(current->infront){
                yoff-=50;
                shade-=40;
+               current=current->infront;
+               goto LOOP2;
        }else{
                yoff=50;
                shade=40;
@@ -95,9 +107,9 @@ 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+width+HLINE>x_start+getWidth()){
+       }else if(v->x+width+HLINE>x_start+getWidth(this)){
                vel->x=0;
-               v->x=x_start+getWidth()-width-HLINE;
+               v->x=x_start+getWidth(this)-width-HLINE;
        }
 }
 
@@ -109,3 +121,35 @@ void World::addLayer(unsigned int width){
        behind=new World(width);
        behind->infront=this;
 }
+
+World *World::goWorldLeft(vec2 *loc,const float width){
+       if(toLeft&&loc->x<x_start+HLINE*10){
+               loc->x=toLeft->x_start+getWidth(toLeft)-HLINE*10;
+               loc->y=toLeft->line[0].y;
+               return toLeft;
+       }
+       return this;
+}
+
+World *World::goWorldRight(vec2 *loc,const float width){
+       if(toRight&&loc->x+width>x_start+getWidth(this)-HLINE*10){
+               loc->x=toRight->x_start+HLINE*10;
+               loc->y=toRight->line[toRight->lineCount-GEN_INC-1].y;
+               return toRight;
+       }
+       return this;
+}
+
+World *World::goWorldBack(vec2 *loc,const float width){
+       if(behind&&loc->x>(int)(0-getWidth(behind)/2)&&loc->x<getWidth(behind)/2){
+               return behind;
+       }
+       return this;
+}
+
+World *World::goWorldFront(vec2 *loc,const float width){
+       if(infront&&loc->x>(int)(0-getWidth(infront)/2)&&loc->x<getWidth(infront)/2){
+               return infront;
+       }
+       return this;
+}