Archived

This topic is now archived and is closed to further replies.

GameDev135

smooth scrolling/not wrapping around

Recommended Posts

Hi. I am implementing a 2d side scroller using tiles. I have gotten the scrolling working and everything else is working properly, execpt for one thing. If the left most tile is partly off the screen, the part that is off the screen gets rendered on the right side of it. Is this a problem with clipping? (I am using DirectX. If that is the problem, could you suggest how to fix it?) What are possible solutions to this. Here is my draw tiles function.
  
#define SCREEN_SIZEY 30
#define SCREEN_SIZEX 40
#define WORLD_SIZEY 40
#define WORLD_SIZEX 50
#define TILE_SIZE 16
void draw_tiles(void)
{
   int tile;
  int x, y;
  int scroll_x, scroll_y;   // (NEW) 

  int offset_x, offset_y;   // (NEW) 

  RECT tile_src;
	POINT point;

 for (y = 0; y < SCREEN_SIZEY && y< WORLD_SIZEY; y++) 
  {
    for (x = 0; x < SCREEN_SIZEX && y < WORLD_SIZEX; x++) 
    { 
    
      scroll_x = x + (world_camerax / TILE_SIZE);  
      scroll_y = y + (world_cameray / TILE_SIZE);


      offset_x = world_camerax % (TILE_SIZE - 1);
      offset_y = world_cameray % (TILE_SIZE - 1);

      tile = map[scroll_y][scroll_x];
      tile_src.left    =  (tile - 1) * TILE_SIZE;
      tile_src.top     =  0;
      tile_src.right   =  TILE_SIZE;
      tile_src.bottom  =  TILE_SIZE;
	
		point.x = x * TILE_SIZE   - offset_x;
		point.y = y * TILE_SIZE   - offset_y;

		CopySurfaceToSurface(&tile_src,g_tiles,&point ,g_pBackSurface,TRUE,D3DCOLOR_ARGB(255,255,0,255));
	}
	}
}
  
I appreciate your help

Share this post


Link to post
Share on other sites
Try this... the code I''ve added will "clip" the tiles to the screen.


  
#define SCREEN_SIZEY 30
#define SCREEN_SIZEX 40
#define WORLD_SIZEY 40
#define WORLD_SIZEX 50
#define TILE_SIZE 16

// CODE ADDED

#define SCREEN_PIXELSIZEX 640
#define SCREEN_PIXELSIZEY 480
// END OF ADDED CODE



void draw_tiles(void) {

for (y = 0; y < SCREEN_SIZEY && y < WORLD_SIZEY; y++) {
for (x = 0; x < SCREEN_SIZEX && y < WORLD_SIZEX; x++) {

scroll_x = x + (world_camerax / TILE_SIZE);
scroll_y = y + (world_cameray / TILE_SIZE);
offset_x = world_camerax % (TILE_SIZE - 1);
offset_y = world_cameray % (TILE_SIZE - 1);

tile = map[scroll_y][scroll_x];

tile_src.left = (tile - 1) * TILE_SIZE;
tile_src.top = 0;
tile_src.right = TILE_SIZE;
tile_src.bottom = TILE_SIZE;

point.x = x * TILE_SIZE - offset_x;
point.y = y * TILE_SIZE - offset_y;

// CODE ADDED

// Doublecheck the tile is on-screen.. can never be too careful.

if ( (point.x < SCREEN_PIXELSIZEX) && (point.y < SCREEN_PIXELSIZEY) ) {

// If point is above or left of the screen, crop the top/left corner of the tile.

if ( point.x < 0 ) { tile_src.left -= point.x; point.x = 0; }
if ( point.y < 0 ) { tile_src.top -= point.y; point.y = 0; }

// If point+tilesize is below or right of the screen, crop the bottom/right corner of the tile.

if ( point.x > SCREEN_PIXELSIZEX - TILE_SIZE - 1 ) tile_src.right -= (SCREEN_PIXELSIZEX-point.x);
if ( point.y > SCREEN_PIXELSIZEY - TILE_SIZE - 1 ) tile_src.bottom -= (SCREEN_PIXELSIZEY-point.y);

CopySurfaceToSurface(&tile_src, g_tiles, &point, g_pBackSurface, TRUE, D3DCOLOR_ARGB(255,255,0,255) );
}

// END OF ADDED CODE

}
}
}

Share this post


Link to post
Share on other sites