Background now wraps horizontally
authorIvan Hernandez <iturtleman128@gmail.com>
Sun, 17 Oct 2010 06:50:13 +0000 (01:50 -0500)
committerIvan Hernandez <iturtleman128@gmail.com>
Sun, 17 Oct 2010 06:50:13 +0000 (01:50 -0500)
Background.cpp
Background.h
Level.cpp

index 34ab49b..a90a4b8 100644 (file)
@@ -6,52 +6,72 @@ Background::Background(SDL_Surface *screen, std::string name, std::string filena
 /// \todo refactor all width as w height as h X as x Y as y so on and so forth for everything. different things are annoying
        mScreenSize.width = screen->w;
        mScreenSize.height = screen->h;
+
        source.w = bgSize.width;
        source.h = bgSize.height;
        source.x = bgSamplePos.X;
        source.y = bgSamplePos.Y;
 
-       load(filename);
+       MaxTexWidth = 512; 
+       MaxTexHeight = 512;
 
+       load(filename);
+       
+       // Number of sections each dimension, rounded up
+       _sections.width = (_imageSize.width - 1) / MaxTexWidth + 1;
+       _sections.height =      (_imageSize.height - 1) / MaxTexHeight + 1;
        cout<<"background \""<<_name<<"\" created"<<endl;       
 }
 void Background::draw(){
-       /** \todo make the drawing for everything be sectioned if that'd make it faster otherwise create tiling for backgrounds smaller than the screen size*/
+               /** \todo make the drawing for everything be sectioned if that'd make it faster otherwise create tiling for backgrounds smaller than the screen size*/
        /** \todo decide if wrapping should be a good default.*/
        ///destination rectangle only position is used and describes where the output is drawn.
+       SDL_Rect dest;///<the place where the sample will be drawn (can be reused)
+       dest.x=0;
+       dest.y=0;
+       double x = mPos.X, y = mPos.Y;
+       while(x > mScreenSize.width)//move it to the left til it's 0 or less
+               x-=mScreenSize.width;
+       while(y > mScreenSize.height)//move it down til it's 0 or less
+               y-=mScreenSize.height;
+       while(x < 0)//move it to the right until it's in the drawable area
+               x+=mScreenSize.width;
+       while(y < 0)//move it up til it's 
+               y+=mScreenSize.height;
+
        if(wrapping){
                SDL_Rect extraX;///<keeps track of pos and size of the sample that hangs off the x
                SDL_Rect extraY;///<keeps track of pos and size of the sampel hanging off y
-               SDL_Rect dests;///<the place where the sample will be drawn (can be reused)
-               dests.x=0;
-               dests.y=0;
-               ///hanging off the right side
-               if(mPos.X > 0 || mPos.Y > 0 /*+ _imageSize.width > mScreenSize.width*/){
-                       extraX.x = 0;
-                       extraX.y = 0;
-                       extraX.w = mPos.X;
-                       extraX.h = mScreenSize.height - mPos.Y; 
-                       SDL_BlitSurface(mBackground, &extraX, mScreen, &dests);
+
+               //horizontal wrapping
+               if(x > 0 ){
+                       extraX.x = mScreenSize.width - x;       
+                       extraX.y = y;   
+                       extraX.w = x;
+                       extraX.h = mScreenSize.height - y;      
+                       SDL_BlitSurface(mBackground, &extraX, mScreen, &dest);
+               }
+               /// \todo vertical wrapping
+               if(y > 0){
+                       extraX.x = x;
+                       extraX.y = mScreenSize.height + y;      
+                       extraX.w = mScreenSize.width + x;
+                       extraX.h = y;   
+                       SDL_BlitSurface(mBackground, &extraX, mScreen, &dest);
                }
-               ///hanging off the left side
-               if(mPos.X + _imageSize.width < mScreenSize.width){
+               /*
+               if(x + _imageSize.width < mScreenSize.width){
                        extraX.x = 0;
                        extraX.y = 0;
-                       extraX.w = -mPos.X;
+                       extraX.w = -x;
                        extraX.h = mScreenSize.height;  
-                       SDL_BlitSurface(mBackground, &extraX, mScreen, &dests);
-               }
-
-
-
-       }
-       else{
-               SDL_Rect dest;
-       dest.x = mPos.X;
-           dest.y = mPos.Y;
-               
-       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+                       SDL_BlitSurface(mBackground, &extraX, mScreen, &dest);
+               }//*/
        }
+       dest.x = x;
+       dest.y = y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+
 }
 
 SDL_Surface* Background::load(std::string filename){
@@ -71,3 +91,5 @@ void Background::setWrap(bool tf){
 bool Background::getWrap(){
        return wrapping;
 }
+
+
index fceb014..e0ac618 100644 (file)
@@ -15,6 +15,10 @@ class Background{
        private:
        std::string _name;///<The background's name
        SizeD _imageSize;///<The actual background image's full size.
+       SizeD _sections;
+       int MaxTexWidth;  
+       int MaxTexHeight; 
+
        SDL_Surface *mBackground;///<pointer to the actual screen 
        /** \todo _sections;//if needed */
        SDL_Surface *mScreen;//<\todo see about a way to know about the screen without passing around the value (though this is probably most cost effective processor-wise)
@@ -26,7 +30,7 @@ class Background{
 
        
        public:
-       Background(SDL_Surface *screen,std::string name="default", std::string filename="bg.bmp", bool wrap=true, Point2D ScreenPosition=Point2D(50,0), SizeD screenSize=SizeD(800,600)/** \todo figure out how to get the game window's size to the background.*/,Point2D bgSamplePos=Point2D(0,0), SizeD bgSize=SizeD(800,600));
+       Background(SDL_Surface *screen,std::string name="default", std::string filename="garden.jpg", bool wrap=true, Point2D ScreenPosition=Point2D(50,0), SizeD screenSize=SizeD(800,600),Point2D bgSamplePos=Point2D(0,0), SizeD bgSize=SizeD(800,600));
        void draw();/**< Draw background. */
        std::string getName(){ return _name; }/** Returns name. */
        void setName(std::string name){ _name = name; }/** Set Name. */
@@ -35,5 +39,10 @@ class Background{
        SizeD getPosition(){return mPos;}
        void setWrap(bool tf);
        bool getWrap();
+       void xadd(int num) {mPos.X += num;}/**< Increase X coordiante by a given amount. */
+    void yadd(int num) {mPos.Y += num;}/**< Increase Y coordinate by a given amount. */
+    void xset(int x) {mPos.X = x;}/**< Sets the Background's X Coordinate. */
+    void yset(int y) {mPos.Y = y;}/**< Sets the Background's Y coordinate.  */
+    void setPosition(int x, int y) {mPos.X = x; mPos.Y = y;}/**< Sets the Background's X an Y coordinate. */
 };
 
index b170cf0..dc2152c 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -106,11 +106,14 @@ void Level::postEvent(SDL_Event event)
 /** Will handle all movement and dynamic triggers. */
 void Level::update()
 {
-       /*int* numkeys;*//// \todo Get keyboard as whole input to function correctly.
-       Uint8 *keys = SDL_GetKeyState(/*numkeys*/NULL);
-       //std::cout << *numkeys << ">>>";
-       if ( keys[SDLK_LEFT] ) {  mSprites[0]->xadd(-1); }
+       Uint8 *keys = SDL_GetKeyState(NULL);
+       if ( keys[SDLK_LEFT] ) { mSprites[0]->xadd(-1); }
        if ( keys[SDLK_RIGHT] ) { mSprites[0]->xadd(1); }
        if ( keys[SDLK_UP] ) { mSprites[0]->yadd(-1); }
        if ( keys[SDLK_DOWN] ) { mSprites[0]->yadd(1); }
+       if ( keys[SDLK_a] ) { mBackground->xadd(-1); }
+       if ( keys[SDLK_d] ) { mBackground->xadd(1); }
+       if ( keys[SDLK_w] ) { mBackground->yadd(-1); }
+       if ( keys[SDLK_s] ) { mBackground->yadd(1); }
+
 }