]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
world gen
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 10 Sep 2015 01:04:08 +0000 (21:04 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 10 Sep 2015 01:04:08 +0000 (21:04 -0400)
include/World.h
src/World.cpp
src/main.cpp

index 4a097a636a60b330d89c182b11564576d0b3aac6..212fe3877bef834c0abe680b52c73e1695354f18 100644 (file)
@@ -2,21 +2,19 @@
 #define WORLD_H
 
 #include <common.h>
-#include <cstring>
 
-#define LAYER0_Y (-0.8f)
-#define TEX_SIZE ( 0.2f)
+#define HLINE (2.0f/ 200 )
 
 class World {
 private:
-       struct layer_t {
-               unsigned int tex;
-               float offset; 
-       } layer[4];
+       struct line_t {
+               // x = 2.0 (window width) / HLINES
+               float start; // Where to change to dirt, going down (y)
+       } *line;
+       unsigned int lineCount;
 public:
-       World(const char *l1,const char *l2,const char *l3,const char *bg);
+       World(float width);
        void draw(void);
-       void update(int player_accel);
 };
 
 #endif // WORLD_H
index feeeb1f8e70a933a6efd9fc15dd521bf73573e53..3d8cfb7e6b8d01ccb7b4d0cea84c9a46f6b30d7d 100644 (file)
@@ -1,38 +1,31 @@
 #include <World.h>
 
-World::World(const char *l1,const char *l2,const char *l3,const char *bg){
-       unsigned char i=0;
-       SDL_Surface *l;
-       const char *f[4]={l1,l2,l3,bg};
-       memset(layer,0,sizeof(struct layer_t)*4);
-       for(;i<4;i++){
-               l=IMG_Load(f[i]);
-               if(l!=NULL){
-                       glGenTextures(1,&layer[i].tex);
-                       glBindTexture(GL_TEXTURE_2D,layer[i].tex);
-                       glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
-                       glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
-                       glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
-                       glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
-                       glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,l->w,l->h,0,GL_RGB,GL_UNSIGNED_BYTE,l->pixels);
-                       SDL_FreeSurface(l);
-               }
+World::World(float width){
+       unsigned int i;
+       lineCount=width/HLINE;
+       if((line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)))==NULL){
+               std::cout<<"Failed to allocate memory!"<<std::endl;
+               abort();
+       }
+       line[0].start=(rand()%100)/100.0f-1; // lazy
+       for(i=1;i<lineCount;i++){
+               line[i].start=line[i-1].start+(float)((rand()%20)-10)/1000.0f;
        }
 }
 void World::draw(void){
-       int i;
-       float x;
-       glEnable(GL_TEXTURE_2D);
-       for(i=2;i>=0;i--){
-               glBindTexture(GL_TEXTURE_2D,layer[i].tex);
-               glBegin(GL_QUADS);
-                       for(x=-1;x<=1;x+=(TEX_SIZE/(float)(i+1))){
-                               glTexCoord2d(1,1);glVertex2f(x             ,LAYER0_Y-TEX_SIZE+(i*.2));
-                               glTexCoord2d(0,1);glVertex2f(x+TEX_SIZE,LAYER0_Y-TEX_SIZE+(i*.2));
-                               glTexCoord2d(0,0);glVertex2f(x+TEX_SIZE,LAYER0_Y                        +(i*.2));
-                               glTexCoord2d(1,0);glVertex2f(x             ,LAYER0_Y                    +(i*.2));
-                       }
-               glEnd();
-       }
-       glDisable(GL_TEXTURE_2D);
+       unsigned int i;
+       glBegin(GL_QUADS);
+               for(i=0;i<lineCount;i++){
+                       glColor3ub(0,255,0);
+                       glVertex2f((HLINE*i)-1      ,line[i].start);
+                       glVertex2f((HLINE*i)-1+HLINE,line[i].start);
+                       glVertex2f((HLINE*i)-1+HLINE,line[i].start-0.02);
+                       glVertex2f((HLINE*i)-1      ,line[i].start-0.02);
+                       glColor3ub(150,100,50);
+                       glVertex2f((HLINE*i)-1      ,line[i].start-0.02);
+                       glVertex2f((HLINE*i)-1+HLINE,line[i].start-0.02);
+                       glVertex2f((HLINE*i)-1+HLINE,-1);
+                       glVertex2f((HLINE*i)-1      ,-1);
+               }
+       glEnd();
 }
index 61920e7cf49c3d497c7b549e5a0c7e6d7f70ee7b..cb93ff4c949ca358adbaa32dcb265c7b5b21e41a 100644 (file)
@@ -1,4 +1,5 @@
 #include <common.h>
+#include <ctime>
 
 SDL_Window    *window = NULL;
 SDL_Surface   *renderSurface = NULL;
@@ -39,7 +40,7 @@ int main(int argc,char **argv){
                std::cout << "SDL was not able to initialize! Error: " << SDL_GetError() << std::endl;
                return -1;
        }
-       
+       srand(time(NULL));
        glClearColor(.3,.5,.8,0);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
@@ -48,10 +49,7 @@ int main(int argc,char **argv){
        ****     GAMELOOP      ****
        **************************/
        
-       World *w=new World("res/dirt.jpg",
-                                          "res/dirt.jpg",
-                                          "res/dirt.jpg",
-                                          "res/dirt.jpg");
+       World *w=new World(2);
        
        while(gameRunning){
                ui.handleEvents();                                                              // Handle events