Skip to content

Commit 246cbfc

Browse files
committed
Fix build_texture_atlas
Wide textures not fitting into a current atlas size was not triggering atlas texture resize.
1 parent 3bd19f2 commit 246cbfc

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/text/atlas.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl Atlas {
141141
let y = self.cursor_y;
142142

143143
// texture bounds exceeded
144-
if self.cursor_y + height as u16 > self.image.height {
144+
if self.cursor_y > self.image.height || self.cursor_x > self.image.width {
145145
// reset glyph cache state
146146
let sprites = self.sprites.drain().collect::<Vec<_>>();
147147
self.cursor_x = 0;
@@ -151,11 +151,15 @@ impl Atlas {
151151
let old_image = self.image.clone();
152152

153153
// increase font texture size
154-
self.image = Image::gen_image_color(
155-
self.image.width * 2,
156-
self.image.height * 2,
157-
Color::new(0.0, 0.0, 0.0, 0.0),
158-
);
154+
// note: if we tried to fit gigantic texture into a small atlas,
155+
// new_width will still be not enough. But its fine, it will
156+
// be regenerated on the recursion call.
157+
let new_width = self.image.width * 2;
158+
let new_height = self.image.height * 2;
159+
160+
println!("{new_width} {new_height}");
161+
self.image =
162+
Image::gen_image_color(new_width, new_height, Color::new(0.0, 0.0, 0.0, 0.0));
159163

160164
// recache all previously cached symbols
161165
for (key, sprite) in sprites {

src/texture.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ impl Image {
196196

197197
/// Modifies a pixel [Color] in this image.
198198
pub fn set_pixel(&mut self, x: u32, y: u32, color: Color) {
199+
assert!(x < self.width as u32);
200+
assert!(y < self.height as u32);
201+
199202
let width = self.width;
200203

201204
self.get_image_data_mut()[(y * width as u32 + x) as usize] = color.into();

0 commit comments

Comments
 (0)