Created
June 28, 2024 14:30
-
-
Save karl-zylinski/a5f996ef03f46998b9886fb456279e08 to your computer and use it in GitHub Desktop.
Takes a tileset and generates a new one with 1 px padding around all the tiles. The pixel at the border of the tile is copied to that border. This fixes subpixel camera interpolation glitches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
refresh_padded_tileset :: proc() { | |
if g_mem.tileset_padded.id != 0 { | |
rl.UnloadTexture(g_mem.tileset_padded) | |
g_mem.tileset_padded = {} | |
} | |
ts_source := load_image(.Tileset) | |
defer rl.UnloadImage(ts_source) | |
tileset_height := ts_source.height / TileHeight | |
ts_padded := rl.Image { | |
width = ts_source.width + 4*2, | |
height = ts_source.height + tileset_height*2, | |
mipmaps = 1, | |
format = ts_source.format, | |
} | |
pixels := make([]rl.Color, ts_padded.width*ts_padded.height, context.temp_allocator) | |
ts_padded.data = rawptr(&pixels[0]) | |
for tile_idx in 0..<tileset_height*4 { | |
tile_x := f32(tile_idx % 4) | |
tile_y := f32(tile_idx / 4) | |
px := tile_x * TileHeight | |
py := tile_y * TileHeight | |
ts: f32 = TileHeight | |
{ | |
source := Rect { | |
px, | |
py, | |
ts, | |
ts, | |
} | |
dest := Rect { | |
px + 2*tile_x + 1, | |
py + 2*tile_y + 1, | |
ts, | |
ts, | |
} | |
rl.ImageDraw(&ts_padded, ts_source, source, dest, rl.WHITE) | |
} | |
// Fill in borders of padded tileset | |
// Top | |
{ | |
source := Rect { | |
px, | |
py, | |
ts, | |
1, | |
} | |
dest := Rect { | |
px + 2*tile_x + 1, | |
py + 2*tile_y, | |
ts, | |
1, | |
} | |
rl.ImageDraw(&ts_padded, ts_source, source, dest, rl.WHITE) | |
} | |
// Bottom | |
{ | |
source := Rect { | |
px, | |
py + ts - 1, | |
ts, | |
1, | |
} | |
dest := Rect { | |
px + 2*tile_x + 1, | |
py + 2*tile_y + ts + 1, | |
ts, | |
1, | |
} | |
rl.ImageDraw(&ts_padded, ts_source, source, dest, rl.WHITE) | |
} | |
// Left | |
{ | |
source := Rect { | |
px, | |
py, | |
1, | |
ts, | |
} | |
dest := Rect { | |
px + 2*tile_x, | |
py + 2*tile_y + 1, | |
1, | |
ts, | |
} | |
rl.ImageDraw(&ts_padded, ts_source, source, dest, rl.WHITE) | |
} | |
// Right | |
{ | |
source := Rect { | |
px + ts - 1, | |
py, | |
1, | |
ts, | |
} | |
dest := Rect { | |
px + 2*tile_x + ts + 1, | |
py + 2*tile_y + 1, | |
1, | |
ts, | |
} | |
rl.ImageDraw(&ts_padded, ts_source, source, dest, rl.WHITE) | |
} | |
} | |
g_mem.tileset_padded = rl.LoadTextureFromImage(ts_padded) | |
rl.SetTextureWrap(g_mem.tileset_padded, .CLAMP) | |
rl.SetTextureFilter(g_mem.tileset_padded, .BILINEAR) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment