Skip to content

Commit

Permalink
use the new methods throughout macroquad
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrgani committed Jan 9, 2025
1 parent 8b11aba commit 1019ade
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
10 changes: 5 additions & 5 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ impl Font {
let sprite = self.atlas.lock().unwrap().new_unique_id();
self.atlas.lock().unwrap().cache_sprite(
sprite,
Image {
bytes: bitmap
Image::from_parts(
width,
height,
bitmap
.iter()
.flat_map(|coverage| vec![255, 255, 255, *coverage])
.collect(),
width,
height,
},
),
);
let advance = metrics.advance_width;

Expand Down
31 changes: 18 additions & 13 deletions src/text/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ 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() as u16, image.height() as u16, image.bytes());
ctx.texture_set_filter(
texture,
miniquad::FilterMode::Nearest,
Expand Down Expand Up @@ -79,30 +80,32 @@ impl Atlas {
}

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

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

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() as u16,
self.image.height() as u16,
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 +126,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(), sprite.height());

let x = if self.cursor_x + (width as u16) < self.image.width {
let x = if self.cursor_x + (width as u16) < self.image.width() as u16 {
if height as u16 > self.max_line_height {
self.max_line_height = height as u16;
}
Expand All @@ -141,7 +144,9 @@ 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() as u16 > self.image.height() as u16
|| x + sprite.width() as u16 > self.image.width() as u16
{
// reset glyph cache state
let sprites = self.sprites.drain().collect::<Vec<_>>();
self.cursor_x = 0;
Expand All @@ -154,8 +159,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() as u16 * 2;
let new_height = self.image.height() as u16 * 2;

self.image =
Image::gen_image_color(new_width, new_height, Color::new(0.0, 0.0, 0.0, 0.0));
Expand Down
19 changes: 8 additions & 11 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
text::atlas::SpriteKey, Error,
};

use crate::color::BLANK;
pub use crate::quad_gl::FilterMode;
use crate::quad_gl::{DrawMode, Vertex};
use glam::{vec2, Vec2};
Expand Down Expand Up @@ -774,7 +775,7 @@ impl Texture2D {

/// Creates a Texture2D from an [Image].
pub fn from_image(image: &Image) -> Texture2D {
Texture2D::from_rgba8(image.width, image.height, &image.bytes)
Texture2D::from_rgba8(image.width() as u16, image.height() as u16, image.bytes())
}

/// Creates a Texture2D from a miniquad
Expand Down Expand Up @@ -816,10 +817,10 @@ impl Texture2D {
let ctx = get_quad_context();
let (width, height) = ctx.texture_size(self.raw_miniquad_id());

assert_eq!(width, image.width as u32);
assert_eq!(height, image.height as u32);
assert_eq!(width, image.width() as u32);
assert_eq!(height, image.height() as u32);

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

// Updates the texture from an array of bytes.
Expand Down Expand Up @@ -850,7 +851,7 @@ impl Texture2D {
y_offset,
width,
height,
&image.bytes,
image.bytes(),
);
}

Expand Down Expand Up @@ -950,12 +951,8 @@ impl Texture2D {
pub fn get_texture_data(&self) -> Image {
let ctx = get_quad_context();
let (width, height) = ctx.texture_size(self.raw_miniquad_id());
let mut image = Image {
width: width as _,
height: height as _,
bytes: vec![0; width as usize * height as usize * 4],
};
ctx.texture_read_pixels(self.raw_miniquad_id(), &mut image.bytes);
let mut image = Image::gen_image_color(width as u16, height as u16, BLANK);
ctx.texture_read_pixels(self.raw_miniquad_id(), image.bytes_mut());
image
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/ui/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@ impl Skin {
.color_inactive(Color::from_rgba(238, 238, 238, 128))
.text_color(Color::from_rgba(0, 0, 0, 255))
.color(Color::from_rgba(220, 220, 220, 255))
.background(Image {
width: 16,
height: 30,
bytes: include_bytes!("combobox.img").to_vec(),
})
.background(Image::from_parts(
16,
30,
include_bytes!("combobox.img").to_vec(),
))
.build(),
tabbar_style: Style {
margin: Some(RectOffset::new(2., 2., 2., 2.)),
Expand All @@ -429,15 +429,15 @@ impl Skin {
.background_margin(RectOffset::new(1., 1., 1., 1.))
.color_inactive(Color::from_rgba(238, 238, 238, 128))
.text_color(Color::from_rgba(0, 0, 0, 255))
.background(Image {
width: 3,
height: 3,
bytes: vec![
.background(Image::from_parts(
3,
3,
vec![
68, 68, 68, 255, 68, 68, 68, 255, 68, 68, 68, 255, 68, 68, 68, 255, 238,
238, 238, 255, 68, 68, 68, 255, 68, 68, 68, 255, 68, 68, 68, 255, 68, 68,
68, 255,
],
})
))
.build(),
window_titlebar_style: Style {
color: Color::from_rgba(68, 68, 68, 255),
Expand Down

0 comments on commit 1019ade

Please sign in to comment.