Coordinate for a specific tile

Started by
4 comments, last by SquareDanceSteve 19 years, 3 months ago
How do you get the screen coordinates for a cell without looping through the whole map to calculate the x,y?
Advertisement
i'm afraid we'll need more information?

Are you having trouble determine where on screen to plot a tile?

Or something else? Your post is unclear.

Get off my lawn!

I guess I'm a bit confused myself. I want to be able to scroll the map but I don't want to redraw every tile in the game loop.

I know I need to buffer the full screen of the map first, then when I scroll, I just need to update the parts that were off the screen. But I'm having trouble with the calculations of finding the specific row/column I want to draw.
You're going to have to redraw every single tile. This is simply because the entire screen changes. Usually, what's used today is double-buffering, where you draw to an offscreen buffy(non-rendered memory) then render the data in that memory once all your off-screen drawing is complete. That is, of course, unless you decide to use Carmack's adaptive tile refresh, which is simply drawing the changed part of the screen. For instance, if a tile moves 5 pixels to the left, you'd have to figure out what to draw in those 5 pixels of now freed up space.

However, seeing as normal hardware will allow you the overhead of rendering every single tile again on the screen. If you want to use Carmack's method, you'll need to go very VERY low level on this and would have to develop your renderer as a software-based engine, seeing as different hardware would force you to rewrite your renderer for every single variation.

Therefore, from what I understand you to say, you'll need ot rerender every single on-screen tile if the screen changes.
Tarviathun: assuming his level is larger than the screen, drawing all the tiles could cost hundreds of times more than only drawing the tiles on the screen.

OP: I'm sure you keep somewhere some position information about each tile. This could be two indices (i,j) in a bidimensional array that contains all your tiles, or maybe an (x,y) position from the top left corner of the map (not the screen).

Once you have this information, you convert it into pixels (by multiplying by the width or height of tiles, for instance), and substract from it the x and y position of the top left hand corner of the screen. You then have the screen coordinates of the tiles.

By the way, the overhead of redrawing the entire screen, as opposed to only drawing the new tiles, is minimal. Consider that instead of simply throwing tiles at the renderer, you will:
- need to keep track of modifications everywhere
- read and write all the pixels on the screen anyway, so you can scroll
- perform a lot of checks to determine whether a given render requires redrawing an area or not

However, this becomes useful if you ever have to draw an area with no scrolling (since this reduces the redrawn areas to those where action happens, something often smaller than 1/10th of the screen).
If you are rendering a lot of small tiles your computer will feel the drain. You must use at least a double buffer and render only what you must.

say your grid is 1000x1000, your screen res is 800x600, your tiles are all 32x32

the upper left hand corner of your screen is at pixel location (x4235, y2372)

xTileStart = cint( xScreen / TileWidth )
yTileStart = cint( yScreen / TileHeight )

xTileStart = cint(4235/32)
xTileStart = 132
yTileStart = cint(2372/32)
yTileStart = 74

your end tile render points
xTileStart = cint( (xScreen + ScreenWidth) / TileWidth )
yTileStart = cint( (yScreen + ScreenHeight) / TileHeight )

xTileEnd = 157
yTileEnd = 92

so render column 132 to 157 and render then down from 74 to 92

to double buffer, you copy the entire screen to the second buffer

now move your screen to the new location

now you need to render the next frame, grab the location for the buffer to be blitted
xTileStart - xOldTileStart = xDoubleBuffer
yTileStart - yOldTileStart = yDoubleBuffer

now you know where to place your buffer, but you now have new tiles to be drawn, you know that the buffer image will always be 800x600, through simple math you can easily determin the colums if and or rows that need to be drawn
-----------------www.stevemata.com

This topic is closed to new replies.

Advertisement