6 hours ago, ItsQuesoTime said:
How would I draw the tiles in terms of the world, not the camera? Because the way I currently have things is just me drawing the world at the top left corner of the camera, which won't work if I'm going to have scrolling.
I'll try to clarify what I was doing in the Render pseudo code.
I'll do it twice to show how it works.
Round 1:
void Render()
{
.. Get camera position in pixels
camera_x = ..
camera_y = ..
chunk_id[4] = GetCameraChunks( camera_x, camera_y )
Let say the camera's origin is located at 960, 540.
camera_x = 960
camera_y = 540
We'll look at the GetCameraChunks. BTW, remember when I said I didn't check my math; I didn't account for off by 1 problem so I forgot to subtract 1 to the maximums.
chunk_id[4] GetCameraChunks( camera_x, camera_y )
{
min_x = camera_x - SCREEN_HX
max_x = camera_x + SCREEN_HX - 1
min_y = camera_y - SCREEN_HY
max_y = camera_y + SCREEN_HY - 1
chunk_id[0] = pair< GetChunkCoord( min_x, min_y ) >
chunk_id[1] = pair< GetChunkCoord( max_x, min_y ) >
chunk_id[2] = pair< GetChunkCoord( min_x, max_y ) >
chunk_id[3] = pair< GetChunkCoord( max_x, max_y ) >
}
min_x = 960 - 960 = 0
max_x = 960 + 960 - 1 = 1919
min_y = 540 - 540 = 0
max_y = 540 + 540 - 1= 1079
We'll also look at what GetChunkCoord does with these values.
chunk_x, chunk_y GetChunkCoord( pixel_x, pixel_y)
{
chunk_x = pixel_x / CHUNK_SIZE
chunk_y = pixel_y / CHUNK_SIZE
}
chunk_x = pixel_x / CHUNK_SIZE = 0 / 3200 = 0
chunk_y = pixel_y / CHUNK_SIZE = 0 / 3200 = 0
chunk_x = pixel_x / CHUNK_SIZE = 1919 / 3200 = 0
chunk_y = pixel_y / CHUNK_SIZE = 0 / 3200 = 0
chunk_x = pixel_x / CHUNK_SIZE = 0 / 3200 = 0
chunk_y = pixel_y / CHUNK_SIZE = 1079 / 3200 = 0
chunk_x = pixel_x / CHUNK_SIZE = 1919 / 3200 = 0
chunk_y = pixel_y / CHUNK_SIZE = 1079 / 3200 = 0
Note that this is normal behavior for division for integer types.
So going back to GetCameraChunks:
chunk_id[0] = pair< GetChunkCoord( min_x, min_y ) > = pair< 0, 0>
chunk_id[1] = pair< GetChunkCoord( max_x, min_y ) > = pair< 0, 0>
chunk_id[2] = pair< GetChunkCoord( min_x, max_y ) > = pair< 0, 0>
chunk_id[3] = pair< GetChunkCoord( max_x, max_y ) > = pair< 0, 0>
I'll skip ahead to this section of the Render function:
..Tile's pixel location in world space
world_px = (TILE_SIZE * tile_x) + (chunk.id[x] * CHUNK_SIZE)
world_py = (TILE_SIZE * tile_y) + (chunk.id[y] * CHUNK_SIZE)
..Camera's top left px coord in world space
camera_px = camera_x - SCREEN_HX
camrea_py = camera_y - SCREEN_HY
..Tile's pixel location in screen space
tile_px = world_px - camera_px
tile_py = world_py - camera_py
world_px = (TILE_SIZE * tile_x) + (chunk.id[x] * CHUNK_SIZE) = ( 64 * 0 ) + ( 0 * 3200 ) = 0
world_py = (TILE_SIZE * tile_y) + (chunk.id[y] * CHUNK_SIZE) = ( 64 * 0 ) + ( 0 * 3200 ) = 0
camera_px = camera_x - SCREEN_HX = 960 - 960 = 0
camrea_py = camera_y - SCREEN_HY = 540 - 540 = 0
tile_px = world_px - camera_px = 0 - 0 = 0
tile_py = world_py - camera_py = 0 - 0 = 0
At this point render the tile at tile_px and tile_py which is top left corner of that tile image.
Round 2: We moved the camera by 10px in the update function.
camera_x += 10 = 960 + 10 = 970
camera_y += 10 = 540 + 10 = 550
GetCameraChunks( 970, 550 ) will yield again 4x pair<0, 0>
world_px = (TILE_SIZE * tile_x) + (chunk.id[x] * CHUNK_SIZE) = ( 64 * 0 ) + ( 0 * 3200 ) = 0
world_py = (TILE_SIZE * tile_y) + (chunk.id[y] * CHUNK_SIZE) = ( 64 * 0 ) + ( 0 * 3200 ) = 0
camera_px = camera_x - SCREEN_HX = 970 - 960 = 10
camrea_py = camera_y - SCREEN_HY = 550 - 540 = 10
tile_px = world_px - camera_px = 0 - 10 = -10
tile_py = world_py - camera_py = 0 - 10 = -10
At this point render the tile at tile_px and tile_py which is top left corner of that tile image but partially off the screen.
The conclusion is that horizontal and vertical scrolling is built into the formula. Just move the camera an the world will move with it.