Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

Help with tile rendering problem.[fixed]

This topic is 4844 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

Ok the issue is that when I try to move the tile map with the arrow keys it moves BUT the tils do not move in unesen and the slowly start to overlap each other.

Tileset.world_cameray -=4;
Tileset.world_cameray +=4;
Tileset.world_camerax -=4;
Tileset.world_camerax +=4;

///////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//draws the lvl 1 tile set
void draw_tiles(void)
{


	for(Tileset.layer=0; Tileset.layer<Layer;Tileset.layer++){
		for(Tileset.y=0; Tileset.y < Tileset.Screen_sizey;Tileset.y++){		
			for(Tileset.x=0; Tileset.x < Tileset.Screen_sizex; Tileset.x++){  
				
      
      Tileset.scroll_x = Tileset.x + (Tileset.world_camerax/Tileset.Tile_size  );//this allows scrolling on y axis
      Tileset.scroll_y = Tileset.y + (Tileset.world_cameray/Tileset.Tile_size );//this allows scrolling on x axis
      
      Tileset.offset_x =Tileset.scroll_x  % Tileset.Tile_size;       
      Tileset.offset_y =Tileset.scroll_y  % Tileset.Tile_size;   
          
             
          Tileset.tiles = map[Tileset.layer][Tileset.scroll_y][Tileset.scroll_x];
     

 
    tile_src.left = ((Tileset.tiles - 1)%4) *Tileset.Tile_size;
    tile_src.top =  ((Tileset.tiles - 1) /4) * Tileset.Tile_size ;
    tile_src.right = tile_src.left + Tileset.Tile_size;
    tile_src.bottom = tile_src.top + Tileset.Tile_size  ; 

dstr.left= (Tileset.x*Tileset.Tile_size)- Tileset.offset_x;
dstr.top= (Tileset.y*Tileset.Tile_size)- Tileset.offset_y ;
dstr.right = (Tileset.x*Tileset.Tile_size) + (tile_src.right - tile_src.left) ;
dstr.bottom = (Tileset.y*Tileset.Tile_size)  + (tile_src.bottom - tile_src.top)  ;

lpBackBuffer->Blt(&dstr,g_pddTile,&tile_src,DDBLT_WAIT|DDBLT_KEYSRC,NULL);  
}  
 }
	}
} 


#include <iostream>
#include <stdlib.h>
#include <string>
#include <windows.h>
#include <mmsystem.h>
#include <fstream>

struct Tile
{
int Tile_size;
int Screen_sizex;
int Screen_sizey;
int world_camerax;
int world_cameray;
int scroll_x;
int scroll_y;
int offset_x;
int offset_y;
int tiles;
int x;
int y;
int layer;
bool wakable;
}; 
  int move = 0; 
//Tile_size,world_sizex,world_sizey,Screen_sizex,Screen_sizey               
Tile Tileset = { 125,25, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, true };
const int Mapx = 50;
const int Mapy = 47;
const int Layer =3;

int map[Layer][Mapy][Mapx];

ifstream MapFile("maps\\level1.txt");


the above code is what is related to the problem. can some one tell me why the above code makes the tiles overlap? Thanks [Edited by - kingpinzs on May 17, 2005 9:30:44 PM]

Share this post


Link to post
Share on other sites
Advertisement

// Shouldn't you use
Tileset.offset_x =Tileset.world_camerax % Tileset.Tile_size;
Tileset.offset_y =Tileset.world_cameray % Tileset.Tile_size;

// instead of
Tileset.offset_x =Tileset.scroll_x % Tileset.Tile_size;
Tileset.offset_y =Tileset.scroll_y % Tileset.Tile_size;

Share this post


Link to post
Share on other sites
So can any one help me on figureing out the math on my tile engine? it looks like the left side grows by about 4 pixels and the right side moves fine. so the left side over laps the nobering tile right side. So How could I fix that?

Share this post


Link to post
Share on other sites
I wouldn't be suprised if your problem was division inaccuracy. In my expereince, things worked "better" when tiles were positioned in relation to their neighbors rather than mathematically calculated.

To be blunt, people will be hesitant to help you solve problems when you seem to do so little work trying to find the problem yourself.

Share this post


Link to post
Share on other sites
How could I work it out on paper? Then I could figure it out on my own. but I dont know how to do it on paper and get the result that is supose to be there.

Share this post


Link to post
Share on other sites
On mine, it was only one layer, but I did get it scrolling smooth.

You can look at the source if you like.

scroller.zip

The one problem I had was I was only drawing just enough tiles to cover the screen, so when I moved stuff over to the right, there was nothing to show up on the screen to the left, So I just made sure the engine was drawing an extra row of tiles to that side.

*EDIT* I didn't include Y scrolling, due to laziness. Works the exact same way, though.

Share this post


Link to post
Share on other sites
wasted_druid the code would help. But I am not using a vector I am using an array and also I am using direct draw that needs rects. Does any one have any other ideas?

Share this post


Link to post
Share on other sites
SDL uses rects too. I just have a function that figures it out for me. It's in "wgraphics.h". Anyways, I use the vector just like I would an array. I just didn't want to bother with memory allocation. The logic behind the implementation is what you need to look at.


1. I have a 2d array of tiles representing the map.
2. I make sure I know the width and height of the tiles.
3. I know the absolute X position in the world (in pixels).
4. I figure out how many tiles across and up and down I'm going to render.
-numX = (SCREEN_WIDTH/TILE_WIDTH) + 1; numY = (SCREEN_HEIGHT/TILE_HEIGHT) + 1;
5. I figure out what X and Y positions to start rendering from in the 2d array
-startX = (ABS_X_POS/TILE_WIDTH) ; startY = (ABS_Y_POS/TILE_WIDTH)
6. I figure out my offset to move those tiles correctly.
-xOffset = (ABS_X_POS%TILE_WIDTH) ; yOffset = (ABS_Y_POS/TILE_WIDTH)
7. I start a rendering loop.

for(i = startX; i < numX; i++){
for(j = startY; j < startY; j++){
int drawX = (i * TILE_WIDTH) - xOffset;
int drawY = (j * TILE_WIDTH) - yOffset;
Draw(2dArray[j], drawX, drawY);
}
}

8. If you need rects, I'm sure you can figure them out using drawX, drawY, and your TILE_WIDTH

9. Some of my logic might be off, I'm at my parents' house so I don't have my code on me.
10. That Should be all you need to get smooth scrolling.


Good luck. Look over my code I linked and compare it to this. Maybe it'll help.

Share this post


Link to post
Share on other sites
Quote:
Original post by wasted_druid



1. I have a 2d array of tiles representing the map.
//I understand this

2. I make sure I know the width and height of the tiles.
//I know this 128 pixels for both

3. I know the absolute X position in the world (in pixels).
//what is this? I am not sure what this is for. Is this like if I wanted to start and 0,0 wich would be the top left corner of the map or if I wanted to start at 800,600 and have it in the top left corner. which would be about the middle of the map

4. I figure out how many tiles across and up and down I'm going to render.
-numX = (SCREEN_WIDTH/TILE_WIDTH) + 1; numY = (SCREEN_HEIGHT/TILE_HEIGHT) + 1;
//which if my screen size is 800x600 I would just put that in there so I know what that is.

5. I figure out what X and Y positions to start rendering from in the 2d array
-startX = (ABS_X_POS/TILE_WIDTH) ; startY = (ABS_Y_POS/TILE_WIDTH)

//I belive ABS_X_POS,ABS_Y_POS is figured out in #3. so once I know what #3 is I will have this solved

6. I figure out my offset to move those tiles correctly.
-xOffset = (ABS_X_POS%TILE_WIDTH) ; yOffset = (ABS_Y_POS/TILE_WIDTH)

//same with this one. Once I have #3 solved I will have this one solved

7. I start a rendering loop.

for(i = startX; i < numX; i++){
for(j = startY; j < startY; j++){
int drawX = (i * TILE_WIDTH) - xOffset;
int drawY = (j * TILE_WIDTH) - yOffset;
Draw(2dArray[j], drawX, drawY);
}
}



and then just run the loop.







Can you tell me if my coments are right and what #3 is for? Thanks for the help wasted_druid

Share this post


Link to post
Share on other sites
#3. If you want to display the far left and top corner of the map it's zero. I thought of it this way:

If I had a 20x40 map of tiles that are 32x32 pixels, I have an absolute size of 640pixels x 1280pixels. That's the size of my world. The abs_x_pos / abx_y_pos are the coordinates of the top left corner of the rectangle of it that I'm going to render.

This isn't by any means the only way to do things, it's just the way I figured it out.

Your comments look good to me.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!