Skip to main content
GameDev.net gamedev.net
🔒 Locked

issues with draw_tiles function

Started by GameDev135 Nov 15, 2004 at 10:36 PM 0 replies 900+ views
Original Post
GameDev135
GameDev135
EDIT:: Okay, I managed to solve the problem regarding tile_src.left getting crazy numbers--turns out that my values for world_cameray were being computed incorrectly, so the value for world_cameray was more than it should possibly be leading to problems......Now my issue is with clipping. How can I stop the tiles from "bleeding" over to the other side of the screen? Do I need to expand my for loop for drawing the tiles to include an extra iteration? That is, if the screen is divided normally into say 20 tiles wide, when there is an offset, I have to draw 19 tiles completely, plus parts of 2. So how should my iteration account for this? One thing I am thinking is iterate from tile 2-20 then manually add tiles 1 and 21. Would that be a good thing to do ? [/edit] Hey, I am having a problem with my rendering...I have traced it down to my draw tiles function and it involves the fact that tile_src.left can equal absolutely crazy numbers..... Here is my function--i put some extra comments in to explain some odd things:

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;
	int numRows = g_level.GetRows();
	int numCols = g_level.GetCols();
	short int *temp = g_level.GetMap();	

	for (y = 0; y < SCREEN_SIZEY  &&  y< numRows; y++)  {
		for (x = 0; x < SCREEN_SIZEX  && y < numCols; x++) { 
			scroll_x = x + (world_camerax / g_level.GetTileSize().x);  
			scroll_y = y + (world_cameray / g_level.GetTileSize().y);

			offset_x = world_camerax % (g_level.GetTileSize().x );
			offset_y = world_cameray % (g_level.GetTileSize().y ); //someone suggested to me that i put the following 2 lines instead, but those don't work either.
	//	offset_x = world_camerax & g_level.GetTileSize().x;
	//	offset_y = world_cameray & g_level.GetTileSize().y;
		
	tile = * ( temp + (scroll_y * numCols) + scroll_x);
	/* tile is a pointer to a short int. The way I store the map is as a dynamically allocated array of short ints....i realize this makes little sense, but its a long story why I did this, and at this point its too late to change it anyway....but this calculation has always been proper previous to this */			
			tile_src.left    =  (tile - 1) * g_level.GetTileSize().x;
			tile_src.top     =  0;
			tile_src.right   =  g_level.GetTileSize().x;
			tile_src.bottom  =  g_level.GetTileSize().y;
	
			point.x = x * g_level.GetTileSize().x - offset_x;
			point.y = y * g_level.GetTileSize().y - offset_y;
			
			
		CopySurfaceToSurface(&tile_src,g_tiles,&point ,g_pBackSurface,TRUE,D3DCOLOR_ARGB(255,255,0,255));   /* this function is from The Zen Direct 3d Book by Walsh--I have used it many times before and it doesn't cause issues */

		}
	}
}


I should note that the reason I am all of a sudden running into issues with this is that previously you could only scroll in multiples of the tilesize. Now I would like more smooth scrolling.... Btw, if I get this to work, do you have any suggestions for what to do about some clipping problems with parts of the tile carrying over to the other half of the screen? Thanks for your help! [Edited by - GameDev135 on November 15, 2004 11:37:49 PM]
Feel free to email me at NYYanks432@hotmail.com if you have any questions
the_moo
the_moo
hey GameDev135,
ive just done a similar thing with my editor. my advice is to iterate through the tiles that will definately be on screen + 1 row on all sides. so if tiles 10-30 were gonna FULLY be on screen, iterate from 9 to 31.
if you can't do this, you could also use another method:
iterate through all the tiles in the map, checking on each one whether it is in the screen position:
/*for the sake of this eg. "tiles" is an array keeping all your tiles with x and y ints being the position of each tile in respect to the drawing surface*/for(int i = 0; i < NumOfTiles; ++i){  if ((tiles.x+TileWidth < 0) && (tiles.x <= ScreenWidth) &&    (tiles.y+TileHeight < 0) && (tiles.y <= ScreenHeight))  {    //tile is therefore inside the screen so draw it to the screen  }}

keep in mind that this example isnt optimised like the first suggestion, so it could get a bit costly to do this if your map is large...

anyway, hope that helps!
the_moo
the_moo

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.