Jump to content
  • Advertisement

Archived

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

GameDev135

implementing side scolling. possibly a clipping problem

This topic is 5898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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, which is one thing which I dont fully grasp the code for. I am using DirectX. 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;  
int offset_x, offset_y;   
 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
Advertisement
This is one solution:

    
#define SCREEN_SIZEY 30 #define SCREEN_SIZEX 40 #define WORLD_SIZEY 40 #define WORLD_SIZEX 50 #define TILE_SIZE 16void draw_tiles(void){ int tile; int x, y; int scroll_x, scroll_y; int offset_x, offset_y; 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;
if(tile_src.left + TILE_SIZE > SCREEN_SIZEX)
tile_src.right = TILE_SIZE - ((tile_src.left + TILE_SIZE)-SCREEN_SIZEX);
else
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)); } }}


Basically all this is doing is only drawing the part of the tile that will be onscreen (only the right hand side of the screen is checked for), it will be slower but I think it will work, assuming I understand the way your CopySurfaceToSurface function works, and the SCREEN_SIZEX and TILE_SIZE defines use the same units.

If you run into similar problems you can implement a similar method to clip the tiles with that side of the screen as well, or whatever.

This might not be the best way of doing it butit is a way of doing it.

Anyway, hope I helped

P.S. Sorry about the bad formatting on most of the code, but it shouldn't matter, up until the if it is the same as the original

[edited by - Grambo on July 30, 2002 7:26:26 PM]

Share this post


Link to post
Share on other sites
quote:

tile_src.right = TILE_SIZE;


I think this line is not Correct. According to you code your tileset bitmap must be Blt to Offscreen buffer horizontal, right?
So i think it must be:
tile_src.right = tile_src.left + TILE_SIZE;


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!