Need some advice and insight *SOLVED*

Started by
1 comment, last by xM1k3x 15 years, 9 months ago
Hey guys, On my on going process to build a small one level game I have run into many problems many of which you have all helped me solve and I am extremely grateful I have learned alot from all of you. THis is one of my problems that I am currently having. Basically I have a vertical scroller and after it has gone through all the numbers on this array(see first box) it stops well sorta it just freaks out and the screen kind of just flutters and flickers but its not scrolling anymore.

int MAPDATA[MAPWIDTH*MAPHEIGHT] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,
70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,
92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,
110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,
158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,
174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,
190,191,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,
63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,
104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,
120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,
136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,
152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,
168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191
};


Here are the functions that make it scroll: Initialization:


ScrollY = GAMEWORLDHEIGHT - SCREEN_HEIGHT;


Function: Update Scroll Position

void UpdateScrollPosition()
{
	SpeedY = -4;

	//update horizontal scrolling position and speed
    ScrollX += SpeedX;

    if (ScrollX < 0) 
	{
        ScrollX = 0;
        SpeedX = 0;
	}
    else if (ScrollX > GAMEWORLDWIDTH - WINDOWWIDTH)
	{
        ScrollX = GAMEWORLDWIDTH - WINDOWWIDTH;
        SpeedX = 0;
	}
    //update vertical scrolling position and speed
    ScrollY += SpeedY;
	if (ScrollY < 0)
	{
        SpeedY =  0;
	}
    //if (ScrollY > GAMEWORLDHEIGHT - WINDOWHEIGHT)
	//{
  //      ScrollY = GAMEWORLDHEIGHT - WINDOWHEIGHT;
	//}
}



Function: Draw Tile


//This function does the real work of drawing a single tile from the 
//source image onto the tile scroll buffer. Parameters provide much 
//flexibility.
void DrawTile(LPDIRECT3DSURFACE9 source,	// source surface image
				int tilenum,				// tile #
				int width,					// tile width
				int height,					// tile height
				int columns,				// columns of tiles
				LPDIRECT3DSURFACE9 dest,	// destination surface
				int destx,					// destination x
				int desty)					// destination y
{
    
    //create a RECT to describe the source image
    RECT r1;
    r1.left = (tilenum % columns) * width;
    r1.top = (tilenum / columns) * height;
    r1.right = r1.left + width;
    r1.bottom = r1.top + height;
    
    //set destination rect
	RECT r2 = {destx,desty,destx+width,desty+height};
    
    //draw the tile 
    d3ddev->StretchRect(source, &r1, dest, &r2, D3DTEXF_NONE);
}


Function: Draw Tiles

//This function fills the tilebuffer with tiles representing
//the current scroll display based on scrollx/scrolly.
void DrawTiles()
{
    int tilex, tiley;
    int columns, rows;
    int x, y;
    int tilenum;
    
    //calculate starting tile position
    tilex = ScrollX / TILEWIDTH;
    tiley = ScrollY / TILEHEIGHT;
    
    //calculate the number of columns and rows
    columns = WINDOWWIDTH / TILEWIDTH;
    rows = WINDOWHEIGHT / TILEHEIGHT;
    
    //draw tiles onto the scroll buffer surface
  
	for (y = 0; y <= rows; y++)
	{
		
        for (x=0; x<=columns; x++)
		{
			//retrieve the tile number from this position
            tilenum = MAPDATA[((tiley + y) * MAPWIDTH + (tilex + x))];

			//draw the tile onto the scroll buffer
            DrawTile(tiles,tilenum,TILEWIDTH,TILEHEIGHT,16,scrollbuffer,
                x*TILEWIDTH,y*TILEHEIGHT);
		}
	
	}

}



Function: Draw Scroll Window


//This function draws the portion of the scroll buffer onto the back buffer
//according to the current "partial tile" scroll position.
void DrawScrollWindow()
{
    //calculate the partial sub-tile lines to draw using modulus
    int partialx = ScrollX % TILEWIDTH;
    int partialy = ScrollY % TILEHEIGHT;
    
    //set dimensions of the source image as a rectangle
	RECT r1 = {partialx,partialy,partialx+WINDOWWIDTH,partialy+WINDOWHEIGHT};
        
    //set the destination rectangle
    //This line draws the virtual scroll buffer to the screen exactly as is,
    //without scaling the image to fit the screen. If your screen does not
    //divide evenly with the tiles, then you may want to scale the scroll
    //buffer to fill the entire screen. It's better to use a resolution that
    //divides evenly with your tile size.

    //use this line for scaled display
	//RECT r2 = {0, 0, WINDOWWIDTH-1, WINDOWHEIGHT-1};  
    
    //use this line for non-scaled display
    RECT r2 = {0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1};

    //draw the "partial tile" scroll window onto the back buffer
    d3ddev->StretchRect(scrollbuffer, &r1, backbuffer, &r2, D3DTEXF_NONE);
}


These functions are all called in this order: Update scroll position Draw Tiles( Draw tile is called within this function) Draw Scroll window So basically now that you have all the background info for how my scrolling works maybe someone could tell me how to extend the length of the background perhaps so that it runs infinetly until the player destroys all the enemies? THanks [Edited by - xM1k3x on July 18, 2008 1:15:07 PM]
Advertisement
First of all, in your UpdateScrollPosition() function, you need to wrap the ScrollY position back to the bottom of the map once it reaches the top.

void UpdateScrollPosition(){	SpeedY = -4;	//update horizontal scrolling position and speed    ScrollX += SpeedX;    if (ScrollX < 0) 	{        ScrollX = 0;        SpeedX = 0;	}    else if (ScrollX > GAMEWORLDWIDTH - WINDOWWIDTH)	{        ScrollX = GAMEWORLDWIDTH - WINDOWWIDTH;        SpeedX = 0;	}    //update vertical scrolling position and speed    ScrollY += SpeedY;	if (ScrollY < 0)	{           //wrap the ScrollY back to the bottom of the map           //once it reached the top           ScrollY =  GAMEWORLDHEIGHT + ScrollY; //since ScrollY is negative, this is subtracting from the bottom of the world	}}



Then you need to change your DrawTiles() function, so that when its fetching tiles outside of the bottom y boundary of the array, it wraps the y index around and grabs the tiles from the "top" (top rows) of the array. This "stitches" the top and the bottom of your level together whenever the bottom of your screen is outside the bottom boundary of your map.


//This function fills the tilebuffer with tiles representing//the current scroll display based on scrollx/scrolly.void DrawTiles(){    int tilex, tiley;    int columns, rows;    int x, y;    int tilenum;        //calculate starting tile position    tilex = ScrollX / TILEWIDTH;    tiley = ScrollY / TILEHEIGHT;        //calculate the number of columns and rows    columns = WINDOWWIDTH / TILEWIDTH;    rows = WINDOWHEIGHT / TILEHEIGHT;        //draw tiles onto the scroll buffer surface  	for (y = 0; y <= rows; y++)	{		        for (x=0; x<=columns; x++)		{                   //to wrap the y index, you modulo it with the                    //number of rows in your array, which is MAPHEIGHT                   //This means the y index will always be within the                   //the "vertical boundary" of the array                   int wrappedYIndex = (tiley + y) % MAPHEIGHT;			//retrieve the tile number from this position            tilenum = MAPDATA[((wrappedYIndex) * MAPWIDTH + (tilex + x))];			//draw the tile onto the scroll buffer            DrawTile(tiles,tilenum,TILEWIDTH,TILEHEIGHT,16,scrollbuffer,                x*TILEWIDTH,y*TILEHEIGHT);		}		}}


Hope this helps.

[Edited by - snisarenko on July 18, 2008 3:24:01 AM]
Quote:Original post by snisarenko
First of all, in your UpdateScrollPosition() function, you need to wrap the ScrollY position back to the bottom of the map once it reaches the top.

*** Source Snippet Removed ***


Then you need to change your DrawTiles() function, so that when its fetching tiles outside of the bottom y boundary of the array, it wraps the y index around and grabs the tiles from the "top" (top rows) of the array. This "stitches" the top and the bottom of your level together whenever the bottom of your screen is outside the bottom boundary of your map.


*** Source Snippet Removed ***

Hope this helps.


Your answer was perfect and everything worked out correctly. I can see the logic behind the thinking now so I will add that to my memory bank for future projects thanks for the help it is much appreciated.

This topic is closed to new replies.

Advertisement