Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image rework for soundness and usability #748

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async fn main() {
loop {
clear_background(WHITE);

let w = image.width();
let h = image.height();
let w = image.width() as usize;
let h = image.height() as usize;

for y in 0..h as i32 {
for x in 0..w as i32 {
Expand Down
38 changes: 19 additions & 19 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,31 @@ fn color_from_bytes() {
);
}

impl Into<[u8; 4]> for Color {
fn into(self) -> [u8; 4] {
[
(self.r * 255.) as u8,
(self.g * 255.) as u8,
(self.b * 255.) as u8,
(self.a * 255.) as u8,
]
impl From<[u8; 4]> for Color {
fn from(value: [u8; 4]) -> Color {
Color::new(
value[0] as f32 / 255.,
value[1] as f32 / 255.,
value[2] as f32 / 255.,
value[3] as f32 / 255.,
)
}
}

impl Into<Color> for [u8; 4] {
fn into(self) -> Color {
Color::new(
self[0] as f32 / 255.,
self[1] as f32 / 255.,
self[2] as f32 / 255.,
self[3] as f32 / 255.,
)
impl From<Color> for [u8; 4] {
fn from(value: Color) -> Self {
[
(value.r * 255.) as u8,
(value.g * 255.) as u8,
(value.b * 255.) as u8,
(value.a * 255.) as u8,
]
}
}

impl Into<[f32; 4]> for Color {
fn into(self) -> [f32; 4] {
[self.r, self.g, self.b, self.a]
impl From<Color> for [f32; 4] {
fn from(value: Color) -> [f32; 4] {
[value.r, value.g, value.b, value.a]
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ impl Font {
let sprite = self.atlas.lock().unwrap().new_unique_id();
self.atlas.lock().unwrap().cache_sprite(
sprite,
Image {
bytes: bitmap
Image::from_raw_parts(
width,
height,
bitmap
.iter()
.flat_map(|coverage| vec![255, 255, 255, *coverage])
.collect(),
width,
height,
},
),
);
let advance = metrics.advance_width;

Expand Down
34 changes: 18 additions & 16 deletions src/text/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Atlas {

pub fn new(ctx: &mut dyn miniquad::RenderingBackend, filter: miniquad::FilterMode) -> Atlas {
let image = Image::gen_image_color(512, 512, Color::new(0.0, 0.0, 0.0, 0.0));
let texture = ctx.new_texture_from_rgba8(image.width, image.height, &image.bytes);
let texture = ctx.new_texture_from_rgba8(image.width(), image.height(), &image.bytes());
ctx.texture_set_filter(
texture,
miniquad::FilterMode::Nearest,
Expand Down Expand Up @@ -79,30 +79,32 @@ impl Atlas {
}

pub fn width(&self) -> u16 {
self.image.width
self.image.width()
}

pub fn height(&self) -> u16 {
self.image.height
self.image.height()
}

pub fn texture(&mut self) -> miniquad::TextureId {
let ctx = get_quad_context();
if self.dirty {
self.dirty = false;
let (texture_width, texture_height) = ctx.texture_size(self.texture);
if texture_width != self.image.width as _ || texture_height != self.image.height as _ {
if texture_width != self.image.width() as _
|| texture_height != self.image.height() as _
{
ctx.delete_texture(self.texture);

self.texture = ctx.new_texture_from_rgba8(
self.image.width,
self.image.height,
&self.image.bytes[..],
self.image.width(),
self.image.height(),
&self.image.bytes()[..],
);
ctx.texture_set_filter(self.texture, self.filter, miniquad::MipmapFilterMode::None);
}

ctx.texture_update(self.texture, &self.image.bytes);
ctx.texture_update(self.texture, self.image.bytes());
}

self.texture
Expand All @@ -123,9 +125,9 @@ impl Atlas {
}

pub fn cache_sprite(&mut self, key: SpriteKey, sprite: Image) {
let (width, height) = (sprite.width as usize, sprite.height as usize);
let (width, height) = (sprite.width() as usize, sprite.height() as usize);

let x = if self.cursor_x + (width as u16) < self.image.width {
let x = if self.cursor_x + (width as u16) < self.image.width() {
if height as u16 > self.max_line_height {
self.max_line_height = height as u16;
}
Expand All @@ -141,7 +143,7 @@ impl Atlas {
let y = self.cursor_y;

// texture bounds exceeded
if y + sprite.height > self.image.height || x + sprite.width > self.image.width {
if y + sprite.height() > self.image.height() || x + sprite.width() > self.image.width() {
// reset glyph cache state
let sprites = self.sprites.drain().collect::<Vec<_>>();
self.cursor_x = 0;
Expand All @@ -154,8 +156,8 @@ impl Atlas {
// note: if we tried to fit gigantic texture into a small atlas,
// new_width will still be not enough. But its fine, it will
// be regenerated on the recursion call.
let new_width = self.image.width * 2;
let new_height = self.image.height * 2;
let new_width = self.image.width() * 2;
let new_height = self.image.height() * 2;

self.image =
Image::gen_image_color(new_width, new_height, Color::new(0.0, 0.0, 0.0, 0.0));
Expand All @@ -174,9 +176,9 @@ impl Atlas {
for j in 0..height {
for i in 0..width {
self.image.set_pixel(
x as u32 + i as u32,
y as u32 + j as u32,
sprite.get_pixel(i as u32, j as u32),
x + i as u16,
y + j as u16,
sprite.get_pixel(i as u16, j as u16),
);
}
}
Expand Down
Loading
Loading